-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMAIN_Pipeline_WatershedAndColourSegmentation.py
224 lines (161 loc) · 7.77 KB
/
MAIN_Pipeline_WatershedAndColourSegmentation.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 22 15:24:32 2021
@author: genevieve.hayes
Main Watershed Pipeline
"""
# In[0]: IMPORT
# Import the necessary packages
from __future__ import print_function
from PIL import Image
from skimage.feature import peak_local_max
from skimage.morphology import watershed
from scipy import ndimage
import argparse
import imutils
import cv2
import numpy as np
import random as rng
import matplotlib.pyplot as plt
import matplotlib
# Import functions
from MAIN_Functions import *
# In[1]: LOAD IMAGE
# Set SAVEIMG to 1 to save images
SAVEIMG = 1
# high res parameters
min_radius_kn = 2
max_radius_kn = 100
min_radius_kp = 3
max_radius_kp = 18
## low res parameters
#min_radius_kn = 3
#max_radius_kn = 16
#min_radius_kp = 3
#max_radius_kp = 18
# SET INPUT FILEPATH
#High res
filepath = "/Users/genevieve.hayes/Desktop/ENPH 455 Thesis/Sample 6B/Bladder 1 TMA - QATA3_sample6B.tiff"
#filepath = "/Users/genevieve.hayes/Desktop/ENPH 455 Thesis/Bladder 1 TMA - Sample 6D.tiff"
#filepath = "/Users/genevieve.hayes/Desktop/ENPH 455 Thesis/Bladder 1 TMA - Sample 6G.tiff"
#Low res
#filepath = "/Users/genevieve.hayes/Desktop/ENPH 455 Thesis/Sample6G.tiff"
#filepath = "/Users/genevieve.hayes/Desktop/ENPH 455 Thesis/Sample6D.tiff"
#filepath = "/Users/genevieve.hayes/Desktop/ENPH 455 Thesis/Sample6B.tiff"
#filepath = "/Users/genevieve.hayes/Desktop/ENPH 455 Thesis/Bladder 1 TMA - QATA3_FULL2 copy.tiff"
BGR_fullimg = load_BGR_img(filepath)
cv2.imshow('Full image', BGR_fullimg)
cv2.waitKey(1)
Nx, Ny, w = np.shape(BGR_fullimg)
x_iterations = np.floor(Nx/500)
y_iterations = np.floor(Ny/500)
TOTAL_kngn = np.array([])
TOTAL_kngp = np.array([])
TOTAL_kpgn = np.array([])
TOTAL_kpgp = np.array([])
#In[2]: CROP IMAGE
#for ind_x in range(0, int(x_iterations)):
for ind_x in [0]:
#for ind_y in range(0, int(y_iterations)):
for ind_y in [0]:
BGR_cropimg = BGR_fullimg[2500:3000,2500:3000]
#BGR_cropimg = BGR_fullimg[ind_x*500:(ind_x*500+500),ind_y*500:(ind_y*500+500)]
#BGR_cropimg = BGR_fullimg[4*500:(4*500+500),2*500:(2*500+500)]
BGR_cropimg_duplicate = BGR_cropimg
cv2.imshow('Original Image', BGR_cropimg)
cv2.waitKey(1)
print("------------------------------------------------- ")
print("\nImage Coordinates: ",ind_x*500,ind_y*500)
#In[3]: IDENTIFY RED REGION AND FILL IN HOLES
mask_red, mask_nored = create_red_mask(BGR_cropimg)
mask_closedred = fill_mask_holes(mask_red, np.array([0,0])) #if kernel = 0, use default
#In[4]: REMOVE FILLED RED REGION FROM IMAGE
image_nored = removeRedRegionFromImage(BGR_cropimg, mask_closedred)
cv2.imshow('No red white region',image_nored)
cv2.waitKey(1)
cv2.imshow('Inverted image',255-image_nored)
cv2.waitKey(1)
#Plot pyramid mean shift filtering
spatial_window_radius = 19
color_window_radius = 20
shifted = cv2.pyrMeanShiftFiltering(255-image_nored, sp = spatial_window_radius, sr = color_window_radius)
cv2.imshow('PMS Image',shifted)
cv2.waitKey(1)
#In[5]: APPLY WATERSHED SEGMENTATION TO IMAGE WIHOUT RED REGION + IDENTIFY THE BROWN RANGE AND BLUE RANGE CELLS (KRTN5-) + REMOVE NOISE
markers, thresh, dist, gray, markersimg = preprocessing_for_watershed(image_nored)
#cv2.imshow("Pyramid Mean Shifted Image", shifted)
#cv2.waitKey()
BGR_cropimg_watershed, total, brown, blue = watershedSegmentation_whiteRegion(BGR_cropimg, markers, thresh, dist, gray, markersimg, min_radius_kn, max_radius_kn)
cv2.imshow("Grayscale Image", gray)
cv2.waitKey(1)
thresholdim = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
cv2.imshow("Thresholded Image", thresholdim)
cv2.waitKey(1)
cv2.imshow("Dist map image", dist)
cv2.waitKey(1)
cv2.imshow('Markers', markersimg)
cv2.waitKey()
# show the output watershed image
cv2.imshow("White Region Watershed Segmented Image", BGR_cropimg_watershed)
cv2.waitKey(1)
print('\n KRT5 -')
print('Number of segments larger than radius 6:', total)
print('Number of brown segments larger than radius 6:', brown)
print('Number of blue segments larger than radius 6:', blue)
#In[6]: INVERT MASK TO GET ONLY RED REGION
mask_closed_nored = cv2.bitwise_not(mask_closedred)
image_justred = removeRedRegionFromImage(BGR_cropimg, mask_closed_nored)
#cv2.imshow('Red region', image_justred)
#cv2.waitKey(1)
cv2.imshow('Closed red mask', mask_closed_nored)
cv2.waitKey(1)
#In[7]: REMOVE RED STAIN FROM RED REGION IMAGE
mask_red_noredstain, mask_red_redstain = create_light_red_mask(image_justred)
image_redregion_noredstain = removeRedRegionFromImage(image_justred, mask_red_noredstain)
cv2.imshow('No red red region', image_redregion_noredstain)
cv2.waitKey(1)
#In[8]: APPLY WATERSHED SEGMENTATION TO RED REGION WITHOUT STAIN + IDENTIFY THE BROWN RANGE AND BLUE RANGE CELLS (KRT5+) + REMOVE NOISE
markers_red, thresh_red, dist_red, gray_red, markersimg_red = preprocessing_for_watershed(image_redregion_noredstain)
BGR_cropimg_watershed_inred, total_inred, brown_inred, blue_inred = watershedSegmentation_redRegion(BGR_cropimg_duplicate, markers_red, thresh_red, dist_red, gray_red, markersimg_red, min_radius_kp, max_radius_kp)
#cv2.imshow('Duplicate of original image', BGR_cropimg_duplicate)
#cv2.waitKey(1)
# show the output watershed image
cv2.imshow("Total Watershed Segmented Image", BGR_cropimg_watershed_inred)
cv2.waitKey(1)
print('\n KRT5 +')
print('Number of segments larger than radius 6:', total_inred)
print('Number of brown segments larger than radius 6:', brown_inred)
print('Number of blue segments larger than radius 6:', blue_inred)
TOTAL_kngn = np.append(TOTAL_kngn, blue)
TOTAL_kngp = np.append(TOTAL_kngp, brown)
TOTAL_kpgp = np.append(TOTAL_kpgp, brown_inred)
TOTAL_kpgn = np.append(TOTAL_kpgn, blue_inred)
if SAVEIMG == 1:
filename_output = "Outputs_WHR6G/"+str(ind_x)+str(ind_y)+ "processed_img_section.tiff"
cv2.imwrite(filename_output, BGR_cropimg_watershed_inred)
print('- Image Saved -')
print('\nIMAGE PROCESSING COMPLETE')
print('---- ---- ---- ----')
# In[]: FINAL CLASSIFICATIONS
print('\nTOTAL KRT5-/GATA3+:', np.sum(TOTAL_kngp))
print('TOTAL KRT5+/GATA3+:', np.sum(TOTAL_kpgp))
print('TOTAL KRT5+/GATA3-:', np.sum(TOTAL_kpgn))
print('TOTAL KRT5-/GATA3-:', np.sum(TOTAL_kngn))
# In[]: BAR PLOTS
x = np.array(["Sample6G"])
y = np.array([np.sum(TOTAL_kngp),np.sum(TOTAL_kngn),np.sum(TOTAL_kpgp),np.sum(TOTAL_kpgn)])
width = 0.2
matplotlib.rc('font', serif='Helvetica Neue')
matplotlib.rc('text', usetex='false')
matplotlib.rcParams.update({'font.size': 16})
fig = matplotlib.pyplot.gcf()
fig.set_size_inches(4.5, 5.5)
#plt.bar(x,y)
p1 = plt.bar(x, y[0], width, color='g')
p2 = plt.bar(x, y[1], width, bottom=y[0], color='c')
p3 = plt.bar(x, y[2], width, bottom=y[0]+y[1], color='r')
p4 = plt.bar(x, y[3], width, bottom=y[0]+y[1]+y[2], color='b')
plt.ylabel("Number of Cells Identified")
plt.legend((p1[0], p2[0], p3[0], p4[0]), ('KRT5-/GATA3+', 'KRT5-/GATA3-', 'KRT5+/GATA3+', 'KRT5+/GATA3-'), fontsize=10, ncol=1, framealpha=0, fancybox=True,bbox_to_anchor=(1.04,1))
plt.show()