计算图像的唯一像素值

use*_*617 1 python numpy

我想计算唯一像素值的数量,并过滤掉超过阈值的数量并将其保存在字典中。

# Assume an image is read as a numpy array
np.random.seed(seed=777)
s = np.random.randint(low=0, high = 255, size=(100, 100, 3))
print(s)
Run Code Online (Sandbox Code Playgroud)

这就是我计算唯一值(1*3 数组)数量的方法。

np.unique(img.reshape(-1, 3), axis=0, return_counts=True)
Run Code Online (Sandbox Code Playgroud)

如何添加一些逻辑来过滤并将其转换为字典?

moz*_*way 6

您可以使用:

np.random.seed(seed=1606)
img = np.random.randint(low=0, high = 255, size=(100, 100, 3))

# set upi threshold
thresh = 2

# count unique values and set up mask
vals, counts = np.unique(img.reshape(-1, 3), axis=0, return_counts=True)
mask = counts>thresh

# form a dictionary of values/counts > threshold
out = dict(zip(map(tuple, vals[mask]), counts[mask]))
Run Code Online (Sandbox Code Playgroud)

输出:

{(163, 209, 247): 3}
Run Code Online (Sandbox Code Playgroud)