视频处理简单实例 [OpenCV 笔记2]
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)