Ask Your Question

Revision history [back]

Segmentation Fault using FlannBasedMatcher.knnMatch()

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")