ReyaLabColumbia commited on
Commit
af137d0
·
verified ·
1 Parent(s): daa3b76

Upload Colony_Analyzer_AI2_HF.py

Browse files
Files changed (1) hide show
  1. Colony_Analyzer_AI2_HF.py +9 -18
Colony_Analyzer_AI2_HF.py CHANGED
@@ -12,14 +12,16 @@ import cv2
12
  #this is the huggingface version
13
  def cut_img(img):
14
  img_map = {}
15
- i_num = img.shape[0]/512
16
- j_num = img.shape[1]/512
 
17
  count = 1
18
- for i in range(int(i_num)):
19
- for j in range(int(j_num)):
20
- img_map[count] = img[(512*i):(512*(i+1)), (512*j):(512*(j+1))]
21
- count +=1
22
- return(img_map)
 
23
 
24
  import numpy as np
25
 
@@ -107,12 +109,6 @@ def eval_img(image):
107
  # num_necrosis/num_colony
108
 
109
  def find_colonies(mask, size_cutoff, circ_cutoff):
110
- # Explicitly ensure mask is 2D
111
- if mask.ndim == 3:
112
- mask = mask[:, :, 0] # if mask has multiple channels
113
- elif mask.ndim == 4:
114
- mask = mask[0, :, :, 0] # if there's an extra batch/channel dimension
115
-
116
  binary_mask = np.where(mask == 1, 255, 0).astype(np.uint8)
117
  print(np.max(binary_mask))
118
  contours, _ = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
@@ -134,11 +130,6 @@ def find_colonies(mask, size_cutoff, circ_cutoff):
134
  return(contoursf)
135
 
136
  def find_necrosis(mask):
137
- # Explicitly ensure mask is 2D
138
- if mask.ndim == 3:
139
- mask = mask[:, :, 0] # if mask has multiple channels
140
- elif mask.ndim == 4:
141
- mask = mask[0, :, :, 0] # if there's an extra batch/channel dimension
142
  binary_mask = np.where(mask == 2, 255, 0).astype(np.uint8)
143
  print(np.max(binary_mask))
144
  contours, _ = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
 
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
+ count += 1
24
+ return img_map
25
 
26
  import numpy as np
27
 
 
109
  # num_necrosis/num_colony
110
 
111
  def find_colonies(mask, size_cutoff, circ_cutoff):
 
 
 
 
 
 
112
  binary_mask = np.where(mask == 1, 255, 0).astype(np.uint8)
113
  print(np.max(binary_mask))
114
  contours, _ = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
 
130
  return(contoursf)
131
 
132
  def find_necrosis(mask):
 
 
 
 
 
133
  binary_mask = np.where(mask == 2, 255, 0).astype(np.uint8)
134
  print(np.max(binary_mask))
135
  contours, _ = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)