Python中计算特定值图像的每个像素的高效方法?

3

我有一张由七种不同颜色组成的RGB图像。

我希望能够高效地统计每种像素类型在图像中出现的次数,而不是手动循环每个像素(如果可能的话,使用numpy操作是可以的,因为它速度更快)。

我尝试将其加载到numpy数组中,得到了一个大小为N*M*3的数组,但我无法找到一种方法来计算特定值的像素数量...

有什么想法吗?

谢谢!


1
你是否考虑使用直方图? - Employee
1
你能提供一张图片和一段代码吗? - dobkind
七种颜色是固定的吗?您需要知道所有颜色通道才能决定颜色吗?因为如果例如红色(或其他两个颜色之一)通道足以区分所有七种颜色,那将使事情变得更容易。 - Paul Panzer
@Brenlla 我认为这不是一个直接的重复,因为使用的颜色数量很少,可能会允许一些优化。 - Paul Panzer
@PaulPanzer 这是真的。但是,OP也没有能够提供任何可工作代码片段。因此,最好从一些基础内容开始,这些内容将适用于具有>7种颜色的图像,并稍后再考虑优化问题。 - Brenlla
2个回答

8
只需部分展平并使用 np.unique,并设置 return_counts = Trueaxis = 0
flat_image = image.reshape(-1, 3)  # makes one long line of pixels
colors, counts = np.unique(flat_image, return_counts = True, axis = 0)

或者写成一行:

colors, counts = np.unique(image.reshape(-1, 3), 
                           return_counts = True, 
                           axis = 0)

谢谢!我想出了一个类似但更复杂的解决方案...这太完美了! - Adrien Nivaggioli

2

由于只有七种颜色,简单的掩码在合理的假设下将会相当竞争。以下时间是针对100x100x3 @ 8位随机图像的:

timings
np.unique 6.510251379047986
masking   0.2401340039796196

请注意,加速的很多但不是全部原因是将通道合并为单个通道。
代码:
import numpy as np

def create(M, N, k=7):
    while True:
        colors = np.random.randint(0, 256**3, (k,), dtype=np.int32)
        if np.unique(colors).size == k:
            break
    picture = colors[np.random.randint(0, k, (M, N))]
    RGB = np.s_[..., :-1] if picture.dtype.str.startswith('<') else np.s_[..., 1:]
    return picture.view(np.uint8).reshape(*picture.shape, 4)[RGB]

def f_df(image):
    return np.unique(image.reshape(-1, 3), 
                     return_counts = True, 
                     axis = 0)

def f_pp(image, nmax=50):
    iai32 = np.pad(image, ((0, 0), (0, 0), (0, 1)), mode='constant')
    iai32 = iai32.view(np.uint32).ravel()

    colors = np.empty((nmax+1,), np.uint32)
    counts = np.empty((nmax+1,), int)
    colors[0] = iai32[0]
    counts[0] = 0
    match = iai32 == colors[0]
    for i in range(1, nmax+1):
        counts[i] = np.count_nonzero(match)
        if counts[i] == iai32.size:
            return colors.view(np.uint8).reshape(-1, 4)[:i, :-1], np.diff(counts[:i+1])
        colors[i] = iai32[match.argmin()]
        match |= iai32 == colors[i]
    raise ValueError('Too many colors')



image = create(100, 100, 7)

col_df, cnt_df = f_df(image)
col_pp, cnt_pp = f_pp(image)
#print(col_df)
#print(cnt_df)
#print(col_pp)
#print(cnt_pp)
idx_df = np.lexsort(col_df.T)
idx_pp = np.lexsort(col_pp.T)

assert np.all(cnt_df[idx_df] == cnt_pp[idx_pp])

from timeit import timeit
print('timings')
print('np.unique', timeit(lambda: f_df(image), number=1000))
print('masking  ', timeit(lambda: f_pp(image), number=1000))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接