gpt4 book ai didi

matlab - 这个 MATLAB Prewitt 运算符实现有什么问题?

转载 作者:太空宇宙 更新时间:2023-11-03 20:18:08 25 4
gpt4 key购买 nike

我正在尝试编写用于边缘检测的 Prewitt 运算符的实现代码。到目前为止,我已经尝试过:

openImage = im2double(rgb2gray(imread(imageSource)));
[rows,cols] = size(openImage);

N(1:rows,1:cols)=0;
for i=1:rows-2;
for j=1:rows-2;
N(i,j)=-1*openImage(i,j)-1*openImage(i,j+1)-1*openImage(i,j+2)+0+0+0+1*openImage(i+2,j)+1*openImage(i+2,j+1)+1*openImage(i+2,j+2);
end;
end;
O(1:rows,1:cols)=0;
for i=1:rows-2;
for j=1:rows-2;
O(i,j)=-1*openImage(i,j)+0+1*openImage(i,j+2)-1*openImage(i+2,j)+0+1*openImage(i+1,j+2)-1*openImage(i+2,j)+0+1*openImage(i+2,j+2);
end
end
Z = N + O;

为此我可以获得:

enter image description here

可以看出,该图像处理了一半图像,一半空白。我做错了什么?

这是原始图片:

enter image description here

最佳答案

考虑以下代码:

I = im2double(rgb2gray(..));
[rows,cols] = size(I);

N = zeros(size(I));
for i=2:rows-1;
for j=2:cols-1;
N(i,j) = 1*I(i-1,j-1) + 1*I(i-1,j) + 1*I(i-1,j+1) + ...
0 + 0 + 0 + ...
-1*I(i+1,j-1) + -1*I(i+1,j) + -1*I(i+1,j+1);
end
end

O = zeros(size(I));
for i=2:rows-1;
for j=2:cols-1;
O(i,j) = 1*I(i-1,j-1) + 0 + -1*I(i-1,j+1) + ...
1*I(i,j-1) + 0 + -1*I(i,j+1) + ...
1*I(i+1,j-1) + 0 + -1*I(i+1,j+1);
end
end

Z = sqrt(N.^2 + O.^2);
imshow(Z)

然后 compare it到:

Gx = conv2(I, [-1 0 1; -1 0 1; -1 0 1]);
Gy = conv2(I, [-1 -1 -1; 0 0 0; 1 1 1]);

G = sqrt(Gx.^2 + Gy.^2);
T = atan2(Gy, Gx);

imshow(G)

请注意,您可能必须使用 imshow(result,[]) 才能正确显示 [0,1] 值范围之外的结果。

关于matlab - 这个 MATLAB Prewitt 运算符实现有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31183602/

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