user1874538
user1874538

Reputation: 262

OpenCv Convexity Defects

I am working through some examples I have found online to create convex hulls using opencv c++ 2.4.4. I was looking into using convexity defects but am running into issues with the c++ implementation of these classes

I have followed the link below as it is the most similar to my issue but still no luck. Calculating convexityDefects using OpenCV 2.4 in c++

I am recieving the error below.

openCV Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == 1 && ((1 << type0) & fixedDepthMask) != 0)) in create

Additionally I have pasted by current code below.

Thank you for any help,

int c = 0;
VideoCapture cap(0);

Mat frame;
Mat edges;
Mat threshold_output;
cv::vector<cv::Vec4i> hierarchy;
std::vector<std::vector<cv::Point> > contours;
RNG rng(12345);

while( c != 27)
{
    cap >> frame;
    cvtColor(frame, edges, CV_BGR2GRAY);

    threshold(edges, threshold_output, 100, 255, CV_THRESH_OTSU );
    findContours( threshold_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));

    std::vector<Vec4i> defects;
    vector<cv::vector<int> >hull( contours.size() );

    for (int i = 0; i < contours.size(); i++)
    {
        convexHull(contours[i], hull[i], false );
        if (contours[i].size() >3 )
        {
            convexityDefects(contours[i], hull[i], defects[i]);
        }
    }
}

Upvotes: 2

Views: 5662

Answers (1)

berak
berak

Reputation: 39786

std::vector<Vec4i> defects;   

should be

std::vector< std::vector<Vec4i> > defects( contours.size() );

(one for each contour / hull)

Upvotes: 3

Related Questions