How can i count the number of pixels for a specific color using the L*A*B?

1 view (last 30 days)
I want to write a code that will count the number of pixels for all the tints of a specific color in an image.

Accepted Answer

Image Analyst
Image Analyst on 26 Jun 2020
Just make a mask and use nnz():
lMask = lImage > Lmin & lImage <= Lmax;
aMask = aImage > Amin & aImage <= Amax;
bMask = bImage > Bmin & bImage <= Bmax;
overallMask = lMask & aMask & bMask;
numberOfPixels = nnz(overallMask)
Of you can make a 3-D histogram.
Look at colorcloud().
  4 Comments
Stephani Kanga
Stephani Kanga on 28 Jun 2020
Yes for example i have this image and i want to calculate the % of the pixels that have dark purple-black color.
Image Analyst
Image Analyst on 28 Jun 2020
OK. So did you do it? The demo gives you the class for each pixel so just sum them up and divide by the number of pixels and multiply by 100 to get percent area for each class.
classCounts = histcounts(pixel_labels)
percentAreas = 100 * classCounts / numel(pixel_labels)
The tricky part is you have to know which class is dark purple but you can look at the cluster centers to get the color of each class and then you can figure out what class number dark purple is. That class number could possibly vary each time you run the program because kmeans uses random numbers.

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!