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

29 次查看(过去 30 天)
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)

采纳的回答

DGM
DGM 2022-2-15
编辑:DGM 2022-2-15
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Descriptive Statistics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by