创建新的多维数组

时间:2017-03-13 13:07:34

标签: python arrays numpy

我想获得这种数组:

ves = np.zeros((6,5), dtype=object)

因此我创建了一个新的数组6乘4乘3.

ves[0][0] = np.array([0,0,-1])
ves[0][1] = np.array([0,0,1])
ves[0][2] = np.array([0,0,0])
ves[0][3] = np.array([0,0,0])

ves[1][0] = np.array([0,0,0])
ves[1][1] = np.array([0,0,0])
ves[1][2] = np.array([0,0,0])
ves[1][3] = np.array([0,0,0])

我开始填充元素:

[ -1.00000000e+00   0.00000000e+00  -6.12323400e-17]
[  1.00000000e+00   0.00000000e+00   6.12323400e-17]
[ 0.  0.  0.]
[ 0.  0.  0.]
[ 0.  0.  0.]
[ 0.  0.  0.]
....

并注意到我没有得到预期的结果。

# based on https://github.com/torch/image/blob/9f65c30167b2048ecbe8b7befdc6b2d6d12baee9/generic/image.c
def rgb_to_lab(srgb):
    with tf.name_scope("rgb_to_lab"):
        srgb = check_image(srgb)
        srgb_pixels = tf.reshape(srgb, [-1, 3])

        with tf.name_scope("srgb_to_xyz"):
            linear_mask = tf.cast(srgb_pixels <= 0.04045, dtype=tf.float32)
            exponential_mask = tf.cast(srgb_pixels > 0.04045, dtype=tf.float32)
            rgb_pixels = (srgb_pixels / 12.92 * linear_mask) + (((srgb_pixels + 0.055) / 1.055) ** 2.4) * exponential_mask
            rgb_to_xyz = tf.constant([
                #    X        Y          Z
                [0.412453, 0.212671, 0.019334], # R
                [0.357580, 0.715160, 0.119193], # G
                [0.180423, 0.072169, 0.950227], # B
            ])
            xyz_pixels = tf.matmul(rgb_pixels, rgb_to_xyz)

        # https://en.wikipedia.org/wiki/Lab_color_space#CIELAB-CIEXYZ_conversions
        with tf.name_scope("xyz_to_cielab"):
            # convert to fx = f(X/Xn), fy = f(Y/Yn), fz = f(Z/Zn)

            # normalize for D65 white point
            xyz_normalized_pixels = tf.multiply(xyz_pixels, [1/0.950456, 1.0, 1/1.088754])

            epsilon = 6/29
            linear_mask = tf.cast(xyz_normalized_pixels <= (epsilon**3), dtype=tf.float32)
            exponential_mask = tf.cast(xyz_normalized_pixels > (epsilon**3), dtype=tf.float32)
            fxfyfz_pixels = (xyz_normalized_pixels / (3 * epsilon**2) + 4/29) * linear_mask + (xyz_normalized_pixels ** (1/3)) * exponential_mask

            # convert to lab
            fxfyfz_to_lab = tf.constant([
                #  l       a       b
                [  0.0,  500.0,    0.0], # fx
                [116.0, -500.0,  200.0], # fy
                [  0.0,    0.0, -200.0], # fz
            ])
            lab_pixels = tf.matmul(fxfyfz_pixels, fxfyfz_to_lab) + tf.constant([-16.0, 0.0, 0.0])

        return tf.reshape(lab_pixels, tf.shape(srgb))

我做错了什么?

1 个答案:

答案 0 :(得分:0)

根据您的预期结果,删除最后一个索引:

ves = np.zeros((6,5), dtype=object)

ves[0][0] = np.array([0,0,-1])

依旧......