调整图像大小后,OpenCV VideWriter无法正常工作 [英] OpenCV VideWriter not working after image resize
问题描述
我正在使用OpenCV 3和Visual Studio. 这里的问题是,除了默认的完整摄像机分辨率外,我无法保存任何特定分辨率的视频. 没有错误,但是视频文件没有增长.它保持在5.54kb.
I am using OpenCV 3 and Visual Studio. The issue here is that I am unable to save video for any particular resolution other than default full camera resolution. There is no error, but video file doesn't grow. It stays at 5.54kb.
这是我的代码:
#include"opencv2\opencv.hpp"
using namespace cv;
using namespace std;
VideoCapture cap_cam1(0);
double Width = cap_cam1.get(CV_CAP_PROP_FRAME_WIDTH);
double Height = cap_cam1.get(CV_CAP_PROP_FRAME_HEIGHT);
cv::Size frameSize(static_cast<int>(Width), static_cast<int>(Height));
string videoFileName = "D://current.avi";
VideoWriter cam1_write(videoFileName, CV_FOURCC('D', 'I', 'V', '3'), 10.0, frameSize, true);
Mat image;
int main()
{
while (true)
{
cap_cam1 >> image;
resize(image, image, Size(320, 240));
imshow("image", image);
cam1_write.write(image);
if (waitKey(33) == 27)break;
}
}
如果删除resize
函数,则文件大小会增加,并且会添加框架.
我还尝试在VideoWriter
定义之后添加以下行
If I remove the resize
function, then file size grows and frames are added.
I also tried adding below lines after VideoWriter
definition
cap_cam1.set(CV_CAP_PROP_FRAME_WIDTH, 240);
cap_cam1.set(CAP_PROP_FRAME_HEIGHT,320);
在所有文件大小都保持在5.54kb之后,我还尝试了在VideoWriter
定义下更改分辨率.
I also tried changing resolution at VideoWriter
definition, after everything file size remains at 5.54kb.
如何以自定义分辨率录制视频?
How to record video at custom resolution?
推荐答案
您正在使用
double Width = cap_cam1.get(CV_CAP_PROP_FRAME_WIDTH);
double Height = cap_cam1.get(CV_CAP_PROP_FRAME_HEIGHT);
cv::Size frameSize(static_cast<int>(Width), static_cast<int>(Height));
VideoWriter cam1_write(videoFileName, CV_FOURCC('D', 'I', 'V', '3'), 10.0, frameSize, true);
这意味着您要编写具有VideoCapture图像大小的视频.
which means you want to write a video with the VideoCapture image size.
在这里您必须更改为
cv::Size targetSize = cv::Size(320,240);
VideoWriter cam1_write(videoFileName, CV_FOURCC('D', 'I', 'V', '3'), 10.0, targetSize, true);
现在在您的循环中使用
resize(image, image, targetSize);
这意味着在创建VideoWriter时必须确定想要的输出图像大小.这是因为典型的编解码器假定图像分辨率不变(已知).
That means you have to decide what kind of output image size you want to have when creating the VideoWriter. That's because typical codecs assume constant (known) image resolution.
要设置分辨率,您需要输入一些错字:
For setting resolution you have some typo:
cap_cam1.set(CV_CAP_PROP_FRAME_WIDTH, 240);
cap_cam1.set(CAP_PROP_FRAME_HEIGHT,320);
这意味着分辨率为cv::Size(240,320)
,因此如果将其中的顺序更改为cv::Size(320,240)
,则您的代码可能会起作用.查看@MBo的评论
this would mean a resolution of cv::Size(240,320)
, so your code might work if you changed the order there to cv::Size(320,240)
. See comment of @MBo
这篇关于调整图像大小后,OpenCV VideWriter无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!