使用简单的BlobDetector进行对象检测,但它不起作用

Object detection with simpleBlobDetector, but it doesn't work

本文关键字:检测 不起作用 对象 简单 BlobDetector      更新时间:2023-10-16

simpleBlobDetector的代码有问题。我可以很好地构建和运行所有代码,但程序检测到的斑点只有一个像素左右的大小。我已经尝试过更改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;}

尝试此操作并为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;

}