Opencv2.4.9 SimpleBlobDetector掩码不起作用

时间:2014-06-23 01:59:28

标签: c++ opencv feature-detection

我已经仔细阅读了这个网站的解释,但无济于事......希望有人知道答案。

我正在使用simpleBlobDetector来跟踪一些blob。我想通过detect方法指定一个掩码,但由于某种原因,掩码似乎不起作用 - 我的关键点显示整个图像。以下是我的代码的一些片段:

Mat currFrame;
Mat mask;
Mat roi;
cv::Ptr<cv::FeatureDetector> blob_detector = new cv::SimpleBlobDetector(params);//custom set of params I've left out for legibility 
blob_detector->create("SimpleBlob");

vector<cv::KeyPoint> myblob;

while(true)
{   
    captured >> currFrame; // get a new frame from camera >> is grab and retrieve in one go, note grab does not allow frame to be modified but edges can be

    // do nothing if frame is empty
    if(currFrame.empty())
    {
        break;
    }

    /******************** make mask***********************/
    mask = Mat::zeros(currFrame.size(),CV_8U);
    roi = Mat(mask,Rect(400,400,400,400));
    roi = 255;

    /******************** image cleanup with some filters*/
    GaussianBlur(currFrame,currFrame, Size(5,5), 1.5, 1.5);
    cv::medianBlur(currFrame,currFrame,3);

    blob_detector->detect(fgMaskMOG,myblob,mask);//fgMaskMOG is currFrame after some filtering and background subtraction
    cv::drawKeypoints(fgMaskMOG,myblob,fgMaskMOG,Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );

    imshow("mogForeground", fgMaskMOG);
    imshow("original", currFrame);
    imshow("mask",mask);
    if(waitKey(1) != -1)
        break;
}

问题是,我确认我的面具是通过使用SurfFeatureDetector正确制作的,如此处所述(OpenCV: howto use mask parameter for feature point detection (SURF))如果有人能看到我的面具有什么问题,我真的很感激帮助。对于凌乱的代码感到抱歉!

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,找不到解决办法,所以我通过自己查看掩码解决了这个问题:

    String yourInfo = "line1=necessary information 1, line2=necessary information 2";
    String[] parts = yourInfo.split(",");

    String info1 = parts[0].split("=")[1];
    String info2 = parts[1].split("=")[1];

答案 1 :(得分:0)

我发现我的opencv2.4.8这段代码:

void SimpleBlobDetector::detectImpl(const cv::Mat& image, std::vector<cv::KeyPoint>& keypoints, const cv::Mat&) const
{
    //TODO: support mask
    keypoints.clear();
    Mat grayscaleImage;

表示尚不支持此选项。

过滤keyPoints的解决方案并不是很好,因为需要花时间(你必须检测整个图像中的blob)。

更好的解决方法是在检测前降低ROI并在检测后移动每个KeyPoint:

int x = 500;
int y = 200;
int width = 700;
int height = 700;

Mat roi = frame(Rect(x,y,width,height));

blob_detector.detect(roi, keypoints);

for (KeyPoint &kp : keypoints)
{
    kp.pt.x +=x;
    kp.pt.y +=y;
}

drawKeypoints(frame, keypoints, frame,Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);