Search code examples
cimagematlabswitchingphase

How to switching magnitude and phase information between two input images?


I am a student that is studying for matlab.

==================================================================

[Question]

Switching magnitude and phase information between two input images

 load two different input images

 And compare the results of switching the magnitude and phase information of the given inputs.

==================================================================

Reference 1 : Internet users

Reference 2 : http://paeton.tistory.com/15

==================================================================

My answer :

cm=imread('image1.bmp');
figure, imshow('image1.bmp');

cf=fftshift(fft2(cm));
g1=mat2gray(fspecial('gaussian',480,10));
cg1=cf.*g1;
figure,fftshow(cg1,'log');
cgi1=ifft2(cg1);
fftshow(cgi1,'abs');

cf=fftshift(fft2(cm));
g1=mat2gray(fspecial('gaussian',480,50));
cg1=cf.*g1;
figure,fftshow(cg1,'log');
cgi1=ifft2(cg1);
fftshow(cgi1,'abs');

==================================================================

I submit the answer to the teacher, but is wrong.

From it's been two months, but not everyone can give the answer.

I do not know how to solve this problem.

Please help me. Phase and Magnitude to switch the source code, please.


Solution

  • Oh... I feel so sorry for you.
    You should be answering your homework questions by yourself!

    function I_must_understand_this_code_before_I_submit_my_homework( )
    
    % read images
    im{1} = im2double( imread( 'image1.bmp' ) );
    im{2} = im2double( imread( 'image2.bmp' ) );
    assert( all( size(im{1}) == size(im{2}) ), 'images must have same size' );
    
    % FFT - separate magnitude and phase no fftshift
    mag = cell(1,2);
    phz = cell(1,2);
    for ii=1:2
        I = zeros( size(im{ii}) );
        for c = 1:size(im{ii},3) % for color images
            I(:,:,c) = fft2( im{ii}(:,:,c) );
        end
        mag{ii} = abs( I ); % magnitude of fft
        phz{ii} = angle( I ); % phase
    end
    
    % combine (still in Fourier domain)
    C{1} = mag{1}.*exp( j * phz{2} ); % mag of first with phase of second
    C{2} = mag{2}.*exp( j * phz{1} ); % mag of second with phase of first
    
    % back to image domain
    comb = cell(1,2);
    for ii=1:2
        for c = 1:size(C{ii},3)
            comb{ii}(:,:,c) = ifft2( C{ii}(:,:,c) )
        end
        comb{ii} = abs(comb{ii}); % discard imaginary part
    end
    % dispaly
    figure('Name','I should forever be grateful to stack overflow!');
    subplot(221);
    imshow( im{1} ); title('image1');
    subplot(222);
    imshow( im{2} ); title('image2');
    subplot(223);
    imshow( comb{1} ); title('mag of 1 with phase of 2');    
    subplot(224);
    imshow( comb{2} ); title('mag of 2 with phase of 1');