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

1 回表示 (過去 30 日間)
DannyP99
DannyP99 2020 年 7 月 7 日
コメント済み: Mahesh Taparia 2020 年 7 月 15 日
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?

回答 (1 件)

Mahesh Taparia
Mahesh Taparia 2020 年 7 月 15 日
編集済み: Mahesh Taparia 2020 年 7 月 15 日
Hi
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:
a=imread('image.jpeg');
gray=rgb2gray(a);
w=gray<multithresh(gray); %%%%%%%%%%%%% Set the threshold as per your calculation
figure;imshow(w)
conComp=bwconncomp(w);
nComp=[];
for i=1:size(conComp.PixelIdxList,2)
nComp=[nComp size(conComp.PixelIdxList{1,i},1)];
end
topBlackPortion=sum(maxk(nComp,2)); %%%%%Eliminating the 2 areas with maximum values which is top and bottom
remainingPortion=size(gray,1)*size(gray,2)-topBlackPortion;
remainingBlackPotion=sum(nComp)-topBlackPortion;
fractionOfPortion=remainingBlackPotion/remainingPortion; %%% this is the ratio of black portion in the central part
Hope it will help!
  2 件のコメント
madhan ravi
madhan ravi 2020 年 7 月 15 日
Preallocation is really significant instead of appending an array.
Mahesh Taparia
Mahesh Taparia 2020 年 7 月 15 日
Yeah, but it is a small array, does not take much time.

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

Community Treasure Hunt

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

Start Hunting!

Translated by