在利用opencv讀取視頻,去除視頻畫面上的水印、台標和logo,並重新在生成新的視頻。VideoWriter 類出現報錯 “[mjpeg @ 0x27ee9e0] buffer smaller than minimum size”,查了很久都沒有找到原因。
代碼如下:
<span >#include "stdio.h" #include "stdlib.h" #include "time.h" #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include "opencv2/photo/photo.hpp" #include <iostream> using namespace std; using namespace cv; int main() { clock_t start, finish; start = clock(); cout << "start!!" << endl; string video_path = "/home/dyx/Desktop/test_logs/"; VideoCapture capture(video_path + "test.mp4"); if (!capture.isOpened()) cout << "fail to open!" << endl; //獲取幀率 double fps =capture.get( CV_CAP_PROP_FPS); cout << "幀率為:" << fps << endl; Size videoSize(capture.get( CV_CAP_PROP_FRAME_WIDTH),capture.get( CV_CAP_PROP_FRAME_HEIGHT)); int tem=CV_FOURCC('F', 'L', 'V', '1'); cout<<tem<<endl; VideoWriter writer(video_path + "VideoTest.avi", CV_FOURCC('D', 'I', 'V', 'X') , fps, videoSize); // VideoWriter writer(video_path + "VideoTest.avi", CV_FOURCC('F', 'L', 'V', '1'), fps, videoSize); //VideoWriter writer(video_path + "VideoTest264.avi", CV_FOURCC('X', '2', '6', '4'), fps, videoSize); Mat frame; Mat inpainted; Mat inpaintMask = imread("/home/dyx/Desktop/test_logs/log5.jpg"); cv::cvtColor(inpaintMask, inpaintMask, CV_BGR2GRAY); int i = 0; while (capture.isOpened()) { i++; cout << i << endl; capture >> frame; if (!capture.read(frame)||i>=10000)// get a new frame from camera or video { cout<<"finlish!"<<endl; break; } inpaint(frame, inpaintMask, inpainted, 8, CV_INPAINT_TELEA); writer << inpainted; //cvWaitKey(1000 / fps); if (cvWaitKey(20) == 27) { break; } imshow("video", inpainted); } finish = clock(); long time = (double)(finish - start) / CLOCKS_PER_SEC; cout << time << endl; printf("時間:%f seconds\n", time); return 0; } </span>最後,通過調試發現是代碼的問題,主要是VideoWriter的videoSize出了問題。把“Size videoSize(CV_CAP_PROP_FRAME_WIDTH,CV_CAP_PROP_FRAME_HEIGHT) ”改為“Size videoSize(capture.get( CV_CAP_PROP_FRAME_WIDTH),capture.get( CV_CAP_PROP_FRAME_HEIGHT))”,即可解決!
因為CV_CAP_PROP_FRAME_WIDTH=3,CV_CAP_PROP_FRAME_HEIGHT=4,即為videoSize(3,4),新生成的視頻的寬高太小導致的。視頻videoSize的大小最好不要小於videoSize(320,240).