How to find number of Pixel green colour image by using webcamera

1 回表示 (過去 30 日間)
clc
clear
clear all
images= []
webcamlist()
cam= webcam(2)
preview(cam)
pause(2);
images= snapshot(cam)
imshow(images)
r_channel= images(:,:,1);
g_channel= images(:,:,2);
b_channel= images(:,:,3);
images(:,:,1) =0;
images(:,:,3) = 0;
image(images);
mask= g_channel > 100, b_channel > 800, r_channel < 800, b_channel < 0, r_channel < 0;
numBlackPixels = sum(~mask(:))
gr_ratio = double(g_channel)./double(r_channel);
gb_ratio = double(g_channel)./double(b_channel);
rb_ratio = double(r_channel)./double(b_channel);
% gr_ratio(isnan(g_ratio))=0;
% grayImage = rgb2gray(image);
% binaryImage = grayImage > 20;
% binaryImage = bwareaopen(binaryImage, 50);
% binaryImage = imfill(binaryImage, 'holes');
% grayImage(~binaryImage) = 0;
falseMatrix= zeros(7,7);
targetMatrix= ones(7,7);
checkMatrix= ones(7,7);
fprintf('mask has %d black pixels.\n', numBlackPixels);
if targetMatrix == checkMatrix
% cam = camStop;
return
end
  2 件のコメント
DGM
DGM 2021 年 10 月 13 日
編集済み: DGM 2021 年 10 月 13 日
What exactly are you trying to do?
This
mask = g_channel > 100, b_channel > 800, r_channel < 800, b_channel < 0, r_channel < 0;
Is the same as this
mask = g_channel > 100, % only this part is assigned to mask
b_channel > 800, % this does nothing
r_channel < 800, % this does nothing
b_channel < 0, % this does nothing
r_channel < 0; % this does nothing
which, other than dumping a bunch of unused garbage to console, does the same as this:
mask = g_channel > 100;
It should stand to reason that not all colors described by this mask are green (e.g. [255 255 255]).
One might expect that this is a set of upper and lower thresholds for each color channel being applied, but the values and relations are nonsense for what I'm assuming is a uint8 image.
mask = g_channel > 100 & b_channel > 800 & r_channel < 800 & b_channel < 0 & r_channel < 0;
Why test that B and R are above/below 800, when they won't be above 255? How would B or R ever be less than zero?
Since you're asking how to count "green pixels", it's up to you to define the limits of what constitutes "green".
Iszuzuldin amirull bin mohd janudin
Iszuzuldin amirull bin mohd janudin 2021 年 10 月 27 日
Thanks for Explaination

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

採用された回答

Image Analyst
Image Analyst 2021 年 10 月 13 日
Use the Color Threshold app on the Apps tab of the tool ribbon to define "green". Full demo is attached.
  4 件のコメント
Image Analyst
Image Analyst 2021 年 10 月 27 日
@Iszuzuldin amirull bin mohd janudin, certainly. To get yellow...
  1. Like I said, run the Color Thresholder app on the Apps tab of the tool ribbon. Did you try this?
  2. Choose HSV color space.
  3. Adjust the hue arrows to select only yellow.
  4. Adjust the Saturation thresholds to be about from 0.25 to 1.
  5. Click the Export Function button to get the code that you can call from your main code.
Iszuzuldin amirull bin mohd janudin
Iszuzuldin amirull bin mohd janudin 2021 年 10 月 27 日
I have tried it, it worked. Thanks for sharing

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

その他の回答 (1 件)

yanqi liu
yanqi liu 2021 年 10 月 14 日
sir,may be you should upload your image file to develop some function

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by