目录
一、OpenCV简介
基于C++图像处理框架
图像–视觉效果(像素的显示)
内存–数据存储(像素数据的存储RGB, YUV)
算法–对图像像素处理 比如把一张彩色图像–>灰度图
依赖平台:
window下 基于Qt5.12.0
ubuntu下 基于ubuntu16.04 编译器gcc 5.4.0
arm平台 基于ubuntu16.04环境下交叉编译的 编译器 arm-linux-gcc/g++ 5.4.0
二、OpenCV在不同平台的配置
1.window使用opencv
(1)添加环境变量
把运行库依赖添加到环境变量中(window下环境变量–重启qtcreator)
F:\cc_code\video\004\OpencvForQt\opencv64\x64\mingw\bin
在系统环境变量中添加(可能需要重启系统/qtcreator)
在当前工程中添加变量
注:在当前工程中添加就只能在本工程中使用
(2)创建项目
创建一个Qt工程OpencvForQt
在工程文件中添加头文件路径, 和库路径
#添加opencv的库路径
#LIBS += F:/cc_code/video/004/OpencvForQt/opencv64/x64/mingw/lib/libopencv_world344.dll.a
LIBS += $$PWD/opencv64/x64/mingw/lib/libopencv_world344.dll.a
#添加opencv的头文件路径
INCLUDEPATH += F:/cc_code/video/004/OpencvForQt/opencv64/include
INCLUDEPATH += F:/cc_code/video/004/OpencvForQt/opencv64/include/opencv
INCLUDEPATH += F:/cc_code/video/004/OpencvForQt/opencv64/include/opencv2
若使用OpenCV4.0及以上版本,由于OpenCV4.0取消了所有的C语言接口部分,故路径配置不需要添加最后一级文件目录名为“opencv”的路径,即需要添加的路径如下:
#添加opencv的库路径
#LIBS += F:/cc_code/video/004/OpencvForQt/opencv64/x64/mingw/lib/libopencv_world344.dll.a
LIBS += $$PWD/opencv64/x64/mingw/lib/libopencv_world344.dll.a
#添加opencv的头文件路径
INCLUDEPATH += F:/cc_code/video/004/OpencvForQt/opencv64/include
INCLUDEPATH += F:/cc_code/video/004/OpencvForQt/opencv64/include/opencv2
在主函数中添加代码
#include "opencvforqt.h"
#include <QApplication>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
OpencvForQt w;
w.show();
Mat image = imread("./my.jpg");
imshow("myimage", image);
waitKey(30);
return a.exec();
}
注:将my.jpg添加到当前项目运行文件夹中
2. linux搭建opencv的环境
(1)添加环境变量
放在系统配置文件中
sudo vim /etc/bash.bashrc
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/opencv_pc/lib//添加到文件最后面
把opencv的库路径添加到环境变量LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/opencv_pc/lib
注:仅当前项目可用
(2)创建项目
用qtcreator在linux共享目录下创建一个工程OpencvLinux
在工程文件中添加库路径和头文件路径, 库名
LIBS += -L/opt/opencv_pc/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
INCLUDEPATH += /opt/opencv_pc/include
INCLUDEPATH += /opt/opencv_pc/include/opencv
INCLUDEPATH += /opt/opencv_pc/include/opencv2
在主函数中添加opencv代码
#include "opencvlinux.h"
#include <QApplication>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
OpencvLinux w;
Mat mat = imread("./my.jpg");
imshow("image",mat);
waitKey(1);
w.show();
return a.exec();
}
(3)安装ffmpeg库
如果要使用opencv中的摄像头要安装ffmpeg
sudo apt-get install ffmpeg
例:
#include "opencvlinux.h"
#include <QApplication>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
OpencvLinux w;
VideoCapture cap(0); //摄像头对象
Mat image;
cap>>image; //获取摄像头一帧数据
Mat mat = imread("./my.jpg");
imshow("my.jpg",mat);
imshow("image",image);//显示数据
waitKey(1);
w.show();
return a.exec();
}