2011-12-27 53 views
1

大家好我有一些問題需要從視頻中提取清晰的輪廓。目前輪廓看起來像是一團糟。我下面的步驟是:如何使用openCV庫在視頻中提取清晰的輪廓

  1. 以視頻作爲輸入
  2. 將其轉爲灰度
  3. 閾值,爲二進制
  4. 查找與cvFindContours輪廓()
  5. 遍歷輪廓和

    :使用cvDrawContours

的代碼是下面繪製它們3210

IplImage *inFrame; 
IplImage *outFrame = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3); 
CvScalar inPixel, outPixel; 

//J.M ================================================// 
CvMemStorage *mem = cvCreateMemStorage(0), *polymem = cvCreateMemStorage(0); 
CvSeq *contours = NULL, *ptr = NULL, *poly = NULL, *convex_hull = NULL; 
IplImage *cc_color = cvCreateImage(cvGetSize(outFrame), IPL_DEPTH_8U, 3); 
CvScalar externalColor = CV_RGB(255, 252, 0); 
CvScalar holeColor = CV_RGB(255, 0, 0); 
CvFont font; 
int contourNum = 0; 
//=========================================================================// 

frame_no = 0; 
processingVideo = true; 
this->GetDlgItem(IDC_STOP)->EnableWindow(true); 

CString title = "Video Processing"; 
cvNamedWindow(title, CV_WINDOW_AUTOSIZE); 

CvVideoWriter *writer = cvCreateVideoWriter(pathOutVideo,CV_FOURCC('I', 'Y', 'U', 'V'),video->getFPS(),cvSize(width,height),1); 

// variables for counting time 
clock_t begin, current; 
long int secs, rem_secs, t1, t2; 
begin = clock(); 

while(inFrame=cvQueryFrame(video->getCapture())) 
{ 
    if(!processingVideo) 
     break; 

    // close opencv window? 
    if(cvGetWindowHandle(title)==NULL) 
     break; 

    //==============================================================================================// 

    /*Create gray-scale image*/ 
    IplImage *im_gray = cvCreateImage(cvGetSize(outFrame), IPL_DEPTH_8U, 1); 
    cvCvtColor(inFrame, im_gray, CV_RGB2GRAY); 
    outFrame = im_gray; 

    /*Convert gray-scale image to binary*/ 
    IplImage* img_bw = cvCreateImage(cvGetSize(im_gray), IPL_DEPTH_8U, 1); 
    cvThreshold(im_gray, img_bw, 128, 255, CV_THRESH_BINARY | CV_THRESH_OTSU); 
    outFrame = img_bw; 

    contourNum = cvFindContours(outFrame, mem, &contours, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0)); 

    /* I have tried using convex_hull but with no success 
    convex_hull = cvConvexHull2(contours, polymem, CV_CLOCKWISE, 2); 
    poly = cvApproxPoly(convex_hull, sizeof(CvContour), polymem, CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0); 
    float size = fabs(cvContourArea(poly, CV_WHOLE_SEQ)); */ 

    for (ptr = contours; ptr != NULL; ptr = ptr->h_next) { 
      cvDrawContours(cc_color, ptr, externalColor, holeColor, -1, CV_FILLED, 8, cvPoint(0,0)); 
    } 

    outFrame = cc_color; 
    //==============================================================================================// 

我還提供了各種步驟結果的圖像實例。

  1. 源視頻輸入(http://imageshack.us/photo/my-images/823/sourcevideo.jpg/
  2. 灰度視頻(http://imageshack.us/photo/my-images/23/grayscalei.jpg/
  3. 二進制視頻(http://imageshack.us/photo/my-images/684/binaryi.jpg/
  4. 最終輪廓輸出(http://imageshack.us/photo/my-images/35/finaloutput.jpg/

如果您需要更多的信息,只是這麼說:) 我想知道如果你們中的任何一個人可以給我一些建議,如何進行或我做錯了什麼。謝謝 !!!

回答

0

您的Step: 3 Threshold it to binary需要一些改進。我想你想讓輪廓圍繞語義上有意義的對象來繪製。在這種情況下,由於相同的灰度級值是許多對象的一部分 - 所得圖像不是對象邊界的準確度量。

你需要的可能是嘗試分割方法 - 如分水嶺算法或區域增長算法。 OpenCV本身有很多實現。

+0

非常感謝! –