Question

I'm using OpenCV for image processing. I am looking for a human body, wich I want to isolate (segment).

Currently, I am able to find the contour of the body, and approximate the contour with a Polygon. Next, I would like to use that contour in cvWatershed, to really isolate the body.

Does anyone know how I can draw a contour at an offset towards the center? To illustrate, see the image below.

enter image description here

Blue: the polygon approximation of the contour

Red: the polygon I would like have, but am unable to find. (In the image above, I used photoshop...)

Here's how I find & draw the current contour:

CvContourScanner scanner = cvStartFindContours(mask, pStorage, sizeof(CvContour),    CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
CvSeq* c;
CvSeq* polyContour;
int numCont = 0;
int perimScale = 4;
int contour_approx_level = 6;
while((c = cvFindNextContour(scanner)) != NULL)
{
    CvSeq* c_new;

    // Polygonal approximation
    c_new = cvApproxPoly(c, sizeof(CvContour), pStorage, CV_POLY_APPROX_DP, contour_approx_level, 0);

    // Create the new contour
    cvSubstituteContour(scanner, c_new);
    numCont++;
}

polyContour = cvEndFindContours(&scanner);
int i = 0;
for(i=0, c=polyContour; c!=NULL; c = c->h_next, i++)
{
    cvDrawContours(pOutput, c, cvScalar(255,125,0), cvScalar(255,255,0), -1, 2, 8);
}

/* Draw the contour at an offset towards the center here */
// Based upon the answers, I found 2 solutions

EDIT: I have found two solutions, based upon the answers below:

// 1) Erode - 
// void cvErode( const CvArr* A, CvArr* C, IplConvKernel* B=0, int iterations=1 );
cvErode(pOutput, pOutput, NULL, 3);

// 2) Another option - draw with a black border and thick pencil:
cvDrawContours(pOutput, c, cvScalarAll(0), cvScalarAll(0), 12, 2, 8);
Was it helpful?

Solution

Just erode the blue polygon you found before getting the contour. Here for C API (sorry, I'm not very familiar with the C API).

// void cvErode( const CvArr* A, CvArr* C, IplConvKernel* B=0, int iterations=1 );
cvErode(pOutput, pOutput, NULL, 3);

OTHER TIPS

It is probably not the most elegant thing, but it will certainly work:

  1. Draw the original contour in a new array.
  2. Compute its distance transform.
  3. Threshold: if you want an offset of ten pixels, keep pixels in the distance map higher than ten.
  4. Find the contour of the new image.

A similar and probably less non-rigorous thing could be done directly on the contour with the function pointPolygonTest().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top