如何在大型pointcloud中有效地将RGB值转换为HSV

时间:2016-03-23 12:19:43

标签: python numpy 3d point-clouds scikit-image

所以,我使用像这样结构化的pointcloud:

cloud1 = PyntCloud.from_ply('/home/david/Escritorio/CloudCompareGarden1 - Cloud.ply')

cloud1.vertex
Out[3]: 
array([ (-3.6057682037353516, -0.18922901153564453, 150.49591064453125, 206, 205, 208, 2033.0),
       (-3.605868101119995, -0.18912899494171143, 150.4914093017578, 187, 186, 188, 2033.0),
       (-3.3447682857513428, -1.1726289987564087, 149.6341094970703, 115, 81, 65, 1313.0),
       ...,
       (-1.169768214225769, -0.8984289765357971, 147.5572967529297, 43, 22, 11, -1631.0),
       (-1.1530683040618896, -0.8945289850234985, 147.5579071044922, 46, 26, 19, -1375.0),
       (-1.151968240737915, -0.8948289752006531, 147.55889892578125, 52, 33, 27, -1055.0)], 
      dtype=[('x', '<f4'), ('y', '<f4'), ('z', '<f4'), ('red', 'u1'), ('green', 'u1'), ('blue', 'u1'), ('scalar_Scalar_field', '<f4')])

cloud1.vertex['red']
Out[4]: array([206, 187, 115, ...,  43,  46,  52], dtype=uint8)

我想将每个点的RGB值转换为HSV,然后&#34;追加&#34; HSV值为每个点的3个独立标量字段。

我已经尝试了colorsys模块和scikit-image color module

我以这个功能结束,有效:

 def add_hsv(self):
    """  Adds to PyntCloud.vertex the values obtained from conversion RGB-HSV

    This function expects the PyntCloud to have a numpy structured array
    as PyntCloud.vertex attribute, with valid RGB values correctly named
    ('red', 'green', 'blue')

    The corresponding HSV values will be added as 3 news SF for every point
    in PyntCloud.vertex

    Examples
    --------
    >>> cloud = PyntCloud.from_ply('example.ply')
    >>> cloud.add_hsv()
    """

    #: get the rgb from vertex in the right format (creating a fake image)
    #: I should propably wrap this stack method as a function as I use it with distance functions too
    rgb = np.array([np.vstack([self.vertex['red'], self.vertex['green'], self.vertex['blue']]).transpose()])

    #: convert rgb to hsv and get the values undoing the fake image
    hsv = color.rgb2hsv(rgb)[0]

    #: get the values independently
    h = np.array(hsv[:,0] * 360, dtype=[('Hue', np.uint16)])
    s = np.array(hsv[:,1] * 100, dtype=[('Saturation',np.float16)])
    v = np.array(hsv[:,2] * 100, dtype=[('Value', np.float16)])

    #: merge the structured arrays and replace the old vertex attribute
    self.vertex = join_struct_arrays([self.vertex, h, s, v])           

结果:

cloud1.add_hsv()

cloud1.vertex
Out[6]: 
array([ (-3.6057682037353516, -0.18922901153564453, 150.49591064453125, 206, 205, 208, 2033.0, 0.7222222222222222, 0.014423076923076872, 0.8156862745098039),
       (-3.605868101119995, -0.18912899494171143, 150.4914093017578, 187, 186, 188, 2033.0, 0.7499999999999988, 0.010638297872340538, 0.7372549019607844),
       (-3.3447682857513428, -1.1726289987564087, 149.6341094970703, 115, 81, 65, 1313.0, 0.05333333333333332, 0.4347826086956522, 0.45098039215686275),
       ...,
       (-1.169768214225769, -0.8984289765357971, 147.5572967529297, 43, 22, 11, -1631.0, 0.057291666666666664, 0.7441860465116279, 0.16862745098039217),
       (-1.1530683040618896, -0.8945289850234985, 147.5579071044922, 46, 26, 19, -1375.0, 0.043209876543209874, 0.5869565217391305, 0.1803921568627451),
       (-1.151968240737915, -0.8948289752006531, 147.55889892578125, 52, 33, 27, -1055.0, 0.04000000000000002, 0.4807692307692308, 0.20392156862745098)], 
      dtype=[('x', '<f4'), ('y', '<f4'), ('z', '<f4'), ('red', 'u1'), ('green', 'u1'), ('blue', 'u1'), ('scalar_Scalar_field', '<f4'), ('Hue', '<f8'), ('Saturation', '<f8'), ('Value', '<f8')])

但我不喜欢这样一个事实:我必须生成一个假的&#39;图像以便能够使用skimage rgb2hsv function,然后撤消假图像。

所以我的问题是:

  1. 根据我的数据结构,有没有办法更有效地使用skimage转换功能?
  2. 是否还有其他RGB-HSV转换功能可以简化我的功能(维护我原来的PyntCloud结构)?
  3. 我应该编写自己的RGB-HSV转换函数,并针对我的特定数据结构进行优化吗?
  4. 非常感谢

0 个答案:

没有答案