Calculating the Percent White Area from Stacked Images
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Naman Chaturvedi
2018년 8월 3일
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!
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!