How to count pixels/measure area in binary image?

11 views (last 30 days)
Andrew Ferguson on 6 Jan 2023
Answered: Kevin Holly on 6 Jan 2023
I have an image (attached) where I want to count the number of pixels/measure the area of pixels that are in the space between the circles. I also want to individually count each sapce between the circles as its own element (as opposed to counting all the total spaces as one element). I have looked thorough the documentation on regionprops and am thinking perhaps I could attempt to get this using maybe ConvexHull in regionprops, but I am not really sure. I was thinking also maybe I could use bwconvhull, but when I tried to apply it I just got a blank image. I apologize as I am new to iamge processing in MATLAB, but could anyone provide guidance on how I could best achieve my goal? My current code is attached below, which is really just converting the image to binary and trying to use bwconvhull.
% Clear variables, figures, screen
clear all
close all
clc
% Read in image, display image
imshow(image)
% Convert image to grayscale
image_gray = rgb2gray(image);
figure
imshow(image_gray)
% Convert image to binary
threshold = 0.65;
image_binary = imbinarize(image_gray,threshold);
figure
imshow(image_binary)
% Calculate specific properties of image
stats = regionprops('table',image_binary,'Centroid','MajorAxisLength','MinorAxisLength','ConvexArea','ConvexHull','ConvexImage');
% Create subplot of original & binary image
figure
subplot(1,2,1);
imshow(image)
subplot(1,2,2);
imshow(image_binary)
% Create convex hull around objects
CH_objects = bwconvhull(image_binary,'objects');
figure
imshow(CH_objects);
title('Objects Convex Hull');
Matt J on 6 Jan 2023
Please make life easy and attach image_binary in a .mat file.
Andrew Ferguson on 6 Jan 2023
I have attached a .fig and .m file for "image_binary" above. Will that work? I appreciate the help on the question.

Kevin Holly on 6 Jan 2023
img = im2bw(img);
imshow(img)
se = strel('sphere',15);;
circles=imopen(~img,se);
imshow(circles)
se = strel('sphere',2);
spacebetween = imopen(~img-circles,se);
figure
imshow(spacebetween)
rp = regionprops('table',spacebetween>0,"Centroid", "Area")
rp = 20×2 table
Area Centroid ____ ________________ 117 4.4359 66.795 18 1.9444 412.67 276 10.964 420.72 1749 143.78 67.376 1049 133.53 243.34 1071 149.16 418.44 26 286.88 294.77 1882 323.67 74.011 445 303.78 238.35 1535 325.22 416.11 51 357.47 233.24 1758 493.47 233.9 1590 496.42 74.286 1442 499.51 411.27 65 593.95 109.18 26 611.23 98.769
rp.Area
ans = 20×1
117 18 276 1749 1049 1071 26 1882 445 1535

Matt J on 6 Jan 2023
Edited: Matt J on 6 Jan 2023
Let's consider 3 regions B (the background, which you have as a binary image), C (the circles), and D (the connective arms between the circles that you are trying to obtain). You can use bwlalphaclose in this FEX download,
to seal the gaps between the circles, giving you B|D. The complement of B|D gives you the circles C. Once you have C, you obtain the region of interst D according to ~(B|C).
I could demonstrate this for you if you would attach image_binary as suggested above.

Categories

Find more on Image Arithmetic in Help Center and File Exchange

R2022b

Community Treasure Hunt

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

Start Hunting!