Created
July 16, 2021 14:48
-
-
Save DreamOneYou/e2892f7ccf282a0c73f070a2832495d9 to your computer and use it in GitHub Desktop.
对图像进行分割
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import torch | |
import numpy as np | |
def split_image(img, crop_size=(128,128,128)): | |
patient_image = img # (1, 240, 240, 155, 4) | |
patient_image = patient_image[0, ...] # (240, 240, 155, 4) | |
patient_image = patient_image.permute(3, 0, 1, 2) # (1, 4, 155, 240, 240) | |
patient_image = patient_image.cpu().numpy() | |
pasient_image = crop_pad(patient_image, crop_size) | |
patient_image = torch.from_numpy(pasient_image).permute(1, 0, 2, 3, 4) # (C, S, T, Y, W) | |
patient_image = patient_image.unsqueeze(0).numpy() | |
return patient_image | |
def cal_crop_num_img(img_size, in_size): | |
if img_size[0] % in_size[0] == 0: | |
crop_n1 = math.ceil(img_size[0] / in_size[0]) + 1 | |
else: | |
crop_n1 = math.ceil(img_size[0] / in_size[0]) | |
if img_size[1] % in_size[1] == 0: | |
crop_n2 = math.ceil(img_size[1] / in_size[1]) + 1 | |
else: | |
crop_n2 = math.ceil(img_size[1] / in_size[1]) | |
if img_size[2] % in_size[2] == 0: | |
crop_n3 = math.ceil(img_size[2] / in_size[2]) + 1 | |
else: | |
crop_n3 = math.ceil(img_size[2] / in_size[2]) | |
return crop_n1, crop_n2, crop_n3 | |
def crop_pad(image, crop_size): | |
image_size = image.shape | |
crop_n1, crop_n2, crop_n3 = cal_crop_num_img(image_size[1:], crop_size) | |
img_as_np = multi_cropping(image, | |
crop_size=crop_size[0], | |
crop_num1=crop_n1, crop_num2=crop_n2, crop_num3=crop_n3) | |
return img_as_np | |
def multi_cropping(image, crop_size, crop_num1, crop_num2, crop_num3): | |
img_depth, img_height, img_width = image.shape[1], image.shape[2], image.shape[3] | |
cropped_imgs = [] | |
dim0_stride = stride_size(img_depth, crop_num1, crop_size) | |
dim1_stride = stride_size(img_height, crop_num2, crop_size) | |
dim2_stride = stride_size(img_width, crop_num3, crop_size) | |
for d in range(crop_num1): | |
for i in range(crop_num2): | |
for j in range(crop_num3): | |
cropped_imgs.append(cropping(image, crop_size, | |
dim0_stride*d, dim1_stride*i, dim2_stride*j)) | |
return np.asarray(cropped_imgs) | |
def cropping(image, crop_size, dim0, dim1, dim2): | |
cropped_img = image[:, dim0:dim0+crop_size, dim1:dim1+crop_size, dim2:dim2+crop_size] | |
return cropped_img | |
def stride_size(image_len, crop_num, crop_size): | |
if crop_num - 1 == -1: | |
return 0 | |
else: | |
return int((image_len - crop_size)/(crop_num - 1)) | |
if __name__ =="__main__": | |
device = torch.device('cuda') | |
image_size = 128 | |
image = torch.rand((1, 155, 240, 240, 4), device=device) | |
x = split_image(image, (128,128,128)) | |
print("before Pinjie",image.shape) | |
print("after Pinjie",x.shape) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
遇到图像太大,不能送入网络训练时,考虑对图像进行切割。