0

该算法旨在将罗伯茨算子应用于图像,并将结果存储在新文件中。

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

我是 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 列的像素?

4

1 回答 1

2

这不符合你的想法:

for y=0 :1: y<I_H

你实际上想要:

for y = 0:I_H
于 2011-07-09T03:30:06.760 回答