python image feature detection algorithm (1): Python implementation sift and Harris
In this paper, we will introduce two local descriptor algorithms for image matching. sift[paper Links] and Harris, they are in many applications have a relatively important role, such as target matching, target tracking, creating panorama, augmented reality technology and the calculation of the three-dimensional image reconstruction, and the commonly used features are color, corner point, Feature points, contours, textures, etc., are used in a lot of content. Harris Corner Detection Algorithm (also called Harris&stephens Corner detector) is the basis of feature point detection, and it is a very simple corner detection algorithm. Harris puts forward the concept of applying the gray difference of adjacent pixel points to determine whether it is a corner point (interest Point), edge, Smooth region. Harris Corner Detection principle is the use of mobile windows in the image to calculate the gray-scale changes in the value of the key processes, including the conversion of gray-scale images, the calculation of the difference image, Gaussian smoothing, the calculation of local extremum, to confirm the corner point (key point). sift[Homepage], a scale invariant feature transformation (Scale-invariantfeature transform,sift), is a description used in the field of image processing. This kind of description has the scale invariance, can detect the key point in the image, is one kind of local characteristic descriptor. The method was first published at the International Conference on Computer Vision (internationalconference on Computer VISION,ICCV) in 1999 by David Lowe[homepage, and again by David in 2004 Lowe is published in International Journal of Computer Vision (IJCV) after finishing. As of August 2014, the paper was cited more than 25,000 times a single article.
First, preliminary preparation
1. Installation configuration python and its associated software package (recommended to use Anaconda, with its own complete library, without time-consuming and laborious installation)
2. Installation Configuration Vlfeat Open Source Toolkit (WINDOWS10/64 bit system for example)
We have the Open Source Toolkit vlfeat the binaries available to compute the SIFT features of the image, Vlfeat kits can be downloaded from http://www.vlfeat.org/and binaries can be run on all major platforms. Vlfeat's homepage, as shown in the following illustration, shows that the Vlfeat Toolkit contains a large number of complete code implementations. The Vlfeat library is written in C, but we can use the command line interface provided by the library.
Download the file Vlfeat-0.9.20-bin on this interface and unzip the Sift.exe and Vl.dll under the VLFEAT-0.9.20/BIN/WIN32 (incompatible test under Win64) folder under the current engineering directory. In this way, the preliminary work has been completed.
Second, write code
Python's specific code for implementing SIFT and Harris is as follows:
#-*-Coding:utf-8-*-# Yan Zhenguo from PIL import ' from pylab import * ' numpy import * import OS from SCIPY.N DIMAGE Import Filters Def process_image (imagename, Resultname, params= "--edge-thresh--peak-thresh 5"): "" "Proces s an image and save the results in a file. ' "' If Imagename[-3:]!= ' PGM ': # Create a PGM file IM = Image.open (imagename). Convert (' L ') I M.save (' tmp.pgm ') imagename = ' TMP.PGM ' CMMD = str ("Sift" + imagename + "--output=" + Resultname + "" + PA
Rams) Os.system (CMMD) print (' Processed ', imagename, ' to ', Resultname) def read_features_from_file (filename):
"" "Read feature properties and return in matrix form." "" f = loadtxt (filename) return f[:,: 4], f[:, 4:] # feature locations, descriptors def write_features_to_file (Filenam
E, locs, desc): "" "Save feature location and descriptor to file." "" Savetxt (filename, hstack (locs, desc)) def plot_features (IM, locs, circle=false): "" "Show image with features. Input:im (image as Array), locs (row, col, scale, orientation of each feature). "" "Def Draw_circle (c, r): t = arange (0, 1.01,.) * 2 * pi x = r * COS (t) + c[0] y = R * si N (t) + c[1] Plot (x, y, ' B ', linewidth=2) imshow (IM) if circle:for p in locs:draw_ci Rcle (P[:2], p[2]) Else:plot (locs[:, 0], locs[:, 1], ' OB ') axis (' off ') def compute_harris_response (IM, S
igma=3): "" "Compute the Harris Corner detector response function for each pixel in a graylevel image." " # Derivatives IMX = zeros (im.shape) Filters.gaussian_filter (IM, (Sigma, Sigma), (0, 1), imx) Imy = zeros (im . Shape) Filters.gaussian_filter (IM, (Sigma, Sigma), (1, 0), Imy) # Compute components of the Harris matrix Wx x = Filters.gaussian_filter (IMX * imx, sigma) Wxy = Filters.gaussian_filter (IMX * imy, sigma) Wyy = Filters.gaussi An_filter (Imy * Imy, SIGMA) # determinant and Trace Wdet = wxx * WYY-WXY * 2 WTR = wxx + wyy return wdet/wtr def get_ha Rris_points (Harrisim, min_dist=10, threshold=0.1): "" "return corners from a Harris response image Min_dist is The minimum number of pixels separating corners and image boundary. "" "# Find top corner candidates above a threshold Corner_threshold = Harrisim.max () * Threshold harrisim_t = (Harrisim > Corner_threshold) * 1 # Get coordinates of candidates coords = Array (Harrisim_t.nonzero ()). T # ... and their values candidate_values = [harrisim[c[0], c[1]] for C in coords] # sort candidates (reverse To get descending order) index = Argsort (candidate_values) [:: -1] # Store allowed point locations in array al Lowed_locations = Zeros (harrisim.shape) allowed_locations[min_dist:-min_dist, min_dist:-min_dist] = 1 # Select th
e best points taking min_distance into account filtered_coords = [] For I in Index:if allowed_locations[coords[i, 0], coords[i, 1]] = = = 1:filtered_coords.append (coords[ I]) allowed_locations[(coords[i, 0]-min_dist):(coords[i, 0] + min_dist), (Coords[i, 1]-Min_dis T):(coords[i, 1] + min_dist)] = 0 return filtered_coords def plot_harris_points (image, Filtered_coords): "" "Plo TS Corners found in image. "" "Figure () Gray () imshow (image) plot ([p[1] for P in filtered_coords],[p[0] for P in filtered_coords], ' * ') axis (' off ') title (' Harris-features ') show () Imname = ' building.jpg ' im = Array (Image.open (imname). Convert
(' L ')) Process_image (imname, ' building.sift ') l1, D1 = read_features_from_file (' Building.sift ') figure () Gray () "" "Figure1:
SIFT features "" "Plot_features (IM, L1, circle=false) title (' Sift-features ')" "" Figure2: Use a circle to represent a feature scale SIFT feature "" "Figure () Gray () Plot_features (IM, L1, circle=true) title (' Detect-sift-features ') "" "Figure3:harris Corner Detection Results" "" Harrisim = Compute_harriS_response (IM) filtered_coords = get_harris_points (Harrisim, 6, 0.05) plot_harris_points (IM, filtered_coords)
Iii. Results of operation
1, SIFT characteristics
2, the use of the circle to represent the characteristics of the SIFT feature scale
3, Harris Corner Point
In the follow-up work, I will continue to show you the image feature detection related to the work and in-depth learning network brings endless fun, I will discuss the world of image and deep learning mysteries. Of course, if you are interested, my Weibo will share with you the cutting-edge technologies in artificial intelligence, machine learning, depth learning and computer vision.
Iv. Reference Documents
1.Python Computer Vision Programming
2. Baidu Encyclopedia Sift:https://baike.baidu.com/item/sift/1396275?fr=aladdin