OpenCV视频操作

读取视频


//---------------------------------【头文件、命名空间包含部分】----------------------------
//		描述:包含程序所使用的头文件和命名空间
//-------------------------------------------------------------------------------------------------
#include <opencv2\opencv.hpp>  
using namespace cv;

//-----------------------------------【main( )函数】--------------------------------------------
//		描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main()
{
	//【1】读入视频
	VideoCapture capture("1.avi");//在实例化VideoCapture的同时进行初始化
	//另外一种写法
	/*videocapture capture;
	capture.open("1.avi");*/
	//【2】循环显示每一帧
	while (1)
	{
		Mat frame;//定义一个Mat变量,用于存储每一帧的图像
		capture >> frame;  //读取当前帧
		if (frame.empty())
		{
			break;
		}
		imshow("读取视频", frame);  //显示当前帧
		waitKey(30);  //延时30ms
	}
	return 0;
}

读取当前帧:

  /** @brief Stream operator to read the next video frame.
    @sa read()
    */
    virtual VideoCapture& operator >> (CV_OUT Mat& image);

    /** @brief Grabs, decodes and returns the next video frame.

    @param [out] image the video frame is returned here. If no frames has been grabbed the image will be empty.
    @return `false` if no frames has been grabbed

    @note In @ref videoio_c "C API", functions cvRetrieveFrame() and cv.RetrieveFrame() return image stored inside the video capturing structure. It is not allowed to modify or release the image! You can copy the frame using cvCloneImage and then do whatever you want with the copy.
     */
    CV_WRAP virtual bool read(OutputArray image);

调用摄像头

//---------------------------------【头文件、命名空间包含部分】----------------------------
//		描述:包含程序所使用的头文件和命名空间
//-------------------------------------------------------------------------------------------------
#include <opencv2\opencv.hpp>  
using namespace cv;

//-----------------------------------【main( )函数】--------------------------------------------
//		描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main()
{
	//【1】从摄像头读入视频
	VideoCapture capture(0);

	//【2】循环显示每一帧
	while (1)
	{
		Mat frame;  //定义一个Mat变量,用于存储每一帧的图像
		capture >> frame;  //读取当前帧
		imshow("读取视频", frame);  //显示当前帧
		waitKey(30);  //延时30ms
	}
	return 0;
}

这里调用摄像头只是简单的修改了capture的初始化参数为0就可以了

对采集的视频进行canny边缘检测

//---------------------------------【头文件、命名空间包含部分】----------------------------
//		描述:包含程序所使用的头文件和命名空间
//-------------------------------------------------------------------------------------------------
#include <opencv2\opencv.hpp>  
using namespace cv;

//-----------------------------------【main( )函数】--------------------------------------------
//		描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main()
{
	//【1】从摄像头读入视频
	VideoCapture capture(0);
	Mat edges;

	//【2】循环显示每一帧
	while (1)
	{
		Mat frame;  //定义一个Mat变量,用于存储每一帧的图像
		capture >> frame;  //读取当前帧

		//将原图像转为灰度图像
		cvtColor(frame,edges,COLOR_BGR2GRAY);
		//使用3X3内核进行降噪(2X3+1 = 7)
		blur(edges, edges, Size(7, 7));
		//进行canny边缘检测并显示
		Canny(edges, edges, 0, 30, 3);
		imshow("被Canny后的视频", frame);  //显示当前帧
		if (waitKey(30) >= 0) break;
		//waitKey(30);  //延时30ms
	}
	return 0;
}

参考

【浅墨的游戏编程Blog】毛星云(浅墨)的专栏](https://blog.csdn.net/poem_qianmo/article/category/1923021)

openCV3编程入门

转载于:https://www.cnblogs.com/flyingcr/p/10493219.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值