After carrying out texture segmentation techniques on an image and then applying a binary mask I am trying to create an if else statement that will label the figure as a fail if the pixel count for black pixels in binary mask is above 1?

1 view (last 30 days)
I am relatively new to this so New ideas are welcome, another way around this may be to have a function that will scan through the binary image pixel by pixel and if it picks up black pixels then how will I create an if else statement to label or have a print function saying picture equals 'FAIL'??
Here is my code:
I = imread('CYMDK_ABS.jpg') //read image in//
%figure, imshow(I) // display initial image//
E = entropyfilt(I) // apply entropy filter//
Eim = mat2gray(E) // scale it down to a double image//
imshow(Eim) // show updated image//
BW1 = im2bw(Eim, .3) // apply binary mask with threshold value .3//
defect = true // define variable defect//
count = 0 // define counter //
if count>1
[row col]=find(BW1==0) // this function returns all pixels equal to 0//
defect = true
else defect = false
count = count + 1
end
%imshow(BW1)

Accepted Answer

Image Analyst
Image Analyst on 14 Nov 2013
It's not a very useful binary image if it must be all completely white. But anyway, to detect if there is a black pixel somewhere in the image:
if any(~BW1(:))
% At least one pixel is black.
end
  2 Comments
Cormac
Cormac on 15 Nov 2013
Using the threshold '.3' was the best way to highlight the cluster. I could then use the ~BW1 to reverse the pixels when viewing it so the cluster would appear as white pixels rather than black. However thank you for the response. I appreciate the help.
Out of curiosity does this function scan through each pixel in the image one by one??
Image Analyst
Image Analyst on 15 Nov 2013
any() looks at the image and processes each column, like many MATLAB functions such as sum(), etc. To avoid getting an array of numbers (one for each column) I turn the 2D array into a 1D array by using (:). Then any() checks to see if there is any pixel in that 1D array that is non-zero. Inverting it will tell you if there is at least one pixel that is exactly zero.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!