我经常使用 opencv 绘图函数直接在来自 opencv 网络摄像头流的 2D numpy 数组图像缓冲区上绘制 2D 图。而且,我将 numpy 数组发送给 imshow 和视频编写器以监视和创建视频。
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
ret, frame = cap.read() # frame is a 2D numpy array w640 h480
h,w,_ = frame.shape # (480,640,3)
x = np.arange(w)
writer = cv2.VideoWriter( 'out.avi', cv2.cv.FOURCC('D','I','V','3'),
fps=30, frameSize=(w,h), isColor=True )
while True:
ret, frame = cap.read() # frame is a 2D numpy array w640 h480
B = frame[:,:,0].sum(axis=0)
B = h - h * B / B.max() …
Run Code Online (Sandbox Code Playgroud) 将多帧16位TIFF图像的平均值作为numpy数组的最快/内存有效方法是什么?
到目前为止我提出的是下面的代码.令我惊讶的是,method2比method1更快.
但是,对于从未假设的剖析,测试它!所以,我想测试更多.值得尝试魔杖?我没有在这里包括,因为在安装了ImageMagick-6.8.9-Q16和MAGICK_HOME后,它仍然没有导入...在Python中用于多页tiff的任何其他库?GDAL对此可能有点太多了.
(编辑)我包括了libtiff.仍然方法2最快,内存效率很高.
from time import time
#import cv2 ## no multi page tiff support
import numpy as np
from PIL import Image
#from scipy.misc import imread ## no multi page tiff support
import tifffile # http://www.lfd.uci.edu/~gohlke/code/tifffile.py.html
from libtiff import TIFF # https://code.google.com/p/pylibtiff/
fp = r"path/2/1000frames-timelapse-image.tif"
def method1(fp):
'''
using tifffile.py by Christoph (Version: 2014.02.05)
(http://www.lfd.uci.edu/~gohlke/code/tifffile.py.html)
'''
with tifffile.TIFFfile(fp) as imfile:
return imfile.asarray().mean(axis=0)
def method2(fp):
'primitive peak memory friendly way with tifffile.py'
with tifffile.TIFFfile(fp) as imfile:
nframe, …
Run Code Online (Sandbox Code Playgroud) 我经常需要堆叠2d numpy数组(tiff图像).为此,我首先将它们添加到列表中并使用np.dstack.这似乎是获取3D阵列堆叠图像的最快方法.但是,有更快/更有效的方式吗?
from time import time
import numpy as np
# Create 100 images of the same dimention 256x512 (8-bit).
# In reality, each image comes from a different file
img = np.random.randint(0,255,(256, 512, 100))
t0 = time()
temp = []
for n in range(100):
temp.append(img[:,:,n])
stacked = np.dstack(temp)
#stacked = np.array(temp) # much slower 3.5 s for 100
print time()-t0 # 0.58 s for 100 frames
print stacked.shape
# dstack in each loop is slower
t0 = time()
temp …
Run Code Online (Sandbox Code Playgroud)