Check each pixel for a specific color

3 次查看(过去 30 天)
I want to check the value of each pixel in image to see if the image is black and i get this error " Attempted to access l(0,0); index must be a positive integer or logical."
for i=0:m
for j=0:n
if l(i,j)==0
count=count+1;
end
end
Does anyone know the best way to do this?

回答(2 个)

Doug Hull
Doug Hull 2013-3-27
编辑:Image Analyst 2013-3-27
MATLAB uses ones based indexing. There is no 0,0 element. Try starting at 1,1.
A better way is like this:
a = [11 22 33; 22 11 44; 11 11 11]
a==11 %returns binary matrix
nnz(a==11) %counts the number of 11's in the matrix

Ahmed A. Selman
Ahmed A. Selman 2013-3-27
Try
I=imread('imageHere');
[a,b,c]=size(I); % change is needed
for i=1:a
for j=1:b
F(i,j)=all(I(i,j)==0);
end
end
everywhere you got 0 in the color picture you'll get a value of one in the logical matrix F. If your image was already black-white, replace and use
[a,b]=size(I);
  2 个评论
Image Analyst
Image Analyst 2013-3-27
all() is unnecessary because you're looking at just a single element. Anyway, the whole thing can be done in one line like Doug showed:
F = I==0;
The 2D matrix F will be true (1) wherever (the badly-named) I is zero.
Ahmed A. Selman
Ahmed A. Selman 2013-3-28
I agree, the bit of code is written to the whole image while the needed is (perhaps) for a single pixel. Yet, the original question is to detect single pixel _for a _specific color. That is quite odd. Regards.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by