码迷,mamicode.com
首页 > 其他好文 > 详细

视频处理简单实例 [OpenCV 笔记2]

时间:2016-07-16 00:42:28      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

VideoCapture是OpenCV 2.X中新增的类,提供从摄像机或视频文件捕获视频的C++接口。利用它读入视频的方法一般有两种:

// method 1
VideoCapture capture;
capture.open("1.avi");
// method 2
VideoCapture capture("1.avi");

读取并播放视频

ReadPlayVideo.cxx

#include <opencv2/opencv.hpp>

int main(){
    // read video
    cv::VideoCapture capture("1.avi");
    
    // show each frame
    while(1){
        cv::Mat frame;
        
        // read current frame;
        capture >> frame;
        if(frame.empty()) break;
        
        // show current frame
        imshow("Read Video", frame);
        cv::waitKey(30);
    }
    return 0;
}

CMakeList.txt

cmake_minimum_required (VERSION 2.8)
project (ReadPlayVideo)

# find OpenCV packages
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

# add the executable
add_executable (ReadPlayVideo ReadPlayVideo.cxx)
target_link_libraries(ReadPlayVideo opencv_core opencv_highgui opencv_videoio opencv_imgcodecs)

调用摄像头采集图像

GetVideoFromCam.cxx

#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp> 
int main(){
    // read video
    cv::VideoCapture capture(0);
    
    // show each frame
    while(1){
        cv::Mat frame, edges;
        
        // read current frame;
        capture >> frame;
        
        // convert to gray-scale
        cv::cvtColor(frame, edges, CV_BGR2GRAY);
        
        // denoise
        cv::blur(edges, edges, cv::Size(7,7));
        
        // canny operator
        cv::Canny(edges, edges, 0, 30, 3);
        
        // show current frame
        imshow("Read Video", edges);
        
        // if input ‘e‘, exit
        char c = cv::waitKey(30);
        if (c==e) {
            break;
        }
    }
    return 0;
}

CMakeList.txt

cmake_minimum_required (VERSION 2.8)
project (ReadPlayVideo)

# find OpenCV packages
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

# add the executable
add_executable (ReadPlayVideo ReadPlayVideo.cxx)
target_link_libraries(ReadPlayVideo opencv_core opencv_highgui opencv_videoio opencv_imgcodecs opencv_imgproc)

 

视频处理简单实例 [OpenCV 笔记2]

标签:

原文地址:http://www.cnblogs.com/Xiaoyan-Li/p/5674775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!