ReyaLabColumbia commited on
Commit
be670f6
·
verified ·
1 Parent(s): 2c6a1ea

Upload Colony_Analyzer_AI2_HF.py

Browse files
Files changed (1) hide show
  1. Colony_Analyzer_AI2_HF.py +78 -22
Colony_Analyzer_AI2_HF.py CHANGED
@@ -8,37 +8,90 @@ Created on Thu Mar 20 14:23:27 2025
8
 
9
  import os
10
  import cv2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  #this is the huggingface version
13
- def cut_img(img):
14
  img_map = {}
15
  width, height = img.size
16
- i_num = height // 512
17
- j_num = width // 512
18
  count = 1
19
  for i in range(i_num):
20
  for j in range(j_num):
21
- cropped_img = img.crop((512*j, 512*i, 512*(j+1), 512*(i+1)))
 
 
 
 
 
22
  img_map[count] = cropped_img
23
- #print(type(cropped_img))
24
  count += 1
25
- return img_map
26
 
27
  import numpy as np
28
 
29
- def stitch(img_map):
30
- rows = [
31
- np.hstack([img_map[1], img_map[2], img_map[3], img_map[4]]), # First row (images 0 to 3)
32
- np.hstack([img_map[5], img_map[6], img_map[7], img_map[8]]), # Second row (images 4 to 7)
33
- np.hstack([img_map[9], img_map[10], img_map[11], img_map[12]]) # Third row (images 8 to 11)
34
- ]
35
- # Stack rows vertically
36
- return(np.vstack(rows))
37
-
38
-
39
-
40
  from PIL import Image
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
 
44
  import matplotlib.pyplot as plt
@@ -214,21 +267,24 @@ def main(args):
214
  min_circ = args[2]
215
  do_necrosis = args[3]
216
  colonies = {}
217
- img_map = cut_img(args[0])
218
  for z in img_map:
219
  img_map[z] = eval_img(img_map[z])
220
  del z
221
- p = stitch(img_map)
222
  colonies = analyze_colonies(p, min_size, min_circ, np.array(args[0]))
223
  if len(colonies) <=0:
 
224
  caption = np.ones((150, 2048, 3), dtype=np.uint8) * 255 # Multiply by 255 to make it white
