Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

opencv and FFT

hello, how can I display phase image with opencv and FFT , i only find magnitude image. please help me. Thanks a lot.

opencv and FFT

hello, how can I display function fft_img = get_FFT() % **************************************** % Compute the FFT phase image with opencv 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 only find magnitude image.
please help me.
Thanks a lot.= 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

click to hide/show revision 3
No.3 Revision

opencv and FFT

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

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

'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

end CtrlFigUsrDat = get(CtrlFigHdl, 'UserData');

'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

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

end