Binary Image: Count number of pixels that are 1.

조회 수: 24 (최근 30일)
Justine Schneider
Justine Schneider 2017년 7월 3일
댓글: Image Analyst 2022년 2월 1일
The code below is a simplified version of the one part of a longer code that I am running into an error. In the code below, I successfully convert the gray scale image into a binary image. Then I try to count the number of pixels in the image that are 1 (by using the for loop and bin()). However, MATLAB says, "Undefined function or variable 'rows'." Then if I type a number instead of saying rows columns, I get the error, "if bin(i,j) == 1."
I've looked at the syntax of the bin command, and I have tried problem solving this. However, I don't know if the for loop knows to apply itself to the image. Please let me know how I can fix this for loop to work properly.
Code:
I = imread('rice.png');
figure; imshow(I);
bw = imbinarize(I);
figure; imshow(bw);
ctr= 0;
for i = 1 : rows
for j = 1 : columns
if bin(i,j) == 1
ctr = ctr + 1;
end
end
end

채택된 답변

Andrei Bobrov
Andrei Bobrov 2017년 7월 3일
May be so:
I = imread('rice.png');
figure; imshow(I);
bw = imbinarize(I);
figure; imshow(bw);
ctr= 0;
for i = 1 : rows
for j = 1 : columns
if bw(i,j) == 1
ctr = ctr + 1;
end
end
end
  댓글 수: 8
oussama zayene
oussama zayene 2022년 2월 1일
Did you resolve this, if yes can you please share the code ?
Thanks
Image Analyst
Image Analyst 2022년 2월 1일
I'm sure that after almost 2 years he did. It probably went something like this:
props = regionprops(bw, 'BoundingBox');
for k = 1 : numel(props)
thisBB = props(k).BoundingBox;
croppedImage = imcrop(bw, thisBB);
% Now do something with cropped image.....
end

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Jan
Jan 2017년 7월 3일
Alternatively without a loop:
bw = imbinarize(I);
ctr = sum(bw(:) == 1);
or even shorter for a binary image:
ctr = sum(bw(:));

James Tursa
James Tursa 2020년 4월 6일
nnz(bw)

Image Analyst
Image Analyst 2018년 2월 4일
편집: Image Analyst 2022년 2월 1일
If you want to know why your code didn't work, the problem with your code is that you forgot to define rows and columns for your image. After imread(), you need this:
[rows, columns, numberOfColorChannels] = size(I);
But better, like Jan said, you can simply could the number of white/true/1 pixels with this:
numWhite = sum(BW(:)) % numWhite is what you called ambiguously called ctr ("counter", "centroid"?)
No nested for loop is even needed.
Or even better is @James Tursa's answer:
numWhite = nnz(bw);

bipul
bipul 2018년 9월 23일
편집: bipul 2018년 9월 23일
function ctr = PixelCounter(bw)
%I = imread('rice.png');
%I = I(4:15, 18:29);
%figure; imshow(I);
%bw = im2bw(I)
%figure; imshow(bw);
ctr= 0;
[rows columns] = size(bw);
for i = 1 : rows
for j = 1 : columns
if bw(i,j) == 1
ctr = ctr + 1;
end
end
end
%fprintf('1 are: %d \n', ctr);
%disp(ctr);
end
  댓글 수: 1
Image Analyst
Image Analyst 2018년 9월 23일
편집: Image Analyst 2022년 2월 1일
Hopefully you don't also do this. The preferred way is to use nnz() like @James Tursa showed.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by