MATLAB: Calculating the number of pixels inside a freehand region.

imageImage Processing Toolboximageprocessing

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.

Best Answer

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(:))