Search code examples
imageif-statementcoordinatespixel

How to write an if else statement in matrix matlab


I am trying to read an image and get the x,y and pixel coordinates. It is an RGB image which has a size (282,282,3). However I get pixel coordinates matrix of (282*3, 282, pixel values). Further, though the if else condition is working for normal given values, in this code, it is not working. Can anyone help me find where I went wrong?

clear all
clc

A = double(imread('F:\02.jpg'));
size(A)
[length, width] = size(A);
[x, y] = meshgrid(1:width, 1:length);
z(:) = A(:)/255;
if (z >=0.50000)
   z =1;
   elseif(z <0.50000)
   z=0;
end
Z = z(:)

Solution

  • As z is not a scalar but matrix or vector a logical comparison z >= val also results in a matrix/vector (of mixed ones and zeros). What you can do is to use this result as indexing, for example

    ix = z >= 0.5;
    z( ix) = 1;
    z(~ix) = 0;