沿RGB分量的像素的散点图3d

时间:2019-03-13 14:43:48

标签: plot plotly-python

我正在尝试对图像的所有像素的RGB分量进行3d绘图(每个像素都是3d空间中的点,其中轴分别为R,G和B),而具有每个像素都用其颜色绘制

https://en.wikipedia.org/wiki/FLAGS_register

我使用了以下方法,这种方法在像素少的情况下效果很好:

import plotly.offline as pyo
import plotly.graph_objs as go
import numpy as np

pyo.init_notebook_mode()

data = list()
# for each pixel
for pixel in image_to_plot:
    # add trace as follows:
    data.append(
        go.Scatter3d(
            # only one point
            x=[pixel[0]],
            y=[pixel[1]],
            z=[pixel[2]],
            # set its color
            mode='markers',
            marker=dict(size=2, color='rgb('+', '.join(pixel.astype(str))+')'),
        )
    )

layout = dict(scene=dict(xaxis=dict(title='R'), yaxis=dict(title='G'), zaxis=dict(title='B')), showlegend=False)
pyo.iplot({'data': data, 'layout': layout}, filename='so_example')

其中image_to_plot是一个由像素组成的numpy数组(形状为(1000, 3))。

但是痕迹数量的增加几乎使我的浏览器崩溃了1000点,甚至无法渲染。 肯定有更好的方法吗?

我的目标是使这项工作获得100k至1M点。对于减少的颜色数量(例如在将K-Means聚类后将点分组为相同颜色的迹线),这可以很好地工作。

我考虑过以另一种方式进行此操作,并为每种现有颜色绘制一条迹线,以避免在不同迹线中出现多个重叠点,但这会导致16M(256 ^ 3)条可能的迹线,因此也不合适。

1 个答案:

答案 0 :(得分:0)

marker字典可以列出其键color的列表并将其按元素应用,因此可以这样做:

# Instead of this in a for-loop:
x=[pixel[0]]
color='rgb('+', '.join(pixel.astype(str))+')'

# Do this:
x=image_to_plot[:, 0]
color=list(map(lambda e: 'rgb('+', '.join(e.astype(str))+')', image_to_plot))

变量data的完整解决方案:

data = [
    go.Scatter3d(
        x=image_to_plot[:, 0],
        y=image_to_plot[:, 1],
        z=image_to_plot[:, 2],
        mode='markers',
        marker=dict(
            size=2,
            color=list(map(lambda e: 'rgb('+', '.join(e.astype(str))+')', image_to_plot)),
        ),
    )
]

这是一种更漂亮的方法,可以在几秒钟内在我的计算机上显示多达500k点。超过此阈值,将达到内存限制。