Calculating the area of a selected region as a percentage of total image
Show older comments
I have an image of bone on an implant. I would like to get the percentage of the implant covered by bone. I have written code for collecting pixels with a value less than 250 and pixels greater than 250 and dividing them, but the background is all white. How can I crop the image to get rid of the background or is there a more efficient/exact way to get this value? The photo is like the one attached but with a white background and taken from straight above. Thank you!
1 Comment
Luciano Garim
on 5 Oct 2020
Hi,Jenna Wahbeh.
You may use some image segmentation technique. Algorithms like k-means are efficient when want to segment you image.
Other option is use the Image processing toolbox. The option threshold color is a good one.
I hope helped you!
Answers (1)
Nitin Kapgate
on 8 Oct 2020
You can use the roipoly function to interactively specify polygonal region of interest (ROI) and create a binary mask for the same. This binary mask can be then used to calculate the area of ROI as a percentage of total image.
You can use the following code snippet to begin with:
% Load an inbuilt MATLAB image
I = imread('eight.tif'); % Read your own image here
% Interactively select the polygon ROI
% For more information about interactive selection of ROI, refer:
% https://www.mathworks.com/help/images/ref/roipoly.html#mw_826b4f37-e92a-4a20-ab6f-c2aa6d12d500
BW = roipoly(I); % Binary mask image
% Display input image and the binary mask image
figure;
imshow(I);
figure;
imshow(BW); % The white portion is ROI
% number of white pixels in the binary image(Area of ROI)
nWhite = sum(BW(:));
% number of black pixels in the binary image(Area of background)
nBlack = numel(BW) - nWhite;
% ROI as a percentage of Total Image
percentageROI = (nWhite / (nWhite + nBlack)) * 100;
Categories
Find more on Region and Image Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!