필터 지우기
필터 지우기

How to count the number of pixels from an image for specifies colors?

조회 수: 7 (최근 30일)
Jotheeshwar V
Jotheeshwar V 2018년 7월 4일
편집: Jan 2019년 6월 11일
I have used the code stated below.
if true
% code
imread color.jpg
load color.jpg
redChannel = color(:, :, 1);
greenChannel = color(:, :, 2);
blueChannel = color(:, :, 3);
whitePixels = redChannel == 255 & ...
greenChannel == 255 & ...
blueChannel == 255;
count = sum(whitePixels(:));
Red1Pixels = redChannel == 255 & ...
greenChannel == 166 & ...
blueChannel == 166;
count = sum(Red1Pixels(:));
Red2Pixels = redChannel == 254 & ...
greenChannel == 85 & ...
blueChannel == 82;
count = sum(Red2Pixels(:))
Blue1Pixels = redChannel == 179 & ...
greenChannel == 169 & ...
blueChannel == 250;
count = sum(Blue1Pixels(:))
Blue2Pixels = redChannel == 255 & ...
greenChannel == 255 & ...
blueChannel == 255;
count = sum(Blue2Pixels(:))
end
and I am getting error as below.
if true
% code
Index exceeds matrix dimensions.
Error in Try_03_07_2018 (line 17) greenChannel = color(:, :, 2); and Try_03_07_2018 is the file name.
Kindly help me out of this.
Regards,
Jotheeshwar V

채택된 답변

Jan
Jan 2018년 7월 4일
편집: Jan 2018년 7월 4일
Neither
imread color.jpg
nor
load color.jpg
import the pixels of the image to th variable color. Use this instead:
color = imread('color.jpg');
By the way: I'd expect load color.jpg to show an error already. So are you sure that your code run at all?
  댓글 수: 3
Image Analyst
Image Analyst 2018년 7월 5일
Did you try my answer below???
Jotheeshwar V
Jotheeshwar V 2018년 7월 5일
Yes, It worked out. Thanks a lot

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2018년 7월 4일
Replace this
imread color.jpg
load color.jpg
with this
fileName = fullfile(pwd, 'color.jpg');
color = imread(fileName );
[rows, columns, numberOfColorChannels] = size(color);
if numberOfColorChannels < 3
errorMessage = sprintf('%s is not a color image!', fileName)
uiwait(errordlg(errorMessage));
return;
end

komal
komal 2019년 6월 7일
After doing skin detection. I want to count the pixels of white region which indicates skin part shown in the image.How can I do it? Please help me out
  댓글 수: 2
Image Analyst
Image Analyst 2019년 6월 7일
numWhitePixels = nnz(skinMask);
where skinMask is your binary image that you get after doing the skin detection (which I assume you already know how to do that part).
Jan
Jan 2019년 6월 11일
편집: Jan 2019년 6월 11일
@komal: Please do no append a new question in the section for answers of another thread. Ask a new question instead.
"Not working" is a lean description of our problem. Please mention any details. Of course you can use nnz to count elements in an array. So if something is not working for you, you have to explain, what the inputs are, what you want to achieve and what you get instead: a result which differs from your expectations, or an error message?
Afert opening a new thread, delete this pseudo answer here. Thanks.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by