opencv and FFT

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

rourou11 gravatar image

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

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 -0600 )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 -0600 )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 -0600 )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 -0600 )edit

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

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

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

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

thanks a lot

rourou11 gravatar imagerourou11 ( 2015-11-26 03:41:50 -0600 )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 -0600 )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 -0600 )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 -0600 )edit

Can you explain better what you need to do? Just for displaying the phase, apply phase on the image, see what you get and post it if you are not sure that it is correct.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-12-03 04:38:11 -0600 )edit

my problem here it's not how to extract magnitude or phase but how to applicate FFT on 3D image sequence of images????

rourou11 gravatar imagerourou11 ( 2015-12-03 04:39:36 -0600 )edit

well ,I try to expess my self better, in fact I have n images and I want to applicate FFT in order to dispaly magnitude and phase images but itsn't like mentioned in the tutorial image by image I want to applicate FFT in the time at pixel1 in im1 at 1t then pixel1 in im2 at t2 then at pixel 1 in im3 at t3... is it clear??? please help me if you can.

rourou11 gravatar imagerourou11 ( 2015-12-03 04:57:25 -0600 )edit

So your problem is more like this one or like this one?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-12-03 07:30:24 -0600 )edit

Or are you trying to filter noise in 3d image (image in time)?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-12-03 07:33:40 -0600 )edit

Why not computing the FFT for all the images then compare them? Or doing a mean (if you want to filter the noise) of all?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-12-03 07:42:26 -0600 )edit

Ok, this is getting long and the code is unreadable split in 5 comments with no formatting... You should update your question and make it beautiful ;)

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-12-03 09:15:12 -0600 )edit

Is this better now???

rourou11 gravatar imagerourou11 ( 2015-12-03 09:30:34 -0600 )edit

No!! Stop posting in comments. Click on edit question and add the code there

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-12-03 09:40:55 -0600 )edit

Niiceee... Now that you have the code, post the question too ;)

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-12-03 10:57:43 -0600 )edit

well, this code display FFT amplitude and phase image but it didn't applicate FFT in all image's pixels it applicate like I said before at each pixel in the time pixel 1 of image1, pixel1 in image2..., pixel1 in image n and then pixel 2 in image1, pixel 2 in image2..., pixel 2 in image n... extract amplitude equation and phase from them and rearrange pixel in their images and display them. I hope this time my problem is clear to you ... and Thanks a lot.

rourou11 gravatar imagerourou11 ( 2015-12-04 03:24:57 -0600 )edit