软件环境配置:
系统环境:WIN10
开发环境:VS2017
opencv:opencv3.4.0
本实例的作用:
- 读取摄像头或者本地视频数据进行播放
- 写入视频数据
- 读取图片在子窗口显示、resize等
void
playVideoFromCam(){
//从摄像头读入视频
VideoCapture capture(0); //0笔记本内置摄像头,1调用usb摄像头
while (1) { Mat frame; //定义一个Mat变量,用于存储每一帧的图像 capture >> frame; //读取当前帧 resize(frame, frame, Size(360, 240)); //改变图像大小 cvtColor(frame, frame, CV_BGR2GRAY); //转为灰度图 imshow("aa", frame); waitKey(30); //延时30ms } }
code:
代码语言:javascript复制//#include<opencv2corecore.hpp>
//#include<opencv2highguihighgui.hpp>
#include <opencv2opencv.hpp>
#include "iostream"
using namespace cv;
//void filePlayVideo();
void videoReadWriter();
int main(){
//filePlayVideo();
videoReadWriter();
system("pause");
return 0;
}
void playVideoFromCam()
{
//从摄像头读入视频
VideoCapture capture(0); //0笔记本内置摄像头,1调用usb摄像头
while (1) {
Mat frame; //定义一个Mat变量,用于存储每一帧的图像
capture >> frame; //读取当前帧
resize(frame, frame, Size(360, 240)); //改变图像大小
cvtColor(frame, frame, CV_BGR2GRAY); //转为灰度图
imshow("aa", frame);
waitKey(30); //延时30ms
}
}
void imageShow()
{
Mat img = imread("Camera20200506134321196.jpg");
namedWindow("游戏原画");
resize(img, img, Size(360, 240));
imshow("游戏原画", img);
waitKey(6000);
}
void filePlayVideo()
{
/***********************************方法1*************************************/
VideoCapture capture;
Mat frame;
frame = capture.open("CAM2202002221459264482.wmv"); //capture.open()的参数为0时为读取摄像头:frame= capture.open(0);
if (!capture.isOpened())
{
printf("can not open ...n");
}
namedWindow("output", CV_WINDOW_AUTOSIZE);
while (capture.read(frame))
{
imshow("output", frame);
waitKey(40);
}
capture.release();
}
//通过摄像头获取视频,然后通过capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT)获取当前帧的宽度和高度,
//创建一个VideoWriter类对象writer进行视频的写入。
void videoWriter()
{
VideoCapture capture;
capture.open(0);
if (!capture.isOpened())
{
printf("can not open ...n");
}
Size size = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT));
VideoWriter writer;
writer.open("E:/image/a2.avi", CV_FOURCC('M', 'J', 'P', 'G'), 10, size, true);
Mat frame, gray;
namedWindow("output", CV_WINDOW_AUTOSIZE);
while (capture.read(frame))
{
//转换为黑白图像
cvtColor(frame, gray, COLOR_BGR2GRAY);
//二值化处理
threshold(gray, gray, 0, 255, THRESH_BINARY | THRESH_OTSU);
cvtColor(gray, gray, COLOR_GRAY2BGR);
imshow("output", gray);
writer.write(gray);
waitKey(10);
}
waitKey(0);
capture.release();
}
//從視頻讀取視頻后再寫入指定的文件
void videoReadWriter()
{
VideoCapture capture;
Mat frame, gray;
frame = capture.open("CAM2202002221459264482.wmv"); //capture.open()的参数为0时为读取摄像头:frame= capture.open(0);
if (!capture.isOpened())
{
printf("can not open ...n");
}
namedWindow("output", CV_WINDOW_AUTOSIZE);
Size size = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT));
VideoWriter writer;
writer.open("a1.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25, size, true);
while (capture.read(frame))
{
//转换为黑白图像
cvtColor(frame, gray, COLOR_BGR2GRAY);
//二值化处理
threshold(gray, gray, 0, 255, THRESH_BINARY | THRESH_OTSU);
cvtColor(gray, gray, COLOR_GRAY2BGR);
imshow("output", gray);
writer.write(gray);
waitKey(40);
}
capture.release();
}
根目錄:
a1.avi