Ask Your Question

Revision history [back]

I don't know if the fillcomplexpolly is computationally heavy or not, so I do it another (very similar) way.

I create a Mask where I draw a rectangle corresponding to the bounding box of the blob, and then use logical "and" between mask and the image with the blobs to segment the blob from everything else. The rest of the process is the same. Try it, I have no idea which method is more optimized.

This code snippet calculates the ration between contour 1 and 2 of blobImage.

    cv::findContours(blobImage, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

    cv::Mat mask1 = cv::Mat::zeros(height, width, CV_8UC1);
    cv::Mat mask2 = cv::Mat::zeros(height, width, CV_8UC1);

    cv::rectangle(mask1, cv::boundingRect(contours[0]), 255, -1, 8 ,0);
    cv::rectangle(mask2, cv::boundingRect(contours[1]), 255, -1, 8 ,0);

    cv::Mat intersection1 = (mask1 && blobImage) // binary image with only with blob 1
    cv::Mat intersection2 = (mask2 && blobImage) // binary image with only with blob 2

    cv::Scalar sum1 = cv::sum(intersection1);
    cv::Scalar sum2 = cv::sum(intersection2);

    double ratio = (sum1[0] / sum2[0]);