Calculating the number of pixels inside a freehand region.

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.

 採用された回答

Spandan Tiwari
Spandan Tiwari 2014 年 7 月 11 日

0 投票

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 件のコメント

Image Analyst
Image Analyst 2014 年 7 月 11 日
Don't you need to cast bw to uint8 so that it's the same class as I before you multiply them?
Spandan Tiwari
Spandan Tiwari 2014 年 7 月 12 日
Since the OP mentioned that it is a two-level image, I am assuming that the class of I is logical. If it' not, then it can be casted to logical by doing logical(I).
Rukevwe Rim-Rukeh
Rukevwe Rim-Rukeh 2022 年 4 月 25 日
can you loop the freehand command, so you dont have to continously draw the area
Image Analyst
Image Analyst 2022 年 4 月 25 日
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

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 7 月 1 日

0 投票

This is exactly what my freehand drawing demo does. See attached.

4 件のコメント

Salman
Salman 2014 年 7 月 1 日
編集済み: Salman 2014 年 7 月 1 日
Hi
Thanks Image Analyst, but i am afraid that what your demo does is a little different than what I want. I have a binary image, and i want to select a freehand region and compute the number of black and white pixels individually. What your demo dose is that it gives me the total number of pixels of the freehand region that i draw.
Image Analyst
Image Analyst 2014 年 7 月 1 日
Post your image and tell me what you're tracing around. If you have a pure black and pure white image rather than gray scale, you'll have to multiply the mask by your image before summing so that you'll get only the few pure black and pure white pixels in the freehand mask area rather than counting the total number of pixels in the area.
Salman
Salman 2014 年 7 月 2 日
編集済み: Salman 2014 年 7 月 2 日
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 2022 年 4 月 25 日
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)

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by