How to count the number of pixels inside a region
94 views (last 30 days)
Show older comments
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!
0 Comments
Answers (2)
Image Analyst
on 15 May 2021
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
Image Analyst
on 18 May 2021
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));
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
DGM
on 17 May 2021
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();
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!