2012-05-04 47 views
4

我使用OpenCV進行圖像處理。 我正在尋找一個人體,我想要隔離(段)。OpenCV - DrawContour在偏移量

目前,我能夠找到身體的輪廓,並用多邊形近似輪廓。接下來,我想在cvWatershed中使用該輪廓,以真正隔離身體。

有沒有人知道我如何繪製一個輪廓在中心的偏移量? 爲了說明,請參閱下圖。

enter image description here

藍:我想多邊形有,但我無法找到:輪廓

紅的多邊形逼近。 (在上圖中,我使用的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 

編輯:我已經找到了兩種解決方案,基於以下答案:

// 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); 

回答

3

只需erode您在獲取輪廓之前找到的藍色多邊形。 Here C API(抱歉,我不是很熟悉C API)。

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

Off course!很簡單!在我的情況下,背景是黑色的,也可以用線條粗細畫出相同的輪廓。 – Entreco

2

它可能不是最優雅的事情,但它肯定會工作:

  1. 在新陣列中繪製原始輪廓。
  2. 計算其距離變換。
  3. 閾值:如果您想要十個像素的偏移量,請將距離圖中的像素保持爲高於十。
  4. 找到新圖像的輪廓。

在函數pointPolygonTest()的輪廓上可以直接使用類似的,也許不太嚴格的事情。

+0

感謝您的回答。也指向我pointPolygonTest()方法。儘管如此,我發現@ lifesayko的答案更容易。 – Entreco