Python SimpleBlobDetector.detect() is very slow with big images and can crash

asked 2017-12-20 09:21:49 -0600

Asgarod gravatar image

updated 2017-12-21 02:47:37 -0600

Hi!

I program in Python 3.X and use the simple blob detector to retreive their key points. This works very good. However when it comes to big images - like really big e.g. 16384x16384 pixels - processing takes really long. Here is the code:

    ...
    if blur_sigma > 0:
      img_blurred = cv2.GaussianBlur(img, (-1, -1), blur_sigma)
    else:
      img_blurred = img
    img_hsv = cv2.cvtColor(img_blurred, cv2.COLOR_BGR2HSV)
    img_filtered = cv2.inRange(img_hsv, tuple(lower_bounds), tuple(upper_bounds))

    ksize = 2 * morph_size + 1
    morph_element = cv2.getStructuringElement(morph_element, (ksize, ksize))
    img_morphed = cv2.morphologyEx(img_filtered,
                                   morph_operator,
                                   morph_element,
                                   borderType=cv2.BORDER_REFLECT)

    blob_params = cv2.SimpleBlobDetector_Params()
    blob_params.filterByInertia = False
    blob_params.minConvexity = 0.0
    blob_detector = cv2.SimpleBlobDetector_create(blob_params)
    key_points = blob_detector.detect(img_morphed)
    ...

Additionally it happened several times that in the last line, during execution, an assertion is thrown which directed me there:

cv2.error: D:\Build\OpenCV\opencv-3.2.0\modules\core\src\matrix.cpp:433: error: (-215) u != 0 in function cv::Mat::create

In there, an allocation fails.

So my questions are: 1) Is this kind of image size and edge case where the detection simply can no longer work with reasonable time? 2) Is the error also related to the image size? 3)Is there a way to improve detection with additonal steps beforehand?

Thanks!

edit retag flag offensive close merge delete

Comments

Related about very large image handling in OpenCV: Very large image not supported (area > 2^32).

Maybe you can split your image and call SimpleBlobDetector for each sub images to avoid the issue with big images?

Your image is very big, apart from subsample your image (divide your image by a factor) and interpolate the coordinates, the processing time will be very long.

Eduardo gravatar imageEduardo ( 2017-12-21 05:15:55 -0600 )edit