opencv入门教程
文章目录
- opencv简介
- opencv使用
- CMakeLists.txt文件说明:
- 实例
- 图形图像的处理
- 视频处理
- 视频监控
- 图像与图形
- 图形绘制
- 人脸识别与标记
- 文本识别(车牌)
opencv简介
opencv是一个开源的计算机视觉库,可以用于图形图像和视频处理
安装:
- 直接安装
apt install libopencv-dev
- 自己编译
本体源码:
https://github.com/opencv/opencv/releases/tag/4.2.0
扩展库源码:
https://github.com/opencv/opencv_contrib/releases
opencv使用
使用OpenCV进行开发
- 搭建环境
- 工具链
C程序的编译过程 – 自动编译管理工具make – 通过Makefile文件来指定自动编译的过程和步骤 – cmake – 通过CMakeLists.txt文件来输出/生产Makefile文件
CMakeLists.txt文件说明:
# cmake needs this line
cmake_minimum_required(VERSION 3.1)
# 定义工程的名字
project(Project_OpenCV_0628)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " config: ${OpenCV_DIR}")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
# 定义使用什么源文件生成指定得输出文件
add_executable(opencv_test video_capture_test.cpp)
# 指定生成输出文件使用得库
target_link_libraries(opencv_test PRIVATE ${OpenCV_LIBS})
实例
图形图像的处理
使用OpenCV库实现图形图像的处理
-
在工作目录下,新建一个项目的目录
china@ubuntu:~$ mkdir Project_OpenCV_0530 -
在项目目录中新建两个文件: test.cpp, CMakeLists.txt
china@ubuntu:~$ cd Project_OpenCV_0530/
china@ubuntu:~/Project_OpenCV_0530$ touch test.cpp
china@ubuntu:~/Project_OpenCV_0530$ touch CMakeLists.txt -
在项目目录下新建一个目录: build
china@ubuntu:~/Project_OpenCV_0530$ mkdir build
china@ubuntu:~/Project_OpenCV_0530$ ls
build CMakeLists.txt test.cpp -
编写CMakeLists.txt文件
# cmake needs this line
cmake_minimum_required(VERSION 3.1)
# 定义工程的名字
project(Project_OpenCV_0530)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " config: ${OpenCV_DIR}")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
# 定义使用什么源文件生成指定得输出文件
add_executable(opencv_test test.cpp)
# 指定生成输出文件使用得库
target_link_libraries(opencv_test PRIVATE ${OpenCV_LIBS})
- 编写cpp文件
先拷贝一个图片文件放到Linux工作目录: bbb.jpg
编写C++源文件:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv; //OpenCV的命名空间
int main(int argc, char *argv[])
{
Mat img = imread("/home/china/bbb.jpg");
imshow("OpenCV显示图片", img);
waitKey(0);
return 0;
}
- 编译
先进入到build目录,执行cmake,生产Makefile文件
cd build/
cmake ..
ls
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
编译
make
Scanning dependencies of target opencv_test
[ 50%] Building CXX object CMakeFiles/opencv_test.dir/test.cpp.o
[100%] Linking CXX executable opencv_test
[100%] Built target opencv_test
运行
./opencv_test
视频处理
- 在Project_OpenCV_0530目录下新建一个C++源文件
video_test.cpp
china@ubuntu:~/Project_OpenCV_0530/build$ cd ..
china@ubuntu:~/Project_OpenCV_0530$ touch video_test.cpp
- 编写C++源文件
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
VideoCapture video;
video.open("/home/china/test.avi");
if(!video.isOpened())
{
return -1;
}
while(true)
{
Mat img;
video >> img;
if(!img.empty())
{
imshow("视频显示", img);
}
if(waitKey(30) > 0)
{
break;
}
}
return 0;
}
- 修改CMakeLists.txt文件
# 定义使用什么源文件生成指定得输出文件
add_executable(opencv_test video_test.cpp)
- 编译
china@ubuntu:~/Project_OpenCV_0530$ cd build
china@ubuntu:~/Project_OpenCV_0530/build$ cmake ..
-- OpenCV library status:
-- config: /usr/local/lib/cmake/opencv4
-- version: 4.5.1
-- libraries:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/china/Project_OpenCV_0530/build
china@ubuntu:~/Project_OpenCV_0530/build$ make
Scanning dependencies of target opencv_test
[ 50%] Building CXX object CMakeFiles/opencv_test.dir/video_test.cpp.o
[100%] Linking CXX executable opencv_test
[100%] Built target opencv_test
- 执行
china@ubuntu:~/Project_OpenCV_0530/build$ ./opencv_test
视频监控
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
VideoCapture video(0);
if(!video.isOpened())
{
return -1;
}
Mat img;
while(true)
{
video >> img;
imshow("视频监控", img);
if(waitKey(30) >= 0)
{
break;
}
}
waitKey(0);
return 0;
}
图像与图形
图像:采集、编码后的数据
图形:计算机绘制渲染的
图片格式
- 位图
- 1、无损:bmp
- 2、有损:jpg/jpeg、png、gif、tiff
*
、webp*
- 矢量图
- svg、eps、ai
图形绘制
示例:
https://docs.opencv.org/4.x/d3/d96/tutorial_basic_geometric_drawing.html
人脸识别与标记
人脸识别:
直接使用OpenCV中的人脸识别模型训练数据
- 企业提供的人脸识别库:
- 百度: http://ai.baidu.com/
- 虹软: https://www.arcsoft.com.cn/
下载SDK,下载demo,文档
示例:
https://docs.opencv.org/4.x/d4/d26/samples_2cpp_2facedetect_8cpp-example.html
文本识别(车牌)
示例:
https://docs.opencv.org/4.x/db/da4/samples_2dnn_2text_detection_8cpp-example.htm