def prepare_patient_images(patient_id, intermediate_crop=0): file_lst = [] prefix = str(patient_id).rjust(4, '0') src_files = helpers.get_files(settings.BASE_PREPROCESSEDIMAGES_DIR, prefix + "*.png") patient_dir = helpers.get_pred_patient_dir(patient_id) helpers.create_dir_if_not_exists(patient_dir) patient_img_dir = helpers.get_pred_patient_img_dir(patient_id) helpers.create_dir_if_not_exists(patient_img_dir) helpers.delete_files(patient_img_dir, "*.png") dummy = numpy.zeros((settings.TARGET_SIZE, settings.TARGET_SIZE)) cv2.imwrite(patient_img_dir + "dummy_overlay.png", dummy) for src_path in src_files: file_name = ntpath.basename(src_path) org_img = cv2.imread(src_path, cv2.IMREAD_GRAYSCALE) cropped_img = helpers.prepare_cropped_sax_image(org_img, clahe=True, intermediate_crop=intermediate_crop, rotate=0) if SCALE_SIZE is not None: cropped_img = cv2.resize(cropped_img, (SCALE_SIZE, SCALE_SIZE), interpolation=cv2.INTER_AREA) cv2.imwrite(patient_img_dir + file_name, cropped_img) file_lst.append([file_name, "dummy_overlay.png"]) with open(patient_img_dir + "pred.lst", "wb") as f: writer = csv.writer(f, delimiter='\t') writer.writerows(file_lst)
def convert_sax_images(rescale=True, base_size=256, crop_size=256): target_dir = settings.BASE_PREPROCESSEDIMAGES_DIR print("Deleting old files..") helpers.delete_files(target_dir, "*.png") print("patient\trows\tcols\tspacing\tfile_name") file_count = 0 for dicom_data in helpers.enumerate_sax_files(): file_count += 1 if dicom_data.in_plane_encoding_direction not in ["ROW", "COL"]: raise Exception("ROW,COL") if dicom_data.spacing[0] != dicom_data.spacing[1]: raise Exception("Data spacings not equal") print( str(dicom_data.patient_id) + "\t" + str(dicom_data.rows) + "\t" + str(dicom_data.columns) + "\t" + str(dicom_data.series_number) + "\t" + str(dicom_data.spacing) + "\t" + str(dicom_data.slice_thickness) + "\t" + str(dicom_data.sequence_name) + "\t" + str(dicom_data.image_position) + "\t" + str(dicom_data.slice_location) + "\t" + dicom_data.in_plane_encoding_direction) location_id = int(dicom_data.slice_location) + 10000 time_id = dicom_data.create_time location_id_str = str(location_id).rjust(5, '0') img_path = target_dir + str(dicom_data.patient_id).rjust( 4, '0') + "_" + dicom_data.series_description.rjust( 8, '0') + "_" + str(dicom_data.instance_number).rjust( 2, '0' ) + "_" + location_id_str + "_" + dicom_data.file_name.replace( ".dcm", ".png") scipy.misc.imsave(img_path, dicom_data.pixel_array) img = cv2.imread(img_path, 0) if dicom_data.in_plane_encoding_direction == "COL": # rotate counter clockwise when image is column oriented.. img = cv2.transpose(img) img = cv2.flip(img, 0) print("") if rescale: scale = dicom_data.spacing[0] img = cv2.resize(img, (0, 0), fx=scale, fy=scale) sq_img = get_square_crop(img, base_size=base_size, crop_size=crop_size) clahe = cv2.createCLAHE(tileGridSize=(1, 1)) cl_img = clahe.apply(sq_img) cv2.imwrite(img_path, cl_img)
def convert_sax_images(rescale=True, base_size=256, crop_size=256): target_dir = settings.BASE_PREPROCESSEDIMAGES_DIR print "Deleting old files.." helpers.delete_files(target_dir, "*.png") print "patient\trows\tcols\tspacing\tfile_name" file_count = 0 for dicom_data in helpers.enumerate_sax_files(): file_count += 1 if dicom_data.in_plane_encoding_direction not in ["ROW", "COL"]: raise Exception("ROW,COL") if dicom_data.spacing[0] != dicom_data.spacing[1]: raise Exception("Data spacings not equal") print str(dicom_data.patient_id) + "\t" + str(dicom_data.rows) + "\t" + str(dicom_data.columns) + "\t" + str(dicom_data.series_number) + "\t" + str(dicom_data.spacing) + "\t" + str(dicom_data.slice_thickness) + "\t" + str(dicom_data.sequence_name) + "\t" + str(dicom_data.image_position) + "\t" + str(dicom_data.slice_location) + "\t" + dicom_data.in_plane_encoding_direction location_id = int(dicom_data.slice_location) + 10000 time_id = dicom_data.create_time location_id_str = str(location_id).rjust(5, '0') img_path = target_dir + str(dicom_data.patient_id).rjust(4, '0') + "_" + dicom_data.series_description.rjust(8, '0') + "_" + str(dicom_data.instance_number).rjust(2, '0') + "_" + location_id_str + "_" + dicom_data.file_name.replace(".dcm", ".png") scipy.misc.imsave(img_path, dicom_data.pixel_array) img = cv2.imread(img_path, 0) if dicom_data.in_plane_encoding_direction == "COL": # rotate counter clockwise when image is column oriented.. img = cv2.transpose(img) img = cv2.flip(img, 0) print "" if rescale: scale = dicom_data.spacing[0] img = cv2.resize(img, (0, 0), fx=scale, fy=scale) sq_img = get_square_crop(img, base_size=base_size, crop_size=crop_size) clahe = cv2.createCLAHE(tileGridSize=(1, 1)) cl_img = clahe.apply(sq_img) cv2.imwrite(img_path, cl_img)
def predict_overlays_patient(patient_id, pred_model_name, pred_model_iter, save_transparents=False, threshold_value=-1): src_image_dir = helpers.get_pred_patient_img_dir(patient_id) overlay_dir = helpers.get_pred_patient_overlay_dir(patient_id) helpers.delete_files(overlay_dir, "*.png") transparent_overlay_dir = helpers.get_pred_patient_transparent_overlay_dir(patient_id) helpers.delete_files(transparent_overlay_dir, "*.png") num_lines = sum(1 for l in open(src_image_dir + "pred.lst")) batch_size = 1 for try_size in [2, 3, 4, 5]: if num_lines % try_size == 0: batch_size = try_size pred_model = mx.model.FeedForward.load(pred_model_name, pred_model_iter, ctx=mx.gpu(), numpy_batch_size=batch_size) if not settings.QUICK_MODE: # 5 crops predictions_list = [] predictions = [] for crop_indents in [[1, 1], [1, CROP_SIZE - 1], [CROP_SIZE - 1, 1], [CROP_SIZE - 1, CROP_SIZE - 1], [CROP_SIZE / 2, CROP_SIZE / 2]]: # for crop_indents in [[CROP_SIZE / 2, CROP_SIZE / 2], [CROP_SIZE / 2, 1], [CROP_SIZE / 2, CROP_SIZE - 1]]: pred_iter = FileIter(root_dir=src_image_dir, flist_name="pred.lst", batch_size=batch_size, augment=False, mean_image=None, crop_size=INPUT_SIZE, crop_indent_x=crop_indents[0], crop_indent_y=crop_indents[1]) tmp_predictions = pred_model.predict(pred_iter) predictions_list.append(tmp_predictions) averaged_overlays = [] for image_index in range(0, predictions_list[0].shape[0]): min_pixels = 99999999. min_index = - 1 max_pixels = -99999999. max_index = - 1 for crop_index in range(0, len(predictions_list)): pred_overlay = predictions_list[crop_index][image_index] pixel_sum = pred_overlay.sum() if pixel_sum < min_pixels: min_pixels = pixel_sum min_index = crop_index if pixel_sum > max_pixels: max_pixels = pixel_sum max_index = crop_index sum_overlay = None sum_item_count = 0 min_index = -1 for crop_index in range(0, len(predictions_list)): if crop_index != max_index: continue pred_overlay = predictions_list[crop_index][image_index] if sum_overlay is None: sum_overlay = pred_overlay sum_item_count += 1 else: sum_overlay += pred_overlay sum_item_count += 1 sum_overlay /= sum_item_count averaged_overlays.append(sum_overlay) predictions = numpy.vstack(averaged_overlays) else: pred_iter = FileIter(root_dir=src_image_dir, flist_name="pred.lst", batch_size=batch_size, augment=False, mean_image=None, crop_size=INPUT_SIZE) predictions = pred_model.predict(pred_iter) for i in range(len(predictions)): y = predictions[i] y = y.reshape(INPUT_SIZE, INPUT_SIZE) border_size = CROP_SIZE / 2 y = cv2.copyMakeBorder(y, border_size, border_size, border_size, border_size, cv2.BORDER_CONSTANT, value=0) y *= 255 if threshold_value >= 0: y[y <= threshold_value] = 0 y[y > threshold_value] = 255 file_name = ntpath.basename(pred_iter.image_files[i]) cv2.imwrite(overlay_dir + file_name, y) if save_transparents: channels = cv2.split(y) # make argb empty = numpy.zeros(channels[0].shape, dtype=numpy.float32) alpha = channels[0].copy() alpha[alpha == 255] = 75 channels = (channels[0], channels[0], empty, alpha) transparent_overlay = cv2.merge(channels) cv2.imwrite(transparent_overlay_dir + file_name, transparent_overlay)