matplotlib 创建具有可能旋转的3D RGB立方体

izkcnapc  于 2023-11-22  发布在  其他
关注(0)|答案(2)|浏览(118)

我想创建/绘制一个3D RGB立方体的可能性旋转它。
大概是这样的:
x1c 0d1x的数据
我认为matplotlib是一个很好的选择,因为它有内置的旋转功能。
我试过这样的方法:

import numpy as np
import matplotlib.pyplot as plt

# Full 8-bit RGB space
bits = 8
cube_dimension = 2**bits
full_rgb_space = np.zeros((cube_dimension, cube_dimension, cube_dimension, 3), dtype=np.uint8)

# Fill the 3D RGB cube
for i in range(cube_dimension):
    for j in range(cube_dimension):
        for k in range(cube_dimension):
            color = (i, j, k)
            full_rgb_space[i, j, k] = color

# Create a figure and 3D axis
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# # Extract the RGB components
r, g, b = full_rgb_space[:, :, :, 0], full_rgb_space[:, :, :, 1], full_rgb_space[:, :, :, 2]
#
# # Reshape the RGB arrays to match the dimensions of the scatter plot
r = r.flatten()
g = g.flatten()
b = b.flatten()
#
# # Create an array of colors for each point
colors = full_rgb_space / 255.0
colors = colors.reshape(-1, 3)
#
# # Display the RGB cube using scatter plot
ax.scatter(r, g, b, c=colors, marker='s')

ax.axis('off')
plt.show()

字符串
它甚至在某种程度上渲染了我想要的,但问题是它太慢/没有优化,即使是简单的旋转也需要很长时间。
很遗憾,我在这件事上采取了错误的方法,但我不知道我应该如何正确地做。

clj7thdc

clj7thdc1#

我想到的最简单的例子是使用S3Dlib,一个Matplotlib第三方包,来生成以下内容:


的数据
代码如下:

import numpy as np
import matplotlib.pyplot as plt
import s3dlib.surface as s3d

rgb_cube = s3d.CubicSurface(5).domain([0,1],[0,1],[0,1])
rgb_cube.map_color_from_op(lambda xyz : xyz)
#rgb_cube.map_color_from_op(lambda xyz : 1-xyz)

minmax, ticks = (-0.1,1.1) , [0,1]
fig = plt.figure(figsize=plt.figaspect(1))
ax = plt.axes(projection='3d', proj_type='ortho')
ax.set(xlabel='R',  ylabel='G',  zlabel='G',
       xticks=ticks,yticks=ticks,zticks=ticks,
       xlim=minmax, ylim=minmax, zlim=minmax,
       title='RGB Color Space')
ax.add_collection3d(rgb_cube.shade(.5))

plt.show()

字符串
注意事项:如果使用上面删除的代码行,则绘制CMY颜色空间。S3Dlib文档还包含Lab and HSV颜色空间图。此外,文档提供了Lab space annimation的示例。

0tdrvxhp

0tdrvxhp2#

我知道这不是最好的解决方案,但至少它比以前的版本更好。

import numpy as np
import matplotlib.pyplot as plt

numberOfPointsInDimension = 64
skip = 256 / numberOfPointsInDimension
cube_dimension = int(256 / skip)
count = 0
# to speed process it replaced counting of elements with constant for some specific dimensions values
if numberOfPointsInDimension == 256:
    count = 390152
elif numberOfPointsInDimension == 128:
    count = 96776
elif numberOfPointsInDimension == 64:
    count = 23816
else:
    for i in range(cube_dimension):
        for j in range(cube_dimension):
            for k in range(cube_dimension):
                if i == 0 or i == cube_dimension-1 or j == 0 or j == cube_dimension-1 or k == 0 or k == cube_dimension-1:
                    count += 1

points = np.zeros((count, 3), dtype=np.uint8)
count = 0
for i in range(cube_dimension):
    for j in range(cube_dimension):
        for k in range(cube_dimension):
            if i == 0 or i == cube_dimension-1 or j == 0 or j == cube_dimension-1 or k == 0 or k == cube_dimension-1:
                color = (i * skip, j * skip, k * skip)
                points[count] = color
                count += 1

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Set equal aspect ratio for the 3D plot
ax.set_box_aspect([1, 1, 1])
# # Extract the RGB components
r, g, b = points[:, 0], points[:, 1], points[:, 2]
# # Reshape the RGB arrays to match the dimensions of the scatter plot
r = np.ravel(r)
g = np.ravel(g)
b = np.ravel(b)
# # Create an array of colors for each point
colors = points / 255.0
# Display the RGB cube using scatter plot
scaling = 200
ax.scatter(r, g, b, c=colors, marker='s', s=scaling, alpha=1)
ax.axis('off')

plt.show()

字符串

相关问题