Diff of /features/read_csv.py [000000] .. [f77492]

Switch to unified view

a b/features/read_csv.py
1
import csv 
2
import platform 
3
import numpy as np 
4
import matplotlib.pyplot as plt 
5
import os
6
import otsu 
7
from FeatureExtract import *
8
from skimage.feature import hog, haar_like_feature,local_binary_pattern,multiblock_lbp,daisy
9
import pysift
10
import time 
11
12
def readCSV(filename):
13
    lines = []
14
    with open(filename, "r") as f:
15
        csvreader = csv.reader(f)
16
        for line in csvreader:
17
            lines.append(line)
18
    return lines 
19
20
ori_path = 'C:/Users/RL/Desktop/可解释性的特征学习分类/nodules/ori_hu/'
21
22
def get_feature_name(temp, index, value):
23
    name = []
24
    for t in temp:
25
        if float(t[index]) == value:
26
            if len(t[1]) == 1:
27
                name.append('LIDC-IDRI-000' + t[1] + '_' + t[3] + '_' + t[2] + '.npy')
28
            elif len(t[1]) == 2:
29
                name.append('LIDC-IDRI-00' + t[1] + '_' + t[3] + '_' + t[2] + '.npy')
30
            elif len(t[1]) == 3:
31
                name.append('LIDC-IDRI-0' + t[1] + '_' + t[3] + '_' + t[2] + '.npy')
32
            else:
33
                name.append('LIDC-IDRI-' + t[1] + '_' + t[3] + '_' + t[2] + '.npy')
34
    return name 
35
36
def read_name():
37
    labelCSV = readCSV('C:/Users/RL/Desktop/可解释性的特征学习分类/特征图片/malignancy.csv')
38
    Max = []
39
    Min = []
40
    temp = [labelCSV[i] for i in range(len(labelCSV))]
41
    final = np.array([[float(temp[i][j + 21]) for j in range(9)]  for i in range(len(temp)) if '0' not in temp[i]])
42
    Max = final.max(axis=0)
43
    Min = final.min(axis=0)
44
    # prob_map = [[final[i][j] / (Max[j] + Min[j] + 1) if j != 8 else final[i][j] >= 3.5 for j in range(final.shape[1])] for i in range(final.shape[0])]
45
    # 29 28 27毛刺 26分叶 25 24 23 22内在的 21
46
    # print(prob_map[0])  
47
    name_maoci= get_feature_name(temp, 27, Max[6])
48
    name_fenye = get_feature_name(temp, 26, Max[5])
49
    name_solid = get_feature_name(temp, 28, Max[7])
50
    name_non_solid = get_feature_name(temp, 28, 3)
51
    name_moboli = get_feature_name(temp, 28, 1)
52
    # with open('C:/Users/RL/Desktop/可解释性的特征学习分类/特征图片/name.txt',"w") as f:
53
    #     f.write("*" * 10 + "maoci:"+ '\n')
54
    #     for name in name_maoci:
55
    #         f.write(name + '\n')
56
    #     f.write("*" * 10 + "fenye:"+ '\n')
57
    #     for name in name_fenye:
58
    #         f.write(name+ '\n')
59
    #     f.write("*" * 10 + "shixing:"+ '\n')
60
    #     for name in name_solid:
61
    #         f.write(name+ '\n')
62
    #     f.write("*" * 10 + "yashixing:"+ '\n')
63
    #     for name in name_non_solid:
64
    #         f.write(name+ '\n')
65
    #     f.write("*" * 10 + "moboli:"+ '\n')
66
    #     for name in name_moboli:
67
    #         f.write(name+ '\n')
68
    return name_maoci, name_fenye, name_solid, name_non_solid, name_moboli
69
70
def read_lidc(filename):
71
    image = np.load(ori_path + filename)
72
    return image 
73
 
74
def image_feature_extract(image):
75
    features = [image]
76
    # otsu_image = otsu.helper(image.copy())
77
    features.append(otsu.helper(image.copy()))
78
    features.append(gabor(image.copy()))
