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)
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Descriptive Statistics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!