gpt4 book ai didi

python - OpenCV Python : Occasionally get segmentation fault when using FlannBasedMatcher

转载 作者:太空宇宙 更新时间:2023-11-03 11:28:29 25 4
gpt4 key购买 nike

我正在尝试使用 SURF 和 kNN 对对象进行分类。该代码运行良好,但偶尔会崩溃并显示“段错误”。我不确定我是否做错了什么,但我很确定它已得到纠正。如果您想重现问题,这里是输入文件。

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

在我写这段代码之前,我试图创建一个包含所有训练数据的 kNN 模型,并且只匹配一次。然而,它总是失败并在“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)

非常感谢您的帮助。

最佳答案

在它失败的地方,可能是一张空白图片或一张图片只有很少的描述符。然后描述符矩阵为空,因此失败。

关于python - OpenCV Python : Occasionally get segmentation fault when using FlannBasedMatcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28583304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com