Using numpy array for feature detection

I am trying to use OpenCVs feature detection on numpy arrays. Unfortunately OpenCV raises an error when I want to detect features:

error: OpenCV(4.0.1) C:\ci\opencv-suite_1573470242804\work\modules\imgproc\src\resize.cpp:3787: error: (-215:Assertion failed) inv_scale_x > 0 in function 'cv::resize'

I tried it in the context of a bigger project but my minimum working example is:

import cv2 as cv
import numpy as np
from numpy import random
import matplotlib.pyplot as plt

orb_parameters = dict(nfeatures=15000,scaleFactor=1.2,nlevels=30,firstLevel=0,WTA_K=2,scoreType=1,patchSize=20,fastThreshold=2)

descriptor = cv.ORB_create(**orb_parameters)
detector = descriptor

FLANN_INDEX_LSH = 6
index_params = dict(algorithm=FLANN_INDEX_LSH,table_nummer=6,key_size=12,multi_probe_level=1)
search_params = dict(checks = 50)

matcher = cv.FlannBasedMatcher(index_params,search_params)

np_scene = random.randint(0,255,(80,80),np.uint8)
np_query = random.randint(0,255,(80,80),np.uint8)

keypoints_query = detector.detect(np_query,None)
keypoints_scene = detector.detect(np_scene,None)

keypoints_query, descriptors_query = descriptor.compute(np_query, keypoints_query)
keypoints_scene, descriptors_scene = descriptor.compute(np_scene, keypoints_scene)

matches = matcher.knnMatch(descriptors_query,descriptors_scene,k=2)

drawnMatches = cv.drawMatches(np_query, keypoints_query, np_scene, keypoints_scene, matches)

plt.figure()
plt.imshow(drawnMatches)
plt.show()

Could somebody point me in the right direction on how to use the feature detection functionality of OpenCV with numpy arrays?

welcome.

have you given your parameters much thought? please vary the parameters of your ORB instances.

30 levels of factor 1.2 gives you a factor of 237. I believe your parameters are nonsensical for the size of input (80x80) you give. and so is asking for fifteen thousand features from a noisy patch of 80x80.

Thank you crackwitz, you are right. The size of my input array is not sufficient for my ORB settings. I got these parameters with a bigger array and thought it would apply for a smaller one. From cv::ORB Class Reference I thought the fifteen thousand features are equal to a maximum feature count (0 to 15k features) which ORB would detekt and in my preliminary testing this worked fine:
grafik

Unfortunately, these arrays expend in the runtime and at the start they are obviously too small and therefore producing the error.

Is there a guideline in regards to the tuning of parameters?

not really, apart from trial and error and experience. I suppose it might help to read the original paper on ORB but I didn’t.

granted, OpenCV code could be more helpful in its error message… that seems to happen during the algorithm’s operation. it should sanity check its inputs some more.

1 Like

Well, yes in this situation I would have expected a more meaningful tracelog or even the feature detector not replying at all till it has enought size to operate.

I have read the paper, but it seems I have to read it again more thouroughly to tune my parameters.

However, thank you that answers my question :slight_smile: