【问题标题】:color object tracking in openCV keeps detecting the skinopenCV中的颜色对象跟踪不断检测皮肤
【发布时间】:2013-06-03 20:03:40
【问题描述】:

我打算做的如下:

修复一个彩色对象。使用带有 camshift 的直方图反投影方法跨视频帧跟踪它。我使用以下代码,它总是最终检测到皮肤。我知道我犯了一些非常简单的错误。如果有人能指出来会很有帮助。

//I have included only the integral parts of code. There are no compilation errors.

    int lowerH =80, upperH =100, lowerS =80, upperS =255, lowerV =80, upperV =255;

    CvScalar output_min =cvScalar(lowerH, lowerS, lowerV, 0); //Color Track
    CvScalar output_max =cvScalar(upperH, upperS, upperV, 0);

    CvScalar output_min2 =cvScalar(0, lowerS, lowerV, 0); //Color Track
    CvScalar output_max2 =cvScalar(180, upperS, upperV, 0);

    while(true){
        frame =cvQueryFrame(capture);

        cvCvtColor(frame, output, CV_BGR2HSV);
        cvInRangeS(output, output_min, output_max, output_mask);

        blobs =CBlobResult(output_mask, NULL, 0);
        blobs.Filter(blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, 35);

        int num_blobs =blobs.GetNumBlobs();
        for(int i=0; i<num_blobs;++i){
            currentBlob = blobs.GetBlob( i );                
            sortedBlobs.push_back(currentBlob);
        }

        if(num_blobs){
            sort(sortedBlobs.begin(), sortedBlobs.end(), local::sortBlobs);
            CvRect blobRect =sortedBlobs[0].GetBoundingBox();

            initX =blobRect.x;
            initY =blobRect.y;
            initWidth =blobRect.width;
            initHeight =blobRect.height;
            initFrame =cvCloneImage(frame);
        }

            int c=cvWaitKey(40);
        if((char)c ==27)break;
    }

    CvRect selection;
    selection.x = initX;
    selection.y = initY;
    selection.width = initWidth;
    selection.height = initHeight;

    CvHistogram *hist;
    int hist_bins = 30;          
    float hist_range[] = {0, 180}; 
    float* range = hist_range;
    hist = cvCreateHist(1, &hist_bins, CV_HIST_ARRAY, &range, 1);           

    cvCvtColor(initFrame, output, CV_BGR2HSV);
    cvInRangeS(output, output_min2, output_max2, output_mask);
    cvSplit(output, hue, 0, 0, 0);

    cvSetImageROI(hue, selection);
    cvSetImageROI(output_mask, selection);

    cvCalcHist(&hue, hist, 0, output_mask);
    float max_val = 0.f;
    cvGetMinMaxHistValue(hist, 0, &max_val, 0, 0 );
    cvConvertScale(hist->bins, hist->bins,
                 max_val ? 255.0/max_val : 0, 0);

    cvResetImageROI(hue);
    cvResetImageROI(output_mask);


    CvBox2D curr_box;
    CvRect prev_rect =selection;
    CvConnectedComp components;
    bool rectFlag =false;
    CvPoint Pt =cvPoint(0,0), prevPt =cvPoint(0,0);
    int clearCounter =0;
    while(true){
        frame =cvQueryFrame(capture);
        if(!frame)break;

        cvCvtColor(frame, output, CV_BGR2HSV);
        cvInRangeS(output, output_min2, output_max2, output_mask);
        cvSplit(output, hue, 0, 0, 0);

        cvCalcBackProject(&hue, prob, hist);
        cvAnd(prob, output_mask, prob, 0);

        cvCamShift(prob, prev_rect, cvTermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 1), &components, &curr_box);

        prev_rect = components.rect;
        curr_box.angle = -curr_box.angle;

        cvEllipseBox(frame, curr_box, CV_RGB(255,0,0), 3, CV_AA, 0);

        int c=cvWaitKey(40);
        if((char)c ==27)break;
    }

编辑:

请检查代码的编辑部分,其中在创建直方图时,我使用 0-180 范围内的遮罩的色调值。如果我使用缩小范围 80-100,我会在组件和 curr_box 中得到垃圾值。

更新: 输出图片

这是最初检测到的绿色斑点,应在整个录制过程中进行跟踪。

这就是发生的事情。绿色斑点在蒙版图像中完全变黑,而是跟踪皮肤。

【问题讨论】:

  • 您不是在修复颜色以跟踪肤色吗?好像是这样的。
  • 您能提供一些样品吗?例如您的输入视频和跟踪结果。
  • @Nallath:我想肤色的 H 值在 0-20 范围内(如果我错了,请纠正我)。
  • 我刚查了一下,皮肤的H值确实在10-30之间。
  • 我会放一些图片样本。

标签: c++ c algorithm opencv image-processing


【解决方案1】:

首先,对所有的混乱表示歉意。代码中有一个非常愚蠢的错误。在将填充的红色矩形放置在原始框架上后,我正在克隆原始框架。

    CvPoint pt1, pt2;
    pt1.x = blobRect.x;  
    pt1.y = blobRect.y;  
    pt2.x = blobRect.x + blobRect.width;  
    pt2.y = blobRect.y + blobRect.height;

    cvRectangle( frame, pt1, pt2, cvScalar(0, 0, 255, 0), CV_FILLED, 8, 0 );

    initX =blobRect.x;
    initY =blobRect.y;
    initWidth =blobRect.width;
    initHeight =blobRect.height;
    initFrame =cvCloneImage(frame);

因此,创建的直方图始终为红色。解决方法很简单。

【讨论】:

    猜你喜欢
    • 2011-12-19
    • 2012-10-09
    • 1970-01-01
    • 2015-12-03
    • 2023-03-19
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    相关资源
    最近更新 更多