I am calculating different attributes for connected regions of the image, with the final goal of classification. One of the attributes I am working with is a sparsity measure, calculated as the ratio of the area of the region (in pixels) and the area of the region's convex hull.
For now, I am doing this with
std::vector <cv::Point2f> region; std::vector <cv::Point2f> convex_hull; // fill the region ... cv::convexHull(cv::Mat(region), convex_hull, false); double ratio = (double)(region.size()) / std::abs(cv::contourArea(convex_hull));
The problems with this approach is that pixels are considered to have an area of 1 when calculating the region area, but are treated as points in convex hull calculation, causing disparity. An example would be a 4-pixel rectangle with the pixels coordinates ((1,1), (1,2), (2,1), (2,2)). Convex hull contains the same 4 points. But, the area of the region is calculated as 4 (counting the pixels), and the convex hull area as 1 (a 1x1 rectangle with corners in (1,1), (1,2), (2,1), (2,2)).
Can anybody suggest a way to compute these two areas in the same way, so that the range of my sparsity measure is in fact [0,1] and the region area never gets calculated as larger than convex hull area?