79
80
81
    # 添加新的方式
82
    # lbp = local_binary_pattern(image.copy(), 3, 3, method='var')
83
    # features.append(otsu._otsu(lbp))
84
    # features.append(edge_detection(image.copy()))
85
    _, hog_image = hog(gabor(image.copy()), orientations=9, pixels_per_cell=(8, 8),cells_per_block=(2, 2), visualize=True, multichannel=False)
86
    features.append(hog_image) 
87
88
89
    features.append(np.transpose(super_pixel(image.copy()),(2,0,1))[0])
90
    features.append(local_binary_pattern(image.copy(), 4, 4, method='var')) # 会产生无穷大或者是无穷小
91
    fd, hog_image = hog(image.copy(), orientations=9, pixels_per_cell=(4, 4),cells_per_block=(2, 2), visualize=True, multichannel=False)
92
93
    _, descs_img = daisy(image.copy(), step=180, radius=58, rings = 2,  histograms=10,orientations=8,visualize=True)
94
    features.append(hog_image)
95
    features.append(np.transpose(descs_img.copy(),(2,0,1))[0])
96
    features.append(descs_img)
97
    kps, _ = pysift.computeKeypointsAndDescriptors(image.copy())
98
    x = []
99
    y = []
100
    for kp in kps:
101
        x.append(kp.pt[0])
102
        y.append(kp.pt[1])
103
    return features, x, y
104
105
# 接下里就是 统计每个属性的图像特征输出
106
if __name__ == "__main__":
107
    a1, a2, a3, a4, a5 = read_name()
108
    ori_image = []
109
    # 不清楚normalize的影响
110
    ori_image.append([normalization(truncate_hu(read_lidc('LIDC-IDRI-0007_2_3000631.npy')))])# 毛刺
111
    ori_image.append([normalization(truncate_hu(read_lidc('LIDC-IDRI-0060_1_3000575.npy')))])# 分叶
112
    ori_image.append([normalization(truncate_hu(read_lidc('LIDC-IDRI-0003_4_3000611.npy')))]) # 磨玻璃
113
    ori_image.append([normalization(truncate_hu(read_lidc('LIDC-IDRI-0003_2_3000611.npy')))]) # 实性
114
    ori_image.append([normalization(truncate_hu(read_lidc('LIDC-IDRI-0008_1_3000549.npy')))]) # 亚实性 
115
    ori_image.append([normalization(truncate_hu(read_lidc('LIDC-IDRI-0132_1_5418.npy')))]) # 空洞
116
    X = []
117
    Y = []
118
    for i in range(6):
119
        features, x, y = image_feature_extract(ori_image[i][0])
120
        for feature in features:
121
            ori_image[i].append(feature)
122
        # print(len(ori_image[i]))
123
        X.append(x)
124
        Y.append(y)
125
    name = ["origial","sift-key-point","otsu","gabor","new","super-pixel","lbp","hog","daisy-three-dim","daisy-gray"]
126
    plt.figure()
127
    # 对实性结节 对亚实性结节 对分叶 对毛玻璃 对空洞 对毛刺
128
    numRows = 6
129
    numCols = 10
130
    font2 = {'family' : 'Times New Roman',
131
            'weight' : 'normal',
132
            'size'   : 7,
133
            }
134
    for i in range(numRows):
135
        for j in range(numCols):
136
            ax = plt.subplot(numRows,numCols,1 + i * numCols + j)
137
            if i == 0:
138
                ax.set_title(name[j],font2)
139
            if j != numCols - 1:
140
                plt.imshow(ori_image[i][j], cmap="gray")
141
            else:
142
                plt.imshow(ori_image[i][j])
143
            if j == 1:
144
                plt.scatter(X[i], Y[i], color='red', s=3, alpha=0.5)
145
            plt.xticks([])
146
            plt.yticks([])
147
    # plt.savefig("C:/Users/RL/Desktop/可解释性的特征学习分类/特征图片/pictures/test" + str(time.time()) + ".png",dpi=1500)
148
    plt.show()