有关医学图像数据集读取的相关操作

一:os.listdir(path)

path="C:/Users/Administrator/source/repos/pytest/pytest"
file_list=os.listdir(path)             #获取pytest文件夹下所有文件名和文件夹名的列表,按字母顺讯排列
for name in file_list:
    print(name)


-----------------output
mnist_eval.py                          #文件名
mnist_inference.py                       
mnist_train.py
model                                  #文件夹名
module1.py
pytest.pyproj
test2.py
__pycache__

二:读取csv文件

import tensorflow as tf
import csv

label_path="D:/dl/cancer_data/train_label.csv"       #文件名由于个人设置的编码方式导致用中文文件名会报错
with open(label_path,'r') as label_file:             #‘r’:以读取方式打开
    label_list=csv.reader(label_file) #读取csv文件
    print(label_list)                                #output:<_csv.reader object at 
                                                     0x000001FEEE287590>,instead of a list
#打开方式等价于:label_list=csv.reader(open(label_path,'r')
    cnt=0
    for label in label_list:
        print(label)                                 #不能用label_list[i]的方式访问,输出是一维数组的形式
        cnt+=1
        if cnt>=10:break

----------------------output
<_csv.reader object at 0x000001FEEE287590>
['id\tret']                                #标签集的第一行并不是数据,使用时要去掉
['0013EDC2-8D7A-4A41-AEB5-D3BB592306D2\t1']#字符串形式,\t制表符,1 or 0 是标签
['0030CBD1-2472-42C4-8CE4-E01A4E8E2F09\t1']
['0036DF08-EEEC-467C-8CF1-5A54E0B13CE8\t1']
['003D2553-266F-47E3-A420-F5B8F95217A7\t0']
['0072E2C1-C395-409B-8078-365DD5C0513E\t0']
['00C1E256-E3F7-431D-BCB6-DD1EF09E7DFE\t0']
['00C79BB9-C2BC-4BF2-A8E6-5C3866DCBF3D\t0']
['00CAD9C8-D90B-43C4-9964-79CAE6493DA7\t1']
['00D5ADEF-A62D-4DF3-A9C0-31000C826023\t0']

三:读取pydicom文件

import tensorflow as tf
import pydicom
import matplotlib.pyplot as plt


img=pydicom.read_file("D:\\dl\cancer_data\\trainingData\\00C1E256-E3F7-431D-BCB6-DD1EF09E7DFE\\\
fd0beb35-2d21-4bb8-a9d8-91effa9b5e6d_00001.dcm")      #读取图像
img_pixel=img.pixel_array                             #获得图像的像素值

print(img_pixel.shape)  
print(img_pixel)
plt.imshow(img_pixel)                                 #对要显示的图像进行处理,imshow(X, cmap=None),
                                                      #cmap是颜色图谱(colormap),默认绘制为RGB颜色空
                                                       间,cmap=plt.cm.bone:黑白色
plt.show()                                            #显示图像

--------------------output
(512, 512)
[[  21   26   27 ...   22   23   26]
 [  20   19   24 ...   22   25   25]
 [  22   17   20 ...   23   25   23]
 ...
 [1236 1307 1316 ...   93   88   87]
 [1107 1249 1306 ...  100   95   88]
 [ 890 1098 1222 ...   91   90   85]]

猜你喜欢

转载自blog.csdn.net/qq_41033241/article/details/87874825
今日推荐