Ask Your Question

Revision history [back]

There is no standard method to find contour overlap, but this function will do the trick:

     int calculateIntersectionArea(const cv::Mat& blobImg, const std::vector<cv::Point> contour1, const std::vector<cv::Point> contour2)
{
    cv::Mat aux1 = cv::Mat::zeros(blobImg.size(), CV_8UC1);
    cv::Mat aux2 = cv::Mat::zeros(blobImg.size(), CV_8UC1);

    cv::rectangle(aux1, cv::boundingRect(contour1), 255, -1, 8, 0);
    cv::rectangle(aux2, cv::boundingRect(contour2), 255, -1, 8, 0);

    cv::bitwise_and(blobImg, aux1, aux1);
    cv::bitwise_and(blobImg, aux2, aux2);

    cv::Mat intersectionMat = aux1 & aux2;

    cv::Scalar intersectionArea = cv::sum(intersectionMat);

    return (intersectionArea[0]);
}