Matlab: How can I find the number of black pixels in a color image?

I want to find the number of black pixels in a color image using MATLAB, how can I do that?

Thank.

+3


source to share


1 answer


Use nnz

like this -

nnz(all(im==0,3))

      

where im

is your image data.

The alternatives are sum(reshape(all(im==0,3),[],1))

and sum(sum(all(im==0,3)))

.



The black pixels are assumed to be triplets (0,0,0)

.


Instead, if you are defining black pixels as pixels that have range values [0 th]

for the same pixel location across all channels, use this -

nnz(all(im<=th,3))

      

+7


source







All Articles