[英]Trouble writing video to file using opencv
我正在嘗試使用 Visualstudio 2013 中的 C++ 中的 OpenCV 將捕獲的視頻寫入文件。程序似乎從筆記本電腦上的網絡攝像頭捕獲視頻,並且還能夠將每幀保存為圖像,但是當我將幀寫入視頻文件時,我最終得到了一個 6kb 的文件。 程序沒有給我任何錯誤,因為我遵循了 OpenCV 文檔中的說明。
我正在粘貼程序以供審核。 請建議我如何使它成為一個成功的程序。
謝謝你。
#include <stdio.h>
#include <iostream>
#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
using namespace std;
using namespace cv;
int main()
{
VideoCapture video_capture(0);
if (!video_capture.isOpened())
{
cout << "Error in opening video feed!" << endl;
getchar();
return -1;
}
// Creating the window to view video feed
String window_name = "Video_Feed";
namedWindow(window_name, CV_WINDOW_AUTOSIZE);
//
Mat frame;
// Filename
String filename = "...\\first_recording.avi";
// four character code
int fcc = CV_FOURCC('M', 'P', '4', '2');
// frames per sec
int fps = 10;
// frame size
Size frame_size(CV_CAP_PROP_FRAME_WIDTH, CV_CAP_PROP_FRAME_HEIGHT);
VideoWriter video_writer = VideoWriter(filename,fcc,fps,frame_size,true);
if (!video_writer.isOpened())// || video_writer.isOpened == NULL)
{
cout << "Error in opening video writer feed!" << endl;
getchar();
return -1;
}
int frame_count = 0;
while (frame_count < 100)
{
bool cap_success = video_capture.read(frame);
if (!cap_success)
{
cout << "Error in capturing the image from the camera feed!" << endl;
getchar();
break;
}
imshow(window_name, frame);
//imwrite("cap.jpg", frame);
video_writer.write(frame);
switch (waitKey(10))
{
case 27:
return 0;
break;
}
frame_count++;
}
//scvReleaseVideoWriter;
destroyWindow(window_name);
return 0;
}
請找到以下代碼來編寫視頻文件。
int main(int argc, char* argv[])
{
// Load input video
cv::VideoCapture input_cap("test8.avi");
if (!input_cap.isOpened())
{
std::cout << "!!! Input video could not be opened" << std::endl;
return -1;
}
// Setup output video
cv::VideoWriter output_cap("output.avi",
input_cap.get(CV_CAP_PROP_FOURCC),
input_cap.get(CV_CAP_PROP_FPS),
cv::Size(input_cap.get(CV_CAP_PROP_FRAME_WIDTH), input_cap.get(CV_CAP_PROP_FRAME_HEIGHT)));
if (!output_cap.isOpened())
{
std::cout << "!!! Output video could not be opened" << std::endl;
return -1;
}
// Loop to read frames from the input capture and write it to the output capture
cv::Mat frame;
while (true)
{
if (!input_cap.read(frame))
break;
output_cap.write(frame);
}
// Release capture interfaces
input_cap.release();
output_cap.release();
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.