This algorithm is meant to apply a Roberts operator to an image, and store the result in a new file.
Instead, this code outputs the exact same image as the input.
I am new to Matlab, so your tips and feedback on my code are welcome.
I know there is a built in function for this purpose, I am doing this as an exercise.
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: trie开发者_开发百科d 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
EDIT - I have another question as well - in this example, if said x = Img(x,y), would that get the pixel at row x, column y, or the pixel at row y, column x?
This doesn't do what you think:
for y=0 :1: y<I_H
You actually want:
for y = 0:I_H
精彩评论