繁体   English   中英

如何在python中为3D灰度图像着色

[英]How to color a 3d grayscale image in python

我想在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,但我不想使用coordiantes来给出我想要改变的3d中灰度像素的值。

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()
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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM