How do I calculate areas of very small objects, sometimes 2 pixels in area? MATLAB's regionprops() seems to do this well, and will return values of even 1 for a point. I've read widely on this issue and everyone seems to caution against self-intersecting contours but offers no alternative around that. Here is a sample of my code:
void cMcDetect::shapeFeats(vector<Point> contours, const cv::Mat &img)
{
// % Area, Compactness, Orientation, Eccentricity, Solidity
double A, C, O=NAN, E, S;
vector<Point> ch; convexHull(contours, ch);
Moments mu = moments(contours,0);
double CHA = contourArea(ch);
A=contourArea(contours); // Object Area
S = A/CHA; // Solidity
E = (mu.m20+mu.m02 + sqrt( pow(mu.m20-mu.m02,2)+4*pow(mu.m11,2) ))/ // Eccentricity
(mu.m20+mu.m02 - sqrt( pow(mu.m20-mu.m02,2)+4*pow(mu.m11,2) ));
Rect boundrect = boundingRect(contours); // Compactness
C = A/(boundrect.width*boundrect.height);
if(contours.size()>=5){
RotatedRect rotrect = fitEllipse(contours); // Orientation
O = rotrect.angle;
}
printf("%f %f %f %f %f\n",A, C, O, E, S);
}
I get very strange area values such as 0, 1.5 for object areas. I don't expect decimal areas as I expect the function to return the raw sum of pixels such that a point will have an area of 1. Any developments as far as this issue is concerned? It also seems to be affecting other derived values such as Eccentricity. I guess I can summarize the question as: how do I get the raw pixel count of connected components and make Opencv use this in calculating Hu moments and other derived values correctly where the area is needed? If not possible, can you suggest some design/approach readjustments to circumvent the issue? I would have liked to have opencv do it so I can take advantage of its other functions such as hu moments and ellipse fitting.