我正在使用以下代码:
import numpy as np
import cv2
from matplotlib import pyplot as plt
# file
filename1 = 'img1.png'
filename2 = 'img2.jpg'
img1 = cv2.imread(filename1,0)
img2 = cv2.imread(filename2,0)
# resize
height1, width1 = img1.shape
if height1 > 2000 or width1 > 2000:
img1 = cv2.resize(img1, None, fx=0.25, fy=0.25)
height1, width1 = img1.shape
height2, width2 = img2.shape
if height2 > 2000 or width2 > 2000:
img2 = cv2.resize(img2, None, fx=0.25, fy=0.25)
height2, width2 = img2.shape
# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
# FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50) # or pass empty dictionary
flann = cv2.FlannBasedMatcher(index_params,search_params)
matches = flann.knnMatch(des1,des2,k=2)
# Need to draw only good matches, so create a mask
matchesMask = [[0,0] for i in xrange(len(matches))]
# ratio test as per Lowe's paper
for i,(m,n) in enumerate(matches):
if m.distance < 0.7*n.distance:
matchesMask[i]=[1,0]
draw_params = dict(matchColor = (0,255,0),
singlePointColor = (255,0,0),
matchesMask = matchesMask,
flags = 0)
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params)
plt.imshow(img3,),plt.show()
我在使用 flann.knnmatch 时遇到此错误:
OpenCV Error: Assertion failed (The data should normally be NULL!) in allocate, file /Users/jenkins/miniconda/1/x64/conda-bld/conda_1486587097465/work/opencv-3.1.0/modules/python/src2/cv2.cpp, line 163
Traceback (most recent call last):
File "FLANN_MATCHING.py", line 36, in <module>
matches = flann.knnMatch(des1,des2,k=2)
cv2.error: /Users/jenkins/miniconda/1/x64/conda-bld/conda_1486587097465/work/opencv-3.1.0/modules/python/src2/cv2.cpp:163: error: (-215) The data should normally be NULL! in function allocate
对于我发现的,3.1.0 版没有解决方案,或者至少我不知道如何应用这里制作的"补丁":
https://github.com/opencv/opencv/issues/5667
我使用以下代码安装了opencv:
conda install anaconda
conda install python=3.5
conda install -c menpo opencv3
有没有办法选择3.0.0版本来经历此错误而不是3.1.0?
我只是想从opencv中学习一点,但我花更多的时间试图正确安装它而不是编码。我一直在遵循本教程,但现在我被困在尝试让 FLANN 工作时: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_matcher/py_matcher.html
最后,我不是通过使用 3.0.0 版本而是无法使用 conda 安装的 3.2.0 来解决问题。
1º 我完全卸载了 conda 和 python3。
https://stackoverflow.com/questions/22585235/python-anaconda-how-to-safely-uninstall
2º 我按照本教程使用 brew 安装了 python3 和 opencv 3.2.0:
http://www.pyimagesearch.com/2016/12/19/install-opencv-3-on-macos-with-homebrew-the-easy-way/
现在发生的一些错误被考虑在内:
http://www.pyimagesearch.com/2017/05/15/resolving-macos-opencv-homebrew-install-errors/
3º 安装马图库。
pip3 install matplotlib
现在,代码可以正常工作:
python3 --version
Python 3.6.2
python3
Python 3.6.2 (default, Jul 17 2017, 16:44:32)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0'