示例#1
0
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)
示例#2
0
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)
示例#4
0
def preprocess_extradata(start_dir):
    #src_dir = settings.BASE_DIR + "extrasets/"
    dirs_and_files = os.listdir(start_dir)
    dirs = []
    dicom_paths = []
    for dir_or_file in dirs_and_files:
        if os.path.isdir(start_dir + dir_or_file):
            dirs.append(start_dir + dir_or_file + "/")
        else:
            if dir_or_file.endswith(".dcm") or "CT." in dir_or_file:
                dicom_paths.append(start_dir + dir_or_file)

    # patient_imgs = load_patient(dicom_files)
    #patient_pixels = get_pixels_hu(patient_imgs)

    for dicom_path in dicom_paths:
        try:
            dicom_slice = dicom.read_file(dicom_path)
            slice_location = 0
            slice_location = str(int(round(dicom_slice.SliceLocation * 1000)))
            image = dicom_slice.pixel_array
            image = image.astype(numpy.int16)
            image[image == -2000] = 0
            intercept = dicom_slice.RescaleIntercept
            slope = dicom_slice.RescaleSlope
            if slope != 1:
                print("Slope <> 1!!!")
                image = slope * image.astype(numpy.float64)
                image = image.astype(numpy.int16)
            image += numpy.int16(intercept)

            image = helpers.normalize_hu(image)
            tmp = dicom_path.replace(settings.BASE_DIR + "extradata/", "")
            file_name = ntpath.basename(dicom_path)
            patient_id = tmp.replace(file_name, "").replace("/", "")
            file_name = file_name.replace(".dcm", "")
            target_dir = settings.BASE_DIR + "extradata_processed/" + patient_id + "/"
            if not os.path.exists(target_dir):
                print("Patient: ", patient_id)
                os.mkdir(target_dir)
            target_path = target_dir + slice_location + "_" + file_name + "_i.png"
            cv2.imwrite(target_path, image * 255)
        except:
            print("EXCEPTION")

    for sub_dir in dirs:
        preprocess_extradata(sub_dir)
示例#5
0
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)