How to count the number of pixels inside a region

91 visualizaciones (últimos 30 días)
Macarena Santillan
Macarena Santillan el 15 de Mayo de 2021
Comentada: Image Analyst el 18 de Mayo de 2021
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!

Respuestas (2)

Image Analyst
Image Analyst el 15 de Mayo de 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 comentarios
Macarena Santillan
Macarena Santillan el 17 de Mayo de 2021
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!
Image Analyst
Image Analyst el 18 de Mayo de 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));

Iniciar sesión para comentar.


DGM
DGM el 15 de Mayo de 2021
Editada: DGM el 15 de Mayo de 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 comentarios
Macarena Santillan
Macarena Santillan el 17 de Mayo de 2021
Editada: Macarena Santillan el 17 de Mayo de 2021
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
DGM
DGM el 17 de Mayo de 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();

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by