在opencv中将非标准分辨率的帧写入视频
Write frames with non-standard resolution to video in opencv
关于这个问题:
是否可以使用 opencv 的 cv2.VideoWriter
创建具有 "non-standard" 视频分辨率(即非标准纵横比)的视频?到目前为止我的代码:
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video_out = cv2.VideoWriter("video_out.avi", fourcc, 25, (99, 173))
cap = cv2.VideoCapture("video_in.avi")
while(cap.isOpened()):
ret, frame = cap.read()
frame_out = frame[50:50+173,400:400+99]
video_out.write(frame_out)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
我也尝试过其他视频格式(H264、MJPG),但没有成功。
编辑:没有成功意味着输出视频已创建,但仍然是空的。如果我使用原始帧大小,帧会写入视频。
编辑:Micka 的回答有效,但我现在也将 python 代码设为 运行:缺少彩色视频输出的布尔参数。
video_out = cv2.VideoWriter("video_out.avi", fourcc, 25, (99, 173), False)
cv2.VideoWriter("video_out.avi", fourcc, 25, (173, 99))
为大小为 173x99(宽 x 高)的帧创建编写器。
frame_out
是一个大小为 99x173 的框架(框架索引为 [y, x])。
更改索引以写入匹配的帧大小。
对我来说,这段代码确实有效,但 MJPG 确实将奇数分辨率四舍五入为偶数分辨率。
H264 根本不适用于该分辨率。
int main(int argc, char* argv[])
{
// start camera
cv::VideoCapture cap(0);
// read a single image to find camera resolution
cv::Mat image;
cap >> image;
if (image.empty())
{
std::cout << "Could not find/open camera. Press Enter to exit." << std::endl;
std::cin.get();
return 0;
}
cv::Size targetSize(199, 171);
cv::VideoWriter writer("out.avi", CV_FOURCC('M','J','P','G'), 25, targetSize, true); // does create a 198x170 video file.
//cv::VideoWriter writer("out.avi", -1, 25, targetSize, true); // does not work for x264vfw for example with an error message.
while (cv::waitKey(30) != 'q')
{
cap >> image;
if (!image.empty())
{
cv::imshow("captured image", image);
// resize the actual image to a target size
cv::Mat writableImage;
cv::resize(image, writableImage, targetSize);
writer.write(writableImage);
}
}
// release the camera
cap.release();
writer.release();
std::cout << "Press Enter to exit." << std::endl;
std::cin.get();
return 0;
}
一般来说,许多编解码器都受到某些像素块约束的限制,例如在每个维度上具有 2、4、8、16 或 32 的倍数。要么是因为算法本身,要么是一些硬件指令的优化。
在OSX环境中:
- OSX:高山脉 (10.13.6)
- Python: 3.6.3
- 打开 CV(使用预建 python pck):opencv-python-headless 4.1.0.25
唯一对我有用的组合是 mp4 包装器(OSX 不喜欢 avi)和 mp4v
编解码器。也试过 avc1
但它不会写一个非标准尺寸的框架
codec = 'mp4v'
fourcc = cv2.VideoWriter_fourcc(*codec)
size = ( 1000 , 500 )
fps = 15
writer = cv2.VideoWriter('filename.mp4', fourcc, fps, size, True)
关于这个问题:
是否可以使用 opencv 的 cv2.VideoWriter
创建具有 "non-standard" 视频分辨率(即非标准纵横比)的视频?到目前为止我的代码:
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video_out = cv2.VideoWriter("video_out.avi", fourcc, 25, (99, 173))
cap = cv2.VideoCapture("video_in.avi")
while(cap.isOpened()):
ret, frame = cap.read()
frame_out = frame[50:50+173,400:400+99]
video_out.write(frame_out)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
我也尝试过其他视频格式(H264、MJPG),但没有成功。
编辑:没有成功意味着输出视频已创建,但仍然是空的。如果我使用原始帧大小,帧会写入视频。
编辑:Micka 的回答有效,但我现在也将 python 代码设为 运行:缺少彩色视频输出的布尔参数。
video_out = cv2.VideoWriter("video_out.avi", fourcc, 25, (99, 173), False)
cv2.VideoWriter("video_out.avi", fourcc, 25, (173, 99))
为大小为 173x99(宽 x 高)的帧创建编写器。
frame_out
是一个大小为 99x173 的框架(框架索引为 [y, x])。
更改索引以写入匹配的帧大小。
对我来说,这段代码确实有效,但 MJPG 确实将奇数分辨率四舍五入为偶数分辨率。 H264 根本不适用于该分辨率。
int main(int argc, char* argv[])
{
// start camera
cv::VideoCapture cap(0);
// read a single image to find camera resolution
cv::Mat image;
cap >> image;
if (image.empty())
{
std::cout << "Could not find/open camera. Press Enter to exit." << std::endl;
std::cin.get();
return 0;
}
cv::Size targetSize(199, 171);
cv::VideoWriter writer("out.avi", CV_FOURCC('M','J','P','G'), 25, targetSize, true); // does create a 198x170 video file.
//cv::VideoWriter writer("out.avi", -1, 25, targetSize, true); // does not work for x264vfw for example with an error message.
while (cv::waitKey(30) != 'q')
{
cap >> image;
if (!image.empty())
{
cv::imshow("captured image", image);
// resize the actual image to a target size
cv::Mat writableImage;
cv::resize(image, writableImage, targetSize);
writer.write(writableImage);
}
}
// release the camera
cap.release();
writer.release();
std::cout << "Press Enter to exit." << std::endl;
std::cin.get();
return 0;
}
一般来说,许多编解码器都受到某些像素块约束的限制,例如在每个维度上具有 2、4、8、16 或 32 的倍数。要么是因为算法本身,要么是一些硬件指令的优化。
在OSX环境中:
- OSX:高山脉 (10.13.6)
- Python: 3.6.3
- 打开 CV(使用预建 python pck):opencv-python-headless 4.1.0.25
唯一对我有用的组合是 mp4 包装器(OSX 不喜欢 avi)和 mp4v
编解码器。也试过 avc1
但它不会写一个非标准尺寸的框架
codec = 'mp4v'
fourcc = cv2.VideoWriter_fourcc(*codec)
size = ( 1000 , 500 )
fps = 15
writer = cv2.VideoWriter('filename.mp4', fourcc, fps, size, True)