c++ OpenCV: find 'inverse' inner convex hull

117 Views Asked by At

I have the following code snippet that reads a grayscale mask (1), computes the contours (2) and convex hulls (3) and draws these on the resulting image. The input image for this algorithm is shown below. Note that the input can be any artibrary shape (circle, ellipse, rectangle, ..): Input image

cv::Mat mask = cv::imread("testdata//polar.png", cv::IMREAD_GRAYSCALE);
cv::Mat result;
cv::cvtColor(mask, result, cv::COLOR_GRAY2BGR);
            
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(mask, contours, hierarchy, cv::RetrievalModes::RETR_LIST, cv::ContourApproximationModes::CHAIN_APPROX_NONE);
    
std::vector<std::vector<cv::Point>>hull(contours.size());       
for (auto  i = 0; i < contours.size(); ++i)
{
    cv::convexHull(contours[i], hull[i], false);        
    cv::drawContours(result, hull, i, cv::Scalar(0, 0, 255), 2);
}
    
cv::imwrite("C:\\TEMP\\stackOverflowResult.png", result);

The inner and outer convex hulls are depicted in red, as shown below: Inner and outer convex hull result

Altough the results are correct in terms of convex hull area/principle, I would like to see that the INNER convex hull follows the green path - thereby enclosing the concavity so it can be detected. Note that the OUTER convex hull is correct.

Does anyone know how I can force openCV's convex hull algorithm to take the green path instaid of the red one - under the constraint that morphological operations cannot be used: I'm looking for a parameter free solution.

I've tried morphological operations and OpenCV's convexityDefects with no succes. Altough this can be used effectively (as shown by my tests), the fine-tuning of parameters is not what we want.

0

There are 0 best solutions below