老老老規矩,上代碼
先把核心代碼單獨搞出來喵一眼:ios
vtColor(frame, BlackWhiteVideo, CV_BGR2GRAY);
來頭擡起來看黑板:ide
#include <iostream> #include <opencv2\opencv.hpp> #include <opencv2\highgui\highgui_c.h> using namespace std; using namespace cv; void main() { VideoCapture capture; capture.open("E:\\gongye1.mp4"); if (!capture.isOpened()) { cout << "can not open ...\n"; return; } cvNamedWindow("play vedio"); long totalFrameNumber = capture.get(CAP_PROP_FRAME_COUNT); // 獲取視頻的總幀數 long i = 1; while (true) { Mat frame; Mat BlackWhiteVideo; VideoWriter videowriter("E:\\gongye1change.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'), 30, Size(640, 640), true); //改變圖像的幀速爲30,圖像幀的大小是(640,480) capture >> frame; if (i > totalFrameNumber) break; i++; imshow("play vedio", frame); // 窗口展現原視頻 char c = waitKey(33); if (c == 27) break; //按「Esc」退出 if (c == 32) waitKey(0);//空格實現暫停 // CV_BGR2GRAY枚舉值爲6 2.3.4.5:偏黃,6.7.10.11:黑白 cvtColor(frame, BlackWhiteVideo, CV_BGR2GRAY); // 這句是關鍵 imshow("BlackWhiteVideo", BlackWhiteVideo); // 窗口展現黑白視頻 } capture.release(); cvDestroyWindow("play vedio"); }