MATLAB — 无法导入 cv2 库
MATLAB — Unable to Import cv2 Library
我是 OpenCV 的初学者,正在尝试 运行 一个开源程序。
http://asrl.utias.utoronto.ca/code/gpusurf/index.html
我目前安装了 Computer Vision Toolbox OpenCV Interface 20.1.0 和 Computer Vision Toolbox 9.2。
我无法 运行 这个简单的开源特征匹配算法而不会遇到错误。
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
% read images
img1 = cv2.imread('[INSERT PATH #1]');
img2 = cv2.imread('[INSERT PATH #2]');
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY);
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY);
%sift
sift = cv2.xfeatures2d.SIFT_create();
keypoints_1, descriptors_1 = sift.detectAndCompute(img1,None);
keypoints_2, descriptors_2 = sift.detectAndCompute(img2,None);
len(keypoints_1), len(keypoints_2)
返回以下消息:
Error: File: Keypoints.m Line: 1 Column: 8
The import statement 'import cv2' cannot be found or cannot be imported. Imported names must end with '.*' or be
fully qualified.
但是,当我删除第 1 行时,我反而收到以下错误。
Error: File: Keypoints.m Line: 2 Column: 8
The import statement 'import matplotlib.pyplot' cannot be found or cannot be imported. Imported names must end
with '.*' or be fully qualified.
最后,按照错误消息只会导致 cv2 库出现一系列进一步的错误。有什么想法吗?
那是因为您使用的代码不是 MATLAB 代码,而是 python 代码。
根据您链接的网站:
From within Matlab
The parallel implementation coded in Matlab can be run by using the surf_find_keypoints()
function. The output keypoints can be sorted by strength using surf_best_n_keypoints()
, and plotted using surf_plot_keypoints()
.
检查您是否下载了正确的文件,然后重试。
此外,Matlab OpenCV Interface is designed to integrate C++ OpenCV code, not python. Documentations here.
是的,这是 Python 代码是正确的。我建议检查您的 dependencies/libraries。 PyCharm IDE 是我个人使用的,因为它可以轻松处理所有库。
如果您最终尝试了 PyCharm,请将鼠标悬停在 CV2 上时单击红色图标。然后它会提示您下载库。
编辑:
使用 Python 可以完成一些设置。使用 pip:
安装opencv-python
pip 安装 opencv-python
安装opencv-contrib-python
pip 安装 opencv-contrib-python
不幸的是,筛选功能存在一些问题,因为默认情况下它被排除在较新的免费版本的 OpenCV 之外。
import cv2
Image_1 = cv2.imread("Image_1.png", cv2.IMREAD_COLOR)
Image_2 = cv2.imread("Image_2.jpg", cv2.IMREAD_COLOR)
Image_1 = cv2.cvtColor(Image_1, cv2.COLOR_BGR2GRAY)
Image_2 = cv2.cvtColor(Image_2, cv2.COLOR_BGR2GRAY)
sift = cv2.SIFT_create()
keypoints_1, descriptors_1 = sift.detectAndCompute(Image_1,None)
keypoints_2, descriptors_2 = sift.detectAndCompute(Image_2,None)
len(keypoints_1), len(keypoints_2)
我收到的错误:
"/Users/michael/Documents/PYTHON/Test Folder/venv/bin/python" "/Users/michael/Documents/PYTHON/Test Folder/Testing.py"
Traceback (most recent call last):
File "/Users/michael/Documents/PYTHON/Test Folder/Testing.py", line 9, in <module>
sift = cv2.SIFT_create()
AttributeError: module 'cv2.cv2' has no attribute 'SIFT_create'
Process finished with exit code 1
我是 OpenCV 的初学者,正在尝试 运行 一个开源程序。 http://asrl.utias.utoronto.ca/code/gpusurf/index.html
我目前安装了 Computer Vision Toolbox OpenCV Interface 20.1.0 和 Computer Vision Toolbox 9.2。
我无法 运行 这个简单的开源特征匹配算法而不会遇到错误。
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
% read images
img1 = cv2.imread('[INSERT PATH #1]');
img2 = cv2.imread('[INSERT PATH #2]');
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY);
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY);
%sift
sift = cv2.xfeatures2d.SIFT_create();
keypoints_1, descriptors_1 = sift.detectAndCompute(img1,None);
keypoints_2, descriptors_2 = sift.detectAndCompute(img2,None);
len(keypoints_1), len(keypoints_2)
返回以下消息:
Error: File: Keypoints.m Line: 1 Column: 8
The import statement 'import cv2' cannot be found or cannot be imported. Imported names must end with '.*' or be
fully qualified.
但是,当我删除第 1 行时,我反而收到以下错误。
Error: File: Keypoints.m Line: 2 Column: 8
The import statement 'import matplotlib.pyplot' cannot be found or cannot be imported. Imported names must end
with '.*' or be fully qualified.
最后,按照错误消息只会导致 cv2 库出现一系列进一步的错误。有什么想法吗?
那是因为您使用的代码不是 MATLAB 代码,而是 python 代码。
根据您链接的网站:
From within Matlab
The parallel implementation coded in Matlab can be run by using the
surf_find_keypoints()
function. The output keypoints can be sorted by strength usingsurf_best_n_keypoints()
, and plotted usingsurf_plot_keypoints()
.
检查您是否下载了正确的文件,然后重试。
此外,Matlab OpenCV Interface is designed to integrate C++ OpenCV code, not python. Documentations here.
是的,这是 Python 代码是正确的。我建议检查您的 dependencies/libraries。 PyCharm IDE 是我个人使用的,因为它可以轻松处理所有库。
如果您最终尝试了 PyCharm,请将鼠标悬停在 CV2 上时单击红色图标。然后它会提示您下载库。
编辑: 使用 Python 可以完成一些设置。使用 pip:
安装opencv-python pip 安装 opencv-python
安装opencv-contrib-python pip 安装 opencv-contrib-python
不幸的是,筛选功能存在一些问题,因为默认情况下它被排除在较新的免费版本的 OpenCV 之外。
import cv2
Image_1 = cv2.imread("Image_1.png", cv2.IMREAD_COLOR)
Image_2 = cv2.imread("Image_2.jpg", cv2.IMREAD_COLOR)
Image_1 = cv2.cvtColor(Image_1, cv2.COLOR_BGR2GRAY)
Image_2 = cv2.cvtColor(Image_2, cv2.COLOR_BGR2GRAY)
sift = cv2.SIFT_create()
keypoints_1, descriptors_1 = sift.detectAndCompute(Image_1,None)
keypoints_2, descriptors_2 = sift.detectAndCompute(Image_2,None)
len(keypoints_1), len(keypoints_2)
我收到的错误:
"/Users/michael/Documents/PYTHON/Test Folder/venv/bin/python" "/Users/michael/Documents/PYTHON/Test Folder/Testing.py"
Traceback (most recent call last):
File "/Users/michael/Documents/PYTHON/Test Folder/Testing.py", line 9, in <module>
sift = cv2.SIFT_create()
AttributeError: module 'cv2.cv2' has no attribute 'SIFT_create'
Process finished with exit code 1