在mac os中使用conda降级Opencv

时间:2017-07-25 12:42:33

标签: python macos opencv conda flann

我正在使用此代码:

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版没有解决方案,或者至少我不知道如何应用&#34;补丁&#34;这是在这里做的:

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

1 个答案:

答案 0 :(得分:0)

最后,我得解决的问题不是使用3.0.0版本,而是3.2.0无法从conda安装。

1º我完全从conda卸载了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º安装matplotlib。

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'