How to convolve an image with a 2D function and plot the result
41 views (last 30 days)
Show older comments
David Okeson
on 14 Oct 2018
Commented: Sunil Vyas
on 21 Jul 2022
Hi, I have a bar phantom image attached that I want to convolve with the 2d function h1(x,y)=e^(-5x^2-5y^2). I'm not very familiar with Matlab or similar programs so bear with me.
I have created and plotted the function h1 using the following code:
x = [-1: .01: 1];
y = [-1: .01: 1];
[x_,y_] = meshgrid(x,y);
h1_ = exp((-5.*x_.^2)-(5.*y_.^2));
surf(x_,y_,h1_)
I then inserted the bar phantom image into Matlab using:
I = imread('bar phantom1.png');
From here I'm stuck and I've tried a number of things unsuccessfully. I've tried:
H1_ = abs(fft2(h1_));
I1 = abs(fft2(I));
g = ifft2(H1_.*I1);
And get the error: Array dimensions must match for binary array op.
I tried changing the dimensions of my x and y variables to match the dimensions of my image but the result g is a blank white image.
I also played tried to avoid using fft2 using imfilter, double, and conv2 but only got all white or black resulting images. Any help would be greatly appreciated.
0 Comments
Accepted Answer
Image Analyst
on 14 Oct 2018
Try this:
kernel = h1_ / sum(h1_(:));
filteredImage = conv2(double(I), kernel, 'same');
imshow(filteredImage, []);
3 Comments
Image Analyst
on 14 Oct 2018
For a color image, you can use imfilter() or split the color image into individual color channels and process each one at a time:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
filteredImageR = conv2(double(redChannel), kernel, 'same');
filteredImageG = conv2(double(greenChannel), kernel, 'same');
filteredImageB = conv2(double(blueChannel), kernel, 'same');
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!