HeshamAI commited on
Commit
86ef68b
·
verified ·
1 Parent(s): ba7fac0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -44
app.py CHANGED
@@ -121,51 +121,64 @@ class DicomAnalyzer:
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
- x = int((evt.index[0] + self.pan_x) / self.zoom_factor)
129
- y = int((evt.index[1] + self.pan_y) / self.zoom_factor)
130
-
131
- height, width = self.current_image.shape[:2]
132
- x = max(0, min(x, width-1))
133
- y = max(0, min(y, height-1))
134
-
135
- mask = np.zeros_like(self.current_image, dtype=np.uint8)
136
- y_indices, x_indices = np.ogrid[:self.current_image.shape[0], :self.current_image.shape[1]]
137
- radius = self.circle_diameter / 2
138
- distance_from_center = np.sqrt(
139
- (x_indices - x)**2 + (y_indices - y)**2
140
- )
141
- mask[distance_from_center <= radius] = 1
142
-
143
- roi_pixels = self.current_image[mask == 1]
144
-
145
- pixel_spacing = float(self.dicom_data.PixelSpacing[0])
146
- area_pixels = np.sum(mask)
147
- area_mm2 = area_pixels * (pixel_spacing ** 2)
148
- mean = np.mean(roi_pixels)
149
- stddev = np.std(roi_pixels)
150
- min_val = np.min(roi_pixels)
151
- max_val = np.max(roi_pixels)
152
-
153
- result = {
154
- 'Area (mm²)': f"{area_mm2:.3f}",
155
- 'Mean': f"{mean:.3f}",
156
- 'StdDev': f"{stddev:.3f}",
157
- 'Min': f"{min_val:.3f}",
158
- 'Max': f"{max_val:.3f}",
159
- 'Point': f"({x}, {y})"
160
- }
161
- self.results.append(result)
162
- self.marks.append((x, y, self.circle_diameter))
163
- print(f"ROI analyzed at point ({x}, {y})")
 
 
 
 
 
 
 
 
 
 
 
 
 
164
 
165
- return self.update_display(), self.format_results()
166
- except Exception as e:
167
- print(f"Error analyzing ROI: {str(e)}")
168
- return self.display_image, f"Error analyzing ROI: {str(e)}"
169
 
170
  def update_display(self):
171
  try:
 
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: