-
Notifications
You must be signed in to change notification settings - Fork 1
/
tumor_detect2.py
148 lines (114 loc) · 3.71 KB
/
tumor_detect2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import cv2
import numpy as np
import matplotlib.pyplot as plt
import imutils
from imutils import perspective
import sys
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
def nothing(x):
pass
def tumor_part(c):
area = cv2.contourArea(c)
hull = cv2.convexHull(c)
hull_area = cv2.contourArea(hull)
if hull_area!=0:
solidity = float(area)/hull_area
else:
solidity=0
# print(solidity,area)
if solidity>0.5 and area>2000:
# print(area)
return True
else:
return False
def blur_image(img):
# blur = cv2.GaussianBlur(img,(5,5),0)
# blur=cv2.bilateralFilter(img,9,75,75)
kernel = np.ones((5,5),np.float32)/25
blur = cv2.filter2D(img,-1,kernel)
return blur
def enhance(img):
clahe = cv2.createCLAHE(clipLimit=1.0, tileGridSize=(8,8))
gray = cv2.equalizeHist(img)
# gray = clahe.apply(blur)
return gray
def threshold(img,b):
ret,thresh = cv2.threshold(img,b,255,cv2.THRESH_BINARY)
# thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,5,2)
# kernel = np.ones((5,5),np.uint8)
# # erosion = cv2.erode(thresh,kernel,iterations = 1)
# # dilation = cv2.dilate(erosion,kernel,iterations = 1)
# dilation = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
# dilation = cv2.dilate(dilation,kernel,iterations = 1)
return thresh
def contours(img,org,b):
# emg2=enhance(img2)
img2 = threshold(img,b)
cnts = cv2.findContours(img2.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
# print(len(cnts))
img2 = RGB(img2)
org = RGB(org)
for (i,c) in enumerate(cnts):
if tumor_part(c):
# print("so",area,solidity)
cv2.drawContours(org, [c], -1, (1,255,11), 2)
cv2.drawContours(img2, [c], -1, (1,255,11), 2)
return (org,img2)
def RGB(img):
return cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
def k_means(img):
Z = img.reshape((-1,1))
Z = np.float32(Z)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 8
ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))
return res2
def edge_ex(img):
return cv2.Canny(img,100,200)
def show_images(gray,blur,seg,cont_org,cont_mask):
res1 = np.hstack((blur,seg))
res2 = np.hstack((cont_org,cont_mask))
res = np.vstack((res1,res2))
cv2.imwrite("Results/result_kmean.jpg",res)
cv2.imshow("image",res)
def process(img3,b):
gray = cv2.cvtColor(img3, cv2.COLOR_BGR2GRAY)
blur = blur_image(gray)
seg = k_means(blur)
cont_org, cont_mask = contours(seg,gray,b)
seg = RGB(seg)
blur = RGB(blur)
gray = RGB(gray)
show_images(gray,blur,seg,cont_org,cont_mask)
img1 = cv2.imread(f"Datasets/brain_tumor_dataset/yes/{sys.argv[1]}")
org = img1.copy()
process(img1,120)
cv2.createTrackbar('Intensity','image',130,240,nothing)
while True:
b = cv2.getTrackbarPos('Intensity','image')
process(img1,b)
img1 = cv2.imread(f"Datasets/brain_tumor_dataset/yes/{sys.argv[1]}")
k = cv2.waitKey(1) & 0xFF
if k == ord('q'):
break
# plt.subplot(121),plt.imshow(res)
# plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
# plt.subplot(122),plt.imshow(out)
# plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
# plt.suptitle(methods[0])
# plt.show()
# sol.sort(reverse=True)
# for i in range(len(sol)):
# print(*sol[i], sep=" ")
#
# plot_image = np.concatenate((img, dilation), axis=1)
# plt.imshow(cv2.cvtColor(plot_image, cv2.COLOR_BGR2RGB))
# plt.show()
# cv2.imshow("Br",img)
#
# cv2.waitKey(0)
cv2.destroyAllWindows()