フィルターのクリア

How to count the number of pixels of a certain color in an image?

65 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2018 年 9 月 10 日
回答済み: MathWorks Support Team 2019 年 5 月 14 日
I have an image and I want to count the number of red pixels in an image. How do I do that?

採用された回答

MathWorks Support Team
MathWorks Support Team 2019 年 5 月 15 日
You can use logical indexing with the matrix of RGB values to determine which pixels are red. A "pure" red will have RGB value (255,0,0), but a red color may be considered within a range around that. To help illustrate, refer to the example code below. Note that you may need to tweak the precise pixel intensity thresholds for each channel, as per your application-specific requirements and approximated range for what is considered red and what is not.
%%Load image
rgb = imread('peppers.png');
figure(1)
imshow(rgb)
%%Find red points
redPoints = rgb(:,:,1)>=130 & rgb(:,:,2)<=60 & rgb(:,:,3)<=100;
percentRed = 100*(sum(sum(redPoints))/(size(rgb,1)*size(rgb,2)));
fprintf('Image has %d red pixels\n',sum(sum(redPoints)))
fprintf('Image is %.2f percent red\n',percentRed)
%%Highlight red on image
rgbRed = uint8(cat(3,redPoints,redPoints,redPoints)).*rgb;
figure(2)
imshow(rgbRed)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImages についてさらに検索

タグ

タグが未入力です。

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by