HeshamAI commited on
Commit
12b06aa
·
verified ·
1 Parent(s): d065c42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -15
app.py CHANGED
@@ -125,20 +125,32 @@ class DicomAnalyzer:
125
  if self.current_image is None:
126
  return None, "No image loaded"
127
 
128
- # Get raw image data before any processing
129
- raw_image = self.current_image.copy()
130
-
131
- # Get clicked coordinates and adjust for zoom/pan
 
 
 
132
  clicked_x = evt.index[0]
133
  clicked_y = evt.index[1]
134
-
135
- # Convert to original image coordinates
136
- x = int((clicked_x + self.pan_x) / self.zoom_factor)
137
- y = int((clicked_y + self.pan_y) / self.zoom_factor)
138
 
139
- # Create mask for ROI using raw image dimensions
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  mask = np.zeros_like(raw_image, dtype=np.uint8)
141
- y_indices, x_indices = np.ogrid[:raw_image.shape[0], :raw_image.shape[1]]
142
  radius = self.circle_diameter / 2
143
  distance_from_center = np.sqrt(
144
  (x_indices - x)**2 + (y_indices - y)**2
@@ -148,17 +160,18 @@ class DicomAnalyzer:
148
  # Get ROI pixels from raw image
149
  roi_pixels = raw_image[mask == 1]
150
 
151
- # Calculate statistics from raw pixel values
152
  pixel_spacing = float(self.dicom_data.PixelSpacing[0])
153
  area_pixels = np.sum(mask)
154
  area_mm2 = area_pixels * (pixel_spacing ** 2)
155
 
156
- # Calculate statistics without any normalization
157
  mean = np.mean(roi_pixels)
158
  stddev = np.std(roi_pixels)
159
  min_val = np.min(roi_pixels)
160
  max_val = np.max(roi_pixels)
161
 
 
162
  result = {
163
  'Area (mm²)': f"{area_mm2:.3f}",
164
  'Mean': f"{mean:.3f}",
@@ -170,9 +183,14 @@ class DicomAnalyzer:
170
 
171
  self.results.append(result)
172
  self.marks.append((x, y, self.circle_diameter))
173
- print(f"ROI analyzed at point ({x}, {y})")
174
- print(f"Mean: {mean:.3f}, StdDev: {stddev:.3f}")
175
- print(f"Min: {min_val:.3f}, Max: {max_val:.3f}")
 
 
 
 
 
176
 
177
  return self.update_display(), self.format_results()
178
  except Exception as e:
 
125
  if self.current_image is None:
126
  return None, "No image loaded"
127
 
128
+ # Get raw image data
129
+ raw_image = self.current_image
130
+
131
+ # Get image dimensions
132
+ height, width = raw_image.shape[:2]
133
+
134
+ # Calculate coordinates
135
  clicked_x = evt.index[0]
136
  clicked_y = evt.index[1]
 
 
 
 
137
 
138
+ # Adjust coordinates for ImageJ compatibility
139
+ x = int(clicked_x)
140
+ y = int(clicked_y)
141
+
142
+ if self.zoom_factor != 1.0:
143
+ # Adjust for zoom and pan
144
+ x = int((clicked_x + self.pan_x) / self.zoom_factor)
145
+ y = int((clicked_y + self.pan_y) / self.zoom_factor)
146
+
147
+ # Ensure coordinates are within bounds
148
+ x = max(0, min(x, width-1))
149
+ y = max(0, min(y, height-1))
150
+
151
+ # Create circular mask
152
  mask = np.zeros_like(raw_image, dtype=np.uint8)
153
+ y_indices, x_indices = np.ogrid[:height, :width]
154
  radius = self.circle_diameter / 2
155
  distance_from_center = np.sqrt(
156
  (x_indices - x)**2 + (y_indices - y)**2
 
160
  # Get ROI pixels from raw image
161
  roi_pixels = raw_image[mask == 1]
162
 
163
+ # Calculate statistics using raw values
164
  pixel_spacing = float(self.dicom_data.PixelSpacing[0])
165
  area_pixels = np.sum(mask)
166
  area_mm2 = area_pixels * (pixel_spacing ** 2)
167
 
168
+ # Use raw pixel values for statistics
169
  mean = np.mean(roi_pixels)
170
  stddev = np.std(roi_pixels)
171
  min_val = np.min(roi_pixels)
172
  max_val = np.max(roi_pixels)
173
 
174
+ # Store results
175
  result = {
176
  'Area (mm²)': f"{area_mm2:.3f}",
177
  'Mean': f"{mean:.3f}",
 
183
 
184
  self.results.append(result)
185
  self.marks.append((x, y, self.circle_diameter))
186
+
187
+ # Debug information
188
+ print(f"Clicked at: ({clicked_x}, {clicked_y})")
189
+ print(f"Adjusted to: ({x}, {y})")
190
+ print(f"Zoom factor: {self.zoom_factor}")
191
+ print(f"Pan: ({self.pan_x}, {self.pan_y})")
192
+ print(f"ROI Statistics: Mean={mean:.3f}, StdDev={stddev:.3f}")
193
+ print(f"Min={min_val:.3f}, Max={max_val:.3f}")
194
 
195
  return self.update_display(), self.format_results()
196
  except Exception as e: