Eit*_*raf 6 python opencv image-processing python-3.x
我有一个 uint16 类型的灰度图像数据集,我想将其保存为视频文件,输出应该是 uint16 类型的无损视频文件,我尝试了这段代码,
video = cv2.VideoWriter(file_name, 0, fps, (w, h), isColor=False)
for frame in frames:
video.write(frame)
video.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
但opencv只支持uint8,有人知道有什么好方法吗?
小智 8
您可以使用 FFMPEG 来完成。FFV1编解码器是无损的,支持gray16le像素格式(16UC1 中的图像)。您可以制作 .avi 视频。
ffmpeg -i depth_%06d.tif -c:v ffv1 -vf -crf 0 output_ffv1.avi
Run Code Online (Sandbox Code Playgroud)
但是,您的图像必须采用gray16le格式(将图像保存为.tif 即可)。如果您的图像是 PNG 格式,它们可能不会是gray16le格式,而是灰色格式(如果您想稍后从视频中获取图像,这将不起作用)。您可以使用 ffprobe 检查。
ffprobe -i depth_image.png
Run Code Online (Sandbox Code Playgroud)
输出必须具有类似以下内容:
Stream 0:0: Video: tiff, gray16le, 1280x720, 25 tbr, 25 tbn, 25 tbc
Run Code Online (Sandbox Code Playgroud)
一旦你有了你的视频(.avi),你可以用ffprobe再次检查像素格式,它也必须是gray16le。
Stream 0:0: Video: ffv1 (FFV1 / 0x31564646), gray16le, 1280x720, 45067 kb/s, 25 fps, 25 tbr, 25 tbn, 25 tbc
Run Code Online (Sandbox Code Playgroud)
现在,如果您想从视频中获取图像。您可以使用ffmpeg-python。这将从视频中提取unit16数组。然后您可以将此数组保存为 PNG 或 TIFF 或任何您需要的格式。
import ffmpeg
def extract_frame(input_vid, frame_num):
out, _ = (
ffmpeg
.input(input_vid)
.filter_('select', 'gte(n,{})'.format(frame_num))
.output('pipe:', format='rawvideo', pix_fmt='gray16le', vframes=1)
.run(capture_stdout=True, capture_stderr=True)
)
return np.frombuffer(out, np.uint16).reshape([720, 1280])
for i in range($totalFrameNumber):
frame = extract_frame('test_ffv1.avi',i)
Run Code Online (Sandbox Code Playgroud)
将 $totalFrameNumber 更改为 .avi 视频中的总帧数。
| 归档时间: |
|
| 查看次数: |
2259 次 |
| 最近记录: |