简体   繁体   English

在numpy中附加3d数组

[英]appending 3d arrays in numpy

I'm trying to find dominant colors in image and then treshold the most dominant one. 我正在尝试在图像中找到占主导地位的颜色,然后将最占主导地位的颜色变为阈值。 However I'm having trouble with data types. 但是我在处理数据类型时遇到了麻烦。 My formula gives the most dominant color as: 我的公式给出了最主要的颜色为:

color=[10,10,10] # type=numpy.ndarray ,uint8

But it gives assertion error when I try to convert it: 但是当我尝试将其转换时,它会给出断言错误:

color=cv2.cvtColor(color, cv2.COLOR_BGR2HSV) #gives assertion error

What cv2.cvtColor wants as an input is that: cv2.cvtColor想要作为输入的是:

color_ideal=[[[ 10, 10, 10 ]]]  #type=numpy.ndarray, uint8

To obtain it, I managed to manipulate color as such: 为了获得它,我设法像这样操纵颜色:

color=np.uint8(np.atleast_3d(clr).astype(int).reshape(1,1,3))

This seems working, but know I cannot append multiple colors to numpy array.Somehow, after appending the dimension is reduced to 1. My code is: 这似乎可行,但是知道我无法将多种颜色附加到numpy数组。以某种方式,将尺寸附加为1之后,我的代码是:

    color=np.uint8([[[]]])

    for item in clt.cluster_centers_:
       color=np.append(color,(np.uint8(np.atleast_3d(item).astype(int).reshape(1,1,3))))
#returns: color=[10,10,10] somehow its dimension is down to 1

My questions are: 我的问题是:

1-How to properly append color data without loosing its dimension? 1-如何在不丢失尺寸的情况下正确附加颜色数据?

2-Is there easier way to handle this? 2-有没有更简单的方法来解决这个问题? I'm suprised how difficult it is to manipulate custom color pixel. 我很惊讶操纵自定义彩色像素有多困难。

The full code is here in case it helps: 如果有帮助,请参见完整的代码:

<!-- language: lang-py -->


    import cv2
    import numpy as np
    from sklearn.cluster import KMeans 

    def find_kmean_colors(img,no_cluster=2):
        clt = KMeans(no_cluster).fit(img)

        return clt
    def initialize(img='people_frontal.jpg'):
        img=cv2.imread('people_frontal_close_body.jpg')
        img=cv2.bilateralFilter(img,9,75,75)
        return img


    img=initialize()

    img_hsv =cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    img_list= img.reshape((img.shape[0] * img_hsv.shape[1], 3))

    clt=(find_kmean_colors(img_list,1))


    color=np.uint8([[[]]])

    for i in clt.cluster_centers_:
        color=np.append(color,(np.uint8(np.atleast_3d(i).astype(int).reshape(1,1,3))))

    #color=np.uint8(np.atleast_3d(clt.cluster_centers_).astype(int).reshape(1,1,3))

    up=cv2.cvtColor(color,cv2.COLOR_BGR2HSV)

Without the cv2 code, I'm guessing about shapes here. 没有cv2代码,我在这里猜测形状。 But looks like img is a (n,m,3) array. 但是看起来img是一个(n,m,3)数组。 img_list is (m1,3) , and clt as list of m1 items, and clt.cluster_centers_ a list of m1 arrays of shape (3,) . img_list(m1,3)cltm1项的列表, clt.cluster_centers_是形状为(3,)的m1数组的列表。

For test sake lets make a list of lists (it could just as well be a list of arrays): 为了测试起见,让我们列出一个列表(也可以是数组列表):

ctrs=[[10,10,10], [3,5,3], [20,10,10], [0,0,0]]
color = np.array(ctrs,dtype=np.uint8)  # (4,3) array
color = color.reshape(len(ctrs),1,3)

Just wrap it in np.array , and reshape to 3d. 只需将其包装在np.array ,然后重塑为3d即可。

array([[[10, 10, 10]],
       [[ 3,  5,  3]],
       [[20, 10, 10]],
       [[ 0,  0,  0]]], dtype=uint8)

Or it could be reshaped to (1,4,3) or (2,2,3). 或者可以将其重塑为(1,4,3)或(2,2,3)。

Or closer to what you are trying: 或更接近您正在尝试的:

np.concatenate([np.array(i,np.uint8).reshape(1,1,3) for i in ctrs])

You don't want to use atleast_3d here since it reshapes a (N,) array to (1,N,1) (see its docs). 您不希望在这里使用atleast_3d ,因为它将(N,)数组重塑为(1,N,1)(请参阅其文档)。 np.concatenate joins on the 1st axis, where as np.array adds a 1st dimension and then joins. np.concatenate在第一个轴上进行连接,其中np.array添加第一个维,然后进行连接。

You probably could get append to work, but it just does a step by step concatenate, which is slower. 您可能可以append工作,但是它只是一步一步进行串联,因此速度较慢。 In general if you need to append, do it with lists, and then convert to an array at the end. 通常,如果需要附加,请使用列表进行添加,然后在末尾转换为数组。


There are various ways of preserving or restoring dimensions after slicing. 切片后,有多种保存或恢复尺寸的方法。 If color is 3d and you need the i'th row also as 3d: 如果color是3d,并且您需要将第i行也设为3d:

color[[i]]
color[i].reshape(1,...)
color[i][np.newaxis,...]

Reshaping operations like this do not add significant time to the processing, so don't be afraid to use them. 重塑这样的操作不会增加处理时间,因此不要害怕使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM