我正在嘗試按順序?qū)?3D 圖像并將識別標(biāo)簽鏈接到每個文件夾。目前我有代碼可以為 dicom 文件執(zhí)行此操作,但我也在嘗試使用 .tiff 圖像文件:data_dir = "\\tiff\\"patients = os.listdir(data_dir)labels_df = pd.read_csv('\\tiff_labels.csv', index_col = 0)IMG_PX_SIZE = 50HM_SLICES = 20def process_data(patient, labels_df, image_px_size = 50, hm_slices = 20, visualize = False): label = labels_df.at[patient, 'label'] path = data_dir + patient slices = [pydicom.read_file(path + '/' + s, force = True) for s in os.listdir(path)] slices.sort(key = lambda x: int(x.ImagePositionPatient[2]))我嘗試將第 9 行和第 10 行更改為:slices = [cv2.imread(path + '/' + s) for s in os.listdir(path)]slices.sort()我發(fā)現(xiàn)的問題在第 10 行:key = lambda x: int(x.ImagePositionPatient[2])。ImagePositionPatient 是 dicoms 獨(dú)有的東西,無法找到用另一種方式對圖像進(jìn)行排序的方法。我收到錯誤:Traceback (most recent call last): File "preprocessing_data.py", line 83, in <module> image_data, label = process_data(patient, labels_df, image_px_size = IMG_PX_SIZE, hm_slices = HM_SLICES) File "preprocessing_data.py", line 28, in process_data slices.sort()ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
2 回答

冉冉說
TA貢獻(xiàn)1877條經(jīng)驗 獲得超1個贊
我看到的問題是
slices = [cv2.imread(path + '/' + s) for s in os.listdir(path)]
創(chuàng)建一個僅包含圖像數(shù)據(jù)的數(shù)組列表,沒有可用于排序的有意義的信息。如果你想按文件名排序,你可以這樣做:
slices = [[s,cv2.imread(path + '/' + s)] for s in os.listdir(path)]
它為您讀取的每個文件創(chuàng)建一個包含 2 個條目的列表,第一個條目是文件名,第二個條目是圖像數(shù)據(jù)。然后slices.sort()
開箱即用。但是您必須索引這兩個列表才能獲取圖像數(shù)據(jù)。例如,要訪問第 5 個圖像,將是slices[5][1]
.
添加回答
舉報
0/150
提交
取消