【问题标题】:What's the issue in this MATLAB Prewitt operator implementation?这个 MATLAB Prewitt 运算符实现有什么问题?
【发布时间】:2015-07-02 20:26:21
【问题描述】:

我正在尝试编写 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;

我可以获得的:

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

这是原图:

【问题讨论】:

    标签: matlab image-processing edge-detection


    【解决方案1】:

    考虑以下代码:

    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] 值范围之外的结果。

    【讨论】:

      【解决方案2】:
      for j=1:rows-2;
      

      必须

      for j=1:cols-2;
      

      【讨论】:

      • 我得到一个完整的白色图像
      • @diegoaguilar 它对我有用。 j 都改了吗?
      猜你喜欢
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 2011-09-28
      • 2011-12-14
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多