How to Calculate White/Black Ratio within a border of an image

2 views (last 30 days)
DannyP99 on 7 Jul 2020
Commented: Mahesh Taparia on 15 Jul 2020
I am trying to calculate the ratio of black to white in this image without including the large black background above and below the sliver. This is the original image:
This is the image after I binarize it:
In this binarized image, I need to calculate the black to white ratio without incorporating the background. How do I do that?

Answers (1)

Mahesh Taparia
Mahesh Taparia on 15 Jul 2020
Edited: Mahesh Taparia on 15 Jul 2020
You can find the ratio of black to white part by removing the top and bottom black pixels into calculation. After removing those pixels, find the number of black pixels in the middle part (the organ) and take the ratio. For example, consider the sample code below:
w=gray<multithresh(gray); %%%%%%%%%%%%% Set the threshold as per your calculation
for i=1:size(conComp.PixelIdxList,2)
nComp=[nComp size(conComp.PixelIdxList{1,i},1)];
topBlackPortion=sum(maxk(nComp,2)); %%%%%Eliminating the 2 areas with maximum values which is top and bottom
fractionOfPortion=remainingBlackPotion/remainingPortion; %%% this is the ratio of black portion in the central part
Hope it will help!

Community Treasure Hunt

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

Start Hunting!

Translated by