OpenCV C ++ / Obj-C:特定blob内的goodFeaturesToTrack

时间:2011-12-22 15:30:07

标签: opencv blob filtering contour

是否有一个快速的解决方案,只在我所参与的blob的轮廓内指定ROI?

到目前为止我的想法:

  1. 使用boundingRect,但它包含太多我不想分析的内容。
  2. 将goodFeaturesToTrack应用于整个图像,然后循环输出坐标以消除一次外部的blob轮廓
  3. 提前致谢!

    修改

    我找到了我需要的东西:cv :: pointPolygonTest()似乎是正确的,但我不确定如何实现它......

    以下是一些代码:

    // ...
    IplImage forground_ipl = result;
    IplImage *labelImg = cvCreateImage(forground.size(), IPL_DEPTH_LABEL, 1);
    
    CvBlobs blobs;
    bool found = cvb::cvLabel(&forground_ipl, labelImg, blobs);
    IplImage *imgOut = cvCreateImage(cvGetSize(&forground_ipl), IPL_DEPTH_8U, 3);
    
    if (found) {
        vb::CvBlob *greaterBlob = blobs[cvb::cvGreaterBlob(blobs)];
        cvb::cvRenderBlob(labelImg, greaterBlob, &forground_ipl, imgOut);
        CvContourPolygon *polygon = cvConvertChainCodesToPolygon(&greaterBlob->contour);
    }
    

    “polygon”包含我需要的轮廓。

    goodFeaturesToTrack以这种方式实现:

    - (std::vector<cv::Point2f>)pointsFromGoodFeaturesToTrack:(cv::Mat &)_image
    {
        std::vector<cv::Point2f> corners;
        cv::goodFeaturesToTrack(_image,corners, 100, 0.01, 10);
        return corners;
    }
    

    接下来我需要遍历角落并用cv :: pointPolygonTest()检查每个点,对吗?

1 个答案:

答案 0 :(得分:3)

您可以在您感兴趣的区域创建一个掩码:

修改 如何制作面具:

制作面具;

Mat mask(origImg.size(), CV_8UC1);
mask.setTo(Scalar::all(0));
// here I assume your contour is extracted with findContours, 
// and is stored in a vector<vector<Point>> 
// and that you know which contour is the blob
// if it's not the case, use fillPoly instead of drawContour();
Scalar color(255,255,255); // white. actually, it's monchannel.
drawContours(mask, contours, contourIdx, color );

// fillPoly(Mat& img, const Point** pts, const int* npts, 
//         int ncontours, const Scalar& color)

现在你已经准备好使用它了。 ,仔细查看结果 - 我听说过OpenCV中有关功能提取器的掩码参数的一些错误,我不确定它是否与此有关。

// note the mask parameter:

void goodFeaturesToTrack(InputArray image, OutputArray corners, int maxCorners, 
    double qualityLevel, double minDistance, 
    InputArray mask=noArray(), int blockSize=3, 
    bool useHarrisDetector=false, double k=0.04 )

这也将提高你的应用速度 - goodFeaturesToTrack会花费大量时间,如果你只在较小的图像上应用它,那么整体增益就很大。