zoukankan      html  css  js  c++  java
  • OpenCV视频操作

    读取视频

    
    //---------------------------------【头文件、命名空间包含部分】----------------------------
    //		描述:包含程序所使用的头文件和命名空间
    //-------------------------------------------------------------------------------------------------
    #include <opencv2opencv.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 <opencv2opencv.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 <opencv2opencv.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编程入门

  • 相关阅读:
    什么是先进先出淘汰算法,试举出一种实现方法?
    什么是置换算法,在页式系统中常用的置换算法是什么?
    什么是系统的抖动,它有什么危害?
    如果主存中的某页正在与外部设备交换信息,缺页中断时可以将这一页淘汰吗?为了实现正确的页面调度,应如何扩充页表的功能?
    什么是虚拟存储器,在页式系统中如何实现虚拟存储?
    分区分配方法的主要缺点是什么,如何克服这一缺点?
    什么是最坏适应算法?该算法的特点是什么?
    什么是最佳适应算法,该算法的特点是什么?
    JAVA8 之初识函数式编程与函数式接口(一)
    使用 Netty 实现一个 MVC 框架
  • 原文地址:https://www.cnblogs.com/flyingcr/p/10493219.html
Copyright © 2011-2022 走看看