Update app.py
Browse files
app.py
CHANGED
|
@@ -69,7 +69,7 @@ class DicomAnalyzer:
|
|
| 69 |
dtype=cv2.CV_8U
|
| 70 |
)
|
| 71 |
if len(normalized.shape) == 2:
|
| 72 |
-
normalized = cv2.cvtColor(normalized, cv2.
|
| 73 |
return normalized
|
| 74 |
except Exception as e:
|
| 75 |
print(f"Error normalizing image: {str(e)}")
|
|
@@ -121,64 +121,64 @@ class DicomAnalyzer:
|
|
| 121 |
return self.display_image
|
| 122 |
|
| 123 |
def analyze_roi(self, evt: gr.SelectData):
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
|
| 183 |
def update_display(self):
|
| 184 |
try:
|
|
|
|
| 69 |
dtype=cv2.CV_8U
|
| 70 |
)
|
| 71 |
if len(normalized.shape) == 2:
|
| 72 |
+
normalized = cv2.cvtColor(normalized, cv2.COLOR_GRAY2BGR)
|
| 73 |
return normalized
|
| 74 |
except Exception as e:
|
| 75 |
print(f"Error normalizing image: {str(e)}")
|
|
|
|
| 121 |
return self.display_image
|
| 122 |
|
| 123 |
def analyze_roi(self, evt: gr.SelectData):
|
| 124 |
+
try:
|
| 125 |
+
if self.current_image is None:
|
| 126 |
+
return None, "No image loaded"
|
| 127 |
+
|
| 128 |
+
# Get the clicked coordinates
|
| 129 |
+
clicked_x = evt.index[0]
|
| 130 |
+
clicked_y = evt.index[1]
|
| 131 |
+
|
| 132 |
+
# Adjust for pan and zoom
|
| 133 |
+
x = int((clicked_x + self.pan_x) / self.zoom_factor)
|
| 134 |
+
y = int((clicked_y + self.pan_y) / self.zoom_factor)
|
| 135 |
+
|
| 136 |
+
# Ensure coordinates are within image bounds
|
| 137 |
+
height, width = self.current_image.shape[:2]
|
| 138 |
+
x = max(0, min(x, width-1))
|
| 139 |
+
y = max(0, min(y, height-1))
|
| 140 |
+
|
| 141 |
+
# Create mask for ROI
|
| 142 |
+
mask = np.zeros_like(self.current_image, dtype=np.uint8)
|
| 143 |
+
y_indices, x_indices = np.ogrid[:self.current_image.shape[0], :self.current_image.shape[1]]
|
| 144 |
+
radius = self.circle_diameter / 2
|
| 145 |
+
distance_from_center = np.sqrt(
|
| 146 |
+
(x_indices - x)**2 + (y_indices - y)**2
|
| 147 |
+
)
|
| 148 |
+
mask[distance_from_center <= radius] = 1
|
| 149 |
+
|
| 150 |
+
# Calculate statistics
|
| 151 |
+
roi_pixels = self.current_image[mask == 1]
|
| 152 |
+
pixel_spacing = float(self.dicom_data.PixelSpacing[0])
|
| 153 |
+
area_pixels = np.sum(mask)
|
| 154 |
+
area_mm2 = area_pixels * (pixel_spacing ** 2)
|
| 155 |
+
mean = np.mean(roi_pixels)
|
| 156 |
+
stddev = np.std(roi_pixels)
|
| 157 |
+
min_val = np.min(roi_pixels)
|
| 158 |
+
max_val = np.max(roi_pixels)
|
| 159 |
+
|
| 160 |
+
# Adjust coordinates to match ImageJ coordinate system
|
| 161 |
+
# ImageJ coordinates start from top-left
|
| 162 |
+
imagej_x = x
|
| 163 |
+
imagej_y = y
|
| 164 |
+
|
| 165 |
+
result = {
|
| 166 |
+
'Area (mm²)': f"{area_mm2:.3f}",
|
| 167 |
+
'Mean': f"{mean:.3f}",
|
| 168 |
+
'StdDev': f"{stddev:.3f}",
|
| 169 |
+
'Min': f"{min_val:.3f}",
|
| 170 |
+
'Max': f"{max_val:.3f}",
|
| 171 |
+
'Point': f"({imagej_x}, {imagej_y})"
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
self.results.append(result)
|
| 175 |
+
self.marks.append((x, y, self.circle_diameter))
|
| 176 |
+
print(f"ROI analyzed at point ({imagej_x}, {imagej_y})")
|
| 177 |
|
| 178 |
+
return self.update_display(), self.format_results()
|
| 179 |
+
except Exception as e:
|
| 180 |
+
print(f"Error analyzing ROI: {str(e)}")
|
| 181 |
+
return self.display_image, f"Error analyzing ROI: {str(e)}"
|
| 182 |
|
| 183 |
def update_display(self):
|
| 184 |
try:
|