SimpleBlobDetector crashes

Hello,

I use the below link :

to create a SimpleBlobDetector to detect my image’s circles and the below shows the code I’ve implemented for that:

		SimpleBlobDetector::Params params;
	params.blobColor = 0xFF;
	params.thresholdStep = 1;
	params.minRepeatability = 1;

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

	// Filter by Area.
	params.filterByArea = true;
	params.minArea = 25;
	params.maxArea = 200;

	// Filter by Circularity
	params.filterByCircularity = false;
	params.minCircularity = 0.87;
	params.maxCircularity = 1.0;

	// Filter by Convexity
	params.filterByConvexity = false;
	params.minConvexity = 0.87;
	params.maxConvexity = 1.0;

	// Filter by Inertia
	params.filterByInertia = false;
	params.minInertiaRatio = 0.5;
	params.maxInertiaRatio = 1.0;

	std::vector<cv::KeyPoint> keypoints;
	Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);
	detector->detect(edge, keypoints);

The problem is that “keypoints.size()” is 658812184411720610 which is a huge amount and then the application crashes. I tried to deal with params but it doesn’t make any changes to this situation!
the below picture is my photo:
blob

Etra Information:
OpenCV version: 3.4.0
IDE: Visual Studio 2017
Operating System: Windows 10 x64

I appreciate any help.

Yours,

sounds like memory corruption, i.e. some bug in the program trashes unrelated values.

what else is going on in your program?

Thanks for your reply, let me share the code:

#include <opencv2\cudaimgproc.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\core\cuda.hpp>
#include <opencv2\opencv.hpp>
#include <opencv2\imgproc.hpp>

#include <stdio.h>
#include <iostream>
#include <chrono>

using namespace std;
using namespace cv;

int main()
{
	cout << "Processing ..." << endl;

	string filePath = "blob.png";

	Mat frame = imread(filePath);

	if (frame.empty())
	{
		cout << "File doesn't exist!"<<endl;
		return -1;
	}

	Mat gray;
	vector<Vec3f> circles;

	cvtColor(frame, gray, CV_RGB2GRAY);

	threshold(gray, frame, 200, 255, THRESH_BINARY);

	SimpleBlobDetector::Params params;
	params.blobColor = 255;
	params.thresholdStep = 1;
	params.minRepeatability = 1;

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

	// Filter by Area.
	params.filterByArea = true;
	params.minArea = 25;
	params.maxArea = 10000;

	// Filter by Circularity
	params.filterByCircularity = false;
	params.minCircularity = 0.87;
	params.maxCircularity = 1.0;

	// Filter by Convexity
	params.filterByConvexity = false;
	params.minConvexity = 0.87;
	params.maxConvexity = 1.0;

	// Filter by Inertia
	params.filterByInertia = false;
	params.minInertiaRatio = 0.5;
	params.maxInertiaRatio = 1.0;

	std::vector<cv::KeyPoint> keypoints;
	Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);
	detector->detect(frame, keypoints);

	Mat im_with_keypoints;

	drawKeypoints(frame, keypoints, im_with_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

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

	char c = (char)waitKey(5000);

	destroyAllWindows();

	cout << "Finished." << endl;
	return OK;
}

furthermore, I use the inverted form of my sample photo like below but nothing changed:
blob-inv

I appreciate your help.

what exactly is the error message you get?

Does the code work on your machine?

The error message is:
Exception thrown: read access violation.
_Mycont was 0xFFFFFFFFFFFFFFF7.

in the function of vector.cpp

_NODISCARD reference operator*() const
		{	// return designated object
 #if _ITERATOR_DEBUG_LEVEL != 0
		const auto _Mycont = static_cast<const _Myvec *>(this->_Getcont());
		_STL_VERIFY(_Ptr, "can't dereference value-initialized vector iterator");
		_STL_VERIFY(_Mycont->_Myfirst <= _Ptr && _Ptr < _Mycont->_Mylast,
			"can't dereference out of range vector iterator");
 #endif /* _ITERATOR_DEBUG_LEVEL != 0 */

		return (*_Ptr);
		}

exception occur at below line of function

_STL_VERIFY(_Mycont->_Myfirst <= _Ptr && _Ptr < _Mycont->_Mylast,
			"can't dereference out of range vector iterator");

As you can see the message is : “can’t dereference out of range vector iterator” I reckon that the high size of vector cause the problem but why the vector size is so high?

that is a curious error.

please try calling detector->detect(gray, keypoints); instead of detector->detect(frame, keypoints);, see if that works better. I’m not sure if the blob detector can handle actual color pictures.

if that turns out to be the issue and detecting on the gray Mat works, I’d say error checking is insufficient inside of the SimpleBlobDetector class. that would be worthy of a bug report.

I changed it as you told me but again nothing happens!
Still, the vector size is an enormous amount.

Accidently, I change the configuration from debug to release and it works but I don’t know why it works on release but doesn’t work on debug mode.
Do you have any suggestion on this?
Although other functions in openCV work quiet well on both configurations, SimpleBlobDetector cannot work on debug mode.
I have no idea why?
according to below link:

I’ll never get tired of telling people that the C++ OpenCV interface for Windows has the wierdest bugs .

Is that right?

you stated that you run opencv v3.4.0. that is VERY outdated. please update. it makes no sense to support outdated versions.

Indeed, I started by using OpenCV 4.5 which is the latest one. However, I wasn’t able to compile it by CUDA support since the extra module in CMake was only for version 3.4.0 and that’s why I switched to 3.4.0. Unlike 4.5 in which I encounter thousands of compiler errors, the 3.4.0 compiled without any problems.

By the way, Thank you again for your time and consideration. However, I don’t know if there is a bug here in OpenCV 3.4.0 debug mode or something else.

Try this
detector = cv2.SimpleBlobDetector_create()
Instead of
detector = cv2.SimpleBlobDetector()

that’s python code. this thread is about C++ code.

Alright. It worked for me of course in python I was having exactly the same problem