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

조회 수: 5 (최근 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