问题
I'm trying to classify objects using SURF and kNN. The code work well however it occasionally crashes and shows 'Segmentation Fault'. I'm not sure whether I did something wrong but I'm pretty sure that it is corrected. Here is the input file in case that you want to reproduce the issue.
Link to download the dataset
import numpy as np
import cv2
import sys
trainfile = ['/home/nuntipat/Documents/Dataset/Bank/Training/15_20_front.jpg'
, '/home/nuntipat/Documents/Dataset/Bank/Training/15_50_front.jpg'
, '/home/nuntipat/Documents/Dataset/Bank/Training/15_100_front.jpg'
, '/home/nuntipat/Documents/Dataset/Bank/Training/15_500_front.jpg'
, '/home/nuntipat/Documents/Dataset/Bank/Training/15_1000_front.jpg'
, '/home/nuntipat/Documents/Dataset/Bank/Training/16_20_front.jpg'
, '/home/nuntipat/Documents/Dataset/Bank/Training/16_50_front.jpg'
, '/home/nuntipat/Documents/Dataset/Bank/Training/16_500_front.jpg']
testfile = '/home/nuntipat/Documents/Dataset/Bank/20_1.jpg'
# FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)
# Initiate FLANN matcher
flann = cv2.FlannBasedMatcher(index_params, search_params)
# Initiate SURF detector
surf = cv2.xfeatures2d.SURF_create(500)
# Create list of describtor
descriptor = []
for file in trainfile:
img = cv2.imread(file)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
kp, des = surf.detectAndCompute(gray, None)
descriptor.append(des)
# Clasify using test file
img = cv2.imread(testfile)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
kp1, des = surf.detectAndCompute(gray, None)
maxCount = 0
for i, d in enumerate(descriptor):
matches = flann.knnMatch(d, des, k=2)
count = 0
# ratio test as per Lowe's paper
for (m,n) in matches:
if m.distance < 0.7 * n.distance:
count += 1
if count > maxCount:
maxCount = count
maxMatch = i
print maxMatch
Before I wrote this code, I have tried to create a kNN model which contain every training data and do the match only once. However it always fail and cause segmentation fault at "flann.add(descriptors)".
import numpy as np
import cv2
trainfile = ['/home/nuntipat/Documents/Dataset/Bank/100_1.jpg', '/home/nuntipat/Documents/Dataset/Bank/100_2.jpg', '/home/nuntipat/Documents/Dataset/Bank/100_3.jpg']
testfile = '/home/nuntipat/Documents/Dataset/Bank/100_1.jpg'
# FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50) # or pass empty dictionary
# Initiate FLANN matcher
flann = cv2.FlannBasedMatcher(index_params, search_params)
# Initiate SURF detector
surf = cv2.xfeatures2d.SURF_create()
# Train FLANN
for file in trainfile:
img = cv2.imread(file)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
keypoints, descriptors = surf.detectAndCompute(gray, None)
flann.add(descriptors)
Thanks you very much for you help.
回答1:
At the point that it fails, there probably is a blank image or an image with very few descriptors. The descriptor matrix is then empty and hence it fails.
回答2:
It says here in this link the following:
flann.add([descriptors])
http://answers.opencv.org/question/44592/flann-index-in-python-training-fails-with-segfault/
Hope it helps!
来源:https://stackoverflow.com/questions/28583304/opencv-python-occasionally-get-segmentation-fault-when-using-flannbasedmatcher