225
  cv2.putText(caption, 'No colonies detected.', (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 3)
226
- cv2.imwrite('results.png', np.vstack((np.array(args[0]), caption)))
227
  colonies = pd.DataFrame({"Colony Number":[], 'Colony volume':[], "colony_area":[],'mean_pixel_value':[], "centroid":[], "necrotic_area":[],"percent_necrotic":[]})
228
  with pd.ExcelWriter('results.xlsx') as writer:
229
  colonies.to_excel(writer, sheet_name="Colony data", index=False)
230
- return(np.vstack((args[0], caption)), 'results.png', 'results.xlsx')
231
- img = np.array(args[0])
 
 
232
  img = cv2.copyMakeBorder(img,top=0, bottom=10,left=0,right=10, borderType=cv2.BORDER_CONSTANT, value=[255, 255, 255])
233
  #print(colonies.to_string())
234
 
 
8
 
9
  import os
10
  import cv2
11
+ from PIL import Image
12
+
13
+
14
+ def ensure_minimum_size(img, min_width=512, min_height=512):
15
+ width, height = img.size
16
+ if width < min_width or height < min_height:
17
+ return img.resize((max(width, min_width), max(height, min_height)), Image.BICUBIC)
18
+ return img
19
+
20
+ def pad(img_np):
21
+ """
22
+ Pads a numpy image (grayscale or RGB) to 2048x1536 (width x height) with white pixels.
23
+ Pads at the bottom and right as needed.
24
+ """
25
+ target_width, target_height = 2048, 1536
26
+ height, width = img_np.shape[:2]
27
+ pad_bottom = max(0, target_height - height)
28
+ pad_right = max(0, target_width - width)
29
+ # Padding: (top, bottom, left, right)
30
+ if img_np.ndim == 3:
31
+ # Color image (H, W, 3)
32
+ border_value = [255, 255, 255]
33
+ else:
34
+ # Grayscale image (H, W)
35
+ border_value = 255
36
+
37
+ padded = cv2.copyMakeBorder(
38
+ img_np,
39
+ top=0, bottom=pad_bottom,
40
+ left=0, right=pad_right,
41
+ borderType=cv2.BORDER_CONSTANT,
42
+ value=border_value
43
+ )
44
+ return padded
45
+
46
 
47
  #this is the huggingface version
48
+ def cut_img(img, patch_size=512):
49
  img_map = {}
50
  width, height = img.size
51
+ i_num = height // patch_size
52
+ j_num = width // patch_size
53
  count = 1
54
  for i in range(i_num):
55
  for j in range(j_num):
56
+ cropped_img = img.crop((
57
+ patch_size * j,
58
+ patch_size * i,
59
+ patch_size * (j + 1),
60
+ patch_size * (i + 1)
61
+ ))
62
  img_map[count] = cropped_img
 
63
  count += 1
64
+ return img_map, i_num, j_num # Return rows and cols for stitching
65
 
66
  import numpy as np
67
 
68
+ import numpy as np
 
 
 
 
 
 
 
 
 
 
69
  from PIL import Image
70
 
71
+ def stitch(img_map, i_num, j_num, min_width=2048, min_height=1536):
72
+ tiles = []
73
+ count = 1
74
+ for i in range(i_num):
75
+ row_tiles = []
76
+ for j in range(j_num):
77
+ tile = np.array(img_map[count])
78
+ row_tiles.append(tile)
79
+ count += 1
80
+ row_img = np.hstack(row_tiles)
81
+ tiles.append(row_img)
82
+ stitched = np.vstack(tiles)
83
+
84
+ # Pad the stitched image if it's less than min_width/min_height
85
+ h, w = stitched.shape[:2]
86
+ pad_h = max(0, min_height - h)
87
+ pad_w = max(0, min_width - w)
88
+ if pad_h > 0 or pad_w > 0:
89
+ # Pad as (top, bottom), (left, right), (channels)
90
+ if stitched.ndim == 3:
91
+ stitched = np.pad(stitched, ((0, pad_h), (0, pad_w), (0, 0)), 'constant')
92
+ else:
93
+ stitched = np.pad(stitched, ((0, pad_h), (0, pad_w)), 'constant')
94
+ return stitched
95
 
96
 
97
  import matplotlib.pyplot as plt
 
267
  min_circ = args[2]
268
  do_necrosis = args[3]
269
  colonies = {}
270
+ img_map, i_num, j_num = cut_img(ensure_minimum_size(args[0], min_width=512, min_height=512))
271
  for z in img_map:
272
  img_map[z] = eval_img(img_map[z])
273
  del z
274
+ p = stitch(img_map, i_num, j_num)
275
  colonies = analyze_colonies(p, min_size, min_circ, np.array(args[0]))
276
  if len(colonies) <=0:
277
+ img = np.array(pad(args[0]))
278
  caption = np.ones((150, 2048, 3), dtype=np.uint8) * 255 # Multiply by 255 to make it white
279
  cv2.putText(caption, 'No colonies detected.', (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 3)
280
+ cv2.imwrite('results.png', np.vstack((img, caption)))
281
  colonies = pd.DataFrame({"Colony Number":[], 'Colony volume':[], "colony_area":[],'mean_pixel_value':[], "centroid":[], "necrotic_area":[],"percent_necrotic":[]})
282
  with pd.ExcelWriter('results.xlsx') as writer:
283
  colonies.to_excel(writer, sheet_name="Colony data", index=False)
284
+ return(np.vstack((img, caption)), 'results.png', 'results.xlsx')
285
+
286
+ img = np.array(pad(args[0]))
287
+
288
  img = cv2.copyMakeBorder(img,top=0, bottom=10,left=0,right=10, borderType=cv2.BORDER_CONSTANT, value=[255, 255, 255])
289
  #print(colonies.to_string())
290