平台:win10 x64 +JetBrains PyCharm 2018.2.4 x64 +Anaconda3(python3.7.0+opencv3.4.5)
Issue说明:同学发了个python代码,想实现下brisk,帮同学解决,自己试验了下,但是报错ImportError:检查opencv的安装


from os import path
import sys
import numpy as np
try:
import cv2
except ImportError:
print('Couldn\'t find opencv so trying to use the fallback' \
' cv2.pyd (only for windows).')
from _cv2_fallback import cv2
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Helper Functions
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def polygon_area(vertices):
"""Calculate the area of the vertices described by the sequence of vertices.
Thanks to Darel Rex Finley: http://alienryderflex.com/polygon_area/
"""
area = 0.0
X = [float(vertex[0]) for vertex in vertices]
Y = [float(vertex[1]) for vertex in vertices]
j = len(vertices) - 1
for i in range(len(vertices)):
area += (X[j] + X[i]) * (Y[j] - Y[i])
j = i
return abs(area) / 2 # abs in case it's negative
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Fundamental Parts
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# alternative detectors, descriptors, matchers, parameters ==> different results
detector = cv2.BRISK(thresh=10, octaves=1)
extractor = cv2.DescriptorExtractor_create('BRISK') # non-patented. Thank you!
matcher = cv2.BFMatcher(cv2.NORM_L2SQR)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Object Features
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
obj_original = cv2.imread(path.join('source_images', 'object.png'),
cv2.CV_LOAD_IMAGE_COLOR)
if obj_original is None:
print ('Couldn\'t find the object image with the provided path.')
sys.exit()
# basic feature detection works in grayscale
obj = cv2.cvtColor(obj_original, cv2.COLOR_BGR2GRAY)
# mask with white in areas to consider, black in areas to ignore
obj_mask = cv2.imread(path.join('source_images', 'object_mask.png'),
cv2.CV_LOAD_IMAGE_GRAYSCALE)
if obj_mask is None:
print ('Couldn\'t find the object mask image with the provided path.' \
' Continuing without it.')
# keypoints are "interesting" points in an image:
obj_keypoints = detector.detect(obj, obj_mask)
# this lines up each keypoint with a mathematical description
obj_keypoints, obj_descriptors = extractor.compute(obj, obj_keypoints)
print ('Object Summary *************************************************')
print (' {} keypoints'