使用simpleBlobDetector进行对象检测,但它不起作用

时间:2015-12-08 20:36:09

标签: c++ opencv computer-vision

我的simpleBlobDetector代码有问题。我可以很好地构建和运行所有代码,但程序检测到的blob只是像素的大小。我已经尝试更改param.minArea和maxArea,但它不起作用。所以我要求你们帮忙。顺便说一下,我使用的图像已经是灰度级,因此我的阈值命令不能正常工作。先谢谢!

马丁。

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(){
Mat src;
Mat dst;


src = imread("C:\\Users\\martin\\Desktop\\ThermalImage2.png", CV_LOAD_IMAGE_GRAYSCALE); //Load an image from directory path

if (! src.data){
     cout <<  "Could not open or find the image" << endl ; // Look for invalid input
    return -1;
    }


else{

double thresh = 130;  // Threshold
double maxValue = 255; // Value assigned to the pixel if it is over 'thresh'

threshold(src, dst, thresh, maxValue, THRESH_BINARY); // threshold the picture src and call it dst

namedWindow("thresholdedPicture", WINDOW_AUTOSIZE); // Create a window

imshow("thresholdedPicture", dst); // display thresholded picture in the window

}

SimpleBlobDetector::Params params; // Set parameters for the object detection

params.minDistBetweenBlobs = 10; //Minimum distance between blobs

params.filterByColor = true;
params.blobColor = 255;

params.filterByArea = true; // filter by area of the blob
params.minArea = 1 ;// Minimum area of the blob
params.maxArea = 100000; // Maximum area of the blob



vector<KeyPoint> keypoints; 


cv::SimpleBlobDetector detector(params); // Set up the blob detector with the parameters (params)

detector.detect(dst, keypoints); // Input thresholded picture for detection of the blobs

Mat dst_blob_dect; // New array to store the picture with the blobs detected


drawKeypoints( dst, keypoints, dst_blob_dect, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS ); //Drawing a red line around the detected objects



namedWindow("keypoints", WINDOW_AUTOSIZE); // Create a window

imshow("keypoints", dst_blob_dect); // Show the picture with the blobs detected in the window "keypoints"


waitKey(0); // Press any key and the main function returns 0

return 0;}

1 个答案:

答案 0 :(得分:0)

尝试此操作并为params.minDistBetweenBlobs使用不同的值。

#include "stdafx.h"
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/opencv.hpp>
#include <stdio.h>

using namespace std;
using namespace cv;

int main(){
    Mat src;
    Mat dst;

    src = imread("C:\\Users\\sanche8x\\Pictures\\gather.png", CV_LOAD_IMAGE_GRAYSCALE); //Load an image from directory path

    if (! src.data){
       cout <<  "Could not open or find the image" << endl ; // Look for invalid input
       return -1;
    }
    else{
        double thresh = 130;  // Threshold
        double maxValue = 255; // Value assigned to the pixel if it is over 'thresh'    
        threshold(src, dst, thresh, maxValue, THRESH_BINARY); // threshold the picture src and call it dst
        namedWindow("thresholdedPicture", WINDOW_AUTOSIZE); // Create a window
        imshow("thresholdedPicture", dst); // display thresholded picture in the window
    }

    SimpleBlobDetector::Params params; // Set parameters for the object detection
    params.minDistBetweenBlobs = 10; //Minimum distance between blobs
    params.filterByColor = true;
    params.blobColor = 255;
    params.filterByCircularity = false;
    params.filterByConvexity = false;
    params.filterByInertia = false;

    params.filterByArea = true; // filter by area of the blob
    params.minArea = 1 ;// Minimum area of the blob
    params.maxArea = 100000; // Maximum area of the blob

    vector<KeyPoint> keypoints; 

    cv::SimpleBlobDetector detector(params); // Set up the blob detector with the parameters (params)
    detector.detect(dst, keypoints); // Input thresholded picture for detection of the blobs
    Mat dst_blob_dect; // New array to store the picture with the blobs detected    
    drawKeypoints( dst, keypoints, dst_blob_dect, Scalar(0,0,255),   DrawMatchesFlags::DRAW_RICH_KEYPOINTS ); //Drawing a red line around the detected objects

    namedWindow("keypoints", WINDOW_AUTOSIZE); // Create a window

    imshow("keypoints", dst_blob_dect); // Show the picture with the blobs detected in the window "keypoints"


    waitKey(0); // Press any key and the main function returns 0

    return 0;

}