提问人:rzymski 提问时间:10/20/2023 最后编辑:Mike 'Pomax' Kamermansrzymski 更新时间:11/12/2023 访问量:113
创建可旋转的 3D RGB 立方体
Create 3D RGB Cube with possible rotation
问:
我想创建/绘制一个可以旋转它的 3D RGB 立方体。
像这样的东西:
我认为 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()
它甚至在某种程度上呈现了我想要的东西,但问题是它太慢/没有优化,即使是简单的旋转也需要很长时间。
恐怕我很不幸地采取了错误的方法,但我不知道我应该如何正确地做到这一点。
答:
0赞
rzymski
10/29/2023
#1
我知道这不是最好的解决方案,但至少它比以前的版本效果更好。
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()
1赞
Dr.Z
11/11/2023
#2
我想到的最简单的例子是使用 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 和 HSV 颜色空间图的图。此外,该文档还提供了实验室空间公告的示例。
下一个:在 odt 文件中居中文本
评论