【问题标题】:How to color a 3d grayscale image in python如何在python中为3d灰度图像着色
【发布时间】:2018-07-19 15:29:41
【问题描述】:

我想在 3d 中为像素着色

import numpy as np
import matplotlib.pyplot as plt
im = np.random.randint(0, 255, (16, 16))
I = np.dstack([im, im, im])
x = 5
y = 5
I[x, y, :] = [1, 0, 0]
plt.imshow(I, interpolation='nearest' )
plt.imshow(im, interpolation='nearest', cmap='Greys')

此代码用于 2d,但不是坐标,我想给出我想要更改的 3d 灰度像素的值。

【问题讨论】:

    标签: python image image-processing grayscale


    【解决方案1】:
    import numpy as np
    import matplotlib.pyplot as plt
    np.random.seed(4)
    im = np.random.randint(0, 255, (16, 16))
    I = np.dstack([im, im, im])
    I[np.logical_and(np.logical_and(I[:, :, 0]==15, I[:, :, 1]==15), I[:, :, 2]==15)] = [0, 1, 0]
    plt.figure()
    plt.imshow(I, interpolation='nearest' )
    plt.figure()
    plt.imshow(im, interpolation='nearest', cmap='Greys')
    plt.show()
    

    【讨论】:

    • 谢谢,但这是用于 2d 图像,我想为 3d 制作
    【解决方案2】:
    from PIL import Image
    import numpy as np 
    def image_preprocessing(image_file, height, width):  
        image = Image.open(image_file)
        image = image.resize((width, height), Image.ANTIALIAS)        
        np_image = np.array(image)
        np_image = np_image.astype(float)
        np_image = np_image - 128.0
        np_image = np_image / 128.0
    
        if len(np_image.shape) == 2: # 1D image
            new_image = np.zeros((np_image.shape[0], np_image.shape[1], 3))
            new_image[:,:,0] = np_image
            new_image[:,:,1] = np_image
            new_image[:,:,2] = np_image
        else:
            new_image = np_image
    
        # flushing
        np_image = []
    
        return new_image
    

    【讨论】:

      猜你喜欢
      • 2011-11-23
      • 2010-12-22
      • 2023-02-22
      • 1970-01-01
      • 2012-05-28
      • 2017-07-31
      • 2015-07-01
      • 2023-03-26
      相关资源
      最近更新 更多