How to count the number of pixels inside a region

Hello,
I have this Image, all I want to do is count the number of pixels inside a circular region. I tried selecting the ROI interactively, now I have a masked imaged that is all black around the area I am interested in and I still am not sure how to find the number of pixels. I thought that if I selected that region I would be able to just use the numel function to get the number of pixels,but it returns the number of pixels of the whole image, which is the same amount of pixels as the original image...
What I am showing here, is the original image with a circle around (made in paint because its too complicated to show what i did with matlab and this is not letting me upload more than one image)
I spent hours on this and read all articles about segmentation, ROI, pixels, etc and nothing helps...Can someone point me in the right direction? I feel like this is a simple task but there is no helpful information available about it anywhere.
Another question... Does my Image have to be binary or grayscale to do these analysis? My orginal image is a tif, but I converted to binary based on what I read in some articles.
Thanks!

Answers (2)

There are two ways (at least). Since you have a binary image that is black/0/false all around your circle, and white/1/true inside your circular ROI, you can do this
area1 = bwarea(mask) % Counts "fractional" pixels according to an algorithm.
area2 = nnz(mask); % Counts whole (integer) pixels
% use Regionprops
props = regionprops(mask, 'Area');
area = props.Area; % Counts whole (integer) pixels.
As long as your code is well commented with descriptive variable names, I doubt it's too complicated for me (at least).
You can attach more than one image but you have to insert them one image at a time. Click the picture icon, select a picture. Then click it again and insert the second picture.

5 Comments

Hello Image Analyst,
I dont understand why I would want fractional pixels...I just want the number of pixels inside the circle.
I am a newby so if you can explain it for like a 5 y.o that helps.
Thank you !
What does your third line of code do?
Does my Image have to be binary or grayscale to do these analysis?
I used my tif image, made an ROI and counted the number of black pixels. I am assuming that the black pixels are not on my ROI because the ROI looks gray rather than black... Is this a right approach? I guess I can now substract the number of pixels in the image from the number of black ones that should be fine right?
Sorry for so many messages Im struggling! Thank you for your help!
You can either use nnz() like I showed you,
numPixels = nnz(grayImage);
or you can do it the more complicated way you suggested
numPixels = numel(grayImage) - sum(sum(grayImage == 0));

Sign in to comment.

DGM
DGM on 15 May 2021
Edited: DGM on 15 May 2021
In addition to the ways Image Analyst suggested, I suppose there's another possibility if you're using circle roi objects.
A = imread('cameraman.tif');
imshow(A);
c = drawcircle(); % create a circle roi object as an example
% if the roi object is indeed a circle, you could do
a1 = pi*c.Radius^2

7 Comments

Hello DGM,
Thank you for helping. Although I dont understand what you are trying to suggest. How are you counting pixels with that line of code you are showing? What is the code you typed supposed to do? I understand until the third line.
The answer presumes the possibility that you are using an roi object of type circle:
The first three lines replicate such a hypothetical circle object by using drawcircle() to interactively draw the ROI on the specified test image. When the user clicks and drags on the image, this object is created. Since the geometry of the object is in pixels, we can just use simple geometry to calculate the area from the radius.
Such an ROI object doesn't have to be generated interactively, but for the sake of the example, I chose to do so.
Thank you this helps a lot! I used the toolbox app to draw the circle, but then I had to generate a different code to find the resulting masked image. How do I specify which image Im working on with the draw circle function? Does the name of the photo go inside the parenthesis? I have three images that I need to analyze in the same way.
Okay, i tried your way and it does not work. It just says error. If i remove what is inside the parenthesis it returns a blank image in which I can draw a circle. But I need the circle on MY images....
This is all I have so far...
I1=imread('20210514_DriveSpot__AG_01.tif');
I2=imread('20210514_DriveSpot__AG_02.tif');
I3=imread('20210514_DriveSpot__AG_03.tif');
% Change image format to binary (Is this necessary?)
BW1 = imbinarize(I1);
BW2 = imbinarize(I2);
BW3 = imbinarize(I3);
%Counting total number of pixel in each image, works for BW and tif format
%All images in both formats have the same number of pixels (np= 1241632)
np1 = numel(I1);
np2 = numel(I2);
np3 = numel(I3);
%For each image select the middle region that we know a scale for
%if not then just crop the image and count pixels
c = drawcircle(BW1); % create a circle roi object as an example
drawcircle() simply operates on the current axes (or a specified axes). It doesn't display anything. If you display the image with imshow() first, you can then use drawcircle().
So if I do imshow and then drawcircle it opens up the window to draw the circle but once I tr to zoom in to make the circle in the region I want the option to draw goes away. Then I reRun the code, just draw whatever, the figure goes away once im done and there is no way I see my result again and I dont get an option to draw anything on the other two images
You could zoom in on the image first and then issue the drawcircle() command whenever you're ready. If you're wanting to do this in a script, you could probably use an input() call to halt the script until you're ready to continue. Something like
A = imread('cameraman.tif');
imshow(A);
input('Zoom in on the image and hit enter when you''re ready');
c = drawcircle();

Sign in to comment.

Tags

Asked:

on 15 May 2021

Commented:

on 18 May 2021

Community Treasure Hunt

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

Start Hunting!