-4

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.

Elesis
  • 1
  • 2
  • This looks like a start. I suggest an internet search for the terms "image phase magnitude matlab" and perhaps reading up on the conceptual background. As for your code, begin by modifying it so that you read in 2 different images and identify their individual phases and magnitudes – Buck Thorn Jul 30 '13 at 06:57

1 Answers1

5

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');    
Shai
  • 111,146
  • 38
  • 238
  • 371
  • 3
    @Elesis please note that stackoverflow (SO) is usually **not** for doing your homework for you. I felt sorry for you so I posted this aolution. For me to feel good with what I did, I would appreciate it if you study this solution carefully and actually **learn** from it about matlab and image processing. Can you explain why the combined images look the way they are? – Shai Jul 30 '13 at 13:16
  • 1
    This is so awesome, I can barely stand it. You are a kind and merciful god, Shai :) – Stephen Bosch Jul 31 '13 at 09:29