gpt4 book ai didi

matlab - 在 Matlab 上通过高斯系数对图像进行卷积

转载 作者:行者123 更新时间:2023-12-04 07:37:34 25 4
gpt4 key购买 nike

clc;
clear all;
clc;

img = imread ('a.jpg');

img = rgb2gray(img);
figure, imshow(img);

img = double (img);

sigma= 2;
G= zeros(5,5);
for i=1:5
for j=1:5
x=[-2,1,0,-1,2];
y=[-2,1,0,-1,2];
G(i,j)= exp(-(x(i)^2+y(j)^2)/(2*(sigma^2)));
end
end

G_=G/sum(sum(G,1),2);

A= conv2(img,G);

figure, imshow(A);
这是我的图像平滑代码。
我首先计算了归一化的高斯滤波器系数。
对于最后一行,我尝试通过高斯滤波器系数对图像进行卷积,然后显示图像。
但是,它只显示一个白色的空图像。
我认为高斯滤波器系数的值是正确的,所以我认为问题出在卷积上。
卷积不是由 conv2(u,v) 在 matlab 上完成的吗?我究竟做错了什么?

最佳答案

看起来有两个小问题:

  • img = double(img)uint8 转换图像类型至 double .
    在 MATLAB 中,类型为 double 的图像应用像素范围 [0, 1]。
    1以上的所有像素都是白色的,所以显示的图像是白色的。
  • 您正在使用的内核 conv2未标准化。
    您正在使用 A = conv2(img, G)何时使用:A = conv2(img, G_) .

  • 对于第一个问题,有两种选择:
  • 而不是 img = double(img) ,您可以使用:img = im2double(img) (给出与 img = double(img)/255 相同的结果)。
  • 保留 img = double(img) ,但转换为 uint8之前 imshow(A) :imshow(uint8(A)) .

  • 这是更正代码的(选项之一):
    clc
    clear all

    img = imread('a.jpg');

    img = rgb2gray(img);
    figure, imshow(img);

    img = im2double(img); % im2double(img) instead of double(img).

    sigma = 2;
    G = zeros(5,5);
    for i = 1:5
    for j = 1:5
    x = [-2,1,0,-1,2];
    y = [-2,1,0,-1,2];
    G(i,j) = exp(-(x(i)^2+y(j)^2)/(2*(sigma^2)));
    end
    end

    G_ = G/sum(sum(G,1),2);

    A = conv2(img, G_); % G_ instead of G. For better margins: A = conv2(img, G_, 'same');

    figure, imshow(A);

    关于matlab - 在 Matlab 上通过高斯系数对图像进行卷积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67657483/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com