ReyaLabColumbia commited on
Commit
273150e
·
verified ·
1 Parent(s): 6c7c415

Upload Colony_Analyzer_AI2_HF.py

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