python mhd格式图片与rgb格式图片之间的转换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_43061687/article/details/82051674
#mhd2rgb
image = sitk.ReadImage(path_address)
image_array = sitk.GetArrayFromImage(image)

#rgb2mhd
if i.endswith('.png'):
img = cv2.imread(os.path.join(rgb_path, i),0)
# print ("image index:", i[:-4])
img = np.array(img)
print(img.shape)
ept_width = img.shape[1]
ept_height = img.shape[0]
ept_chanel = 1
img_resize = np.zeros([ept_chanel, ept_height, ept_width], dtype=np.uint8)
img_resize[:,(ept_height- img.shape[0]) // 2:(ept_height - img.shape[0]) // 2 + img.shape[0],
(ept_width - img.shape[1]) // 2:(ept_width - img.shape[1]) // 2 + img.shape[1]] = img
img_resize=np.reshape(img_resize,[600,512])
img_resize[img_resize==255]=1
mhd_data = sitk.GetImageFromArray(img_resize)
sitk.WriteImage(mhd_data, mhd_gray_path + i[:-4] + ".mhd")

猜你喜欢

转载自blog.csdn.net/weixin_43061687/article/details/82051674
今日推荐