Ask Your Question

Revision history [back]

I try your code like this code and it's works

cv::findContours(erframe, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE); for (int i = 0; i < contours.size(); i++) { if (contourArea(contours[i]) >= 500) { std::vector<std::vector<cv::point> > tcontours; std::vector<std::vector<cv::point> > hulls(1); std::vector<std::vector<int> > hullsI(1); tcontours.push_back(contours[i]); cv::drawContours(srcdown, tcontours, -1, cv::Scalar(0, 0, 255), 2); cv::convexHull(cv::Mat(tcontours[0]), hulls[0], false, true); cv::convexHull(cv::Mat(tcontours[0]), hullsI[0], false, true); cv::drawContours(srcdown, hulls, -1, cv::Scalar(0, 255, 0), 1); cv::RotatedRect rect = cv::minAreaRect(cv::Mat(tcontours[0])); std::vector<std::vector<cv::vec4i>>convDef(contours.size()); cv::convexityDefects(tcontours[0], hullsI[0], convDef[i]); for (int k = 0; k < hullsI[0].size(); k++){ for (int j = 0; j < convDef[i].size(); j++){ if (convDef[i][j][3] > 20 * 256 /filter defects by depth/){ int ind_0 = convDef[i][j][0];//start point int ind_1 = convDef[i][j][1];//end point int ind_2 = convDef[i][j][2];//defect point cv::circle(srcdown, contours[i][ind_0], 5, cv::Scalar(0, 0, 255), -1); cv::circle(srcdown, contours[i][ind_1], 5, cv::Scalar(255, 0, 0), -1); cv::circle(srcdown, contours[i][ind_2], 5, cv::Scalar(0, 255, 0), -1); cv::line(srcdown, contours[i][ind_2], contours[i][ind_0], cv::Scalar(0, 255, 255), 1); cv::line(srcdown, contours[i][ind_2], contours[i][ind_1], cv::Scalar(0, 255, 255), 1); } } } } }

I try your code like this code and it's works

cv::findContours(erframe, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
    for (int i = 0; i < contours.size(); i++) {
        if (contourArea(contours[i]) >= 500) {
            std::vector<std::vector<cv::point> std::vector<std::vector<cv::Point> > tcontours;
            std::vector<std::vector<cv::point> std::vector<std::vector<cv::Point> > hulls(1);
            std::vector<std::vector<int> > hullsI(1);
            tcontours.push_back(contours[i]);
            cv::drawContours(srcdown, tcontours, -1, cv::Scalar(0, 0, 255), 2);
            cv::convexHull(cv::Mat(tcontours[0]), hulls[0], false, true);
            cv::convexHull(cv::Mat(tcontours[0]), hullsI[0], false, true);
            cv::drawContours(srcdown, hulls, -1, cv::Scalar(0, 255, 0), 1);
            cv::RotatedRect rect = cv::minAreaRect(cv::Mat(tcontours[0]));
            std::vector<std::vector<cv::vec4i>>convDef(contours.size());
std::vector<std::vector<cv::Vec4i>>convDef(contours.size());
            cv::convexityDefects(tcontours[0], hullsI[0], convDef[i]);
            for (int k = 0; k < hullsI[0].size(); k++){
                for (int j = 0; j < convDef[i].size(); j++){
            if (convDef[i][j][3] > 20 * 256 /filter /*filter defects by depth/){
depth*/){
                int ind_0 = convDef[i][j][0];//start point
            int ind_1 = convDef[i][j][1];//end point
            int ind_2 = convDef[i][j][2];//defect point
            cv::circle(srcdown, contours[i][ind_0], 5, cv::Scalar(0, 0, 255), -1);
            cv::circle(srcdown, contours[i][ind_1], 5, cv::Scalar(255, 0, 0), -1);
            cv::circle(srcdown, contours[i][ind_2], 5, cv::Scalar(0, 255, 0), -1);
            cv::line(srcdown, contours[i][ind_2], contours[i][ind_0], cv::Scalar(0, 255, 255), 1);
            cv::line(srcdown, contours[i][ind_2], contours[i][ind_1], cv::Scalar(0, 255, 255), 1);
                    }
                }
            }
        }
    }

}

I try your code like this code and it's works

cv::findContours(erframe, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
CV_CHAIN_APPROX_SIMPLE), cv::Point(0, 0);
    for (int i = 0; i < contours.size(); i++) {
        if (contourArea(contours[i]) >= 500) {
            std::vector<std::vector<cv::Point> > tcontours;
            std::vector<std::vector<cv::Point> > hulls(1);
            std::vector<std::vector<int> > hullsI(1);
            tcontours.push_back(contours[i]);
            cv::drawContours(srcdown, tcontours, -1, cv::Scalar(0, 0, 255), 2);
            cv::convexHull(cv::Mat(tcontours[0]), hulls[0], false, true);
            cv::convexHull(cv::Mat(tcontours[0]), hullsI[0], false, true);
            cv::drawContours(srcdown, hulls, -1, cv::Scalar(0, 255, 0), 1);
            cv::RotatedRect rect = cv::minAreaRect(cv::Mat(tcontours[0]));
            std::vector<std::vector<cv::Vec4i>>convDef(contours.size());
            cv::convexityDefects(tcontours[0], hullsI[0], convDef[i]);
            for (int k = 0; k < hullsI[0].size(); k++){
                for (int j = 0; j < convDef[i].size(); j++){
            if (convDef[i][j][3] > 20 * 256 /*filter defects by depth*/){
                int ind_0 = convDef[i][j][0];//start point
            int ind_1 = convDef[i][j][1];//end point
            int ind_2 = convDef[i][j][2];//defect point
            cv::circle(srcdown, contours[i][ind_0], 5, cv::Scalar(0, 0, 255), -1);
            cv::circle(srcdown, contours[i][ind_1], 5, cv::Scalar(255, 0, 0), -1);
            cv::circle(srcdown, contours[i][ind_2], 5, cv::Scalar(0, 255, 0), -1);
            cv::line(srcdown, contours[i][ind_2], contours[i][ind_0], cv::Scalar(0, 255, 255), 1);
            cv::line(srcdown, contours[i][ind_2], contours[i][ind_1], cv::Scalar(0, 255, 255), 1);
                    }
                }
            }
        }
    }