Update app.py
Browse files
app.py
CHANGED
|
@@ -99,6 +99,7 @@ def extract_region_from_editor(editor_data):
|
|
| 99 |
|
| 100 |
def calculate_blue_intensity(image_np, masks):
|
| 101 |
"""Calculate blue intensity for each cell"""
|
|
|
|
| 102 |
if len(image_np.shape) == 2:
|
| 103 |
# Convert grayscale to RGB
|
| 104 |
image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
|
|
@@ -110,6 +111,10 @@ def calculate_blue_intensity(image_np, masks):
|
|
| 110 |
cell_blue_intensities = {}
|
| 111 |
unique_cells = np.unique(masks)
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
for cell_id in unique_cells:
|
| 114 |
if cell_id == 0: # Skip background
|
| 115 |
continue
|
|
@@ -118,18 +123,24 @@ def calculate_blue_intensity(image_np, masks):
|
|
| 118 |
cell_pixels = image_np[cell_mask]
|
| 119 |
|
| 120 |
if len(cell_pixels) > 0:
|
| 121 |
-
# Calculate mean blue intensity (blue channel)
|
| 122 |
-
blue_intensity = np.mean(cell_pixels[:, 2]) # Blue channel
|
| 123 |
cell_blue_intensities[cell_id] = blue_intensity
|
|
|
|
| 124 |
|
|
|
|
| 125 |
return cell_blue_intensities
|
| 126 |
|
| 127 |
def create_viability_overlay(image_np, masks, blue_threshold):
|
| 128 |
"""Create overlay showing live/dead cells based on blue threshold"""
|
| 129 |
|
| 130 |
if masks is None or image_np is None:
|
|
|
|
| 131 |
return None, 0, 0
|
| 132 |
|
|
|
|
|
|
|
|
|
|
| 133 |
# Ensure image is RGB
|
| 134 |
if len(image_np.shape) == 2:
|
| 135 |
image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
|
|
@@ -139,6 +150,8 @@ def create_viability_overlay(image_np, masks, blue_threshold):
|
|
| 139 |
# Calculate blue intensities
|
| 140 |
cell_blue_intensities = calculate_blue_intensity(image_np, masks)
|
| 141 |
|
|
|
|
|
|
|
| 142 |
# Create overlay - start with original image
|
| 143 |
overlay = image_np.copy().astype(np.float32)
|
| 144 |
|
|
@@ -149,14 +162,20 @@ def create_viability_overlay(image_np, masks, blue_threshold):
|
|
| 149 |
for cell_id, blue_intensity in cell_blue_intensities.items():
|
| 150 |
cell_mask = masks == cell_id
|
| 151 |
|
|
|
|
|
|
|
| 152 |
if blue_intensity > blue_threshold:
|
| 153 |
# Dead cell - color in red
|
| 154 |
overlay[cell_mask] = [255, 0, 0] # Red
|
| 155 |
dead_count += 1
|
|
|
|
| 156 |
else:
|
| 157 |
# Live cell - color in green
|
| 158 |
overlay[cell_mask] = [0, 255, 0] # Green
|
| 159 |
live_count += 1
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
# Blend with original image
|
| 162 |
alpha = 0.6 # Increased alpha for better visibility
|
|
|
|
| 99 |
|
| 100 |
def calculate_blue_intensity(image_np, masks):
|
| 101 |
"""Calculate blue intensity for each cell"""
|
| 102 |
+
# Ensure image is in correct format
|
| 103 |
if len(image_np.shape) == 2:
|
| 104 |
# Convert grayscale to RGB
|
| 105 |
image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
|
|
|
|
| 111 |
cell_blue_intensities = {}
|
| 112 |
unique_cells = np.unique(masks)
|
| 113 |
|
| 114 |
+
print(f"DEBUG: Found {len(unique_cells)} unique mask values: {unique_cells}")
|
| 115 |
+
print(f"DEBUG: Image shape: {image_np.shape}, dtype: {image_np.dtype}")
|
| 116 |
+
print(f"DEBUG: Masks shape: {masks.shape}, dtype: {masks.dtype}")
|
| 117 |
+
|
| 118 |
for cell_id in unique_cells:
|
| 119 |
if cell_id == 0: # Skip background
|
| 120 |
continue
|
|
|
|
| 123 |
cell_pixels = image_np[cell_mask]
|
| 124 |
|
| 125 |
if len(cell_pixels) > 0:
|
| 126 |
+
# Calculate mean blue intensity (blue channel is index 2)
|
| 127 |
+
blue_intensity = np.mean(cell_pixels[:, 2]) # Blue channel
|
| 128 |
cell_blue_intensities[cell_id] = blue_intensity
|
| 129 |
+
print(f"DEBUG: Cell {cell_id}: {len(cell_pixels)} pixels, blue intensity: {blue_intensity:.2f}")
|
| 130 |
|
| 131 |
+
print(f"DEBUG: Total cells with blue intensity: {len(cell_blue_intensities)}")
|
| 132 |
return cell_blue_intensities
|
| 133 |
|
| 134 |
def create_viability_overlay(image_np, masks, blue_threshold):
|
| 135 |
"""Create overlay showing live/dead cells based on blue threshold"""
|
| 136 |
|
| 137 |
if masks is None or image_np is None:
|
| 138 |
+
print("DEBUG: masks or image_np is None")
|
| 139 |
return None, 0, 0
|
| 140 |
|
| 141 |
+
print(f"DEBUG: create_viability_overlay called with threshold: {blue_threshold}")
|
| 142 |
+
print(f"DEBUG: Image shape: {image_np.shape}, Masks shape: {masks.shape}")
|
| 143 |
+
|
| 144 |
# Ensure image is RGB
|
| 145 |
if len(image_np.shape) == 2:
|
| 146 |
image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
|
|
|
|
| 150 |
# Calculate blue intensities
|
| 151 |
cell_blue_intensities = calculate_blue_intensity(image_np, masks)
|
| 152 |
|
| 153 |
+
print(f"DEBUG: Got {len(cell_blue_intensities)} cells with blue intensities")
|
| 154 |
+
|
| 155 |
# Create overlay - start with original image
|
| 156 |
overlay = image_np.copy().astype(np.float32)
|
| 157 |
|
|
|
|
| 162 |
for cell_id, blue_intensity in cell_blue_intensities.items():
|
| 163 |
cell_mask = masks == cell_id
|
| 164 |
|
| 165 |
+
print(f"DEBUG: Processing cell {cell_id}, blue intensity: {blue_intensity:.2f}, threshold: {blue_threshold}")
|
| 166 |
+
|
| 167 |
if blue_intensity > blue_threshold:
|
| 168 |
# Dead cell - color in red
|
| 169 |
overlay[cell_mask] = [255, 0, 0] # Red
|
| 170 |
dead_count += 1
|
| 171 |
+
print(f"DEBUG: Cell {cell_id} classified as DEAD")
|
| 172 |
else:
|
| 173 |
# Live cell - color in green
|
| 174 |
overlay[cell_mask] = [0, 255, 0] # Green
|
| 175 |
live_count += 1
|
| 176 |
+
print(f"DEBUG: Cell {cell_id} classified as LIVE")
|
| 177 |
+
|
| 178 |
+
print(f"DEBUG: Final counts - Live: {live_count}, Dead: {dead_count}")
|
| 179 |
|
| 180 |
# Blend with original image
|
| 181 |
alpha = 0.6 # Increased alpha for better visibility
|