def matchFeatures(queryFeature, trainFeature, matcher):
"""
match(...) function: match query image features and train image features.
parameter:
queryFeature: features of query image
trainFeature: features of train image
matcher: feature matcher
queryImage: this is just for test to show the position of the found image which in the query image
, input query image data which has processed by cv2.imread().
return:
if found matched image ,return image name, otherwise return None.
"""
queryKeypoints = queryFeature[0]
queryDescriptors = queryFeature[1]
trainKeypoints = trainFeature[0]
trainDescriptors = trainFeature[1]
trainImgSize = trainFeature[2]
trainImgHeight = trainImgSize[0]
trainImgWidth = trainImgSize[1]
corners=numpy.float32([[0, 0], [trainImgWidth, 0], [trainImgWidth, trainImgHeight], [0, trainImgHeight]])
raw_matches = matcher.knnMatch(trainDescriptors, queryDescriptors, 2)
queryGoodPoints, trainGoodPoints = filter_matches(trainKeypoints, queryKeypoints, raw_matches)
if len(queryGoodPoints) >= 4:
H,status = cv2.findHomography(queryGoodPoints, trainGoodPoints, cv2.RANSAC, 5.0)
else:
H,status = None,None
res=False
obj_corners=None
if H is not None:
corners = corners.reshape(1, -1, 2)
obj_corners = numpy.int32(cv2.perspectiveTransform(corners, H).reshape(-1, 2))
is_polygon = ispolygon(obj_corners)
if is_polygon:
res=True
del queryKeypoints
del queryDescriptors
del trainKeypoints
del trainDescriptors
del trainImgSize
del corners
del raw_matches
del queryGoodPoints
del trainGoodPoints
del obj_corners
return res
评论列表
文章目录