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编程入门

  • 相关阅读:
    2018 南京网络预赛Sum
    一个莫比乌斯等式的证明
    LOJ 2452 对称 Antisymmetry——用hash求回文串数
    LOJ 103子串查找——用hash代替kmp算法
    LOJ2823 三个朋友 ——查询字串的哈希值
    hash入门
    2019牛客暑期多校训练营(第十场)Coffee Chicken——递归
    2019牛客暑期多校训练营(第十场)Han Xin and His Troops——扩展中国剩余定理
    mutex 的 可重入
    Linux 编译安装Boost
  • 原文地址:https://www.cnblogs.com/flyingcr/p/10493219.html
Copyright © 2011-2022 走看看