Blobdetector Testing Help (4.5.4)

Hello, currently am messing around and trying to get this code working correctly so I can apply it to real world situations but I currently am struggling to get it working. Here is my results:


I’m not sure why the circle is off center, despite looking like it has the right diameter. Any tips? Currently am messing with the params to see if that’ll help.

int main()

    // Read image
    Mat im = imread("testing.png", IMREAD_GRAYSCALE);
    Rect roi;
    Mat frame;

    std::string video = "micro2.mp4";
    VideoCapture cap(video);
    if (!cap.isOpened())
        cout << "we ain't happy\n";
        return 0;
    // take first frame from video
    cap >> frame;

    // Set up the detector with default parameters.
    // Ptr<Feature2D> detector = SimpleBlobDetector::create();

    // Setup SimpleBlobDetector parameters.
    SimpleBlobDetector::Params params;

    // Change thresholds
    params.minThreshold = 10;
    params.maxThreshold = 200;

    // Filter by Area.
    params.filterByArea = false;
    params.minArea = 300;

    // Filter by Circularity
    params.filterByCircularity = true;
    params.minCircularity = 0.1;

    // Filter by Convexity
    params.filterByConvexity = false;
    params.minConvexity = 0.01;

    // Filter by Inertia
    params.filterByInertia = false;
    params.minInertiaRatio = 0.2;

    // Set up detector with params
    Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);

    // Detect blobs.
    std::vector<KeyPoint> keypoints;
    detector->detect(im, keypoints);

    // Draw detected blobs as red circles.
    // DrawMatchesFlags::DRAW_RICH_KEYPOINTS flag ensures the size of the circle corresponds to the size of blob
    Mat im_with_keypoints;
    drawKeypoints(im, keypoints, im_with_keypoints, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );

    // Show blobs
    imshow("keypoints", im_with_keypoints);


since this depends on findContours, which expects white objects on black bg,
try to invert the image, like:

detector->detect(~im, keypoints);