OpenCV中对视频进行读取显示以及调用摄像头都会用到VideoCpature类!VideoCapture类读入视频的方法一般分为先实例化再初始化和在实例化的同时进行初始化两种!这两种写法的和C语言中int类型的定义一样,两种写法如下:
<1>先实例化再初始化:
int 类型:
int a;
a = 1;
VideoCapture 类型:
VideoCapture capture;
capture.open("C:\\Desktop\\1.avi");
<2>实例化的同时进行初始化:
int 类型:
int a = 1;
VideoCapture 类型:
VideoCapture capture("C:\\Desktop\\1.avi");
一、读取并播放原视频和处理后视频
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
int main()
{
VideoCapture capture("C:\\Desktop\\1.avi");//读入视频文件
while (1)//循环显示每一帧
{
Mat frame;
capture >> frame;
namedWindow("未处理视频");
imshow("未处理视频", frame);
waitKey(30);
Mat edges;
cvtColor(frame, edges, CV_BGR2GRAY);//边缘化处理视频
blur(edges, edges, Size(7, 7));
Canny(edges, edges, 0, 30, 3);
namedWindow("处理后视频");
imshow("处理后视频", edges);
waitKey(30);
}
return 0;
}
二、调用摄像头采集图像以及处理图像
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
int main()
{
VideoCapture capture(0);//从摄像头读入图像
Mat edges;
while (1)
{
Mat frame;
capture >> frame;
namedWindow("未处理图像");
imshow("未处理图像", 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;
}