How to set conditions to modify the pixel values: (1) at the right(x+1,y)-and-left (x-1,y); and (2) at the top (x,y-1)-and-bottom (x,y+1) with reference to the central pixel (x,y)

1 view (last 30 days)
Hello!
I need your help to 'conditionally' modify the pixels around an arbitrary selected point/pixel at (x,y) using Matlab. First, please see the following 3 x 3 grid.
top-left (x-1,y-1);
top-middle (x,y-1);
top-right (x+1,y-1);
middle-left (x-1,y);
middle-middle(x,y);
middle-right (x+1,y);
bottom-left (x-1,y+1);
bottom-middle(x,y+1);
bottom-right (x+1,y+1);
As can be seen, in the following code - and thanks to a previously requested/obtained help - I could only modify the pixels at 8 locations adjacent to (x,y) but when I set the following conditions,
sdh = 0;
sdv = 0;
sdh = abs((1/4)*(Y(1:end-2,1:end-2)-Y(1:end-2,3:end)) + (1/2)*(Y(2:end-1,1:end-2)-Y(2:end-1,3:end)) + (1/4)*(Y(3:end,1:end-2)-Y(3:end,3:end)));
sdv = abs((1/4)*(Y(1:end-2,1:end-2)-Y(3:end,1:end-2)) + (1/2)*(Y(1:end-2,2:end-1)-Y(3:end,2:end-1)) + (1/4)*(Y(1:end-2,3:end)-Y(3:end,3:end)));
if if sdh >= sdv
(x+1,y) = (1/2)*((x+1,y)+(x+2,y));
(x,y+1) = (1/2)*((x,y+1)+(x,y+2));
and
if sdh < sdv
(x,y-1) = (1/2)*((x,y-1)+(x,y-2));
(x,y+1) = (1/2)*((x,y+1)+(x,y+2));
I can't modify the pixels at the locations (x+1,y) & (x-1,y) when sdh >= sdv; and, at the locations (x,y-1) & (x,y+1) when sdh < sdv, as indicated above. Can you please have a look at the following code and help me, if possible ?
N = 8;
E = [46,38];
X = magic(N);
Y = false(N+2);
for k = 1:numel(E);
Y(2:end-1,2:end-1) = X==E(k);
Z = Y(1:end-2,2:end-1) | Y(3:end,2:end-1) | Y(2:end-1,3:end) | Y(2:end-1,1:end-2) | Y(1:end-2,1:end-2) | Y(1:end-2,3:end) | Y(3:end,1:end-2) | Y(3:end,3:end);
X(Z) = E(k);
end

Answers (1)

Image Analyst
Image Analyst on 18 Jan 2016
I think you can do what you want with conv2(), but I cant' figure out what you want. What does sdh and sdv represent? Why are there no comments in the code? Did you strip them out before posting or you just forgot to put them in (both are bad)?
And in words, tell me what "sdh >= sdv" represents, and, again in words, what you want to do if that condition is true. Saying that you want to "modify" the pixel is so vague that it's useless.
And, finally, what is the purpose of the "E" variable?
  6 Comments
Image Analyst
Image Analyst on 19 Jan 2016
You're right - I still don't understand, perhaps because you didn't answer my questions.
I don't know what the full code is. Is it the top chunk of code that talks about sdf, etc. Or is it the second chunk of code that talks about magic?
In your second chunk of code, Z will be either true or false, not an index. Why are you using Z as an index into X?
Gobert
Gobert on 19 Jan 2016
Edited: Gobert on 19 Jan 2016
Sir, it is the second. Z is used that way so that pixels at the Y locations in Z can be changed according to the E(k) locations.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!