How to detect percentage of a color range inside an Image?

12 views (last 30 days)
salma salma
salma salma on 19 Jan 2022
Answered: DGM on 19 Jan 2022
Hello
I'm working on this coin detection project using matlab and I wanna know how to detect the color of the center of a coin.
I have these intervals that define the color I want : minvalue = [5 75 85], maxvalue = [50 255 255] and I wanna calculate the percentage of this interval inside an Image.
How can I do that? I did it using inrange with opencv but I couldn't figure it out on matlab.

Answers (1)

DGM
DGM on 19 Jan 2022
Something like this:
A = imread('coloredChips.png'); % i just grabbed a picture that had some greens
imshow(A)
minvalue = [5 75 85];
maxvalue = [50 255 255];
mask = A(:,:,1)>=minvalue(1) & A(:,:,1)<=maxvalue(1) ...
& A(:,:,2)>=minvalue(2) & A(:,:,2)<=maxvalue(2) ...
& A(:,:,3)>=minvalue(3) & A(:,:,3)<=maxvalue(3);
imshow(mask)
% this is the fraction of the image that lies within
% the rectangular prism defined by the two corner points
maskcoverage = mean(mask,'all')
maskcoverage = 0.0305

Community Treasure Hunt

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

Start Hunting!

Translated by