Opencv
opencv是一个开源的图形图像处理工具,具有对图像进行数学建模,对其进行一系列的处理,为图像的识别和训练做准备。
1、使用
(1)在相应的平台用对应的工具编译opencv的源码。
1)linux平台
配置–安装–编译
2)windows平台
CMake
编译会生成的目录包括:库文件、头文件、命令文件、工具文件…
windows 64位-----------------------opencv64.zip
arm-linux------------------------------opencv-3.4.4.arm.tar.bz2
(2)如何在windows的QT中使用opencv
1)解压opencv64.zip到工程源代码目录
2)将opencv的头文件路径、库文件路径、可执行程序路径加入到QT工程中
①在pro文件中添加头文件路径:
INCLUDEPATH += E:\GZ2142\11-Qt\code\s_project\MyWinOpencv\opencv64\include INCLUDEPATH += E:\GZ2142\11-Qt\code\s_project\MyWinOpencv\opencv64\include\opencv INCLUDEPATH += E:\GZ2142\11-Qt\code\s_project\MyWinOpencv\opencv64\include\opencv2
②在pro文件中添加库文件路径
LIBS += E:\GZ2142\11-Qt\code\s_project\MyWinOpencv\opencv64\x64\mingw\lib\libopencv*
如下图:
③添加运行时命令的路径
1、直接将可执行程序的路径加入到系统环境变量path中
步骤:【此电脑】-》【属性】-》【高级系统设置】-》【环境变量】-》【新建】
2、只修改本工程的path路径,添加
E:\GZ2142\11-Qt\code\s_project\MyWinOpencv\opencv64\x64\mingw\bin
3)使用opencv显示图片和视频
测试代码:
#include "mywinopencv.h" #include <QApplication> #include <opencv.hpp> #include <opencv2/core.hpp> #include <opencv2/highgui.hpp> #include <opencv2/imgcodecs.hpp> using namespace cv; int main(int argc, char *argv[]) { QApplication a(argc, argv); #if 0 //使用opencv显示一张图片 Mat myimage = imread("E:/GZ2142/11-Qt/code/s_day05/1.jpg"); imshow("Image",myimage); //等待 waitKey(10); #else Mat myimage; //获取摄像头 VideoCapture video(0); //显示 while(1){ //获取一帧 video>>myimage; //显示一帧 imshow("Video",myimage); //延时 waitKey(1); } #endif MyWinOpencv w; w.show(); return a.exec(); }
运行结果:
显示图片:
(3)在arm-linux中使用opencv
1)解压opencv-3.4.4.arm.tar.bz2到Ubuntu的/opt目录下
2)在工程文件中添加头文件和库文件路径
INCLUDEPATH += /opt/opencv/include INCLUDEPATH += /opt/opencv/include/opencv INCLUDEPATH += /opt/opencv/include/opencv2 LIBS += -L/opt/opencv/lib -lopencv_calib3d -lopencv_core -lopencv_dnn -lopencv_face \ -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgcodecs \ -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo \ -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videoio \ -lopencv_video -lopencv_videostab
3)编写代码然后交叉编译,在开发板上运行
#include "myarmopencv.h" #include <QApplication> #include <opencv.hpp> #include <opencv2/core.hpp> #include <opencv2/highgui.hpp> #include <opencv2/imgcodecs.hpp> using namespace cv; int main(int argc, char *argv[]) { QApplication a(argc, argv); Mat myimage; MyArmOpencv w; w.show(); return a.exec(); }
4)将opencv的所有库文件拷贝到开发板的某个目录,并且设置LD_LIBRARY_PATH环境变量(程序运行时动态库的搜索路径)。
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/Even/opencv/lib
.
opencv的可执行程序的…/…/lib目录必须放上opencv的库文件