Currently I am trying to convolve an image with a Gaussian function and am getting a white screen.
39 ビュー (過去 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 で Signal Generation and Preprocessing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!