查找图像列表的中位数

fer*_*k86 5 opencv numpy image image-processing python-2.7

如果我有一个由3D ndarray表示的图像列表,例如[[x,y,color],...],我可以使用哪些操作来输出值为所有值的中值的图像?我正在使用for循环,发现它太慢了.

Dar*_* M. 5

这是我使用NumPy的矢量化实现:

对于我的测试,我使用了这五个图像:

在此输入图像描述

相关部分:

import numpy as np
import scipy.ndimage

# Load five images:
ims = [scipy.ndimage.imread(str(i + 1) + '.png', flatten=True) for i in range(5)]
# Stack the reshaped images (rows) vertically:
ims = np.vstack([im.reshape(1,im.shape[0] * im.shape[1]) for im in ims])
# Compute the median column by column and reshape to the original shape:
median = np.median(ims, axis=0).reshape(100, 100)
Run Code Online (Sandbox Code Playgroud)

完整的脚本:

import numpy as np
import scipy.ndimage
import matplotlib.pyplot as plt

ims = [scipy.ndimage.imread(str(i + 1) + '.png', flatten=True) for i in range(5)]
print ims[0].shape # (100, 100)

ims = np.vstack([im.reshape(1,im.shape[0] * im.shape[1]) for im in ims])
print ims.shape # (5, 10000)

median = np.median(ims, axis=0).reshape(100, 100)

fig = plt.figure(figsize=(100./109., 100./109.), dpi=109, frameon=False)
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
plt.imshow(median, cmap='Greys_r')
plt.show()
Run Code Online (Sandbox Code Playgroud)

numpy.median五个图像的中位数()结果如下所示:

在此输入图像描述

有趣的部分:mean(numpy.mean)结果如下所示:

在此输入图像描述

好的,科学与艺术相遇.:-)


use*_*160 5

您说您的彩色图像,格式为 3d 列表ndarrays。假设有n图像:

imgs = [img_1, ..., img_n]
Run Code Online (Sandbox Code Playgroud)

其中 imgs 是 a list,eachimg_i是一个ndarray带有 shape 的an (nrows, ncols, 3)。

将列表转换为 4d ndarray,然后取与图像对应的维度的中值:

import numpy as np

# Convert images to 4d ndarray, size(n, nrows, ncols, 3)
imgs = np.asarray(imgs)

# Take the median over the first dim
med = np.median(imgs, axis=0)
Run Code Online (Sandbox Code Playgroud)

这给出了像素级中值。每个像素的每个颜色通道的值是所有图像中相应像素/通道的中值。

的文档asarray()说“如果输入已经是 ndarray,则不执行任何复制”。这表明,如果将原始图像列表存储为 4dndarray而不是列表,操作会更快。在这种情况下,不需要复制内存中的信息(或运行asarray())