Ask Your Question
0

Segmentation Fault using FlannBasedMatcher.knnMatch()

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

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 -0500

berak gravatar image

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

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 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 452 times

Last updated: Sep 19 '17