-
Notifications
You must be signed in to change notification settings - Fork 1
/
GrabCut_Segmentation_skin_lesion.py
202 lines (163 loc) · 8.39 KB
/
GrabCut_Segmentation_skin_lesion.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""
Created on Thu May 24 01:15:31 2018
@author: Fakrul-IslamTUSHAR
"""
# =============================================================================
# Instruction
# =============================================================================
"""
*Put the code to the folder of images
*Give the destination folder path where you want to save the segemntaed images.
"""
# =============================================================================
# Import Libraries
# =============================================================================
import cv2
import numpy as np
from glob import glob
import os
from matplotlib import pyplot as plt
# =============================================================================
# Get the image
# =============================================================================
#Getting all the images in the folder
for im in glob('*.jpg'):
img = cv2.imread(im,-1)
filename_w_ext = os.path.basename(im)
filename, file_extension = os.path.splitext(filename_w_ext)
cv2.namedWindow(filename, cv2.WINDOW_NORMAL)
#cv2.imshow(filename,img)
print filename
#cv2.waitKey(0)
median = cv2.medianBlur(img,5) # Apply Median filter
# =============================================================================
# img = cv2.imread(org_imgName,-1)
Z = median.reshape((-1,3))
# convert to np.float32
Z = np.float32(Z)
# define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 8
ret,label,center=cv2.kmeans(Z,K,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
# Now convert back into uint8, and make original image
center = np.uint8(center)
res = center[label.flatten()]
kmeans_img = res.reshape((img.shape))
cv2.namedWindow("Kmean_img", cv2.WINDOW_NORMAL)
cv2.imshow('Kmean_img',kmeans_img)
#cv2.waitKey(0)
output_path = 'C:\SKINLESIONSEGMENTATION\kmeans' #Give path where you want to save the kmeans images
cv2.imwrite(os.path.join(output_path, filename+'_kmeans' +'.png'),kmeans_img)
cv2.destroyAllWindows()
# =============================================================================
# =============================================================================
# Adaptive histogram equalization
# =============================================================================
clahe = cv2.createCLAHE(clipLimit=3., tileGridSize=(8,8))
hsv = cv2.cvtColor(kmeans_img, cv2.COLOR_BGR2HSV)# convert from BGR to HSV color space
output_path = 'C:\SKINLESIONSEGMENTATION\hsv_img' # Give the path where you want to save HSV images
cv2.imwrite(os.path.join(output_path, filename+'.png'),hsv)
cv2.destroyAllWindows()
h, s, v = cv2.split(hsv) # split on 3 different channels
#apply CLAHE to the L-channel
h1 = clahe.apply(h)
s1 = clahe.apply(s)
v1 = clahe.apply(v)
lab = cv2.merge((h1,s1,v1)) # merge channels
output_path = 'C:\SKINLESIONSEGMENTATION\enhanced_hsv_img' # Destination folder for save Enhances HSV images
cv2.imwrite(os.path.join(output_path, filename+'_AHE' +'.png'),lab)
cv2.destroyAllWindows()
Enhance_img= cv2.cvtColor(lab, cv2.COLOR_LAB2BGR) # convert from LAB to BGR
output_path = 'C:\SKINLESIONSEGMENTATION\enhanced_bgr_image' # Destination folder for save Enhances BGR images
cv2.imwrite(os.path.join(output_path, filename+'_AHE' +'.png'),Enhance_img)
cv2.destroyAllWindows()
# =============================================================================
# making the mask for grabcut
# =============================================================================
hsv = cv2.cvtColor(Enhance_img, cv2.COLOR_BGR2HSV)
lower_green = np.array([50,100,100])
upper_green = np.array([100,255,255])
mask_g = cv2.inRange(hsv, lower_green, upper_green)
output_path = 'C:\SKINLESIONSEGMENTATION\Green_mask'
cv2.imwrite(os.path.join(output_path, filename+'.png'),mask_g)
ret,inv_mask = cv2.threshold(mask_g,127,255,cv2.THRESH_BINARY_INV)
output_path = 'C:\SKINLESIONSEGMENTATION\Inverse_Green_mask'
cv2.imwrite(os.path.join(output_path, filename+'.png'),inv_mask)
res = cv2.bitwise_and(img,img, mask= mask_g)
output_path = 'C:\SKINLESIONSEGMENTATION\rio'
cv2.imwrite(os.path.join(output_path, filename+'.png'),res)
mask = np.zeros(img.shape[:2],np.uint8)
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)
if (np.sum(inv_mask[:])<80039400):
newmask = inv_mask
# wherever it is marked white (sure foreground), change mask=1
# wherever it is marked black (sure background), change mask=0
mask[newmask == 0] = 0
mask[newmask == 255] = 1
dim= cv2.grabCut(img,mask,None,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_MASK)
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
GrabCut_img = img*mask2[:,:,np.newaxis]
output_path = 'C:\SKINLESIONSEGMENTATION\grabcut_hsv'
cv2.imwrite(os.path.join(output_path, filename+'_GrabCut' +'.png'),GrabCut_img)
cv2.destroyAllWindows()
else:
# =============================================================================
# GrabCut
# =============================================================================
# =============================================================================
# =============================================================================
#initializing the Ractangle based on the image dimention
s = (img.shape[0] / 10, img.shape[1] / 10)
rect = (s[0], s[1], img.shape[0] - (3/10) * s[0], img.shape[1] - s[1])
cv2.grabCut(Enhance_img,mask,rect,bgdModel,fgdModel,10,cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
GrabCut_img= img*mask2[:,:,np.newaxis]
plt.imshow(GrabCut_img)
plt.colorbar()
plt.show()
output_path = 'C:\SKINLESIONSEGMENTATION\grabcut_hsv'
cv2.imwrite(os.path.join(output_path, filename+'_GrabCut' +'.png'),GrabCut_img)
cv2.destroyAllWindows()
# =============================================================================
# Binarization
# =============================================================================
imgmask = cv2.medianBlur(GrabCut_img,5)
ret,Segmented_mask = cv2.threshold(imgmask,0,255,cv2.THRESH_BINARY)
output_path = 'C:\SKINLESIONSEGMENTATION\grabcut_hsv_bw'
cv2.imwrite(os.path.join(output_path, filename+'.png'),Segmented_mask)
cv2.destroyAllWindows()
# =============================================================================
# 2nd GRABCUT
# =============================================================================
if (np.sum(inv_mask[:])<80039400):
newmask = inv_mask
# wherever it is marked white (sure foreground), change mask=1
# wherever it is marked black (sure background), change mask=0
mask[newmask == 0] = 0
mask[newmask == 255] = 1
dim2= cv2.grabCut(lab,mask,None,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_MASK)
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
GrabCut_img2 = img*mask2[:,:,np.newaxis]
output_path = 'C:\SKINLESIONSEGMENTATION\grabcut_bgr'
cv2.imwrite(os.path.join(output_path, filename+'_GrabCut' +'.png'),GrabCut_img2)
cv2.destroyAllWindows()
else:
cv2.grabCut(lab,mask,rect,bgdModel,fgdModel,10,cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
GrabCut_img2= img*mask2[:,:,np.newaxis]
output_path = 'C:\SKINLESIONSEGMENTATION\grabcut_bgr'
cv2.imwrite(os.path.join(output_path, filename+'_GrabCut' +'.png'),GrabCut_img2)
cv2.destroyAllWindows()
# =============================================================================
# Binarization
# =============================================================================
imgmask2 = cv2.medianBlur(GrabCut_img2,5)
ret,Segmented_mask2 = cv2.threshold(imgmask2,0,255,cv2.THRESH_BINARY)
output_path = 'C:\SKINLESIONSEGMENTATION\grabcut_bgr_bw'
cv2.imwrite(os.path.join(output_path, filename+'.png'),Segmented_mask2)
cv2.destroyAllWindows()
plt.imshow(GrabCut_img2)
plt.colorbar()
plt.show()
# =============================================================================