Ask Your Question

Rob's profile - activity

2013-03-26 06:42:23 -0600 received badge  Enlightened (source)
2013-03-26 06:42:23 -0600 received badge  Good Answer (source)
2012-10-12 01:21:58 -0600 received badge  Nice Answer (source)
2012-09-29 07:51:23 -0600 answered a question Convexity Defects in OpenCV 2.4.2

This solves your problem:

// 1) Your contour needs to be of type std::vector<cv::point>

std::vector<cv::Point> contour;

// 2) If you've got contour using findcontours, you've got a std::vector<std::vector<cv::point>> contour_set; that you can pass like this: contour_set[i] instead of contour in the convexHull-method (see below)

// 3) Determine the convex Hull based on int-indeces for contour point localization // ATTENTION: <int> gives you the 'contour element indeces' (= convex hull's key points)!!!!

cv::vector<int> convexHull_IntIdx;          
cv::convexHull(contour, convexHull_IntIdx, true);

// 4) Use the convexityDefects method to determine the convexity defects

std::vector<cv::Vec4i> convexityDefectsSet;     // Here will be start, end point and "most deep" point (the convexity defect, see the documentation-picture) as well as its distance (depth) to the start-end-line determined... as a vector: for each convexity defect one vector-element
cv::convexityDefects(contour, convexHull_IntIdx, convexityDefectsSet);

// 5) You can then access the 4 parameters that this function calculates (start, end, depth/defect-point and depth) like this:

for (int cDefIt = 0; cDefIt < convexityDefectsSet.size(); cDefIt++)
int startIdx = convexityDefectsSet[cDefIt].val[0];
int endIdx = convexityDefectsSet[cDefIt].val[1];
int defectPtIdx = convexityDefectsSet[cDefIt].val[2];
double depth = (double)convexityDefectsSet[cDefIt].val[3]/256.0f;  // see documentation link below why this

// do something with *contour[defectPtIdx]*.. This is e.g. the defect point.


See for more infos the documentation: convexityDefects(InputArray contour, InputArray convexhull, OutputArray convexityDefects)

2012-09-29 07:33:13 -0600 received badge  Teacher (source)
2012-09-29 07:25:36 -0600 received badge  Editor (source)
2012-09-29 07:14:41 -0600 received badge  Critic (source)
2012-09-27 16:44:06 -0600 received badge  Necromancer (source)
2012-09-27 12:11:31 -0600 answered a question shape context implementation in opencv

Hey there,

unfortunately, OpenCV has no shape context (SC) implementation accepted, yet. You can find here a C++ implementation that was implemented analogeously to Belongie et al. (2000, 2001, 2002) (I'm not sure if the second link is really their's):

The original (?) MATLAB-implementation can be found here:


And see what happens in the following link. Someone made there a mistake to mix up shape matching and shape context, so read carefully the documentation if you read about shape matching in OpenCV that uses Hu moments and shape matching using shape context. There's nothing for SC in OpenCV like the descriptor (shape context/the histogram [log(r),theta]), assignment problem solver (like hungarian [by munkres in O(n^3)], ...) etc.] or deforming algorithms to model transformations (TPS - Thin Plate Spline, ...) etc. as far as I know). Here the link:

Here the Hu-moments-based shape matching.. Maybe it already satisfies your needs as SC is tricky to implement by yourself. Here the link to matching (...):