def extract_dicom_images_patient(src_dir): target_dir = settings.NDSB3_EXTRACTED_IMAGE_DIR print("Dir: ", src_dir) dir_path = settings.NDSB3_RAW_SRC_DIR + src_dir + "/" patient_id = src_dir slices = load_patient(dir_path) print(len(slices), "\t", slices[0].SliceThickness, "\t", slices[0].PixelSpacing) print("Orientation: ", slices[0].ImageOrientationPatient) assert slices[0].ImageOrientationPatient == [1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000] pixels = get_pixels_hu(slices) image = pixels print(image.shape) invert_order = slices[1].ImagePositionPatient[2] > slices[0].ImagePositionPatient[2] print("Invert order: ", invert_order, " - ", slices[1].ImagePositionPatient[2], ",", slices[0].ImagePositionPatient[2]) pixel_spacing = slices[0].PixelSpacing pixel_spacing.append(slices[0].SliceThickness) image = helpers.rescale_patient_images(image, pixel_spacing, settings.TARGET_VOXEL_MM) if not invert_order: image = numpy.flipud(image) for i in range(image.shape[0]): patient_dir = target_dir + patient_id + "/" if not os.path.exists(patient_dir): os.mkdir(patient_dir) img_path = patient_dir + "img_" + str(i).rjust(4, '0') + "_i.png" org_img = image[i] img, mask = helpers.get_segmented_lungs(org_img.copy()) org_img = helpers.normalize_hu(org_img) cv2.imwrite(img_path, org_img * 255) cv2.imwrite(img_path.replace("_i.png", "_m.png"), mask * 255)
def process_image(src_path): patient_id = src_path[-14:-4] print("Patient: ", patient_id) dst_dir = settings.LUNA16_EXTRACTED_IMAGE_DIR + patient_id + "/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) #print("Img array: ", img_array.shape) origin = numpy.array(itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) #print("Origin (x,y,z): ", origin) direction = numpy.array(itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) #print("Direction: ", direction) spacing = numpy.array(itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) #print("Spacing (x,y,z): ", spacing) rescale = spacing / settings.TARGET_VOXEL_MM #print("Rescale: ", rescale) img_array = helpers.rescale_patient_images(img_array, spacing, settings.TARGET_VOXEL_MM) img_list = [] for i in range(img_array.shape[0]): img = img_array[i] seg_img, mask = helpers.get_segmented_lungs(img.copy()) img_list.append(seg_img) img = normalize(img) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_i.png", img * 255) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_m.png", mask * 255)
def extract_dicom_images_patient(src_dir): target_dir = settings.NDSB3_EXTRACTED_IMAGE_DIR #print("Dir: ", src_dir) settings.log.info("Patient: {0}".format(src_dir)) dir_path = settings.NDSB3_RAW_SRC_DIR + src_dir + "/" patient_id = src_dir slices = load_patient(dir_path) settings.log.info( "Slice number: {0} \t thickness - {1} \t x and y spacing - {2}".format( len(slices), slices[0].SliceThickness, slices[0].PixelSpacing)) #print(len(slices), "\t", slices[0].SliceThickness, "\t", slices[0].PixelSpacing) settings.log.info("Orientation: {0}".format( slices[0].ImageOrientationPatient)) #print("Orientation: ", slices[0].ImageOrientationPatient) #assert slices[0].ImageOrientationPatient == [1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000] cos_value = (slices[0].ImageOrientationPatient[0]) cos_degree = round(math.degrees(math.acos(cos_value)), 2) pixels = get_pixels_hu(slices) image = pixels settings.log.info("image shape: {0}".format(image.shape)) invert_order = slices[1].ImagePositionPatient[2] > slices[ 0].ImagePositionPatient[2] settings.log.info("Invert order: {0} - {1:.6f}, {2:.6f}".format( invert_order, slices[1].ImagePositionPatient[2], slices[0].ImagePositionPatient[2])) #print("Invert order: ", invert_order, " - ", slices[1].ImagePositionPatient[2], ",", slices[0].ImagePositionPatient[2]) pixel_spacing = slices[0].PixelSpacing pixel_spacing.append(slices[0].SliceThickness) image = helpers.rescale_patient_images(image, pixel_spacing, settings.TARGET_VOXEL_MM, verbose=True) if not invert_order: image = numpy.flipud(image) for i in range(image.shape[0]): patient_dir = target_dir + patient_id + "/" if not os.path.exists(patient_dir): os.makedirs(patient_dir) img_path = patient_dir + "img_" + str(i).rjust(4, '0') + "_i.png" org_img = image[i] # if there exists slope,rotation image with corresponding degree if cos_degree > 0.0: org_img = cv_flip(org_img, org_img.shape[1], org_img.shape[0], cos_degree) img, mask = helpers.get_segmented_lungs(org_img.copy()) org_img = helpers.normalize_hu(org_img) cv2.imwrite(img_path, org_img * 255) cv2.imwrite(img_path.replace("_i.png", "_m.png"), mask * 255)
def extract_dicom_images_patient(src_dir): #directory where the extracted images will go target_dir = settings.NDSB3_EXTRACTED_IMAGE_DIR print("Dir: ", src_dir) #directory of the patient (folder name = patientID) dir_path = settings.NDSB3_RAW_SRC_DIR + src_dir + "/" patient_id = src_dir #sorts slices by acquisition, obtains thickness, applies to all slices, then returns array of slices slices = load_patient(dir_path) print(len(slices), "\t", slices[0].SliceThickness, "\t", slices[0].PixelSpacing) print("Orientation: ", slices[0].ImageOrientationPatient) #assert slices[0].ImageOrientationPatient == [1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000] cos_value = (slices[0].ImageOrientationPatient[0]) cos_degree = round(math.degrees(math.acos(cos_value)), 2) #convert to image pixels = get_pixels_hu(slices) image = pixels print(image.shape) #check if order is inverted invert_order = slices[1].ImagePositionPatient[2] > slices[ 0].ImagePositionPatient[2] print("Invert order: ", invert_order, " - ", slices[1].ImagePositionPatient[2], ",", slices[0].ImagePositionPatient[2]) #rescale images based on pixel spacing pixel_spacing = slices[0].PixelSpacing pixel_spacing.append(slices[0].SliceThickness) image = helpers.rescale_patient_images(image, pixel_spacing, settings.TARGET_VOXEL_MM) if not invert_order: image = numpy.flipud(image) #go through the height of the image (rows = [0]) for i in range(image.shape[0]): patient_dir = target_dir + patient_id + "/" if not os.path.exists(patient_dir): os.mkdir(patient_dir) #write image out as .png file img_path = patient_dir + "img_" + str(i).rjust(4, '0') + "_i.png" #original image is the one just written to folder org_img = image[i] # if there exists slope,rotation image with corresponding degree if cos_degree > 0.0: org_img = cv_flip(org_img, org_img.shape[1], org_img.shape[0], cos_degree) img, mask = helpers.get_segmented_lungs(org_img.copy()) #normalize hounsfield units org_img = helpers.normalize_hu(org_img) cv2.imwrite(img_path, org_img * 255) cv2.imwrite(img_path.replace("_i.png", "_m.png"), mask * 255)
def process_image(src_path): """Load the '.mhd' file, extract the 3D numpy array, rescale the data, write out each slice as '.png' and each segmented mask as '.png' """ patient_id = ntpath.basename(src_path).replace( ".mhd", "") #extract patient id from filename print("Patient: ", patient_id) dst_dir = settings.LUNA16_EXTRACTED_IMAGE_DIR + patient_id + "/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) print("Img array: ", img_array.shape) origin = numpy.array( itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) print("Origin (x,y,z): ", origin) direction = numpy.array( itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) print("Direction: ", direction) spacing = numpy.array( itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) print("Spacing (x,y,z): ", spacing) rescale = spacing / settings.TARGET_VOXEL_MM print("Rescale: ", rescale) img_array = helpers.rescale_patient_images(img_array, spacing, settings.TARGET_VOXEL_MM) img_list = [] for i in range(img_array.shape[0]): img = img_array[i] seg_img, mask = helpers.get_segmented_lungs( img.copy()) #find the segmented image and the mask img_list.append(seg_img) img = normalize(img) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_i.png", img * 255) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_m.png", mask * 255)
def process_image(src_path): patient_id = ntpath.basename(src_path).replace(".mhd", "") print("Patient: ", patient_id) dst_dir = settings.LUNA16_EXTRACTED_IMAGE_DIR + patient_id + "/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) print("Img array: ", img_array.shape) origin = numpy.array( itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) print("Origin (x,y,z): ", origin) direction = numpy.array( itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) print("Direction: ", direction) spacing = numpy.array( itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) print("Spacing (x,y,z): ", spacing) rescale = spacing / settings.TARGET_VOXEL_MM print("Rescale: ", rescale) #Rescale our scan img_array = helpers.rescale_patient_images(img_array, spacing, settings.TARGET_VOXEL_MM) img_list = [] #Iterate on each frame for i in range(img_array.shape[0]): img = img_array[i] #Get the segmented lungs, so we know the regions of interest, using Segmentation seg_img, mask = helpers.get_segmented_lungs(img.copy()) img_list.append(seg_img) img = normalize(img) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_i.png", img * 255) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_m.png", mask * 255)
def process_image(src_path): patient_id = ntpath.basename(src_path).replace(".mhd", "") print("Patient: ", patient_id) if patient_id == 'LKDS-00384': return dst_dir = settings.TEST_EXTRACTED_IMAGE_DIR + patient_id + "/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) print("Img array: ", img_array.shape) origin = numpy.array( itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) print("Origin (x,y,z): ", origin) direction = numpy.array( itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) print("Direction: ", direction) spacing = numpy.array( itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) print("Spacing (x,y,z): ", spacing) rescale = spacing / settings.TARGET_VOXEL_MM print("Rescale: ", rescale) #归一化,体素间距为1,z保留512个最多 img_array = helpers.rescale_patient_images(img_array, spacing, settings.TARGET_VOXEL_MM) img_list = [] for i in range(img_array.shape[0]): img = img_array[i] seg_img, mask = helpers.get_segmented_lungs(img.copy()) img_list.append(seg_img) img = normalize(img) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_i.png", img * 255) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_m.png", mask * 255)
def process_image(src_path): patient_id = os.path.basename(src_path).replace(".mhd", "") settings.log.info("Patient: {0}".format(patient_id)) dst_dir = settings.LUNA16_EXTRACTED_IMAGE_DIR + patient_id + "/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) settings.log.info("Img array: {0}".format(img_array.shape)) origin = numpy.array( itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) settings.log.info("Origin (x,y,z): {0}".format(origin)) direction = numpy.array( itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) settings.log.info("Direction: {0}".format(direction)) spacing = numpy.array( itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) settings.log.info("Spacing (x,y,z): {0}".format(spacing)) rescale = spacing / settings.TARGET_VOXEL_MM settings.log.info("Rescale: {0}".format(rescale)) img_array = helpers.rescale_patient_images(img_array, spacing, settings.TARGET_VOXEL_MM) img_list = [] for i in range(img_array.shape[0]): img = img_array[i] seg_img, mask = helpers.get_segmented_lungs(img.copy()) img_list.append(seg_img) img = normalize(img) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_i.png", img * 255) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_m.png", mask * 255)
def extract_dicom_images_patient(src_dir): target_dir = settings.NDSB3_EXTRACTED_IMAGE_DIR print("Dir: ", src_dir) dir_path = settings.NDSB3_RAW_SRC_DIR + src_dir + "/" patient_id = src_dir slices = load_patient(dir_path) print(len(slices), "\t", slices[0].SliceThickness, "\t", slices[0].PixelSpacing) print("Orientation: ", slices[0].ImageOrientationPatient) assert slices[0].ImageOrientationPatient == [ 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000 ] pixels = get_pixels_hu(slices) image = pixels print(image.shape) invert_order = slices[1].ImagePositionPatient[2] > slices[ 0].ImagePositionPatient[2] print("Invert order: ", invert_order, " - ", slices[1].ImagePositionPatient[2], ",", slices[0].ImagePositionPatient[2]) pixel_spacing = slices[0].PixelSpacing pixel_spacing.append(slices[0].SliceThickness) image = helpers.rescale_patient_images(image, pixel_spacing, settings.TARGET_VOXEL_MM) if not invert_order: image = numpy.flipud(image) for i in range(image.shape[0]): patient_dir = target_dir + patient_id + "/" if not os.path.exists(patient_dir): os.mkdir(patient_dir) img_path = patient_dir + "img_" + str(i).rjust(4, '0') + "_i.png" org_img = image[i] img, mask = helpers.get_segmented_lungs(org_img.copy()) org_img = helpers.normalize_hu(org_img) cv2.imwrite(img_path, org_img * 255) cv2.imwrite(img_path.replace("_i.png", "_m.png"), mask * 255)
def process_image(src_path): patient_id = ntpath.basename(src_path).replace(".mhd", "") #df_patient = annotations[annotations['seriesuid'] == patient_id] #print("Patient: ", patient_id) dst_dir = traindata_path + patient_id + "/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) print("Img array: ", img_array.shape) origin = numpy.array( itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) print("Origin (x,y,z): ", origin) direction = numpy.array( itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) print("Direction: ", direction) spacing = numpy.array( itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) print("Spacing (x,y,z): ", spacing) rescale = spacing / setting.TARGET_VOXEL_MM print("Rescale: ", rescale) #calc real origin flip_direction_x = False flip_direction_y = False if round(direction[0]) == -1: origin[0] *= -1 direction[0] = 1 flip_direction_x = True print("Swappint x origin") if round(direction[4]) == -1: origin[1] *= -1 direction[4] = 1 flip_direction_y = True print("Swappint y origin") print("Direction: ", direction) assert abs(sum(direction) - 3) < 0.01 try: img_array = helpers.rescale_patient_images(img_array, spacing, setting.TARGET_VOXEL_MM) except: print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") img_list = [] lung_mask = lung_segmentation.segment_HU_scan_elias(img_array) for i in range(img_array.shape[0]): #img = img_array[i] #img = normalize(img) #mask = lung_mask[i] #cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_i.png", img * 255) #cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_m.png", mask * 255) #orgin kaggle ranking 2 code #nodule_mask = numpy.zeros([512,512]) img = img_array[i] seg_img, mask = helpers.get_segmented_lungs(img.copy()) img_list.append(seg_img) img = normalize(img) #img = normalize(img) #img[mask==0] = 0 cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_i.png", img * 255) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_m.png", mask * 255)