www.gusucode.com > images 案例代码 matlab源码程序 > images/PerformFFTBasedCorrelationToLocateImageFeaturesExample.m
%% Perform FFT-Based Correlation to Locate Image Features % This example shows how to use the Fourier transform to perform % correlation, which is closely related to convolution. Correlation can be % used to locate features within an image. In this context, correlation is % often called _template matching_. %% % Read a sample image into the workspace. bw = imread('text.png'); %% % Create a template for matching by extracting the letter "a" from the % image. Note that you can also create the template by using the % interactive syntax of the |imcrop| function. a = bw(32:45,88:98); %% % Compute the correlation of the template image with the original image by % rotating the template image by 180 degrees and then using the FFT-based % convolution technique. (Convolution is equivalent to correlation if you % rotate the convolution kernel by 180 degrees.) To match the template to % the image, use the |fft2| and |ifft2| functions. In the resulting image, % bright peaks correspond to occurrences of the letter. C = real(ifft2(fft2(bw) .* fft2(rot90(a,2),256,256))); figure imshow(C,[]) % Scale image to appropriate display range. %% % To view the locations of the template in the image, find the maximum % pixel value and then define a threshold value that is less than this % maximum. The thresholded image shows the locations of these peaks as % white spots in the thresholded correlation image. (To make the locations % easier to see in this figure, the example dilates the thresholded image % to enlarge the size of the points.) max(C(:)) thresh = 60; % Use a threshold that's a little less than max. D = C > thresh; se = strel('disk',5); E = imdilate(D,se); figure imshow(E) % Display pixels with values over the threshold.