文章目录
- 前言
- Demo
- 图片
- 录制
- 播放
- 人脸识别
- END
前言
OpenCV - Open Computer Vision Library
OpenCV的名声想必不用多说了。
本文介绍4个基础使用demo。分别为,显示图片,录制视频,播放视频和一个基于开源算法库的人脸识别小demo。
只要环境配好,修改一下目标的资源就可以直接运行。
Demo
图片
#include <iostream>
#include <opencv2/opencv.hpp>void cv_image() {/// 资源路径const char *const resPath = "./myLogo.jpg";/// 打开图片的窗口名称const char *const windowName = "image-test";/// 窗口延时时间 毫秒const int windowDelayTime = 5000;cv::Mat img = cv::imread(resPath);if (img.empty()) {std::cout << "image is empty" << std::endl;return ;}/// 设置窗口名称和自适应cv::namedWindow(windowName, cv::WINDOW_AUTOSIZE);/// 将图片与窗口绑定cv::imshow(windowName, img);/// 等待键盘输入 or 延时cv::waitKey(windowDelayTime);/// 销毁窗口cv::destroyWindow(windowName);
}
录制
注意,这里的有部分参数会根据OpenCV3,4的版本问题而有一定差异。
#include <iostream>
#include <opencv2/opencv.hpp>void cv_recordVideo() {const char *const save_path = "./test.avi";const char *const windowName = "camera-test";const int waitKeyTime = 10;/// 设备的cameraID,一般笔记本自带的就是id=0int camera_id = 0;cv::VideoCapture cap;if (false == cap.open(camera_id)) {std::cout << "camera open failed!" << std::endl;return;}/// 视频的长宽const int width = cap.get(cv::CAP_PROP_FRAME_WIDTH);const int height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);/// 1. 路径/// 2. 编码格式/// 3. 帧率/// 4. 尺寸cv::VideoWriter output_video;//! 第二个参数,在opencv3,4不同
#if CV_VERSION_MAJOR >= 4auto fourcc = cv::VideoWriter::fourcc('M', 'J', 'P', 'G');
#elseauto fourcc = CV_FOURCC('M', 'J', 'P', 'G'),
#endifoutput_video.open(save_path, fourcc, 30.0, cv::Size(width, height));for (cv::Mat frame;;) {cap >> frame;if (frame.empty()) {break;}cv::imshow(windowName, frame);/// 保存output_video.write(frame);/// 按下esc键退出if (27 == cv::waitKey(waitKeyTime)) {break;}} // while (1)
}
播放
注意,因为音视频各种编解码问题,此函数这样的写法并不能让所有的视频文件都能正常解析。
但至少保证,上面一个demo录制下来的视频可以播放。
#include <iostream>
#include <opencv2/opencv.hpp>void cv_showVideo() {const char *const resPath = "./test.avi";const char *const windowName = "video-test";const int windowDelayTime = 200;cv::VideoCapture cap;/// 根据当前pc对该文件的解码能力而定if (false == cap.open(resPath)) {std::cout << "open failed!" << std::endl;return;}cv::namedWindow(windowName, cv::WINDOW_AUTOSIZE);/// 视频的本质就是一张张的图片for (cv::Mat frame;;) {cap >> frame;/// 播放完毕if (frame.empty()) {break;}cv::imshow(windowName, frame);/// 按下esc键退出if (27 == cv::waitKey(windowDelayTime)) {break;}} // while (1)
}
人脸识别
开源人脸识别算法库:opencv/data/haarcascades/haarcascade_frontalface_alt.xml at master · opencv/opencv · GitHub
opencv/data/haarcascades at master · opencv/opencv · GitHub
#include <iostream>
#include <opencv2/opencv.hpp>
#include <vector>void cv_faceRecognition() {const char *const windowName = "camera-test";const int waitKeyTime = 10;/// opencv 的一个开源人脸识别库const char *const face_model_path = "./haarcascade_frontalface_alt.xml";/// 设备的cameraID,一般笔记本自带的就是id=0int camera_id = 0;/// 加载算法文件cv::CascadeClassifier face_model;if (false == face_model.load(face_model_path)) {std::cout << "face_model load failed!\n" << std::endl;return;}cv::VideoCapture cap;if (false == cap.open(camera_id)) {std::cout << "camera open failed!" << std::endl;return;}for (cv::Mat frame;;) {cap >> frame;if (frame.empty()) {break;}std::vector<cv::Rect> faces;face_model.detectMultiScale(frame, faces, 1.1, 2,0 | cv::CASCADE_SCALE_IMAGE,cv::Size(30, 30));/// 绘制人脸for (size_t i = 0; i < faces.size(); i++) {/// 点坐标cv::Point center(faces[i].x + faces[i].width / 2,faces[i].y + faces[i].height / 2);/// 画图ellipse(frame, center,cv::Size(faces[i].width / 2, faces[i].height / 2), 0, 0,360, cv::Scalar(255, 0, 255), 4, 8, 0);}cv::imshow(windowName, frame);/// 按下esc键退出if (27 == cv::waitKey(waitKeyTime)) {break;}} // while (1)
}
END
参考资料:
CMake编译OpenCV: Qt配置OpenCV教程,亲测已试过(详细版)_Wi~的博客-CSDN博客
demo: c++ opencv 显示摄像头-人脸检测_哔哩哔哩_bilibili