OpenCV视频基本操作


利用OpenCV中的VideoCapture类,可以对视频进行读取显示,以及调用摄像头。

读取并播放视频

#include<opencv2/opencv.hpp>
using namespace cv;

int main()
{
	VideoCapture capture("lj.avi");
	while (1)
	{
		Mat frame;
		capture >> frame;
		imshow("读取视频", frame);
		waitKey(30);
	}
	return 0;
}

调用摄像头并采集图像

调用摄像头

将“_.avi”换成0,表示调用摄像头

#include<opencv2/opencv.hpp>
using namespace cv;

int main()
{
	VideoCapture capture(0);
	while (1)
	{
		Mat frame;
		capture >> frame;
		imshow("读取视频", frame);
		waitKey(30);
	}
	return 0;
}

边缘检测并高斯模糊后的摄像头采集视频

int main()
{
	VideoCapture capture(0);
	Mat edges;

	while (1)
	{
		Mat frame;
		capture >> frame;

		cvtColor(frame, edges, CV_BGR2GRAY);
		blur(edges, edges, Size(7, 7));
		Canny(edges, edges, 0, 30, 3);

		imshow("被canny后的视频", edges);
		if(waitKey(30)>=0)break;

	}
	return 0;
}
发布了20 篇原创文章 · 获赞 14 · 访问量 856

猜你喜欢

转载自blog.csdn.net/weixin_43645790/article/details/104063270