在cmd下import cv2报错——OpenCV实现BRISK

本文记录了在Windows环境下,使用CMD运行Python时遇到import cv2错误的过程及解决方案。问题源于Python 3.6.5与Anaconda 3.7.0的版本冲突,以及numpy版本过低。通过调整Python环境,更新numpy,最终成功解决CMD下导入cv2的问题。同时,还解决了PyCharm中识别不到cv2模块的类似问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

平台: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'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值