How can i count the number of pixels for a specific color using the L*A*B?
1 view (last 30 days)
Show older comments
Stephani Kanga
on 26 Jun 2020
Commented: Image Analyst
on 28 Jun 2020
I want to write a code that will count the number of pixels for all the tints of a specific color in an image.
0 Comments
Accepted Answer
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
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.
More Answers (0)
See Also
Categories
Find more on Geometric Transformation and Image Registration in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!