使用OpenCV官方提供文件配置、静态编译并运行OpenCV-2.3.x/2.4.x
当我们需要测试程序,或者想将程序发给其他同事或者同学,以让同事或者同学运行以测试程序性能或者逻辑是否恰当时,是不是还要他人也要像你当初开发程序时一样一步一步的配置OpenCV运行环境!?如果不做这些,还会提示像图1所示的系统错误(无法启动程序,因为计算机中丢失“opencv_highgui241d.dll。尝试重新安装该程序以解决此问题”),这是不是太麻烦了,其实我们可以将程序以静态链接的方式编译,如此便少去了配置运行时环境的步骤了。(2)添加附加依赖项有两种方法,见《OpenCV 2.3.x/2.4.x在Visual Studio 2005/2008和Visual Studio 2010配置方法详解》一文。 编译程序,会发发现程序不能正常编译(错误如图2所示),这主要与MSVCRT.lib(MSVCR90.dll)有关。
图1 动态编译的程序在未配置OpenCV的机器上运行提示系统错误
静态编译,也就是说编译后的程序可以直接拷贝到其他机器上并能直接运行。而不需附带链接库和配置环境变量。
由于OpenCV自2.3.0版本开始就提供了OpenCV-2.3.x -win-superpack.exe或OpenCV-2.4.x.exe文件,该文件解压后我们会发里面有个名为“build”的文件夹,其中有各个平台的链接库,而且还提供了Windows平台下的静态链接库,本文主要讲述一个例子以说明如何配置使用该静态链接库。
配置完Visual Studio 2008中的VC++目录后便可以编写代码了。
(1)编辑代码
“新建项目”,选择“Visual C++-空项目”,输入项目名“StaticOpenCV”,“添加新项-代码-C++ 文件”,输入文件名“StaticOpenCV.cpp”,编辑代码,这里本人使用的测试代码如下所示:
- #include <opencv/highgui.h>
- int main( int argc, char** argv ) {
- IplImage*img = cvLoadImage( argv[1] );
- cvNamedWindow("OpenCV", CV_WINDOW_AUTOSIZE );
- cvShowImage("OpenCV", img );
- cvWaitKey(0);
- cvReleaseImage(&img );
- cvDestroyWindow("OpenCV" );
- return( 0 );
- }
OpenCV-2.4.1库文件列表(使用的时候适当选择,这里我要用的是libjasperd.lib、libjpegd.lib、libpngd.lib"、libtiffd.lib、zlibd.lib(注意这几个必须要用的,因为这些是OpenCV所使用的第三方库)和opencv_highgui241d.lib、opencv_core241d.lib库文件。
- libjasper.lib
- libjasperd.lib
- libjpeg.lib
- libjpegd.lib
- libpng.lib
- libpngd.lib
- libtiff.lib
- libtiffd.lib
- opencv_calib3d241.lib
- opencv_calib3d241d.lib
- opencv_contrib241.lib
- opencv_contrib241d.lib
- opencv_core241.lib
- opencv_core241d.lib
- opencv_features2d241.lib
- opencv_features2d241d.lib
- opencv_ffmpeg241.dll
- opencv_flann241.lib
- opencv_flann241d.lib
- opencv_gpu241.lib
- opencv_gpu241d.lib
- opencv_haartraining_engine.lib
- opencv_haartraining_engined.lib
- opencv_highgui241.lib
- opencv_highgui241d.lib
- opencv_imgproc241.lib
- opencv_imgproc241d.lib
- opencv_legacy241.lib
- opencv_legacy241d.lib
- opencv_ml241.lib
- opencv_ml241d.lib
- opencv_nonfree241.lib
- opencv_nonfree241d.lib
- opencv_objdetect241.lib
- opencv_objdetect241d.lib
- opencv_photo241.lib
- opencv_photo241d.lib
- opencv_stitching241.lib
- opencv_stitching241d.lib
- opencv_ts241.lib
- opencv_ts241d.lib
- opencv_video241.lib
- opencv_video241d.lib
- opencv_videostab241.lib
- opencv_videostab241d.lib
- zlib.lib
- zlibd.lib
- #pragma comment( lib,"libjasperd.lib" )
- #pragma comment( lib,"libjpegd.lib" )
- #pragma comment( lib,"libpngd.lib" )
- #pragma comment( lib,"libtiffd.lib" )
- #pragma comment( lib, "zlibd.lib")
- #pragma comment( lib,"opencv_highgui241d.lib" )
- #pragma comment( lib,"opencv_core241d.lib" )
图2 编译产生很多错误
相关推荐
liugan 2020-03-06
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20