首页 > 解决方案 > OPENCV_ENABLE_NONFREE CMake 选项并在函数“create”中重建库

问题描述

我真的开始使用python了。我从网上下载项目。但是当我尝试运行时,我得到了这些错误。我认为该项目不支持我的安装模块。如何解决这个问题?

错误

回溯(最后一次调用):文件“compare.py”,第 20 行,在 sift = cv2.xfeatures2d.SIFT_create() cv2.error: OpenCV(4.2.0) /Users/travis/build/skvark/opencv-python /opencv_contrib/modules/xfeatures2d/src/sift.cpp:1210: error: (-213: The function/feature is not implemented) 此算法已获得专利,不包含在此配置中;设置 OPENCV_ENABLE_NONFREE CMake 选项并在函数“create”中重建库

完整代码

import cv2
import numpy as np

original = cv2.imread("images/original_golden_bridge.jpg")
image_to_compare = cv2.imread("images/george-washington-bridge.jpg")

# 1) Check if 2 images are equals
if original.shape == image_to_compare.shape:
    print("The images have same size and channels")
    difference = cv2.subtract(original, image_to_compare)
    b, g, r = cv2.split(difference)

    if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:
        print("The images are completely Equal")
    else:
        print("The images are NOT equal")

# 2) Check for similarities between the 2 images

sift = cv2.xfeatures2d.SIFT_create()
kp_1, desc_1 = sift.detectAndCompute(original, None)
kp_2, desc_2 = sift.detectAndCompute(image_to_compare, None)

index_params = dict(algorithm=0, trees=5)
search_params = dict()
flann = cv2.FlannBasedMatcher(index_params, search_params)

matches = flann.knnMatch(desc_1, desc_2, k=2)

good_points = []
ratio = 0.6
for m, n in matches:
    if m.distance < ratio*n.distance:
        good_points.append(m)
print(len(good_points))
result = cv2.drawMatches(original, kp_1, image_to_compare, kp_2, good_points, None)

cv2.imshow("result", result)
cv2.imshow("Original", original)
cv2.imshow("Duplicate", image_to_compare)
cv2.waitKey(0)
cv2.destroyAllWindows()

蟒蛇版本

python --version Python 3.8.1

python2 --version Python 2.7.16

模块版本

pip3 freeze
certifi==2019.11.28
numpy==1.18.1
opencv-contrib-python==4.2.0.32
opencv-python==4.2.0.32

我跟着这个教程

我也跟着这个答案https://stackoverflow.com/a/52514095/8822337

回溯(最后一次调用):文件“compare.py”,第 1 行,在 import cv2 ModuleNotFoundError: No module named 'cv2'

我卸载了 pip3 opencv 模块并安装了这两个。

pip install opencv-python==3.4.2.16
pip install opencv-contrib-python==3.4.2.16

标签: pythonpython-3.xpython-2.7opencv

解决方案


推荐阅读