1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| import SimpleITK as sitk import numpy as np import cv2
def dilateion(image): kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5)) dilate = cv2.morphologyEx(image, cv2.MORPH_DILATE, kernel) return dilate def erode(image): kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3)) erode = cv2.morphologyEx(image, cv2.MORPH_ERODE, kernel) return erode
def edge(image): SE = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) img_grad = cv2.morphologyEx(image, cv2.MORPH_GRADIENT, SE) return img_grad
def openOpreation(image): kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) open = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel) return open
def closeOperation(image): kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) close = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel) return close
def read_3D(image, model="closeOperation"): slices = image.shape[0] result = np.zeros(img_num.shape) for i in range(slices): sli = img_num[i:i + 1, ...] s = sli[0, ...] if model == "dilateion": slice = dilateion(s) elif model =="edge": slice = edge(s) elif model == "erode": slice = erode(s) elif model == "edge": slice = edge(s) elif model == "openOpreation": slice = openOpreation(s) elif model == "closeOperation": slice = closeOperation(s) result[i, ...] = slice return result
def save(x, path): predict_seg = sitk.GetImageFromArray(x) sitk.WriteImage(predict_seg, path)
def read_nii(path): image = sitk.ReadImage(path) img_num = sitk.GetArrayFromImage(image) return img_num if __name__ == "__main__": path = r"D:\myProject\HDC_vessel_seg\datasets\nii\image\vessel_12.nii" imgpath = r"D:\myProject\HDC_vessel_seg\datasets\nii\image\image_12.nii" img_num = read_nii(path) img = read_nii(imgpath) img_num = img_num[10:260,...] img = img[10:260,...] result = read_3D(img_num) name = "closeOperation_" save(result, path.replace("vessel_12", name + "vessel"))
|