image-processing - Python 和 ffmpeg 创建不同的 tiff 堆栈
问题描述
大家好,对图像处理感兴趣的人,
ffmpeg
无需使用and tiffcp
(后者是 Debian 的 libtiff 工具的一部分)编程即可从灰度电影中创建多页 tiff 文件(tiff 堆栈):
ffmpeg -i movie.avi frame%03d.tif
tiffcp frame*.tif stack.tif
使用OpenCV和tifffile库在Python中编程对我来说似乎也是可行的:
import numpy as np
import cv2
import tifffile
cap = cv2.VideoCapture('movie.avi')
success, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
image = np.zeros((300, 400, 500), 'uint8') # pre-allocate some space
i = 0;
while success:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
image[i,:,:] = gray[80:480,0:500]
success, frame = cap.read()
cap.release()
tifffile.imsave('image.tif',image,photometric='minisblack')
但是,结果大小不同。查看Python解决方案的直方图,我意识到它与ffmpeg
解决方案不同。
感谢下面的答案,我将输出文件与file
:
user@ubuntu:~$ file ffmpeg.tif tifffile.tif
ffmpeg.tif: TIFF image data, little-endian
tifffile.tif: TIFF image data, little-endian, direntries=17, height=400, bps=8, compression=none, PhotometricIntepretation=BlackIsZero, description={"shape": [300, 400, 500]}, width=500
此外,我将文件与ffmpeg
:
user@ubuntu:~$ ffmpeg -i ffmpeg.tif -i tifffile.tif
[tiff_pipe @ 0x556cfec95d80] Stream #0: not enough frames to estimate rate; consider increasing probesize
Input #0, tiff_pipe, from 'ffmpeg.tif':
Duration: N/A, bitrate: N/A
Stream #0:0: Video: tiff, gray, 500x400 [SAR 1:1 DAR 5:4], 25 tbr, 25 tbn, 25 tbc
[tiff_pipe @ 0x556cfeca6b40] Stream #0: not enough frames to estimate rate; consider increasing probesize
Input #1, tiff_pipe, from 'tifffile.tif':
Duration: N/A, bitrate: N/A
Stream #1:0: Video: tiff, gray, 500x400 [SAR 1:1 DAR 5:4], 25 tbr, 25 tbn, 25 tbc
我可以使用哪些附加诊断来确定问题?
解决方案
compression algorith
By default ffmpeg
uses the packbits compression algorithm for TIFF output. This can be changed with the -compression_algo
option, and other accepted values are raw, lzw, and deflate:
ffmpeg -i input.avi -compression_algo lzw output_%04d.tif
pixel format
Another difference may be caused by the pixel format (color space and chroma subsampling). See ffmpeg -h encoder=tiff
for a list of supported pixel formats.
Which pixel format gets used depends on your input, and the log/console output will indicate the selected pixel format.
comparing outputs
I don't know what defaults are used by tifffile, but you can run ffmpeg -i ffmpeg.tif -i tifffile.tif
and file ffmpeg.tif tifffile.tif
to view details which may explain the discrepancy.
推荐阅读
- javascript - “无法导入 useState,因为反应中没有 useState 导出”(使用 React Hooks 的流程)
- c++ - 如何计算文件中“列”的数量?
- python - 如何从字典中的多个值缩小整数?
- python - 在 Django 中抽象 API 调用的最佳方法是什么?
- python - 如何在opengl中制作坐标Y的动画
- azure-devtest-labs - 开发测试实验室工件文件命名约定
- c# - 加两位小数?C#中的LINQ结果
- python - 是否有结构化的方式来引用 Python 文档字符串中的函数参数标签?
- cordova - Azure Devops Cordova 命令从昨天开始失败
- javascript - 在 Squarespace 网站上使用 Leafleat.js 地图