opencv and FFT

asked 2015-11-23 04:10:56 -0500

rourou11 gravatar image

updated 2015-12-03 10:59:20 -0500

thdrksdfthmn gravatar image
function fft_img = get_FFT() % ****************************************************************************************************************************
% Compute the FFT phase or Amplitude

CtrlFigHdl              = GetFigHdl('CtrlFig');
ToggleButtonAmpHdl      = findobj(CtrlFigHdl, 'Tag', 'ToggleButtonAmp');
ToggleButtonPhaseHdl    = findobj(CtrlFigHdl, 'Tag', 'ToggleButtonPhase');

% detect if we want a Phase or Amplitude
if get(ToggleButtonAmpHdl, 'Value')
    BufferType = 1;  % FFT Amplitude type
elseif get(ToggleButtonPhaseHdl, 'Value')
    BufferType = 2;  % FFT Phase type
else
    fft_img = [];
    return;
end

CtrlFigUsrDat = get(CtrlFigHdl, 'UserData');

% Do we need to update the FFT?
if (BufferType == CtrlFigUsrDat.Current.BufferType) && ~isempty(CtrlFigUsrDat.Current.Buffer)
    % No we don't
    fft_img = CtrlFigUsrDat.Current.Buffer;
else
    % we need to compute a new fft
    % Notes: fft(uint16) is double; fft(single) is single; fft(double) is double

    clear CtrlFigUsrDat; % Avoid duplication of data
    CtrlFigUsrDat = ClearBuffer('main'); % makes room

    % Get the ROI limits
    [ROI_min_x ROI_max_x ROI_min_y ROI_max_y] = getROIlimits();

    try
        h = [];

        % pre-allocation. We make it single as it's half the data needed and still good enough. 
        % The FFT computation below is converted in single as it is done, plane by plane
        fft_img = zeros(ROI_max_y-ROI_min_y+1, ROI_max_x-ROI_min_x+1, CtrlFigUsrDat.Current.z_dim, 'single');

        sizeOfSingle = 4;

        h = waitbar(0,[num2str(size(fft_img,2)) ' * ' num2str(size(fft_img,1)) ' pixels * ', ...
            num2str(size(fft_img,3)) ' frames * single (32 bpp) = ' num2str(numel(fft_img)*sizeOfSingle/(1024*1024),'%.2f'), ' MB'], ...
            'Name', 'Computing FFT...', 'WindowStyle', 'modal');
        set(h, 'HandleVisibility', 'off'); % do not integrate this into the line above
        pause(0.1); % allow refresh

        if BufferType == 1 % Amplitude
            for i = ROI_min_x:ROI_max_x % along x
                fft_img( : , i-ROI_min_x+1 , :) = abs(fft(single(get_RawData('ROI',i,':')),[],3));
                 if ishandle(h)
                     waitbar((i-ROI_min_x+1)/(ROI_max_x-ROI_min_x+1), h);
                 else
                     error('FFT aborted.');
                 end                
            end
        else % Phase
            for i = ROI_min_x:ROI_max_x % along x
                fft_img( : , i-ROI_min_x+1 , :) = -angle(fft(single(get_RawData('ROI',i,':')),[],3));
                 if ishandle(h)
                     waitbar((i-ROI_min_x+1)/(ROI_max_x-ROI_min_x+1), h);
                 else
                     error('FFT aborted.');
                 end
            end
        end

        % Update Buffer
        CtrlFigUsrDat.Current.BufferType = BufferType;
        CtrlFigUsrDat.Current.Buffer     = fft_img;
        setF(CtrlFigHdl, 'UserData', CtrlFigUsrDat);
    catch FFTError
        messageBox(FFTError.message, 1, 'FFT Error');
        fft_img = []; % means it failed computing the FFT
    end    

    if ishandle(h)
        delete(h)
    end

end
edit retag flag offensive close merge delete

Comments

1

It would be nice if you post the link to what you have found :)

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-11-23 06:11:46 -0500 )edit

If you read this tutorial, you will see in the explanation part that the magnitude is sqrt(Re² + Im²), so it contains both the information of real and imaginary part of the fft. What is in your opinion the to display the phase FFT? What this tutorial is displaying is the phase (angle between the line that pass through the point and the origin) and amplitude (the value at that point)

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-11-23 06:29:36 -0500 )edit

yes I found this tutorial, so this image displayed can be considered as amplitude and phase image in the same time???

rourou11 gravatar imagerourou11 ( 2015-11-23 09:22:54 -0500 )edit

A little doc about it It seems that the magnitude is just in the center

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-11-23 10:29:27 -0500 )edit

and what about the phase image I want just the phase please.

rourou11 gravatar imagerourou11 ( 2015-11-24 02:57:48 -0500 )edit
1

... using the phase function in the same way as the mentioned tutorial?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-11-24 03:35:38 -0500 )edit

thanks a lot

rourou11 gravatar imagerourou11 ( 2015-11-26 03:41:50 -0500 )edit

If you managed to do a phase image, then please add an answer with the code parts that did it and the results. Like this you can get also impressions and maybe corrections about it

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-11-26 04:57:25 -0500 )edit

ok but even with the tutorial it explains how to display FFT magnitude from one image however my application is to how display FFT magnitude and phase from many images, i explain more I should appliacte FFT for the same pixel of all images (like FFT signal) it means FFT for pixel 1.1 from im1 to imFinal and FFF for pixel 1.2 from im1 to imFinal... can you help me please.

rourou11 gravatar imagerourou11 ( 2015-12-03 02:56:04 -0500 )edit

Ok, the idea is simple and logic: You cannot reconstruct the image only from magnitude or only from phase, so the image in the output contains both magnitude and phase (or at least information about it, because phase = atan(imag/real), and in the image you have real on x axis and imag on y axis). I am not really sure how to extract just the phase, but I suppose that the phase function does it. From the phase you will be able to reconstruct just a kind of gradients image.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-12-03 04:31:58 -0500 )edit