文章目录
- 一、读取图片dicom —>截取像素 —>归—化(0,1) —>存储为png
- 二、图像增强:(旋转,平移)
- 三、准备U-net训练数据—数据标记
开发环境 Anaconda:jupyter notebook /pycharm
pip install SimpleItk
pip install tqdm
pip install pydicom
pip install opencv-python
一、读取图片dicom —>截取像素 —>归—化(0,1) —>存储为png
"""
读取dicom图像并将其转换为png图像
读取某文件夹内的所有dicom文件
:param src_dir: dicom文件夹路径
:return: dicom list
"""
import os
import SimpleITK
import dicom
import numpy as np
import cv2
from tqdm import tqdm
def is_dicom_file(filename):
file_stream = open(filename, 'rb')
file_stream.seek(128)
data = file_stream.read(4)
file_stream.close()
if data == b'DICM':
return True
return False
def load_patient(src_dir):
files = os.listdir(src_dir)
slices = []
for s in files:
if is_dicom_file(src_dir + '/' + s):
instance = dicom.read_file(src_dir + '/' + s)
slices.append(instance)
slices.sort(key=lambda x: int(x.InstanceNumber))
try:
slice_thickness = np.abs(slices[0].ImagePositionPatient[2]\
- slices[1].ImagePositionPatient[2])
except:
slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation)
for s in slices:
s.SliceThickness = slice_thickness
return slices
def get_pixels_hu_by_simpleitk(dicom_dir):
reader = SimpleITK.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames(dicom_dir)
reader.SetFileNames(dicom_names)
image = reader.Execute()
img_array = SimpleITK.GetArrayFromImage(image)
img_array[img_array == -2000] = 0
return img_array
def normalize_hu(image):
MIN_BOUND = -1000.0
MAX_BOUND = 400.0
image = (image - MIN_BOUND) / (MAX_BOUND - MIN_BOUND)
image[image > 1] = 1.
image[image < 0] = 0.
return image
if __name__ == '__main__':
dicom_dir = './data/dicom_demo/'
slices = load_patient(dicom_dir)
print('The number of dicom files : ', len(slices))
image = get_pixels_hu_by_simpleitk(dicom_dir)
for i in tqdm(range(image.shape[0])):
img_path = "./temp_dir/dcm_2_png/img_" + str(i).rjust(4, '0') + "_i.png"
org_img = normalize_hu(image[i])
cv2.imwrite(img_path, org_img * 255)
二、图像增强:(旋转,平移)
"""
图像数据增强(augmentation)操作
"""
import random
import numpy as np
import cv2
def random_rotate_img(img, min_angle, max_angle):
"""
图像旋转
random rotation an image
:param img: image to be rotated
:param min_angle: min angle to rotate
:param max_angle: max angle to rotate
:return: image after random rotated
"""
if not isinstance(img, list):
img = [img]
angle = random.randint(min_angle, max_angle)
center = (img[0].shape[0] / 2, img[0].shape[1] / 2)
rot_matrix = cv2.getRotationMatrix2D(center, angle, scale=1.0)
res = []
for img_inst in img:
img_inst = cv2.warpAffine(img_inst, rot_matrix,dsize=img_inst.shape[:2],
borderMode=cv2.BORDER_CONSTANT)
res.append(img_inst)
if len(res) == 0:
res = res[0]
return res
def random_flip_img(img):
'''
图像平移
random flip image,both on horizontal and vertical
:param img: image to be flipped
:return: image after flipped
'''
flip_val = 0
if not isinstance(img, list):
res = cv2.flip(img, flip_val)
else:
res = []
for img_item in img:
img_flip = cv2.flip(img_item, flip_val)
res.append(img_flip)
return res
if __name__ == '__main__':
imagePath = './data/chaper3_img_01.png'
img = cv2.imread(imagePath, cv2.IMREAD_GRAYSCALE)
print(img.shape)
res_rotate = random_rotate_img(img, 30, 90)
cv2.imwrite('./temp_dir/chapter3_rotate_img.png', res_rotate[0])
res_flip = random_flip_img(img)
cv2.imwrite('./temp_dir/chapter3_flip_img.png', res_flip)
三、准备U-net训练数据—数据标记
from scipy import ndimage as ndi
import numpy
import cv2
MASK_MARGIN = 5
def make_mask(v_center, v_diam, width, height):
mask = numpy.zeros([height, width])
v_xmin = numpy.max([0, int(v_center[0] - v_diam) - MASK_MARGIN])
v_xmax = numpy.min([width - 1, int(v_center[0] + v_diam) + MASK_MARGIN])
v_ymin = numpy.max([0, int(v_center[1] - v_diam) - MASK_MARGIN])
v_ymax = numpy.min([height - 1, int(v_center[1] + v_diam) + MASK_MARGIN])
v_xrange = range(v_xmin, v_xmax + 1)
v_yrange = range(v_ymin, v_ymax + 1)
for v_x in v_xrange:
for v_y in v_yrange:
p_x = v_x
p_y = v_y
if numpy.linalg.norm(numpy.array([v_center[0], v_center[1]])\
- numpy.array([p_x, p_y]))<= v_diam * 2:
mask[p_y, p_x] = 1.0
return (mask)
if __name__ == '__main__':
imagePath = './data/chaper3_img_01.png'
img = cv2.imread(imagePath, cv2.IMREAD_GRAYSCALE)
print('before resize: ', img.shape)
img_X = ndi.interpolation.zoom(img, [320/512, 320/512], mode='nearest')
print('before resize: ', img_X.shape)
cv2.imwrite('./temp_dir/chapter3_img_X.png', img_X)
img_Y = make_mask((217, 160), 3, 320, 320)
img_Y[img_Y < 0.5] = 0
img_Y[img_Y > 0.5] = 255
nodule_mask = img_Y.astype('uint8')
cv2.imwrite('./temp_dir/chapter3_img_Y.png', img_Y)