首页 > 解决方案 > Python 和 ffmpeg 创建不同的 tiff 堆栈

问题描述

大家好,对图像处理感兴趣的人,

ffmpeg无需使用and tiffcp(后者是 Debian 的 libtiff 工具的一部分)编程即可从灰度电影中创建多页 tiff 文件(tiff 堆栈):

ffmpeg -i movie.avi frame%03d.tif
tiffcp frame*.tif stack.tif

使用OpenCVtifffile库在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

我可以使用哪些附加诊断来确定问题?

标签: image-processingffmpegpython-imaging-librarylibtiff

解决方案


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.


推荐阅读