[英]How to produce RGB cube matrix in python?
我试图创建一个大小为256 * 256 * 3的标准化矩阵,该矩阵代表RGB立方体,
我在opencv-中尝试了以下代码(我将numpy导入为np):
R = [np.true_divide(i, 256) for i in xrange(256)]
RGB_Cube = np.zeros((256, 256, 3), dtype=np.float64)
RGB_Cube[:, :, 0] = RGB_Cube[:, :, 1] = RGB_Cube[:, :, 2] = np.tile(R, (256,1))
我得到了这个:
我也尝试过此操作(不规范通道):
R = [i for i in xrange(256)]
# R = np.linspace(0, 1, 256, endpoint=True)
RGB_Cube = np.zeros((256, 256, 3), dtype=np.float64)
RGB_Cube[:, :, 0] = RGB_Cube[:, :, 1] = RGB_Cube[:, :, 2] = np.tile(R, (256,1))
但是我得到了白色的图像。
我想将此矩阵划分为多个子立方体。 然后找到这些长方体的平均值。 之后,我将使用此信息对给定图像进行分割!
我不知道这个问题有多容易,我找不到解决方法。 有人可以帮忙吗?
谢谢
抱歉,我仍然无法理解您的需求。 假设您想要一个“多维数据集”来表示每个可能的8位RGB值,则需要一个256 x 256 x 256(x3)的数组。 不是3 256 x 256(x3)阵列。
请注意-我真的认为您不想这样做。 这样的数据(包括子多维数据集)可以按程序进行处理,而无需将所有内容存储在内存中。 下面的代码存储了约1600万个8位RGB空间值,并在浸入磁盘后大约需要140MB。
无论如何,这里:
import pickle
import numpy as np
# full 8-bit RGB space
bits = 8
cube_dimension = 2**bits
full_rgb_space = np.ndarray((cube_dimension, cube_dimension, cube_dimension, 3),
dtype=np.uint8)
# this is really inefficient and will take a long time.
for i in range(cube_dimension):
print(i) # just to give some feedback while it's working
for j in range(cube_dimension):
for k in range(cube_dimension):
position = color = (i, j, k)
full_rgb_space[position] = color
# save it to see what you've got.
# this gives me a 140MB file.
with open('full_rgb_space.p', 'wb') as f:
pickle.dump(full_rgb_space, f)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.