Calculating the Percent White Area from Stacked Images

1 view (last 30 days)
I have a greyscale image "I", which I rotated 90degrees "Irot" and stacked on top of the original image (stack = I+Irot). To calculate the percentage of the area of the "stack" that is white, I tried to use the following equation: PercentWhite = (1-nnz(stack)/numel(stack))*100. However, the value comes out to zero. Is there another step I need to be taking?
I = imcomplement(imread(figname));
IBW = 0.01*imbinarize(IBW);
Irot = imrotate(IBW, 90);
stack = imcomplement(IBW + Irot);
PercentWhite = (1-nnz(stack)/numel(stack))*100

Answers (1)

Naman Chaturvedi
Naman Chaturvedi on 3 Aug 2018
Hi Sarah,
In the code you mentioned, I have assumed that in your second line of code, you are binarizing I and not IBW and your code is
IBW = 0.01*imbinarize(I);
From my understanding of the problem, you are binarizing a greyscale image, scaling it down by a factor of 100, summing it up with a rotated version of the image and then trying to find the white percentage. While using the equation:
PercentWhite = (1-nnz(stack)/numel(stack))*100;
since all the pixel values of the image matrix stack are non-zero, the function 'nnz' returns the total number of elements. Hence, the equation becomes (1-1)*100. To resolve the issue, you can follow one out of the following two steps:-
1. If you just want to count the number of pixels which are fully white i.e. value 1, you can use the 'floor' function to make all the values between zero and one 0.
stack = imcomplement(IBW + Irot);
stack_floor=floor(stack);
PercentWhite = (1-nnz(stack_floor)/numel(stack_floor))*100;
2. If you want a range of values of pixels to be counted as whites(e.g. if pixel value is >0.7), then, you can use a code like this:-
th=0.7;
th_comp=1-th; %complemented threshold
I = imcomplement(imread(figname));
IBW = 0.01*imbinarize(I);
Irot = imrotate(IBW, 90);
stack = imcomplement(IBW + Irot);
PercentWhite = (1-nnz(stack<th_comp)/numel(stack))*100
Hope this helps!
  1 Comment
Sarah
Sarah on 3 Aug 2018
Thank you. However, the problem is that it is giving me the value for the top stacked image only. Do I have to save the image and reload it to get the value of the two images stacked? Or is there a way to calculate the percent white of the two images stacked without having to save an image and reload it into matlab? (waste of disk space)

Sign in to comment.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!