views:

57

answers:

2

How can I know if an image read with imread is binary in MATLAB

I did this :

Img = imread(IMGsrc);

T = Img== 1 | Img == 0;

If min(min(T)) == ??????

    imshow(T);

end

????? = ??????

A: 

Assuming by "binary" you mean "every pixel is either 1 or 0", a couple things given your image I:

  • size(I) should only return rows and columns (not channels) otherwise it is not binary
  • You can test every pixel is either 1 or 0 with T = I == 1 | I == 0;. If min(min(T)) comes back anything but 1 then at least one pixel failed that test, meaning there is a value that is neither 0 nor 1. (For that matter you could use a similar test to check for any number of enumerated values, not just 0 and 1.)

If you can further clarify what you mean by "binary" that would go a long way to a better answer.

fbrereto
I'm trying to do this.If Img is Binary Then ImgToBin = im2bw(Img); End IfI mean by binary (image with black and white colors).
dotNET
+2  A: 

There are two ways you can test for binary images.

The simplest one is to test whether the image is a logical array (a logical array is returned by functions in the image processing toolbox that return binary image)

isBinaryImage = islogical(img);

Alternatively, you check whether all pixels are either 1 or 0

isBinaryImage = all( img(:)==0 | img(:)==1);
Jonas