对于Debian用户来说,创建自己的libopencv-nonfree软件包非常“容易”。
我按照Python的OpenCV教程进行操作,但是在我的Debian系统中,SIFT和SURF模块缺失。并且,Debian没有可用的包含SIFT和SURF等模块的非免费软件包。
这些模块由于许可问题而被剥离出软件包......
我以前从未为Debian创建过软件包(添加新模块等),但是我遵循了Debian教程中的一些简单步骤,并尝试和猜测了一些东西,经过1天的努力,哇......我得到了一个工作正常的libopencv-nonfree2.4 deb软件包和正确绑定的Python模块。
(我不知道是否还需要安装新构建的python-opencv软件包或仅需要非免费软件包...我重新安装了两者,得到了一个带有所有必要非免费模块的工作正常的Python OpenCV库!)
好的,这里是:
!这是针对libopencv 2.4的!
!您可以执行除安装之外的所有步骤,如同普通用户一样!
我们需要从Debian存储库获取编译和创建新软件包所需的构建基础和一些工具。
sudo apt-get install build-essential fakeroot devscripts
在你的个人主目录中创建一个文件夹并进入该文件夹:
cd ~ && mkdir opencv-debian
cd opencv-debian
下载所需的软件包:
apt-get source libopencv-core2.4
并下载所有必需的依赖包来构建新的opencv
apt-get build-dep libopencv-core2.4
这将下载所需的源代码并创建一个名为“opencv-2.4.9.1+dfsg”的目录。
切换到该目录:
cd opencv-2.4.9.1+dfsg
现在你可以通过输入以下命令来测试包是否无需修改即可构建:
fakeroot debian/rules binary
这需要花费很长时间!这一步应该在没有错误的情况下完成。现在你在opencv-debian目录中有很多.deb软件包。
接下来我们对包定义进行一些修改,让Debian构建非自由模块和软件包!
切换到opencv-debian目录并下载正确的OpenCV源代码...在我的例子中是OpenCV 2.4.9之类的版本。
我从
https://github.com/Itseez/opencv/releases获取了我的版本。
wget https://codeload.github.com/Itseez/opencv/tar.gz/2.4.9
这将会下载opencv-2.4.9.tar.gz文件。
解压缩该存档:
tar -xzvf opencv-2.4.9.tar.gz
这将解压原始源代码到名为opencv-2.4.9的目录中。
现在将非自由模块从原始源代码复制到Debian源代码中:
cp -rv opencv-2.4.9/modules/nonfree opencv-2.4.9.1+dfsg/modules/
好的,现在我们已经有了非自由模块的源代码,但这对于Debian来说还不够...我们需要修改一个文件并创建一个新文件。
我们需要编辑Debian控制文件,并在文件末尾添加一个新的部分:
(我在这里使用mcedit作为编辑器)
mcedit opencv-2.4.9.1+dfsg/debian/control
或使用您选择的任何其他编辑器
并添加此部分:
Package: libopencv-nonfree2.4
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}
Description: OpenCV Nonfree Modules like SIFT and SURF
This package contains nonfree modules for the OpenCV (Open Computer Vision)
library.
.
The Open Computer Vision Library is a collection of algorithms and sample
code for various computer vision problems. The library is compatible with
IPL (Intel's Image Processing Library) and, if available, can use IPP
(Intel's Integrated Performance Primitives) for better performance.
.
OpenCV provides low level portable data types and operators, and a set
of high level functionalities for video acquisition, image processing and
analysis, structural analysis, motion analysis and object tracking, object
recognition, camera calibration and 3D reconstruction.
现在我们创建一个名为libopencv-nonfree2.4.install的新文件。
touch opencv-2.4.9.1+dfsg/debian/libopencv-nonfree2.4.install
并编辑:
mcedit opencv-2.4.9.1+dfsg/debian/libopencv-nonfree2.4.install
并添加以下内容:
usr/lib/*/libopencv_nonfree.so.*
好的,现在重新创建软件包:
cd opencv-2.4.9.1+dfsg
首先进行清理:
fakeroot debian/rules clean
并构建:
fakeroot debian/rules binary
完成之后,您将获得一个全新的构建和一个名为libopencv-nonfree2.4.deb的新软件包!
现在以root身份安装:
dpkg -i libopencv-nonfree2.4.deb
dpkg -i python-opencv.deb
并测试!
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('test.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
sift = cv2.SIFT()
kp = sift.detect(gray,None)
img=cv2.drawKeypoints(gray,kp)
corners = cv2.goodFeaturesToTrack(gray,16,0.05,10)
corners = np.int0(corners)
for i in corners:
x,y = i.ravel()
cv2.circle(img,(x,y),90,255,3)
plt.imshow(img),plt.show()
祝你玩得愉快!
cv2.xfeatures2d.SURF_create
代替cv2.SURF
(许多教程都在使用旧的调用方法)。 - Paulo Scardine