Calculating Circularity in MATLAB
13 次查看(过去 30 天)
显示 更早的评论
I am trying to calculate Circularity using the formula (Perimeter^2)/(4 Pi Area). So, a perfect circle will have a value of 1, and deviations will range from greater than 1 to infinity. However, I am getting some values less than 1 when I run the code, which doesn't make any sense. I am calculating perimeter by getting the edge count of every boundary pixel, instead of using the perimeter function provided by regionprops. Area is from regionprops, and is just the number of total pixels. Does anyone know why this is happening?
Here is the code for the perimeter calculation:
function a = new_perim(image)
notimg = true(size(image)+2);
notimg(2:end-1, 2:end-1) = ~image;
% Find locations where a non-zero pixel is adjacent to a zero pixel,
% for each cardinal direction in turn
topedges = image & notimg(1:end-2, 2:end-1);
leftedges = image & notimg(2:end-1, 1:end-2);
bottomedges = image & notimg(3:end, 2:end-1);
rightedges = image & notimg(2:end-1, 3:end);
% Sum each set of locations separately, then add to get total perimeter
perim = sum(topedges(:)) + sum(leftedges(:)) + ...
+ sum(bottomedges(:)) + sum(rightedges(:));
a = perim;
end
2 个评论
Star Strider
2018-12-17
It might help if you attach a representative image, and post (or attach) your code.
Without those, we have no idea what the problem is. (This will not guarantee an Answer, but it will significantly increase the probability of one.)
回答(1 个)
John D'Errico
2018-12-17
编辑:John D'Errico
2018-12-17
For a circle, perimeter = 2*pi*r. area = pi*r^2.
So the ratio you pose is indeed 1 for a circle. And for any other region, that ratio SHOULD exceed 1, that is, if you computed things correctly.
Consider a region, where this computation yields a value lower than 1. That can only possibly happen if the area is too large compared to the perimeter, or if the computed perimeter is too small. Remember, that you are talking pixels here.
Suppose we have a circular region that contains a number of pixels. The combined area of those pixels is easy to compute, because square pixels pack together perfectly since they tile the plane.
But what is the perimeter? How do you define the length of a pixel around that perimeter? Be careful here. Make sure that you got EVERY pixel around the perimeter. And some pixels are sitting at an angle, relative to that boundary. What is the average length of a pixel as you traverse 360 degrees around that circle? How about pixels that are only partially on the boundary?
The point is, I would bet that your computation of the perimeter may be slightly inaccurate, even for a nicely "circular" region. So, while your formula is correct in an abstract mathematical sense, it is flawed when you interpret it in terms of pixels. That imit of 1 as a minimum may be only an approximate limit.
6 个评论
Image Analyst
2018-12-19
I have seen this often (circularities less than 1). I chalk it up to digitization error, and I adjust the thresholds to take that into account.
John D'Errico
2018-12-20
Yes. The only mathematical way to get a value significantly less than 1 is to mis-estimate one of the parameters in that expression, thus area too high, or perimeter too low. If it is less than 1 by a sufficiently small amount, then you may well ignore it as just numerical error in the computations. If the discrepancy is large, then it suggests there is a mistake in the computation of one or both of those parameters, one that one might be correctable.
That said, floating point computations are not truly mathematics, but only an approximation thereof, one that is typically accurate to roughly 16 decimal digits of precision.
For example, if the ratio is computed less than 1 by an amount on the order of 1e-16, then I would know the problem is down in the least significant bits of the computation. Just ignore that out of hand, since you should never trust the least signigificant bits of a floating point number.
Even if the discrepancy is less than 1 by some still small, but larger amount? Now you might guess the error is probably in the computation of area or perimeter, that they were mis-estimated by some amount. You might decide to ignore it, because you are approximating the perimeter and area down at the pixel level.
If the reported ratio is less than 1 by a large fraction, thus perhaps you see a fraction like 0.75, when you expected 1, then something is seriously wrong in what you computed. That is, the perimeter and area numbers are out of sinc with each other, so you are not computing the perimeter of the same tiled region as that for which you computed the area.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!