Binary image in Matlab
How can I know if an image read w开发者_StackOverflow社区ith imread
is binary in MATLAB
I did this :
Img = imread(IMGsrc);
T = Img== 1 | Img == 0;
If min(min(T)) == ??????
imshow(T);
end
????? = ??????
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);
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
or0
withT = I == 1 | I == 0;
. Ifmin(min(T))
comes back anything but1
then at least one pixel failed that test, meaning there is a value that is neither0
nor1
. (For that matter you could use a similar test to check for any number of enumerated values, not just0
and1
.)
If you can further clarify what you mean by "binary" that would go a long way to a better answer.
精彩评论