【发布时间】:2019-05-27 19:51:44
【问题描述】:
我正在尝试录制我的桌面屏幕并使用 opencv videoWriter 将其保存到视频中,但最终总是有一个甚至无法播放的 6kb 视频。
这是我的代码,我首先为屏幕创建 mat 对象,然后将它们写入文件。
#include "pch.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <opencv2/objdetect/objdetect.hpp>
#include <Windows.h>
using namespace std;
using namespace cv;
int height;
int width;
Mat hwnd2mat()
{
// returning Mat object for screen and working fine as I'm showing it into a window
}
void CaptureScreen()
{
int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
HWND hDesktopWnd = GetDesktopWindow();
int key = 0;
string filename = "D:/outcpp.avi";
cv::Size targetSize = cv::Size(320, 240);
VideoWriter video(filename, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), 10, targetSize);
while (1)
{
Mat frame = hwnd2mat();
cv::Mat image = frame;
cv::Mat dst;
cv::resize(image, dst, targetSize);
if (dst.empty())
break;
video.write(dst);
imshow("Frame", dst);
char c = (char)waitKey(1);
if (c == 27)
break;
}
video.release();
destroyAllWindows();
//readVideo(filename);
}
int main(int argc, char **argv)
{
CaptureScreen();
return 0;
}
【问题讨论】:
-
CaptureScreen看起来不错。hwnd2mat可能存在问题。显示它的代码。 -
@barmark 我刚刚在我的代码中直接使用了这个code