Binary Image: Count number of pixels that are 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

 Accepted Answer

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 Comments

after counting the number of white pixels how can we plot the white pixels along the rows and columns,can anyone help me regarding this?
verticalProfile = sum(binaryImage, 2);
plot(verticalProfile, 'b-', 'LineWidth', 2);
horizontalProfile = sum(binaryImage, 1);
hold on;
plot(horizontalProfile, 'g-', 'LineWidth', 2);
likewise plotting the whitepixels I want to take histogram for whitepixels alone in the binary image can you help me for this thanks in advance.
Such a histogram would have only two rows. One bin for black pixels, and one bin for white pixels because a binary image does not have any other intensities in the distribution. So, what's the point? If you want you can call
imhist(binaryImage)
but again, what's the point?
this is my input image i have to crop the white parts as three separate images, how can i separate it for various images like this? thanks in advance.
Did you resolve this, if yes can you please share the code ?
Thanks
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

Sign in to comment.

More Answers (4)

Alternatively without a loop:
bw = imbinarize(I);
ctr = sum(bw(:) == 1);
or even shorter for a binary image:
ctr = sum(bw(:));
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 on 23 Sep 2018
Edited: bipul on 23 Sep 2018
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 Comment

Hopefully you don't also do this. The preferred way is to use nnz() like @James Tursa showed.

Sign in to comment.

Categories

Find more on Images 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!