Diff of /TumorDetection.py [000000] .. [03d98b]

Switch to unified view

a b/TumorDetection.py
1
from sklearn.model_selection import train_test_split
2
from sklearn.neighbors import KNeighborsClassifier
3
import pandas as pd
4
from functions import *
5
from skimage import morphology, measure, filters
6
from skimage.measure import label, regionprops
7
from sklearn.cluster import KMeans
8
import matplotlib.pyplot as pyplot
9
from skimage.feature import greycomatrix, greycoprops
10
from sklearn.neural_network import MLPClassifier
11
from sklearn import cross_validation
12
import pickle
13
14
path = "featuresdicom.xlsx"
15
INPUT_SCAN_FOLDER = "G:\\final\\dicom final\\database\\malignant\\LIDC-IDRI-0072\\"
16
17
fileDICOMFeatureList = pd.read_excel(path, header=None)
18
19
matrixFeatures = np.array((fileDICOMFeatureList.as_matrix())[1:, :])
20
yMatrixFeatures = matrixFeatures[:, 7]
21
xMatrifFeatures = matrixFeatures[:, 0:7]
22
X_train, X_test, y_train, y_test = train_test_split(xMatrifFeatures, yMatrixFeatures, test_size=0.2, random_state=10)
23
y_train = y_train.astype('int')
24
y_test = y_test.astype('int')
25
26
# clf = MLPClassifier(hidden_layer_sizes=1000,solver='lbfgs')
27
#
28
# clf.fit(X_train, y_train)
29
modelFileMLP = 'mlpmodel.sav'
30
# pickle.dump(clf,open(filename,'wb'))
31
32
modelMLP = pickle.load(open(modelFileMLP, 'rb'))
33
# print(clf)
34
MLPscore = modelMLP.score(X_train, y_train)
35
MLPtest = modelMLP.predict(X_test)
36
37
print('MLP training=', MLPscore * 100)
38
print("MLP testing accuracy=", np.mean(MLPtest == y_test) * 100)
39
KNNmodel = KNeighborsClassifier()
40
kfold = cross_validation.KFold(n=len(X_train), n_folds=10, random_state=10)
41
cv_results = cross_validation.cross_val_score(KNNmodel, X_train, y_train, cv=kfold, scoring='accuracy')
42
message = "%s: %f " % ("KNN cross validation accuracy", cv_results.mean())
43
print(message)
44
K_value = 3
45
neigh = KNeighborsClassifier(n_neighbors=K_value, weights='uniform', algorithm='auto')
46
neigh.fit(X_train, y_train)
47
KNNpredictValue = neigh.predict(X_test)
48
print("KNN testing accuracy=", np.mean(KNNpredictValue == y_test) * 100)
49
50
listProperties = ['contrast', 'dissimilarity', 'homogeneity', 'ASM', 'energy']
51
listFeatures = ['contrast', 'dissimilarity', 'homogeneity', 'ASM', 'energy', 'mean', 'stddev', 'label']
52
properties = np.zeros(6)
53
54
# glcmMatrix = []
55
final = []
56
arrayOriginalImages = dicomRead(INPUT_SCAN_FOLDER)
57
test3D(ConstPixelDims = arrayOriginalImages)
58
tumorArea = []
59
arrayTumorContour = []
60
for z in range(125, 180):
61
62
    tempImageSlice = arrayOriginalImages[z][:][:]
63
    # img=img.pixel_array
64
    #imgg = tempImageSlice
65
    tempImageMask = segment(tempImageSlice)
66
    tempImageMask = np.where(tempImageMask == 255, 1, 0)
67
    # pyplot.imshow(tempImageMask, cmap='gray')
68
    # pyplot.show()
69
    tempImageConvMask = tempImageMask * tempImageSlice
70
    tempImageConvMask = (tempImageConvMask / 256).astype('uint8')
71
    ImageConvMask = tempImageConvMask
72
    tempImageSliceMean = arrayOriginalImages[z][:][:].mean()
73
    tempImageSliceStdDev = arrayOriginalImages[z][:][:].std()
74
    glcmMatrix = (greycomatrix(tempImageConvMask, [1], [0], levels=2 ** 8))
75
76
    for j in range(0, len(listProperties)):
77
        properties[j] = (greycoprops(glcmMatrix, prop=listProperties[j]))
78
79
    arrayFeatureValues = np.array([[properties[0], properties[1], properties[2], properties[3], properties[4], tempImageSliceMean, tempImageSliceStdDev]])
