視頻的輸入和輸出
阿新 • • 發佈:2018-06-04
有一個 控制臺 利用 write VC windows 如果 std imshow
視頻可以理解為一段連續的圖片數據,OpenCV裏可以很簡單的通過兩個類實現視頻的輸入和輸出
VideoCapture,VideoWriter
VideoCapture可以走文件或者攝像頭捕獲圖片數據然後裝入cv::Mat數據結構裏面
// videoreadwrite.cpp: 定義控制臺應用程序的入口點。 // #include "stdafx.h" #include "opencv2/opencv.hpp" int main() { // 從攝像頭捕捉圖像 //cv::VideoCapture vC(0); //while (true) { // cv::Mat frame;// vC >> frame; // cv::imshow("frame",frame); // cv::waitKey(1); //} // 從視屏文件捕捉圖像顯示 //cv::VideoCapture vC("vtest.avi"); //while (true) { // cv::Mat frame; // vC >> frame; // cv::imshow("frame", frame); // cv::waitKey(1); //} // 視屏寫 cv::VideoCapture vC(0); int frame_width = vC.get(CV_CAP_PROP_FRAME_WIDTH); int frame_height = vC.get(CV_CAP_PROP_FRAME_HEIGHT); cv::VideoWriter vW("v.avi", CV_FOURCC(‘M‘, ‘J‘, ‘P‘, ‘G‘), 10, cv::Size(frame_width, frame_height)); while (true) { cv::Mat frame; vC >> frame; cv::imshow("frame",frame); vW.write(frame); char c = (char)cv::waitKey(25); if (c == 27) break; } vC.release(); vW.release(); cv::destroyAllWindows(); return 0; }
輸入數據:從攝像頭捕獲,只需要構造VideoCapture時候填上攝像頭索引值,如果只有一個攝像頭填0即可,如果走文件讀取就填寫視頻文件路徑,構造好對象後只需要利用重載了的>>雙箭頭每次輸出一張圖片數據到cv::Mat對象裏面即可
輸出數據:夠著一個VideoWrite然後利用write函數寫一張一張圖片數據即可
視頻的輸入和輸出