Calculating convexityDefects using OpenCV 2.4 in c++

后端 未结 4 1732
时光取名叫无心
时光取名叫无心 2020-11-30 12:21

I\'m using OpenCV 2.4 to calculate the convex hull of a image.

I\'m also doing some processing to remove some noise from the image, which is not really relevant to

4条回答
  •  难免孤独
    2020-11-30 12:34

    For those who don't read the comments or didn't see it the solution is to use:

    vector >hull;

    to create the hull instead of:

    vector >hull;

    as convexityDefects only works on hulls stored as a series of indices rather than a series of Points.

    Sadly this gives another problem as drawContours only draws contours stored as a series of Points and not as a series of indices! So if you want to draw your hulls at a later stage you may want to create 2 stores when you are finding the hulls, one for drawing and one for finding defects from. Something along the lines of the following works:

      // create storage space
      vector > hullsI(contours.size());
      vector > hullsP(contours.size());
      vector > defects(contours.size());
    
      for(int i = 0; i 

    It may be more efficient to just use a different method to draw the hulls rather than calculating them twice but this was the most elegant way I saw of doing it.

    Also I'm still getting the hang of C/C++ myself (Java guy) but I believe if you add using namespace cv to the top of the code you can save having to have cv:: throughout your code.

    Hope I haven't stepped on toes Innuendo, not my intention at all if I have.

提交回复
热议问题