【图文】【DIY便签】如何自行编译OPENCV使用动态库
1 去官网下载安装包和源码
下面红色圈中的是源码,绿色圈中的是安装包:
2 配置工具链
安装过程不说了,教程到处都是。编译的话使用CMAKE,配置如下:
上面两个路径分别是:
- 源码目录
- 编译生成的文件放置的位置,这里放在源码目录下的build目录中
然后点下面那个Configure,配置工具链,也就是选编译器的bin目录,这个看你自己用的啥编译器,我是安装的GNU工具链,还有就是选make工具,我选的是minGW Make Tool,看你自己用的什么。配置的时候它会自己下载第三方包,可能比较慢。
之后就点Gernate生成makefiles,很快。
3 开始编译
生成完了之后就可以进到源码目录编译了,我这里使用mingw32-make.exe 作为make工具
编译过程会有进度提示:
[100%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/rotationConverters.cpp.obj
[100%] Linking CXX executable ..\..\bin\opencv_interactive-calibration.exe
[100%] Built target opencv_interactive-calibration
[100%] Building CXX object apps/version/CMakeFiles/opencv_version.dir/opencv_version.cpp.obj
[100%] Linking CXX executable ..\..\bin\opencv_version.exe
[100%] Built target opencv_version
[100%] Building CXX object apps/version/CMakeFiles/opencv_version_win32.dir/opencv_version.cpp.obj
[100%] Built target opencv_version
[100%] Building CXX object apps/version/CMakeFiles/opencv_version_win32.dir/opencv_version.cpp.obj
[100%] Linking CXX executable ..\..\bin\opencv_version_win32.exe
[100%] Built target opencv_version
[100%] Building CXX object apps/version/CMakeFiles/opencv_version_win32.dir/opencv_version.cpp.obj
[100%] Built target opencv_version
[100%] Built target opencv_version
[100%] Building CXX object apps/version/CMakeFiles/opencv_version_win32.dir/opencv_version.cpp.obj
[100%] Linking CXX executable ..\..\bin\opencv_version_win32.exe
[100%] Built target opencv_version_win32
[100%] Building CXX object apps/model-diagnostics/CMakeFiles/opencv_model_diagnostics.dir/model_diagnostics.cpp.obj
[100%] Linking CXX executable ..\..\bin\opencv_model_diagnostics.exe
[100%] Built target opencv_model_diagnostics
到100%就编译完了:
build/bin目录下会有一堆动态库:
写一个测试demo,这里用官方提供的:
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
printf("usage: DisplayImage.out <Image_Path>\n");
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], IMREAD_COLOR );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);waitKey(0);waitKey(0);waitKey(0);waitKey(0);
return 0;
}
用下面这个命令编译:
g++ ./showiamge.cpp
-I L:\SoftWares\opencv\opencv\build\include
-L L:/SoftWares/opencv-4.10.0/build/lib
-lopencv_calib3d4100 -lopencv_core4100 -lopencv_dnn4100 -lopencv_features2d4100 -lopencv_flann4100 -lopencv_gapi4100 -lopencv_highgui4100 -lopencv_imgcodecs4100 -lopencv_imgproc4100 -lopencv_ml4100 -lopencv_objdetect4100 -lopencv_photo4100 -lopencv_stitching4100 -lopencv_ts4100 -lopencv_video4100 -lopencv_videoio4100
注意头文件目录要在安装目录里去找,里面有各个模块的头文件:
-I L:\SoftWares\opencv\opencv\build\include
安装目录的\build\include:
源码编译目录下的include\只有一个头文件
生成的a.exe就是编译出来的可执行文件,这时候需要把动态库放到同一个目录下:
然后就可以执行了,完事。