80
81
    # pyplot.imshow(imgg,cmap='gray')
82
    # pyplot.show()
83
    # df = pd.DataFrame(final, columns=listFeatures)
84
85
    y_pred = neigh.predict(arrayFeatureValues)
86
    tempSegmentedImage = tempImageConvMask
87
    print(y_pred)
88
    if (y_pred == 2 or y_pred == 1):
89
        segmented1 = tempSegmentedImage
90
91
        tempSegmentedImageMean = np.mean(tempSegmentedImage)
92
        tempSegmentedImageStdDev = np.std(tempSegmentedImage)
93
        segmentedImage = tempSegmentedImage - tempSegmentedImageMean
94
        segmentedImage = tempSegmentedImage / (tempSegmentedImageStdDev + 0.00001)
95
        # pyplot.imshow(imgg,cmap='gray')
96
        # pyplot.show()
97
        # hist = pyplot.hist(segmented.flatten(), bins=200)
98
99
        ROI = segmentedImage[100:400, 100:400]
100
        ROImean = np.mean(ROI)
101
        ROImaxv = np.max(tempSegmentedImage)
102
        ROIminv = np.min(tempSegmentedImage)
103
        tempSegmentedImage[tempSegmentedImage == ROImaxv] = tempSegmentedImageMean
104
        tempSegmentedImage[tempSegmentedImage == ROIminv] = tempSegmentedImageMean
105
        ROIkmeans = KMeans(n_clusters=3).fit(np.reshape(ROI, [np.prod(ROI.shape), 1]))
106
        ROIkmeanscenters = sorted(ROIkmeans.cluster_centers_.flatten())
107
        ROIkmeansthreshold = np.mean(ROIkmeanscenters)
108
        threshROIImg = np.where(segmentedImage >= ROIkmeansthreshold, 1.0, 0.0)
109
        threshROIImg = morphology.erosion(threshROIImg, np.ones([9, 9]))
110
        threshROIImg = morphology.dilation(threshROIImg, np.ones([9, 9]))
111
        # pyplot.imshow(threshROIImg, cmap='gray')
112
        # pyplot.show()
113
        tumorContours = measure.find_contours(threshROIImg, 0.8)
114
115
        # Display the image and plot all contours found
116
        tempTumorArea = []
117
        if (tumorContours):
118
            contourLabels = label(threshROIImg)
119
            contourRegions = regionprops(contourLabels, threshROIImg)
120
            arrayTumorContour.append(tumorContours)
121
            tempTumorArea = (tempTumorArea.append(contourRegions[i].area) for i in range(len(contourRegions)))
122
            tempTumorArea = (contourRegions[0].area)
123
            tumorArea.append(tempTumorArea)
124
            fig, ax = pyplot.subplots()
125
            ax.imshow(tempImageSlice, interpolation='nearest', cmap=pyplot.cm.gray)
126
127
            for n, singleContour in enumerate(tumorContours):
128
                ax.plot(singleContour[:, 1], singleContour[:, 0], linewidth=2)
129
130
            ax.axis('image')
131
            ax.set_xticks([])
132
            ax.set_yticks([])
133
            # threshROIImg = threshROIImg * imgg
134
            pyplot.imshow(tempImageSlice, cmap='gray')
135
            pyplot.show()
136
137
138
            if (y_pred == 1):
139
                print(str(z) + ' Image is tumorous')
140
                print(tempTumorArea.max())
141
                # if(tempTumorArea<Put area here):
142
                # elif(areaa<Put area here):
143
                # elif(areaa < Put area here):
144
            elif (y_pred == 2):
145
                print(str(z) + ' Image is tumorous')
146
                print(tempTumorArea.max())
147
                # if(areaa<Put area here):
148
                # elif(areaa<Put area here):
149
                # elif(areaa < Put area here):
150
        else:
151
            print(str(z) + ' Image is non tumorous')
152
    else:
153
        print(str(z)+' Image is non tumorous')
154
155
if (len(tumorArea)):
156
    volume = 0;
157
    for i in range(0, len(tumorArea) - 1):
158
        if (i == 0):
159
            volume = volume + (((tumorArea[i] + 0) * 1.25) / 2)
160
        else:
161
            volume = volume + (((tumorArea[i] + tumorArea[i - 1]) * 1.25) / 2)
162
    print(volume)
163
164