Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image processing 如何使用openCV库提取视频中的清晰轮廓_Image Processing_Opencv - Fatal编程技术网

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

Image processing 如何使用openCV库提取视频中的清晰轮廓,image-processing,opencv,Image Processing,Opencv,大家好,我在从视频中提取清晰轮廓时遇到了一些问题。目前,轮廓看起来像一团乱麻。我将采取以下步骤: 将视频作为输入 将其转换为灰度 将其设置为二进制 使用cvFindContours查找等高线 通过等高线循环并使用cvDrawContours绘制等高线 代码如下: IplImage *inFrame; IplImage *outFrame = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3); CvScalar inPixel, outPixel

大家好,我在从视频中提取清晰轮廓时遇到了一些问题。目前,轮廓看起来像一团乱麻。我将采取以下步骤:

将视频作为输入 将其转换为灰度 将其设置为二进制 使用cvFindContours查找等高线 通过等高线循环并使用cvDrawContours绘制等高线 代码如下:

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;
    //==============================================================================================//
我还提供了各种步骤结果的图像实例

源视频输入 灰度视频 二进制视频 最终轮廓输出 如果您需要更多信息,请说: 我想知道你们中是否有人能给我一些建议,比如如何继续或者我做错了什么。谢谢

您的步骤:3将其设置为二进制阈值需要一些改进。我想你想在语义上有意义的物体周围画轮廓。在这种情况下,由于相同的灰度值是许多对象的一部分,因此生成的图像不是对象边界的精确度量

您可能需要尝试分割方法,例如分水岭算法或区域增长算法。OpenCV本身有许多实现