Check each pixel for a specific color

4 Ansichten (letzte 30 Tage)
Shahd Ewawi
Shahd Ewawi am 27 Mär. 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?

Antworten (2)

Doug Hull
Doug Hull am 27 Mär. 2013
Bearbeitet: Image Analyst am 27 Mär. 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 am 27 Mär. 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 Kommentare
Image Analyst
Image Analyst am 27 Mär. 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 am 28 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Images finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by