% EDITSPECT -  Remove periodic interference patterns in an image.
%
% This function allows one to edit the amplitude spectrum of an image by
% 'blacking out' points in the spectrum with the mouse.  
%
% Usage:  newim = editspect(im, cutoff,n)
%
% Arguments:   im      - The image to be processed.
%              cutoff  - The radius of the blackout spot generated by a mouse click.
%                        (A typical value is in the range 0.02 - 0.1)
%              n       - order of the Butterworth function used to construct the spot.
%
% Returns:  newim      - The image reconstructed with the new amplitude
%                        spectrum.
function newim = editspect(im, cutoff, n)    

    [rows, cols] = size(im);
    
    % Calculate the FFT of the image and quadrant shift it..
    imfft = fftshift(fft2(im));
    % Display the log of the magnitude of the FFT.
    imagesc(log(abs(imfft)+eps)), colormap(gray);
    
    % While loop that allows one to repeatedly digitise points in
    % the image of the amplitude spectrum blacking out points as we go.

    but = 1;
    while but==1
        [xc,yc,but] = ginput(1);
        
        % Generate a 'blackspot' filter centred on xc,yc
        blackspotfft = blackspot(im, cutoff, n, xc, yc);
        imfft = blackspotfft.*imfft;
        blackspotfft = blackspot(imfft, cutoff, n, cols - xc, rows - yc);
        imfft = blackspotfft.*imfft;
        imagesc(log(abs(imfft)+eps)), colormap(gray);    
    end  

    newim = real(ifft2(fftshift(imfft)));
