ReyaLabColumbia commited on
Commit
6c7c415
·
verified ·
1 Parent(s): 92fb448

Upload Colony_Analyzer_AI2_HF.py

Browse files
Files changed (1) hide show
  1. Colony_Analyzer_AI2_HF.py +23 -85
Colony_Analyzer_AI2_HF.py CHANGED
@@ -8,96 +8,37 @@ Created on Thu Mar 20 14:23:27 2025
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
- """
46
- Cuts an image (PIL or numpy array) into non-overlapping patch_size x patch_size tiles.
47
- Returns img_map, number of rows, number of cols.
48
- """
49
- # If it's a PIL image, convert to numpy array
50
- if isinstance(img, Image.Image):
51
- img_np = np.array(img)
52
- height, width = img_np.shape[:2]
53
- else:
54
- img_np = img
55
- height, width = img_np.shape[:2]
56
-
57
- i_num = height // patch_size
58
- j_num = width // patch_size
59
  img_map = {}
 
 
 
60
  count = 1
61
  for i in range(i_num):
62
  for j in range(j_num):
63
- cropped_img = img_np[
64
- patch_size * i: patch_size * (i + 1),
65
- patch_size * j: patch_size * (j + 1),
66
- ...
67
- ]
68
  img_map[count] = cropped_img
 
69
  count += 1
70
- return img_map, i_num, j_num
71
 
72
  import numpy as np
73
 
74
- import numpy as np
 
 
 
 
 
 
 
 
 
 
75
  from PIL import Image
76
 
77
- def stitch(img_map, i_num, j_num, min_width=2048, min_height=1536):
78
- tiles = []
79
- count = 1
80
- for i in range(i_num):
81
- row_tiles = []
82
- for j in range(j_num):
83
- tile = np.array(img_map[count])
84
- row_tiles.append(tile)
85
- count += 1
86
- row_img = np.hstack(row_tiles)
87
- tiles.append(row_img)
88
- stitched = np.vstack(tiles)
89
-
90
- # Pad the stitched image if it's less than min_width/min_height
91
- h, w = stitched.shape[:2]
92
- pad_h = max(0, min_height - h)
93
- pad_w = max(0, min_width - w)
94
- if pad_h > 0 or pad_w > 0:
95
- # Pad as (top, bottom), (left, right), (channels)
96
- if stitched.ndim == 3:
97
- stitched = np.pad(stitched, ((0, pad_h), (0, pad_w), (0, 0)), 'constant')
98
- else:
99
- stitched = np.pad(stitched, ((0, pad_h), (0, pad_w)), 'constant')
100
- return stitched
101
 
102
 
103
  import matplotlib.pyplot as plt
@@ -273,24 +214,21 @@ def main(args):
273
  min_circ = args[2]
274
  do_necrosis = args[3]
275
  colonies = {}
276
- img_map, i_num, j_num = cut_img(pad(np.array(args[0]),512,512))
277
  for z in img_map:
278
  img_map[z] = eval_img(img_map[z])
279
  del z
280
- p = stitch(img_map, i_num, j_num)
281
  colonies = analyze_colonies(p, min_size, min_circ, np.array(args[0]))
282
  if len(colonies) <=0:
283
- img = pad(np.array(args[0]))
284
  caption = np.ones((150, 2048, 3), dtype=np.uint8) * 255 # Multiply by 255 to make it white
285
  cv2.putText(caption, 'No colonies detected.', (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 3)
286
- cv2.imwrite('results.png', np.vstack((img, caption)))
287
  colonies = pd.DataFrame({"Colony Number":[], 'Colony volume':[], "colony_area":[],'mean_pixel_value':[], "centroid":[], "necrotic_area":[],"percent_necrotic":[]})
288
  with pd.ExcelWriter('results.xlsx') as writer:
289
  colonies.to_excel(writer, sheet_name="Colony data", index=False)
290
- return(np.vstack((img, caption)), 'results.png', 'results.xlsx')
291
-
292
- img =pad(np.array(args[0]))
293
-
294
  img = cv2.copyMakeBorder(img,top=0, bottom=10,left=0,right=10, borderType=cv2.BORDER_CONSTANT, value=[255, 255, 255])
295
  #print(colonies.to_string())
296
 
 
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
  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