Ask Your Question
0

Segmentation Fault using FlannBasedMatcher.knnMatch()

asked 2017-09-19 11:29:14 -0600

I am using OpenCV 3.0.0, Python 2.7, and Linux Mint 18.2.

I am trying to create an object detector using my built in webcam to provide the query images. Whenever I run the program, the webcam window will come up and draw polylines around the object if it is present, but at some random time after starting the program, it will crash and print out "Segmentation Fault". There is no additional message with the segfault. I've tried to patch the FlannBasedMatcher with

After including print statements after every line, I narrowed the issue down to the knnMatch function. I've tried changing the type of the descriptors that I'm passing knnMatch to float32 and I've tried using flann.add([descriptors]) instead of flann.knnMatch(des1,des2,k=2), but both lead to the same error.

import cv2
import numpy as np

MIN_MATCH_COUNT=30
detector=cv2.ORB_create()
FLANN_INDEX_KDITREE=0

flannParam=dict(algorithm=FLANN_INDEX_KDITREE,tree=4)

flann=cv2.FlannBasedMatcher(flannParam,{})
trainImg=cv2.imread("TrainingData/TrainImg.jpg",0)

trainKP=detector.detect(trainImg,None)
trainKP,trainDesc=detector.compute(trainImg,trainKP)

cam=cv2.VideoCapture(0)

while True:
    ret, QueryImgBGR=cam.read()

    QueryImg=cv2.cvtColor(QueryImgBGR,cv2.COLOR_BGR2GRAY)

    queryKP=detector.detect(QueryImg,None)
    queryKP,queryDesc=detector.compute(QueryImg,queryKP)

    print("matches not found")
    trainDesc = np.float32(trainDesc)
    queryDesc = np.float32(queryDesc)

    flann.add([trainDesc])
    matches=flann.knnMatch(np.float32(queryDesc),k=2)
    print("matches found")
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-09-19 11:36:28 -0600

berak gravatar image

updated 2017-09-19 11:42:23 -0600

ORB descriptors are binary bitstrings, you cannot use them with the FlannbasedMatcher, but have to use the BFMatcher with NORM_HAMMING or NORM_HAMMING2

please do NOT try to convert your descriptors to float here, just leave them as is, and use the proper Matcher class.

you can use the FlannbasedMatcher only with SIFT or SURF, which deliver float features, suitable to be used with the L2 norm

(still, that should never segfault, but thow a proper exception, imho. let's enquire.)

edit flag offensive delete link more

Comments

I've tried both SIFT and SURF with the FlannBasedMatcher, with and without converting to float. Both resulted in segfaults.

I am trying to find enough matches that meet the Lowe's ratio test between the test image and query image. I am taking a frame from a webcam as the query image. This is the print that I get when I run the code. Sometimes the window with the webcam will come up for a second or two, but it always crashes with the segfault.

VIDEOIO ERROR: V4L/V4L2: VIDIOC_S_CROP
len(trainDesc) = 9026
len(queryDesc) = 59
matches not found
matches found
Not enough matches found - 1/30
len(trainDesc) = 9026
len(queryDesc) = 59
matches not found
Segmentation fault
jfreking gravatar imagejfreking ( 2017-09-20 16:22:44 -0600 )edit

I have this same issue but with the newer flann_Index. It seems to depend on the number of features. At around ~1000, it works almost every time, at around 1500, it starts segfaulting ~50% of the time non-deterministically, and ~2000 features, it's basically 100% of the time.

This is with algorithm = 0, 1, 2, 3 and 5. Algorithm 4 is the only one that doesn't segfault (kdtree single?).

I've tried different features as you, and tried all sorts of things, nothing works.

EhsanKia gravatar imageEhsanKia ( 2020-06-14 01:33:34 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-09-19 11:29:14 -0600

Seen: 1,187 times

Last updated: Sep 19 '17