Calculating the Percent White Area from Stacked Images
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (1)
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!
See Also
Categories
Find more on Image Processing Toolbox 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!