필터 지우기
필터 지우기

Check each pixel for a specific color

조회 수: 3 (최근 30일)
Shahd Ewawi
Shahd Ewawi 2013년 3월 27일
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