Search code examples
matlabimage-processingsignal-processingfftphase

How do I swap the phase responses of two images?


I applied the FFT to each image. I extracted the angle or phase of both image 1 and 2. After, I would like to swap the phase responses between both images but keep the magnitude intact. The code I have so far is:

Image1=double(imread('/home/anelmad/Desktop/ml/signal_images_processing/tp6/barbara.png'))/255;
Image2=double(imread('/home/anelmad/Desktop/ml/signal_images_processing/tp6/lena.bmp'))/255;
Image1=fft2(Image1);
Image2=fft2(Image2);

figure(1);
imshow(angle(Image1));
x=(angle(Image1));
angle(Image2)=x;

figure(2);
imshow(Image2);

I get this error :

error: assignment failed, or no method for '<unknown type> = matrix'

I think the problem is that we should modify the complex number within the Fourier Transform matrix (fft2). Is that correct?


Solution

  • Yes, you are correct in that you need to manipulate the phase response. However, you are not swapping the phase response properly. To properly do what you are asking, you must also extract the magnitude of each image as well. Recall from complex analysis where you can represent the frequency response of a signal in terms of its magnitude and phase:

    Therefore, if you want to swap the phase response between both images, you simply need to compute the above equation for each element in the output response where you are taking the magnitude and multiplying by the exponential of the phase with its argument multiplied by the complex number 1j. In addition, you must ensure that you swap the two angles when computing the final frequency response of the two images. I'm also going to call the frequency response variables of both images to be Image1_FFT and Image2_FFT respectively. A caveat is that you must make sure that the FFT sizes between the two images are the same or this won't work. Therefore, we can determine what the largest dimensions are between both images, take the FFT using these many points in both dimensions then when we take the inverse to reconstruct the image. Note that we don't crop the result because it'll show you what the results look like in a more pronounced way. The best effect in illustrating these results would be if both images were the same size.

    % Read the images in   
    Image1=double(imread('/home/anelmad/Desktop/ml/signal_images_processing/tp6/barbara.png'))/255;
    Image2=double(imread('/home/anelmad/Desktop/ml/signal_images_processing/tp6/lena.bmp'))/255;
    
    % Find dimensions and extent of the FFT
    [rows1, cols1] = size(Image1);
    [rows2, cols2] = size(Image2);
    
    rows = max(rows1, rows2);
    cols = max(cols1, cols2);
    
    % Take the FFT
    Image1_FFT=fft2(Image1, rows, cols);
    Image2_FFT=fft2(Image2, rows, cols);
    
    % NEW - Find the magnitudes and phase responses
    mag1 = abs(Image1_FFT);
    mag2 = abs(Image2_FFT);
    pha1 = angle(Image1_FFT);
    pha2 = angle(Image2_FFT);
    
    % Recompute frequency responses by swapping the phases
    out1 = mag1 .* exp(j*pha2);
    out2 = mag2 .* exp(j*pha1);
    
    % Find the inverse images
    out1 = real(ifft2(out1));
    out2 = real(ifft2(out2));
    
    % Show the images
    figure;
    imshow(out1, []);
    figure;
    imshow(out2, []);
    

    Example

    Let's load in the cameraman image and the mandrill image but let's make sure to resize the mandrill image down to the same size as the cameraman image as the mandrill image is bigger. In this case, I want to show you what the results look like with both images being the same size:

    Image1 = im2double(imread('cameraman.tif'));
    load mandrill;
    clear caption;
    Image2 = X / 255; clear X;
    Image2 = imresize(Image2, 0.5, 'bilinear');
    

    They look like the following:

    enter image description here

    enter image description here

    This is what happens when we swap the phase responses of both images and reconstruct the images after the fact:

    enter image description here

    enter image description here

    As you can see, the predominant structure of the images have been swapped. The cameraman is seen in the mandrill image and vice-versa. The point is that the phase plays an important part in the structure of objects in images, not simply just the magnitude... which I assume is the point of this exploratory exercise - to bring you to this conclusion.