Ubuntu+QT5+OpenCV3.4搭建笔记
想在Linux上学习C++图像处理,所以需要在Linux环境下部署开发环境,昨天忙了半天算是部署完成。搭建完成后运行的测试程序效果就是这个视频这样,对调用的摄像头图像做了简单处理,
程序效果:
现在把搭建过程大致记录下来。
安装Ubuntu参考这篇教程:https://blog.csdn.net/lingyunxianhe/article/details/81415675
安装好需要将Ubuntu的源换成国内,教程参考:https://blog.csdn.net/maizousidemao/article/details/79127695
1.做完这些后首先开始安装QT5.到官网下载QT5.12.7,版本选择qt-opensource-linux-x64-5.12.7.run。下载后在Ubuntu终端运行命令安装:
sudo apt-get update
cd DownLoads #有些是中文版的系统,就输入 :cd 下载
chmod +x ./qt-opensource-linux-x64-5.12.1.run
sudo ./qt-opensource-linux-x64-5.12.1.run
出现安装界面,按照提示下一步安装
2.安装OpenCV3.4
首先安装构建OpenCV的相关工具:
// 安装build-essential、cmake、git和pkg-config
sudo apt-get install build-essential cmake git pkg-config
// 安装jpeg格式图像工具包
sudo apt-get install libjpeg8-dev
// 安装tif格式图像工具包
sudo apt-get install libtiff5-dev
// 安装JPEG-2000图像工具包
sudo apt-get install libjasper-dev
// 安装png图像工具包
sudo apt-get install libpng12-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libgtk2.0-dev
sudo apt-get install libatlas-base-dev gfortran
然后安装OpenCV源码包
#整个下载的过程相对漫长,一般在半小时左右
#下载OpenCV
wget -O opencv-3.4.1.zip https://github.com/Itseez/opencv/archive/3.4.1.zip
解压OpenCV
unzip opencv-3.4.1.zip
前面已经把源代码解压了,所以现在要切换到源码的文件夹:
cd opencv-3.4.1
新建一个名为build的文件夹用来存放cmake编译时产生的临时文件:
// 新建build文件夹
mkdir build
// 进入builf文件夹
cd build
设置cmake编译参数,安装目录默认为/usr/local,在终端输入:
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local …
Cmake完后进行正式编译
// 编译,以管理员身份,否则容易出错,-j4用来加快编译速度
sudo make -j4
// 安装
sudo make install
// 更新动态链接库
sudo ldconfig
到此OpenCv的安装完成,现在我们结合QT做测试.,这里运行了一个摄像头调取程序。
打开QtCreator,点击File,选择New File or Project,选择None-Qt Project,选择Plain C++ Project,点击choose ,然后基本都选默认的就行
项目建立完成后在左栏就会看到一个*.pro文件,修改.pro文件中的内容
QT += core
QT -= gui
TARGET = opencv_test
CONFIG += console
CONFIG -= app_bundle
INCLUDEPATH += /usr/local/include \
/usr/local/include/opencv \
/usr/local/include/opencv2
LIBS += /usr/local/lib/libopencv_calib3d.so //usr/local/lib/libopencv_core.so //usr/local/lib/libopencv_features2d.so //usr/local/lib/libopencv_flann.so //usr/local/lib/libopencv_highgui.so //usr/local/lib/libopencv_imgcodecs.so //usr/local/lib/libopencv_imgproc.so //usr/local/lib/libopencv_ml.so //usr/local/lib/libopencv_objdetect.so //usr/local/lib/libopencv_photo.so //usr/local/lib/libopencv_shape.so //usr/local/lib/libopencv_stitching.so //usr/local/lib/libopencv_superres.so //usr/local/lib/libopencv_videoio.so //usr/local/lib/libopencv_video.so //usr/local/lib/libopencv_videostab.so
TEMPLATE = app
SOURCES += main.cpp
然后在main.cpp中添加代码:
```cpp
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
VideoCapture capture(0);
Mat edges;
while (1) {
Mat frame;
capture >> frame;
cvtColor(frame, edges, CV_BGR2GRAY);
blur(edges, edges, Size(7, 7));
Canny(edges, edges, 0, 30, 3);
imshow("边缘检测摄像头", edges);
if (waitKey(30) >= 0) {
break;
}
}
return 0;
}
编译完成运行后就会看到开头那个![在这里插入图片描述](https://img-blog.csdnimg.cn/20200301224458341.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MjMwNTg5,size_16,color_FFFFFF,t_70)效果。