Exemplo n.º 1
0
    def __getitem__(self, idx):
        imgdir = self.testImg[idx]
        img_arr = dicom2array(imgdir)        # 获取具体的图片数据,二维数据
        #  (hieght * width)
        origi_shape = np.array(img_arr.shape)
        if img_arr.shape != (256, 256):
            img_aug = cv2.resize(img_arr, (256, 256))
        else:
            img_aug = img_arr

        # 'studyUid','seriesUid','instanceUid','zindx'
        tag_list = ['0020|000d', '0020|000e', '0008|0018', '0020|0013']
        studyUid, seriesUid, instanceUid, zindx= dicom_metainfo(imgdir, tag_list)
        if self.transform is not None:
            img_aug = self.transform(img_aug)   # 在这里做transform,转为tensor等等
        # print(img_aug.shape)
        # print(img_aug)
        return origi_shape, img_aug, instanceUid, seriesUid, studyUid, zindx
def dicom2TestIdx(img_file):
    """
    input:some ill people's folder which contain the dicom img
    return:{"studyUid":001,"SOPInstaceUID":4.286.2,"img_dir":"./data/test.dic"}
    """
    img_fileName = os.listdir(img_file)
    # make the img list sort by id,like [img1.dcm,img2.dcm,img3.dcm,……]
    img_fileName.sort(key=lambda x: int(x[5:-4]))
    # split the file by dfferent seriesDescription
    # seriesNumber ,InstanceNumber, StudyInstanceUID,  other info tag :'0008|103e' '0002|0003'
    GroupElement=['0020|0011', '0020|0013', '0020|000d']
    res = []
    for i in range(1, len(img_fileName)+1):
        dicImg_path = os.path.join(img_file, img_fileName[i-1])
        # img_info have three item[]
        img_info = dicom_metainfo(dicImg_path, GroupElement)
        img_info.append(dicImg_path)
        print(img_info)
        res.append(img_info)
    return res
def get_info(trainPath, jsonPath):
    """
    :rturn: 图像的位置和标注信息
    """
    # path = "./train150.npy" if "150" in trainPath else "./valid50.npy"
    # path = "./train203.npy" if "203" in trainPath else "./valid150.npy"
    # global config
    # if config["valid"] == 1:
    #     if "round2train" in trainPath:
    #         path="./round2train_pc.npy"
    #     if "round2_valid53" in trainPath:
    #         path = "./round2_valid53_pc.npy"
    # else:
    path = "noSuchfile"
    if not os.path.exists(path):
        annotation_info = pd.DataFrame(columns=('studyUid', 'seriesUid', 'instanceUid', 'annotation'))
        # print(jsonPath)
        # print(os.getcwd())
        json_df = pd.read_json(jsonPath)
        for idx in tqdm(json_df.index):
            studyUid = json_df.loc[idx, "studyUid"]
            seriesUid = json_df.loc[idx, "data"][0]['seriesUid']
            instanceUid = json_df.loc[idx, "data"][0]['instanceUid']
            annotation = json_df.loc[idx, "data"][0]['annotation']
            row = pd.Series(
                {'studyUid': studyUid, 'seriesUid': seriesUid, 'instanceUid': instanceUid, 'annotation': annotation})
            annotation_info = annotation_info.append(row, ignore_index=True)
        dcm_paths = glob.glob(os.path.join(trainPath, "**", "**.dcm"))  # 具体的图片路径
        # print(dcm_paths)
        # 'studyUid','seriesUid','instanceUid'
        tag_list = ['0020|000d', '0020|000e', '0008|0018']
        # ["检查实例号:唯一标记不同检查的号码.", "序列实例号:唯一标记不同序列的号码.", "SOP实例"]
        dcm_info = pd.DataFrame(columns=('dcmPath', 'studyUid', 'seriesUid', 'instanceUid'))
        for dcm_path in dcm_paths:
            try:
                studyUid, seriesUid, instanceUid = dicom_metainfo(dcm_path, tag_list)
                row = pd.Series(
                    {'dcmPath': dcm_path, 'studyUid': studyUid, 'seriesUid': seriesUid, 'instanceUid': instanceUid})

                # print("try: ", dcm_path)
                dcm_info = dcm_info.append(row, ignore_index=True)
            except:
                # print("except: ", dcm_path)
                continue
        result = pd.merge(annotation_info, dcm_info, on=['studyUid', 'seriesUid', 'instanceUid'])
        # result = result.set_index('dcmPath')['annotation']  # 然后把index设置为路径,值设置为annotation
        result = result[["dcmPath", "annotation"]].values
        idx_wrong = []
        print("result len:",len(result))
        for idxx in range(len(result)):
            annotation = result[idxx][1]  # 获取图片的标签
            if len(annotation[0]['data']['point']) == 11:
                continue
            else:
                idx_wrong.append(idxx)
        result_new = np.delete(result, idx_wrong, 0)
        print("result_new len:", len(result_new))
        #delete some row which label wrong
        np.save(path, result_new)
    else:
        result_new = np.load(path, allow_pickle=True)
    return result_new