Spaces:
Build error
Build error
Commit
·
e6770c4
1
Parent(s):
5d81ee1
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from matplotlib import pyplot as plt
|
| 5 |
+
from scipy import ndimage
|
| 6 |
+
from skimage import measure, color, io
|
| 7 |
+
from tensorflow.keras.preprocessing import image
|
| 8 |
+
from scipy import ndimage
|
| 9 |
+
|
| 10 |
+
#Function that predicts on only 1 sample
|
| 11 |
+
def predict_sample(image):
|
| 12 |
+
prediction = model.predict(image[tf.newaxis, ...])
|
| 13 |
+
prediction[prediction > 0.5 ] = 1
|
| 14 |
+
prediction[prediction !=1] = 0
|
| 15 |
+
result = prediction[0]*255
|
| 16 |
+
return result
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def create_input_image(data, visualize=False):
|
| 22 |
+
#Initialize input matrix
|
| 23 |
+
input = np.ones((256,256))
|
| 24 |
+
|
| 25 |
+
#Fill matrix with data point values
|
| 26 |
+
for i in range(0,len(data)):
|
| 27 |
+
if math.floor(data[i][0]) < 256 and math.floor(data[i][1]) < 256:
|
| 28 |
+
input[math.floor(data[i][0])][math.floor(data[i][1])] = 0
|
| 29 |
+
elif math.floor(data[i][0]) >= 256:
|
| 30 |
+
input[255][math.floor(data[i][1])] = 0
|
| 31 |
+
elif math.floor(data[i][1]) >= 256:
|
| 32 |
+
input[math.floor(data[i][0])][255] = 0
|
| 33 |
+
|
| 34 |
+
#Visualize
|
| 35 |
+
if visualize == True:
|
| 36 |
+
plt.imshow(input.T, cmap='gray')
|
| 37 |
+
plt.gca().invert_yaxis()
|
| 38 |
+
|
| 39 |
+
return input
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def get_instances(prediction, data, max_filter_size=1):
|
| 45 |
+
#Adjust format (clusters to be 255 and rest is 0)
|
| 46 |
+
prediction[prediction == 255] = 3
|
| 47 |
+
prediction[prediction == 0] = 4
|
| 48 |
+
prediction[prediction == 3] = 0
|
| 49 |
+
prediction[prediction == 4] = 255
|
| 50 |
+
|
| 51 |
+
#Convert to 8-bit image
|
| 52 |
+
prediction = image.img_to_array(prediction, dtype='uint8')
|
| 53 |
+
|
| 54 |
+
#Get 1 color channel
|
| 55 |
+
cells=prediction[:,:,0]
|
| 56 |
+
#Threshold
|
| 57 |
+
ret1, thresh = cv2.threshold(cells, 0, 255, cv2.THRESH_BINARY)
|
| 58 |
+
#Filter to remove noise
|
| 59 |
+
kernel = np.ones((3,3),np.uint8)
|
| 60 |
+
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
|
| 61 |
+
|
| 62 |
+
#Get the background
|
| 63 |
+
background = cv2.dilate(opening,kernel,iterations=5)
|
| 64 |
+
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
|
| 65 |
+
ret2, foreground = cv2.threshold(dist_transform,0.04*dist_transform.max(),255,0)
|
| 66 |
+
foreground = np.uint8(foreground)
|
| 67 |
+
unknown = cv2.subtract(background,foreground)
|
| 68 |
+
|
| 69 |
+
#Connected Component Analysis
|
| 70 |
+
ret3, markers = cv2.connectedComponents(foreground)
|
| 71 |
+
markers = markers+10
|
| 72 |
+
markers[unknown==255] = 0
|
| 73 |
+
|
| 74 |
+
#Watershed
|
| 75 |
+
img = cv2.merge((prediction,prediction,prediction))
|
| 76 |
+
markers = cv2.watershed(img,markers)
|
| 77 |
+
img[markers == -1] = [0,255,255]
|
| 78 |
+
|
| 79 |
+
#Maximum filtering
|
| 80 |
+
markers = ndimage.maximum_filter(markers, size=max_filter_size)
|
| 81 |
+
# plt.imshow(markers.T, cmap='gray')
|
| 82 |
+
# plt.gca().invert_yaxis()
|
| 83 |
+
|
| 84 |
+
#Get an RGB colored image
|
| 85 |
+
img2 = color.label2rgb(markers, bg_label=1)
|
| 86 |
+
# plt.imshow(img2)
|
| 87 |
+
# plt.gca().invert_yaxis()
|
| 88 |
+
|
| 89 |
+
#Get regions
|
| 90 |
+
regions = measure.regionprops(markers, intensity_image=cells)
|
| 91 |
+
|
| 92 |
+
#Get Cluster IDs
|
| 93 |
+
cluster_ids = np.zeros(len(data))
|
| 94 |
+
|
| 95 |
+
for i in range(0,len(cluster_ids)):
|
| 96 |
+
row = math.floor(data[i][0])
|
| 97 |
+
column = math.floor(data[i][1])
|
| 98 |
+
if row < 256 and column < 256:
|
| 99 |
+
cluster_ids[i] = markers[row][column] - 10
|
| 100 |
+
elif row >= 256:
|
| 101 |
+
# cluster_ids[i] = markers[255][column]
|
| 102 |
+
cluster_ids[i] = 0
|
| 103 |
+
elif column >= 256:
|
| 104 |
+
# cluster_ids[i] = markers[row][255]
|
| 105 |
+
cluster_ids[i] = 0
|
| 106 |
+
|
| 107 |
+
cluster_ids = cluster_ids.astype('int8')
|
| 108 |
+
cluster_ids[cluster_ids == -11] = 0
|
| 109 |
+
|
| 110 |
+
return cluster_ids
|