我编写了这段代码,通过将图像与水平和垂直导数滤波器进行卷积来计算每个像素的梯度向量,然后计算每个像素的梯度幅度,但我在 conv2
中得到以下错误:
Error using conv2 N-D arrays are not supported. Error in X (line 7) Gx=conv2(A,Sx)
A=imread('peppers.png');
figure,imshow(A); title('Sobel Gradient Before');
V= double (A);
Sx = [1,0,-1;2,0,-2;1,0,-1];
Gx=Conv2(A,Sx);
figure
imshow(Gx); title('x-direction');
Sy = [1,2,1;0,0,0;-1,-2,-1];
Gy=Conv2(A,Sy);
figure
imshow(Gy); title('y-direction');
G=sqrt(Gx.^2+Gy.^2);
figure,imshow(G); title('Sobel Gradient After');
最佳答案
对于彩色图像,imread
返回一个 3 维数组,其中第 3 维具有 3 个元素,分别为 R、G、B。您需要先将图像转换为灰度值。或者使用 convn
对彩色图像进行卷积。
要将图像转换为灰度,请使用 rgb2gray
功能。
关于matlab - 不支持使用 conv2 N-D 数组时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49542805/