function y=imfftplot(imagename) % % imfftplot plots spectrum of image and of regions selected by user % % WRITTEN BY William Martin, will@rapidnet.com % % Purpose Plots spectrum of image, of selected region (rectangular or polygon), % plots histogram of area, and edge detects image and overlays % that edge detected (and thresholded part) with original image. % % USAGE imfftplot('filename.ext'); % Where filename is the name of the file you want to use, extension is % an extension that Matlab handles. For a list of supported file types, % type "help imread" in the matlab command window % % OUTPUT These are the steps that will occur when you call this script. % Figure(1): Original Image and Spectrum of image % % Menu: Select either Rectangle or Polygon for selection method. % % The rectangle method assumes you are selecting while % holding down the left mouse button. When you let up, that % will select the region. % % The polygon method works by selecting points using the left % mouse button to select your points. Once you are finished, % right click to select the region of interest(ROI). % % Figure(2): Selected region is displayed along with its spectrum. % % Figure(3): Histogram of selected region (ROI). % % Figure(4): Edge detected regions overlayed on original image. % % % NOTE To clear all windows after you have run this several times, use % 'close all', in the command window. % % SEE ALSO: imread, imagesc, imshow, imhist, fft2, fftshift, edge % read in image and its color map [A,map]= imread(imagename); if(ndims(A)>2) A = rgb2gray(A); % convert to grayscale end figure % plot image and its fourier spectrum. F = fftshift(fft2(A)); subplot(1,2,1),imshow(A), title(strcat('Filename:', imagename)); % display menu answer = menu('Select method to use for spectrum analysis','Rectangle','Polygon'); % do Rectangle selection if(answer==1) % get ROI. copied = imcrop; %colormap('jet'); % uncomment for "pretty colors" subplot(1,2,2),imagesc(log(1+abs(F))) figure subplot(1,2,1),imshow(copied),title('selected area'); copiedfft=fftshift(fft2(copied)); %colormap('jet'); % uncomment for "pretty colors" subplot(1,2,2),imagesc(log(1+ abs(copiedfft))),title('spectrum of selected'); figure, imhist(copied); title('Histogram of selected area'); end % do Polygon selection if(answer==2) [selected,xi,yi]=roipoly; % plot the spectrum of the first image (done after selecting region). subplot(1,2,2),imagesc(log(1+abs(F))) [r,c]=find(selected); ind=sub2ind(size(A),r,c); J=zeros(size(A)); J(ind)=A(ind); figure subplot(1,2,1),imshow(J,map),title('Selected Area'); subplot(1,2,2),imagesc(log(abs(fftshift(fft2(J))))), title('Plot of spectrum of a polygon'); selected=im2uint8(J); figure, imhist(selected); title('Histogram of selected area'); end % perform edge detection on image r=edge(A,'canny'); % convert binary image to pseudo grayscale r=im2uint8(r); % do a linear combination of images A=imlincomb(1,r,1,A,'uint8'); figure imshow(A); title('Edges overlayed original image'); y=F; end