Opencv and python: matchLOGOS not working

I am trying to use matchLOGOS from opencv 4.7.0 and Python 3. But it’s not going well. The documentation says it can be called like this in Python:

cv.xfeatures2d.matchLOGOS(  keypoints1, keypoints2, nn1, nn2, matches1to2   ) ->    None


keypoints1 Input keypoints of image1.

keypoints2 Input keypoints of image2.

nn1 Index to the closest BoW centroid for each descriptors of image1.

nn2 Index to the closest BoW centroid for each descriptors of image2.

matches1to2 Matches returned by the LOGOS matching strategy.

So unlike most functions, the output, matches1to2, is modified in-place and the function returns None. But I’m not getting any results, even with my 2 images are exactly the same. Here is some sample code:

import cv2
from sklearn.cluster import KMeans
from cv2.xfeatures2d import matchLOGOS
from skimage import data
im1 = (getattr(data,'cat')())[:,:,0].astype(np.uint8) # cat image
im2 = im1.copy() # same exact image
sift = cv2.SIFT_create()
kp1, des1 = sift.detectAndCompute(im1, None)
kp2, des2 = sift.detectAndCompute(im2, None)
# create visual_dict from des1
visualDict = KMeans(n_clusters=256, init='k-means++', tol=0.0001, n_init=10, verbose=0).fit(des1)
des1_closest = visualDict.predict(des1) # des1_closest[j] = cluster center label closest to des1[j] 
des2_closest = visualDict.predict(des2) # same
matches_LOGOS = []
matchLOGOS(kp1, kp2, des1_closest, des2_closest, matches_LOGOS )

The result is just matches_LOGOS = [] . Or if I initialize matches_LOGOS = None I get back matches_LOGOS = None . If I don’t initialize matches_LOGOS at all, an error says matches_LOGOS is not declared. I’m thinking the problem is related to the in-place nature of the function, and Python’s api to cv2 isn’t handling it right. Can someone please help? I can’t find any examples of matchLOGOS online.

1 Like

i rather think, you’ve found a bug here.

are you able to (re-)build cv2, locally ?

similar to this other issue, the function declaration here should be changed to:

CV_EXPORTS_W void matchLOGOS(const std::vector<KeyPoint>& keypoints1, const std::vector<KeyPoint>& keypoints2,
                             const std::vector<int>& nn1, const std::vector<int>& nn2,
                             CV_OUT std::vector<DMatch>& matches1to2);

after a rebuild, the resulting py signature should look like:

cv.xfeatures2d.matchLOGOS( keypoints1, keypoints2, nn1, nn2 [, matches1to2] ) -> result # returns matches now

Thanks berak! I’ve never re-built cv2 locally, but I can give it a try. Do you recommend a link that explains how to do it?

if it’s linux, this will also build py bindings