博主QQ:1356438802
QQ群:473383394——UVC&OpenCV47
本文章的开发环境:ubuntu12.04虚拟机 + opencv 2.4.10
一、在上一篇文章中《我的Ubuntu安装使用openCV记录(1)》
我们在ubuntu+opencv2.4.10的环境下,成功测试了sample/c 目录下面的人脸识别例程。我们可以打开build_all.sh这个编译脚本看看。
首先注意我们在《Installing OpenCV 2.1 in Ubuntu》这篇文章中make install之后添加了两项!
1.
sudo gedit /etc/ld.so.conf.d/opencv.conf
/usr/local/lib
2.
sudo gedit /etc/bash.bashrc
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
然后打开build_all.sh
#!/bin/sh
if [ $# -gt 0 ] ; then
base=`basename $1 .c`
echo "compiling $base"
gcc -ggdb `pkg-config opencv --cflags --libs` $base.c -o $base
else
for i in *.c; do
echo "compiling $i"
gcc -ggdb `pkg-config --cflags opencv` -o `basename $i .c` $i `pkg-config --libs opencv`;
done
for i in *.cpp; do
echo "compiling $i"
g++ -ggdb `pkg-config --cflags opencv` -o `basename $i .cpp` $i `pkg-config --libs opencv`;
done
fi
意思是编译的时候会先加载一个opencv相关的配置文件,这个文件就在/usr/local/lib/pkgconfig目录下——opencv.pc
# Package Information for pkg-config
prefix=/usr/local
exec_prefix=${prefix}
libdir=
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 2.4.10
Libs: ${exec_prefix}/lib/libopencv_calib3d.so ${exec_prefix}/lib/libopencv_contrib.so ${exec_prefix}/lib/libopencv_core.so ${exec_prefix}/lib/libopencv_features2d.so ${exec_prefix}/lib/libopencv_flann.so ${exec_prefix}/lib/libopencv_gpu.so ${exec_prefix}/lib/libopencv_highgui.so ${exec_prefix}/lib/libopencv_imgproc.so ${exec_prefix}/lib/libopencv_legacy.so ${exec_prefix}/lib/libopencv_ml.so ${exec_prefix}/lib/libopencv_nonfree.so ${exec_prefix}/lib/libopencv_objdetect.so ${exec_prefix}/lib/libopencv_ocl.so ${exec_prefix}/lib/libopencv_photo.so ${exec_prefix}/lib/libopencv_stitching.so ${exec_prefix}/lib/libopencv_superres.so ${exec_prefix}/lib/libopencv_ts.a ${exec_prefix}/lib/libopencv_video.so ${exec_prefix}/lib/libopencv_videostab.so -lrt -lpthread -lm -ldl
Cflags: -I${includedir_old} -I${includedir_new}
二、我们尝试写一个例程来读取video的数据,
并且模仿上面的方法进行编译
main.cpp
//#include
#include
#include
#include
using namespace cv;
int main(int argc, char *argv[])
{
CvCapture *pCapture = cvCreateCameraCapture(0);
if(NULL == pCapture)
{
printf("cvCreateCameraCapture failed!\n");
return 0;
}
cvNamedWindow("Video", 1);
printf("start grab frame...\n");
while(1)
{
IplImage *pFrame = cvQueryFrame(pCapture);
if( NULL == pFrame )
{
break;
}
cvShowImage("Video", pFrame);
char c = cvWaitKey(33);
if( 27 == c )
{
break;
}
}
cvReleaseCapture(&pCapture);
cvDestroyWindow("Video");
return 0;
}
下面是main.cpp的makefile,写makefile是为了不用每次敲那么长的编译命令!
all:
g++ `pkg-config opencv --cflags` main.cpp -o 1 `pkg-config opencv --libs`
编译通过,然后插入usb camera,在Vmware的菜单中将其加入ubuntu系统中,
如果有,就可以运行程序了。检查/dev/video0文件是否生成。
发现画面非常粗糙,相比《win7_64bits下QT试用openCV库(1)》效果,差了很多很多!
暂时不知道为什么这样
三、接下来我们再修改下我们的makefile,不使用pkg-config
根据上面的分析,我们可以把opencv.pc中的内容加入我们的makefile中,如下:
prefix=/usr/local
exec_prefix=${prefix}
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Cflags=-I/usr/local/include/opencv/ -I/usr/local/include/opencv2/
Libs=${exec_prefix}/lib/*.so -lrt -lpthread -lm -ldl
all:
g++ main.cpp -o 1 ${Cflags} ${Libs}
我们再进一步修改:
prefix=/usr/local
exec_prefix=${prefix}
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Cflags=-I/usr/local/include/opencv/ -I/usr/local/include/opencv2/
Libs=-L${exec_prefix}/lib/*.so -lrt -lpthread -lm -ldl -lopencv_highgui
all:
g++ 1.cpp -o 1 ${Cflags} ${Libs}
实际上这样是makefile最正宗、最原始的写法。任何一个程序,在gcc或者g++编译时,只要能让它在预编译时找到所需要的头文件,在链接时能找到链接库,就可以编译通过。
我们用gcc编译程序时,可能会用到“-I”(大写i),“-L”(大写l),“-l”(小写l)等参数,下面讲述其区别:
例:
gcc -o hello hello.c -I /home/hello/include -L /home/hello/lib -lworld
上面这句表示在编译hello.c时:
-I /home/hello/include表示将/home/hello/include目录作为第一个寻找头文件的目录,寻找的顺序是:/home/hello/include-->/usr/include-->/usr/local/include
-L /home/hello/lib表示将/home/hello/lib目录作为第一个寻找库文件的目录,寻找的顺序是:/home/hello/lib-->/lib-->/usr/lib-->/usr/local/lib
-lworld表示在上面的lib的路径中寻找libworld.so动态库文件(如果gcc编译选项中加入了“-static”表示寻找libworld.a静态库文件)