Update app.py
Browse files
app.py
CHANGED
|
@@ -122,75 +122,76 @@ class DicomAnalyzer:
|
|
| 122 |
print(f"Error handling keyboard input: {str(e)}")
|
| 123 |
return self.display_image
|
| 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 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
def update_display(self):
|
| 195 |
try:
|
| 196 |
if self.original_display is None:
|
|
|
|
| 122 |
print(f"Error handling keyboard input: {str(e)}")
|
| 123 |
return self.display_image
|
| 124 |
|
| 125 |
+
def analyze_roi(self, evt: gr.SelectData):
|
| 126 |
+
try:
|
| 127 |
+
if self.current_image is None:
|
| 128 |
+
return None, "No image loaded"
|
| 129 |
|
| 130 |
+
# Get clicked coordinates
|
| 131 |
+
clicked_x, clicked_y = evt.index[0], evt.index[1]
|
| 132 |
+
|
| 133 |
+
# Transform coordinates to match ImageJ
|
| 134 |
+
x = (clicked_x + self.pan_x) / self.zoom_factor
|
| 135 |
+
y = (clicked_y + self.pan_y) / self.zoom_factor
|
| 136 |
+
|
| 137 |
+
# Get image dimensions
|
| 138 |
+
height, width = self.current_image.shape[:2]
|
| 139 |
+
|
| 140 |
+
# ImageJ-style circle creation
|
| 141 |
+
Y, X = np.ogrid[:height, :width]
|
| 142 |
+
|
| 143 |
+
# ImageJ uses a specific method for radius calculation
|
| 144 |
+
radius = self.circle_diameter / 2.0
|
| 145 |
+
|
| 146 |
+
# Create the circular ROI mask using ImageJ's method
|
| 147 |
+
# ImageJ considers pixels as 1x1 squares centered on integer coordinates
|
| 148 |
+
dist_squared = (X - x)**2 + (Y - y)**2
|
| 149 |
+
mask = dist_squared <= (radius * radius)
|
| 150 |
+
|
| 151 |
+
# Get ROI pixels
|
| 152 |
+
roi_pixels = self.current_image[mask]
|
| 153 |
+
|
| 154 |
+
if len(roi_pixels) == 0:
|
| 155 |
+
return self.display_image, "Error: No pixels selected"
|
| 156 |
|
| 157 |
+
# Get pixel spacing (mm/pixel)
|
| 158 |
+
pixel_spacing = float(self.dicom_data.PixelSpacing[0])
|
| 159 |
+
|
| 160 |
+
# Calculate area exactly as ImageJ does
|
| 161 |
+
n_pixels = np.sum(mask) # Total number of pixels in ROI
|
| 162 |
+
area = n_pixels * (pixel_spacing * pixel_spacing) # Area in mm²
|
| 163 |
+
|
| 164 |
+
# Calculate other statistics
|
| 165 |
+
mean_value = np.mean(roi_pixels)
|
| 166 |
+
std_dev = np.std(roi_pixels, ddof=1) # ImageJ uses n-1 for standard deviation
|
| 167 |
+
min_val = np.min(roi_pixels)
|
| 168 |
+
max_val = np.max(roi_pixels)
|
| 169 |
|
| 170 |
+
# Store results
|
| 171 |
+
result = {
|
| 172 |
+
'Area (mm²)': f"{area:.3f}",
|
| 173 |
+
'Mean': f"{mean_value:.3f}",
|
| 174 |
+
'StdDev': f"{std_dev:.3f}",
|
| 175 |
+
'Min': f"{min_val:.3f}",
|
| 176 |
+
'Max': f"{max_val:.3f}",
|
| 177 |
+
'Point': f"({x:.1f}, {y:.1f})"
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
print(f"ROI Analysis Results:")
|
| 181 |
+
print(f"Number of pixels: {n_pixels}")
|
| 182 |
+
print(f"Pixel spacing: {pixel_spacing} mm")
|
| 183 |
+
print(f"Area: {area:.3f} mm²")
|
| 184 |
+
print(f"Mean: {mean_value:.3f}")
|
| 185 |
+
print(f"StdDev: {std_dev:.3f}")
|
| 186 |
+
print(f"Position: ({x:.1f}, {y:.1f})")
|
| 187 |
+
|
| 188 |
+
self.results.append(result)
|
| 189 |
+
self.marks.append((x, y, self.circle_diameter))
|
| 190 |
|
| 191 |
+
return self.update_display(), self.format_results()
|
| 192 |
+
except Exception as e:
|
| 193 |
+
print(f"Error analyzing ROI: {str(e)}")
|
| 194 |
+
return self.display_image, f"Error analyzing ROI: {str(e)}"
|
| 195 |
def update_display(self):
|
| 196 |
try:
|
| 197 |
if self.original_display is None:
|