convolution of two image in frequency domain?

x= 1:150;y=1:150;
[p,q]=freqspace(150);
[X,Y]=meshgrid(p,q);
R=(X.^2 + Y.^2);
Lambda=633*10^-9;
dis=10*10^-3;
F = (exp(i.*pi.*R))./(Lambda.*dis);
kernel = imag(F); % Gray scale image = imaginary part.
mesh(kernel);
axis square
imshow(kernel, []);
axis on;
colormap(gray(256));
rgbImage = imread('image.jpg');
imshow(rgbImage);
axis on;
% R=rgbImage(:,:,1);
% G=rgbImage(:,:,2);
% B=rgbImage(:,:,3);
grayImage = rgb2gray(rgbImage);
imshow(grayImage, []);
axis on;
grayImage1=fft2(double(grayImage));
kernel1=fft2(kernel);
Imagef=kernel*grayImage1;
Imagefinal=ifft2(Imagef);
imshow(Imagefinal, []);
axis on;
This code has no errors. Its output should be like a hazy image of concentrating circle or may be overlapped, but it shows hazy straight line. Please help.

2 Comments

Are you sure this code has no erros? kernel1=fft2(kernel);
BUT
Imagef=kernel*grayImage1;
NOT kernel1?
Why * not .*?
@Linas Svilainis I think they meant to say "This code has errors." The problem is to multiply the spectrum of a kernel times a spectrum of a filter, you'd need to use .* (dot star) instead of *, and you'd need to make sure they both have the same size (number of pixels).

Sign in to comment.

 Accepted Answer

See my demos, attached. They're good examples that show you how it's done. Once you understand and run those I have no doubt you will be able to do the same thing with your image and kernel.

13 Comments

thanks. when i am able to do it then i will accept your answer ok?
OK, good luck. Post your code if you run into trouble.
Sir I did....i hope now i have done it correctly...
x= 1:256;y=1:256;
[p,q]=freqspace(256);
[X,Y]=meshgrid(p,q);
R=(X.^2 + Y.^2);
Lambda=633*10^-9;
dis=10*10^-3;
F = (exp(i.*pi.*R))./(Lambda.*dis);
kernel = imag(F); % Gray scale image = imaginary part.
mesh(kernel);
axis square
imshow(kernel, []);
axis on;
colormap(gray(256));
rgbImage = imread('image.jpg');
imshow(rgbImage);
axis on;
% R=rgbImage(:,:,1);
% G=rgbImage(:,:,2);
% B=rgbImage(:,:,3);
grayImage = rgb2gray(rgbImage);
imshow(grayImage, []);
axis on;
fftOriginal = fft2(double(grayImage));
shiftedFFT = fftshift(fftOriginal);
frequencyimage=fftshift(fft2(kernel));
imagef=shiftedFFT.*frequencyimage;subplot(2,3,1);imshow(imagef, []);
imagefinal=ifft2(fftshift(imagef));
image3=abs(imagefinal);subplot(2,3,2);
imshow (image3, []);
image4=imag(imagefinal);subplot(2,3,3);
imshow (image3, []);
Warning: Displaying real part of complex input.
> In imuitools\private\imageDisplayValidateParams>validateCData at 149
In imuitools\private\imageDisplayValidateParams at 31
In imuitools\private\imageDisplayParseInputs at 79
In imshow at 199
You did what ? Run into trouble or finish it? It looks fine to me, at least it does what you tell it to do.
TULIKA
TULIKA on 24 Aug 2014
Edited: TULIKA on 24 Aug 2014
i completed the program ....so i said that what i had done it is ok or not.... really thanks. it helps me a lot..i hope the program is ok....there is no error..but some warning..
Yeah, it's fine, though you might also want to display the imaginary part along with the real part, and since it's doing the conversion from complex to real and throwing out a warning, why not avoid the warning and just take the real part yourself before display?
for real part it also shows the warning...
Which line exactly? You forgot to include that vital information. And why do you display image3 twice:
imshow (image3, []);
image4=imag(imagefinal);subplot(2,3,3);
imshow (image3, []);
Why not show image4 instead?
yeah typing mistake in the program.. i had done that later then it shows full black box for imaginary part...is there any wrong coding in the program???in which line??what vital information did i forget to include?
Well, like what line of your code caused the error. Sure the error came later in imshow() But what line of your code caused it? Maybe Warnings don't show that and only error messages to - I don't know. Did you fix the line I told you to and still get the warning? If so, find out what line is causing it and copy all the orange text. Also call title() and give captions to the images you display to help us understand better what we're seeing.
this is the program what I've written last and got this output
x= 1:256;y=1:256;
[p,q]=freqspace(256);
[X,Y]=meshgrid(p,q);
R=(X.^2 + Y.^2);
Lambda=633*10^-9;
dis=10*10^-3;
F = (exp(i.*pi.*R))./(Lambda.*dis);
kernel = imag(F); % Gray scale image = imaginary part.
mesh(kernel);
axis square
imshow(kernel, []);
axis on;
colormap(gray(256));
rgbImage = imread('image.jpg');
imshow(rgbImage);
axis on;
% R=rgbImage(:,:,1);
% G=rgbImage(:,:,2);
% B=rgbImage(:,:,3);
grayImage = rgb2gray(rgbImage);
imshow(grayImage, []);
axis on;
fftOriginal = fft2(double(grayImage));
shiftedFFT = fftshift(fftOriginal);
frequencyimage=fftshift(fft2(kernel));
imagef=shiftedFFT.*frequencyimage;
subplot(2,3,1);
imshow(imagef, []);
title('fft of image');
imagefinal=ifft2(fftshift(imagef));
image3=real(imagefinal);subplot(2,3,2);
imshow (image3, []);
title('real part of image');
image3=imag(imagefinal);subplot(2,3,3);
imshow (image4, []);
title('imaginary part of image');
Warning: Displaying real part of complex input.
> In imuitools\private\imageDisplayValidateParams>validateCData at 149
In imuitools\private\imageDisplayValidateParams at 31
In imuitools\private\imageDisplayParseInputs at 79
In imshow at 199
This is one situation where clear all is good. See how you were able to display image4? But you never defined image4, so where did that image4 come from? Not from this running of the program. It must have existed already. Who knows how many other times this happened with other variables. Either call clear all or put it in the front of your script.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!