Check each pixel for a specific color

6 views (last 30 days)
Shahd Ewawi
Shahd Ewawi on 27 Mar 2013
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?

Answers (2)

Doug Hull
Doug Hull on 27 Mar 2013
Edited: Image Analyst on 27 Mar 2013
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 on 27 Mar 2013
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 Comments
Image Analyst
Image Analyst on 27 Mar 2013
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 on 28 Mar 2013
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.

Sign in to comment.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!