当前位置: 首页 > article >正文

opencv入门教程

文章目录

    • opencv简介
      • opencv使用
        • CMakeLists.txt文件说明:
      • 实例
        • 图形图像的处理
        • 视频处理
        • 视频监控
    • 图像与图形
    • 图形绘制
    • 人脸识别与标记
    • 文本识别(车牌)

opencv简介

opencv是一个开源的计算机视觉库,可以用于图形图像和视频处理
安装:

  1. 直接安装

apt install libopencv-dev

  1. 自己编译

本体源码:
https://github.com/opencv/opencv/releases/tag/4.2.0
扩展库源码:
https://github.com/opencv/opencv_contrib/releases

opencv使用

使用OpenCV进行开发

  1. 搭建环境
  2. 工具链
    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

视频处理
  1. 在Project_OpenCV_0530目录下新建一个C++源文件
video_test.cpp
china@ubuntu:~/Project_OpenCV_0530/build$ cd ..
china@ubuntu:~/Project_OpenCV_0530$ touch video_test.cpp
  1. 编写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;
}
  1. 修改CMakeLists.txt文件
# 定义使用什么源文件生成指定得输出文件
add_executable(opencv_test video_test.cpp)
  1. 编译
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
  1. 执行
    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


http://www.kler.cn/a/289886.html

相关文章:

  • 【IC每日一题:IC常用模块--RR/handshake/gray2bin】
  • C++STL容器——map和set
  • 如何在python中模拟重载初始化函数?
  • Spring Boot中的自动装配机制
  • 马斯克万卡集群AI数据中心引发的科技涟漪:智算数据中心挑战与机遇的全景洞察
  • python魔术方法的学习
  • 爬楼梯[简单]
  • 力扣SQL仅数据库(196~569)
  • AI图像放大工具,图片放大无所不能
  • vue通过html2canvas+jspdf生成PDF问题全解(水印,分页,截断,多页,黑屏,空白,附源码)
  • Kafka【六】Linux下安装Kafka(Zookeeper)集群
  • 【AI】前向和反向传播的关系
  • 深度学习与电网信号故障诊断:基于卷积神经网络和残差网络的应用
  • 【Grafana】Prometheus结合Grafana打造智能监控可视化平台
  • 15、VSCode自定义Markwown编辑环境
  • Spring Cloud Consul 与 Eureka 对比:如何选择最佳服务发现工具
  • 微信小程序客户端与服务端进行WebSocket通信
  • 文本数据分析-(TF-IDF)(2)
  • 初识redis:学习Java客户端
  • 深度学习实用方法 - 调试策略篇
  • 9 月 7-8 日,Rust China Conf 2024 来啦!
  • TPH-YOLOv5:基于Transformer预测头的改进YOLOv5,用于无人机捕获场景的目标检测
  • 华为云征文|华为云Flexus X实例docker部署srs6并调优,协议使用webrtc与rtmp
  • 每天一个数据分析题(五百一十八)- Skip-Gram模型
  • python自动化操作PDF,拆分pdf合并pdf,提取pdf内容
  • 张江创新券的一些介绍