Calculating the number of pixels inside a freehand region.

12 visualizzazioni (ultimi 30 giorni)
I have a binary image, what I want to do is select a freehand region and then find out the total number of white and black pixels inside that region.

Risposta accettata

Spandan Tiwari
Spandan Tiwari il 11 Lug 2014
Since you are already using imfreehand, you can use the method named createMask available in the imfreehand object to rasterize the marked region into a binary image. Then you can just use logical AND to combine the binary image with your original image and sum over all the pixels in the image to get the desired count. I think the code would look something as follows. Say your image is called I.
figure, imshow(I)
f = imfreehand;
% Then draw the region using imfreehand tool
bw = createMask(f);
outI = bw.*I;
numPixelsInsideRegion = sum(outI(:))
  4 Commenti
Rukevwe Rim-Rukeh
Rukevwe Rim-Rukeh il 25 Apr 2022
can you loop the freehand command, so you dont have to continously draw the area
Image Analyst
Image Analyst il 25 Apr 2022
Sure. Just put it in a loop and OR it into the "master" binary image
finalMask = false(rows, columns)
for k = 1 : 10
thisMask = (however you get it, like with drawfreehand());
finalMask = finalMask | thisMask;
end

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 1 Lug 2014
This is exactly what my freehand drawing demo does. See attached.
  4 Commenti
Salman
Salman il 2 Lug 2014
Modificato: Salman il 2 Lug 2014
This is the image
and let's say that this is the free hand region that I select
As you can see that the image contains only two colors, i.e. black and white. Now what I want is, to find the total number of white pixels inside the freehand region that I draw and also the number of black pixels inside the freehand region.
I'll be grateful for any help.
P.S. I'm a newbie to image processing.
Image Analyst
Image Analyst il 25 Apr 2022
See my demo to get the coordinates of the hand-drawn shape. Let's call them x, y, so first create a mask with poly2mask, then use nnz()
[imageRows, imageColumns = size(originalBinaryImage);
insideShape = poly2mask(x, y, imageRows, imageColumns);
newMask = originalBinaryImage & insideShape;
pixelCount = nnz(newMask)

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by