How to handle code producing an exception in OpenCV

I am trying to use the following code to extract blob center coordinates in an image. Problem is the marked line gives me an exception every time and when commented out runs without exceptions. Iv tried different permutations to shed further light but I am still stuck:

threshold(imgUndistorted, matBinImg, 15, 220, THRESH_BINARY);
			imshow("Binary Image:", matBinImg);
Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params); 
detector->detect(matBinImg, binaryKeypoints);
			drawKeypoints(matBinImg, binaryKeypoints, img_binwith_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
(problem lines below)
**x_img = binaryKeypoints[0].pt.x;**
**y_img = binaryKeypoints[0].pt.y;**
Imshow("Test Window", img_binwith_keypoints);

I've also tried:

Point2f p = binaryKeypoints[i].pt;

float x = binaryKeypoints[i].pt.x;
float y = binaryKeypoints[i].pt.y;

Same exception
Exception thrown: read access violation.
binaryKeypoints._Mypair._Myval2._Myfirst was nullptr.

please add the error AS TEXT, not as an image, thank you

then, without looking at it – what if it did not find any keypoints ?

1 Like

That was it - thankyou

Now I need to add a filter to fix it. Any suggestions?

int objDetected = binKeypoints.size();