Beispiel #1
0
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(dicom_dir, rescale=True, crop_size=256):
    """
    :param dicom_dir: 文件路径
    :param rescale:
    :param base_size:
    :param crop_size:
    :return:
    """
    """读取dicom文件,并将其转换为png格式,裁剪,翻转等,然后将图像存储起来"""
    target_dir = settings.BASE_PREPROCESSEDIMAGES_DIR

    file_count = 0
    for dicom_data in helpers.enumerate_sax_files(dicom_dir):
        file_count += 1

        if dicom_data.in_plane_encoding_direction not in ["ROW", "COL"]:
            print("Error: plane_encoding_direction", dicom_data.file_name)
            raise Exception("ROW,COL")

        if dicom_data.spacing[0] != dicom_data.spacing[1]:
            print("Error: spacing")
            raise Exception("Data spacings not equal")

        if file_count % 100 == 0:
            print(str(dicom_data.patient_id) + "\t" + str(dicom_data.series_number))
                  

        location_id = int(dicom_data.slice_location) + 1000   #负数变正数
        location_id_str = str(location_id).rjust(4, '0')

        #rjust 字符串的长度统一
        #命名规则
        img_path = target_dir + str(dicom_data.patient_id).rjust(4, '0') + '_'\
                   + str(dicom_data.series_number).rjust(2, '0') +"_" \
                   +str(dicom_data.instance_number).rjust(3,'0') +  ".png"

        #img = Image.fromarray(dicom_data.pixel_array)       #数组转图像
        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)    #图像转置,将x,y互换
            img = cv2.flip(img, 0)      #纵向翻转图像

        if rescale:         #将图像从像素大小转为物理大小
            scale = dicom_data.spacing[0]
            img = cv2.resize(img, (0, 0), fx=scale, fy=scale)

        sq_img = get_square_crop(img, crop_size=crop_size)
        clahe = cv2.createCLAHE(tileGridSize=(1, 1))    #直方图均衡化,以小块为单位
        cl_img = clahe.apply(sq_img)
        cv2.imwrite(img_path, cl_img)
Beispiel #3
0
def create_csv_data():
    print("Creating csv file from dicom data")
    row_no = 0
    with open(settings.BASE_DIR + "dicom_data.csv", "wb") as csv_file:
        csv_writer = csv.writer(csv_file,
                                delimiter=";",
                                quoting=csv.QUOTE_MINIMAL)
        csv_writer.writerow([
            "patient_id", "slice_no", "frame_no", "rows", "columns", "spacing",
            "slice_thickness", "slice_location", "slice_location2", "plane",
            "image_position", "sv", "time", "manufact", "modelname", "age",
            "birth", "sex", "file_name", "angle", "o1", "o2", "o3", "o4", "o5",
            "o6"
        ])
        for dicom_data in helpers.enumerate_sax_files():
            row_no += 1
            if row_no % 1000 == 0:
                print(row_no)

            csv_writer.writerow([
                str(dicom_data.patient_id),
                str(dicom_data.series_number),
                str(dicom_data.instance_number),
                str(dicom_data.rows),
                str(dicom_data.columns),
                str(dicom_data.spacing[0]),
                str(dicom_data.slice_thickness),
                str(dicom_data.slice_location),
                str(dicom_data.get_location()),
                dicom_data.in_plane_encoding_direction,
                str(dicom_data.image_position),
                str(dicom_data.get_value("SequenceVariant")),
                str(dicom_data.get_value("InstanceCreationTime")),
                str(dicom_data.get_value("Manufacturer")),
                str(dicom_data.get_value("ManufacturerModelName")),
                str(dicom_data.get_value("PatientAge")),
                str(dicom_data.get_value("PatientBirthDate")),
                str(dicom_data.get_value("PatientSex")),
                dicom_data.file_name.replace(".dcm", ""),
                str(dicom_data.get_value("FlipAngle")),
                str(round(dicom_data.image_orientation_patient[0], 2)),
                str(round(dicom_data.image_orientation_patient[1], 2)),
                str(round(dicom_data.image_orientation_patient[2], 2)),
                str(round(dicom_data.image_orientation_patient[3], 2)),
                str(round(dicom_data.image_orientation_patient[4], 2)),
                str(round(dicom_data.image_orientation_patient[5], 2))
            ])
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 create_csv_data(data_dir):
    print("Creating csv file from dicom data")
    row_no = 0
    with open("dicom_data.csv", "w", newline='') as csv_file:
        csv_writer = csv.writer(csv_file, delimiter=",", quoting=csv.QUOTE_MINIMAL)
        csv_writer.writerow(["patient_id", "slice_no", "frame_no",
                             "spacing","slice_thickness", "slice_location", "time"])

        for dicom_data in helpers.enumerate_sax_files(data_dir):
            row_no += 1
            if row_no % 1000 == 0:
                print(row_no)

            csv_writer.writerow([
                str(dicom_data.patient_id),                         #病人编号
                str(dicom_data.series_number),                      #层号,与文件夹数字一致,eg:sax_5中的文件seriesNumber都是5
                str(dicom_data.instance_number),                    #在同一层中的不同时间下的编号。
                str(dicom_data.spacing[0]),                         #像素的物理间距,即实际大小
                str(dicom_data.slice_thickness),                    #层厚
                str(dicom_data.slice_location),                     #当前层的位置,单位mm, 同一层一致
                str(dicom_data.get_value("InstanceCreationTime")),  #当前图片的时间
            ])
def create_csv_data():
    print "Creating csv file from dicom data"
    row_no = 0
    with open(settings.BASE_DIR + "dicom_data.csv", "wb") as csv_file:
        csv_writer = csv.writer(csv_file, delimiter=";", quoting=csv.QUOTE_MINIMAL)
        csv_writer.writerow(["patient_id", "slice_no", "frame_no", "rows", "columns", "spacing", "slice_thickness", "slice_location", "slice_location2", "plane", "image_position", "sv", "time", "manufact", "modelname", "age", "birth", "sex", "file_name", "angle", "o1", "o2", "o3", "o4", "o5","o6"])
        for dicom_data in helpers.enumerate_sax_files():
            row_no += 1
            if row_no % 1000 == 0:
                print row_no

            csv_writer.writerow([
                str(dicom_data.patient_id),
                str(dicom_data.series_number),
                str(dicom_data.instance_number),
                str(dicom_data.rows),
                str(dicom_data.columns),
                str(dicom_data.spacing[0]),
                str(dicom_data.slice_thickness),
                str(dicom_data.slice_location),
                str(dicom_data.get_location()),
                dicom_data.in_plane_encoding_direction,
                str(dicom_data.image_position),
                str(dicom_data.get_value("SequenceVariant")),
                str(dicom_data.get_value("InstanceCreationTime")),
                str(dicom_data.get_value("Manufacturer")),
                str(dicom_data.get_value("ManufacturerModelName")),
                str(dicom_data.get_value("PatientAge")),
                str(dicom_data.get_value("PatientBirthDate")),
                str(dicom_data.get_value("PatientSex")),
                dicom_data.file_name.replace(".dcm", ""),
                str(dicom_data.get_value("FlipAngle")),
                str(round(dicom_data.image_orientation_patient[0], 2)),
                str(round(dicom_data.image_orientation_patient[1], 2)),
                str(round(dicom_data.image_orientation_patient[2], 2)),
                str(round(dicom_data.image_orientation_patient[3], 2)),
                str(round(dicom_data.image_orientation_patient[4], 2)),
                str(round(dicom_data.image_orientation_patient[5], 2))
            ])