Azzera filtri
Azzera filtri

Check each pixel for a specific color

3 visualizzazioni (ultimi 30 giorni)
Shahd Ewawi
Shahd Ewawi il 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?

Risposte (2)

Doug Hull
Doug Hull il 27 Mar 2013
Modificato: Image Analyst il 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 il 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 Commenti
Image Analyst
Image Analyst il 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 il 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.

Accedi per commentare.

Categorie

Scopri di più su Image Processing Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by