在图像上使用Roberts运算符

时间:2011-07-08 23:24:02

标签: matlab edge-detection convolution

此算法旨在将Roberts运算符应用于图像,并将结果存储在新文件中。

相反,此代码输出与输入完全相同的图像。

我是Matlab的新手,所以欢迎您提供有关我的代码的提示和反馈。

我知道有一个内置函数用于此目的,我这样做是为了练习。

function [] = Roberts(filename)
%somehow, it outputs the exact same image back.
%I know that this doesn't include the y component of the Roberts operator'

Img = imread(filename);
NewImg = Img;

SI = size(Img);

I_W = SI(2)
I_H = SI(1)

Roberts = [1,0;0,-1];

M_W = 2;
y = 0;
x = 0;
M_Y = 0;
M_X = 0;
%I initialized these counters here, because Matlab told me that these variables were
%used before they were initialized. This is strange, because they are initialized in the for loop, correct?
    for y=0 :1: y<I_H
        for x=0 :1: x<I_W 

            S = 0;

            for M_Y = 0 :1: M_Y < M_W
                for M_X = 0 :1: M_X < M_W

                    if (x + M_X - 1 < 0) || (x + M_X - 1 > I_W)
                       S = 0;      
                       disp('debug: tried to go beyond the image, value of that component, set to 0');

                    elseif (y + M_Y - 1 < 0) || (y + M_Y - 1 > I_H)
                       S = 0; 
                       disp('debug: tried to go beyond the image, value of that component, set to 0');

                    else
                        S = S + Img(x + M_X - 1, y + M_Y - 1) * Roberts(M_X,M_Y);
                    end
                end

            end

            NewImg(x,y) = S;
        end


    end  


imwrite(NewImg,'Roberts.bmp');
end

编辑 - 我还有另一个问题 - 在这个例子中,如果说x = Img(x,y),那会得到第x行,第y列或第y行,第x列的像素吗? / p>

1 个答案:

答案 0 :(得分:2)

这不符合您的想法:

for y=0 :1: y<I_H

你真的想要:

for y = 0:I_H