尊重原创,以下内容主要参照 Ubuntu14.04安装Opencv, 和Ubuntu14.04下Qt调用Opencv 两篇博文而来。
第一部分安装Opencv2.4.9
1、啥都不说,先系统更新
2、安装相关依赖包
1 |
sudo apt-get install build-essential
libgtk2.0-dev libjpeg-dev libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy python-tk libtbb-dev libeigen3-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev libqt4-dev
libqt4-opengl-dev sphinx-common texlive-latex-extra libv4l-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev default-jdk ant libvtk5-qt4-dev |
3、利用wget获得Opencv2.4.9源文件,等下载完成后解压
4、cmake opencv源文件包
3 |
cmake
-D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON .. |
注意:请确保上面命令没有出现错误,确认 FFMPEG 为YES,否则可能会造成你不能读写视频 。
5、make(时间比较长) 和install
6、配置opencv相关
1 |
sudo gedit
/etc/ld.so.conf.d/opencv.conf |
7、在空文件里添加如下代码并保存
8、执行以下代码
9、打开另外一个文件
1 |
sudo gedit
/etc/ bash .bashrc |
9、在文件末尾添加如下并保存退出
1 |
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/ local /lib/pkgconfig |
10、检查opencv是否安装成功
1 |
cd ~/opencv-2.4.9/samples/c |
老版本的C语言接口
1 |
./facedetect
--cascade= "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" --scale=1.5
lena.jpg |
1 |
./facedetect
--cascade= "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" --nested-cascade= "/usr/local/share/OpenCV/haarcascades/haarcascade_eye.xml" --scale=1.5
lena.jpg |
新的C++接口
1 |
~/opencv-2.4.9/build/bin/cpp-example-grabcut
~/opencv-2.4.9/samples/cpp/lena.jpg |
后面的 这个可以不用管了,直接跳到第二部分
1 |
~/opencv-2.4.9/build/bin/cpp-example-calibration_artificial |
Now let’s run some Python code:
1 |
python
~/opencv-2.4.9/samples/python2/turing.py |
Now you can build a Java sample using ant for example. (Make sure that you change/home/samontab/ with
your actual home directory):
1 |
cd
~/opencv-2.4.9/samples/java/ant |
2 |
ant
-DocvJarDir=/home/samontab/opencv-2.4.9/build/bin -DocvLibDir=/home/samontab/opencv-2.4.9/build/lib |
Now let’s read a video and use OpenGL with Qt through
this great sample that detects the features from the video, then estimates the 3D location of the structure using POSIT, and finally uses
OpenGL to draw in 3D (great sample Javier):
1 |
cd
~/opencv-2.4.9/samples/cpp/Qt_sample |
And finally, let’s build a sample using the 3D visualization module viz:
1 |
cd
~/opencv-2.4.9/samples/cpp/tutorial_code/viz |
2 |
g++
-o widget_pose `pkg-config opencv --cflags` widget_pose.cpp `pkg-config opencv --libs` |
As you can see, now you can use OpenCV with C++, C, Python, and Java. The Qt enhanced 2D interface is enabled, 3D data can be displayed using OpenGL directly, or using the new viz module. Multi threading functionality is enabled using TBB. Also, video support
is enabled as well.
If you correctly finished this tutorial, you are now ready to do some nice computer vision work.
第二部分 Qt的安装
QT的安装过程其实很简单,只需要使用软件中心就可以了,在软件中心里输入Qtcreat就可以的
第三部分 Qt下调用opencv
配置QT, sudo gedit /usr/share/qt4/mkspecs/default/qmake.conf
在文件内添加了下面两句:
QMAKE_INCDIR = /usr/include/opencv
QMAKE_LIBS = -lcvaux -lcv -lcxcore -lhighgui -lml
实例程序,创建一个QT4 Console Application,代码如下:
#include <cv.h>
#include <highgui.h>
int main()
{
IplImage *img = 0;
img = cvLoadImage("Lena.bmp",-1);//Lena.bmp在所建工程的目录下
cvNamedWindow("lena", 1);
cvShowImage("lena", img);
cvWaitKey(0);
return 0;
}
关键的一步,在工程的.pro文件添加下面的内容:
INCLUDEPATH += /usr/include/opencv
LIBS += /usr/lib/libcv.so /usr/lib/libcvaux.so /usr/lib/libcxcore.so /usr/lib/libhighgui.so /usr/lib/libml.so
这时就能编译运行了。
时间: 2024-10-06 08:49:24