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

  • 相关阅读:
    当你进退两难的时候,你想做出决定,抛硬币,当你第一次抛了之后,还想再一次抛的时候,你就知道这个问题的答案了。
    习惯几乎可以绑住一切,只是不能绑住偶然。比如那只偶然尝了鲜血的老虎。
    10个理由让你爱上程序员
    自己不做出点样子,人家想拉你一把都不知你的手在哪里。
    WSL(Windows Subsystem for Linux)笔记一安装与使用
    Nginx(三)-正向代理与反向代理
    Nginx(二)-服务模式运行nginx之WINSW
    Nginx(一)-windows下的安装配置
    WebSocket(一)-RFC6455
    NodeJS笔记(六)-Express HTTP服务器启动后如何关闭
  • 原文地址:https://www.cnblogs.com/flyingcr/p/10493219.html
Copyright © 2011-2022 走看看