如何使用openCV库在视频中提取清晰的轮廓

时间:2011-12-27 21:02:09

标签: image-processing opencv

大家好,我有一些问题从视频中提取清晰的轮廓。目前轮廓看起来像一团糟。我遵循的步骤是:

  1. 将视频作为输入
  2. 将其转换为灰度
  3. 将其阈值设为二进制
  4. 使用cvFindContours()
  5. 查找轮廓
  6. 遍历轮廓并使用cvDrawContours
  7. 绘制它们

    代码如下:

    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/
    5. 如果您需要更多信息,请说出来:) 我想知道你们中的任何人是否可以给我一些建议,如何继续或我做错了什么。谢谢!!!

1 个答案:

答案 0 :(得分:0)

您的Step: 3 Threshold it to binary 需要一些改进。我想你想要在一个语义上有意义的对象周围绘制轮廓。在这种情况下,由于相同的灰度值是许多对象的一部分 - 结果图像不是对象边界的精确度量。

您需要尝试的是分段方法 - 例如分水岭算法或区域增长算法。 OpenCV本身有很多实现。