OpenCV - 在偏移处绘制轮廓

Ent*_*eco 6 drawing opencv contour approximate

我正在使用 OpenCV 进行图像处理。我正在寻找一个人体,我想将其隔离(分段)。

目前,我能够找到身体的轮廓,并用多边形近似轮廓。接下来,我想在 cvWatershed 中使用该轮廓,以真正隔离身体。

有谁知道如何以向中心偏移的方式绘制轮廓?为了说明这一点,请参见下图。

在此输入图像描述

蓝色:轮廓的多边形近似

红色:我想要的多边形,但无法找到。(上图中,我用的是photoshop...)

这是我查找和绘制当前轮廓的方法:

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
Run Code Online (Sandbox Code Playgroud)

编辑:我根据以下答案找到了两种解决方案:

// 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);
Run Code Online (Sandbox Code Playgroud)

Ste*_*eim 3

只需在获取轮廓之前侵蚀您找到的蓝色多边形即可。 这里是C API(抱歉,我对C API不是很熟悉)。

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