How can I detect a color percentage in a picture?

12 ビュー (過去 30 日間)
fatema ali
fatema ali 2019 年 11 月 20 日
コメント済み: DGM 2022 年 5 月 17 日
I have a picture with different colors, but I mainly need the precentage of three colors. how can I do that in matlab?
  2 件のコメント
Daniel M
Daniel M 2019 年 11 月 20 日
編集済み: Daniel M 2019 年 11 月 20 日
What do you mean percentage of a color?
Say you have a picture composed of some red, blue, and yellow. Do you mean you want to get the percentage of the picture that is red (etc.)?
If this is true, then, follow up question: are you looking for exact, unique colors (as in, it would consider different shades of red to be different colors), or you want a broader sense (as in, rgb values of [1 0 0] and [0.9 0 0] would both be considered red)?
fatema ali
fatema ali 2019 年 11 月 20 日
編集済み: fatema ali 2019 年 11 月 20 日
yes exactly, like the precentage of red in a certain picture.
I want a broader sense of colors, like all shades of red are considered as one thing and so on.

サインインしてコメントする。

回答 (1 件)

Kavya Vuriti
Kavya Vuriti 2020 年 2 月 17 日
Hi,
It would be better if you first determine the RGB triplet limits for a specific color and count the number of pixels lying in the above range. The sample code could look like this:
% Read image
I = imread('your_image');
% Define RGB limits
redLowerLimit = your_value;
redUpperLimit = your_value;
blueLowerLimit = your_value;
blueUpperLimit = your_value;
greenLowerLimit = your_value;
greenUpperLimit = your_value;
% Number of pixels of required colour
requiredColor = ( I(:,:,1) >= redLowerLimit & I(:,:,1) <= redUpperLimit ) & ...
( I(:,:,2) >= greenLowerLimit & I(:,:,2) <= greenUpperLimit ) & ( I(:,:,3) >= blueLowerLimit & I(:,:,3) <= blueUpperLimit );
% Percentage of colour in the image
percentColor = 100*(sum(sum(requiredColor))/(size(I,1)*size(I,2)));
Hope this helps.
  2 件のコメント
Aishwarya Ashok kumar
Aishwarya Ashok kumar 2022 年 5 月 17 日
As this code is showing us an error can u help us go solve this error
DGM
DGM 2022 年 5 月 17 日
You need to define the variables. You need to specify an image and select threshold values that are appropriate for your own needs.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by