在 mac os 中使用 conda 降级 Opencv

Downgrade Opencv using conda in mac os

我正在使用此代码:

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 版的解决方案,或者至少我不知道如何应用此处制作的 "patch":

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 中完全卸载了 conda 和 python3。

 

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'