Clear Filters
Clear Filters

Currently I am trying to convolve an image with a Gaussian function and am getting a white screen.

39 views (last 30 days)
When I am trying to get the convolution I am getting blank screen. The code I have for this is given by:
p = imread('barphantom.png');
gp = rgb2gray(p)
figure, imshow(p) %shows the image
n = 1132;
m = 755;
sigma = 5
h = zeros(n,m);
for x=1:n
for y=1:m
h(x,y) = exp(-((x-n/2)^2+(y-m/2)^2)/(2*sigma^2));
end
end
figure, imshow(h) % shows the gaussian figure
k = conv2(gp, h,'same')
figure, imshow(k)

Accepted Answer

DGM
DGM on 15 Feb 2022
Edited: DGM on 15 Feb 2022
Sum-normalize the filter kernel
gp = imread('cameraman.tif');
n = 1132;
m = 755;
sigma = 5;
h = zeros(n,m);
for x=1:n
for y=1:m
h(x,y) = exp(-((x-n/2)^2+(y-m/2)^2)/(2*sigma^2));
end
end
h = h/sum(h(:)); % normalize the filter
%imshow(h) % shows the gaussian figure
k = conv2(im2double(gp), h,'same');
imshow(k)
Your filter also is wayyyy bigger than it needs to be. That only slows down the convolution. Depending on how much support you want, you can make it quite small. For comparison, imgaussfilt() creates the filter such that its width and height are 2*ceil(2*sigma)+1. You might want more support, so pick 2*ceil(3*sigma)+1 or 2*ceil(4*sigma)+1.

More Answers (0)

Categories

Find more on Signal Generation and Preprocessing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!