0

このアルゴリズムは、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 のピクセルを取得しますか?

4

1 に答える 1

2

これはあなたが思うことをしません:

for y=0 :1: y<I_H

あなたは実際に欲しい:

for y = 0:I_H
于 2011-07-09T03:30:06.760 に答える