一组图像中颜色(hue)值(0-359)出现的总和 [英] Sum of the occurence of color(hue) values(0-359) from a set of images
问题描述
我有一个装满图像的文件夹,我想找到出现次数最少的色相值.为此,我为所有色相值创建了一个长度为360的数组,拍摄了我文件夹中的所有图像,进行了遍历,对于每个像素,我在代表色相值的索引处为数组添加了+1.例如,如果我的像素中的色相值为0,则在数组中的索引0处添加+1. 我的问题是:有没有更快或更有效的方法?
I have a folder full of images and i want to find the Hue Values with the smallest occurence. For that i create an array with length 360 for all the hue values, take all the images in my folder, go through it and for each pixel i add +1 in my array at the index which represent the hue value. If i have for example the hue value 0 in my pixel, i add +1 in my array at index 0. My question is: is there a faster or more efficent way to do that?
这是我的代码:
path = 'path'
sub_path = 'sub_path'
sumHueOcc = np.zeros((360, 1), dtype=np.uint64)
for item in dirs:
fullpath = os.path.join(path,item)
pathos = os.path.join(sub_path,item)
if os.path.isfile(fullpath):
f, e = os.path.splitext(pathos)
img = np.array(Image.open(fullpath))
img = np.float32(img)
imgHSV = cv2.cvtColor(img, cv2.COLOR_RGB2HSV_FULL) #RGB because numpy RGB
# want to work with hue only
h, s, v = cv2.split(imgHSV)
# the hue values in one large array
Z = np.array(h, copy=True)
Z = Z.reshape((-1, 1))
# convert to np.float32
Z = np.uint64(Z)
# count each appearence from hue values
for z in Z:
sumHueOcc[z] = sumHueOcc[z] + 1
max = np.argmax(sumHueOcc)
min = np.argmin(sumHueOcc)
print("Minimum 1")
print(min)
sumHueOcc[min] += max
min = np.argmin(sumHueOcc)
print("Minimum 2")
print(min)
sumHueOcc[min] += max
min = np.argmin(sumHueOcc)
print("Minimum 3")
print(min)
sumHueOcc[min] += max
min = np.argmin(sumHueOcc)
print("Minimum 4")
print(min)
推荐答案
我们可以使用np.bincount
进行计数.
We can use np.bincount
to do the counting.
因此,我们从int64
开始对输出数组进行初始化-
So, we initialize the output array at the start with int64
-
sumHueOcc_out = np.zeros((180, 1), dtype=np.int64)
然后,在循环内部,我们替换涉及循环的最里面的部分-
Then, inside the loops, we replace the innermost section involving loops -
# the hue values in one large array
Z = np.array(h, copy=True)
Z = Z.reshape((-1, 1))
# convert to np.float32
Z = np.uint64(Z)
# count each appearence from hue values
for z in Z:
sumHueOcc[z] = sumHueOcc[z] + 1
具有bincount
替代-
sumHueOcc_out.flat += np.bincount(h.astype(np.int64).ravel(),minlength=sumHueOcc.size)
这篇关于一组图像中颜色(hue)值(0-359)出现的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!