HeshamAI commited on
Commit
0464b42
·
verified ·
1 Parent(s): 3d765a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -16
app.py CHANGED
@@ -131,7 +131,7 @@ class DicomAnalyzer:
131
  clicked_x = evt.index[0]
132
  clicked_y = evt.index[1]
133
 
134
- # Transform coordinates
135
  x = clicked_x + self.pan_x
136
  y = clicked_y + self.pan_y
137
  if self.zoom_factor != 1.0:
@@ -144,22 +144,19 @@ class DicomAnalyzer:
144
  # Get image dimensions
145
  height, width = self.original_image.shape[:2]
146
 
147
- # Create mask with larger radius calculation
148
  Y, X = np.ogrid[:height, :width]
149
 
150
- # Increase effective radius to include more boundary pixels
151
- radius = (self.circle_diameter / 2.0) + 1.0 # Increased from 0.5 to 1.0
152
- r_squared = radius * radius
153
 
154
- # Calculate distances with more inclusive boundary
155
  dx = X - x
156
  dy = Y - y
157
  dist_squared = dx*dx + dy*dy
 
158
 
159
- # Include more pixels in the mask
160
- mask = dist_squared <= (r_squared + 0.5) # Increased tolerance
161
-
162
- # Get ROI pixels
163
  roi_pixels = self.original_image[mask]
164
 
165
  if len(roi_pixels) == 0:
@@ -168,17 +165,19 @@ class DicomAnalyzer:
168
  # Get pixel spacing (mm/pixel)
169
  pixel_spacing = float(self.dicom_data.PixelSpacing[0])
170
 
171
- # Calculate statistics
172
  n_pixels = np.sum(mask)
173
  area = n_pixels * (pixel_spacing ** 2)
 
 
174
  mean_value = np.mean(roi_pixels)
175
- std_dev = np.std(roi_pixels, ddof=1)
176
  min_val = np.min(roi_pixels)
177
  max_val = np.max(roi_pixels)
178
 
179
- print(f"\nEnhanced Analysis:")
180
  print(f"Position: ({x}, {y})")
181
- print(f"Effective Radius: {radius}")
182
  print(f"Pixel count: {n_pixels}")
183
  print(f"Area: {area:.3f} mm²")
184
  print(f"Mean: {mean_value:.3f}")
@@ -222,8 +221,8 @@ class DicomAnalyzer:
222
  for x, y, diameter in self.marks:
223
  zoomed_x = int(x * self.zoom_factor)
224
  zoomed_y = int(y * self.zoom_factor)
225
- # Use enhanced radius for display
226
- zoomed_radius = int(((diameter/2.0 + 1.0) * self.zoom_factor)) # Increased from 0.5 to 1.0
227
 
228
  # Draw main circle
229
  cv2.circle(zoomed_bgr,
 
131
  clicked_x = evt.index[0]
132
  clicked_y = evt.index[1]
133
 
134
+ # Transform coordinates to match ImageJ exactly
135
  x = clicked_x + self.pan_x
136
  y = clicked_y + self.pan_y
137
  if self.zoom_factor != 1.0:
 
144
  # Get image dimensions
145
  height, width = self.original_image.shape[:2]
146
 
147
+ # Create mask using exact ImageJ method
148
  Y, X = np.ogrid[:height, :width]
149
 
150
+ # Use exact 9-pixel diameter
151
+ radius = self.circle_diameter / 2.0
 
152
 
153
+ # Calculate distances using ImageJ's method
154
  dx = X - x
155
  dy = Y - y
156
  dist_squared = dx*dx + dy*dy
157
+ mask = dist_squared <= (radius * radius)
158
 
159
+ # Get ROI pixels from original image
 
 
 
160
  roi_pixels = self.original_image[mask]
161
 
162
  if len(roi_pixels) == 0:
 
165
  # Get pixel spacing (mm/pixel)
166
  pixel_spacing = float(self.dicom_data.PixelSpacing[0])
167
 
168
+ # Calculate area using exact pixel count
169
  n_pixels = np.sum(mask)
170
  area = n_pixels * (pixel_spacing ** 2)
171
+
172
+ # Calculate statistics
173
  mean_value = np.mean(roi_pixels)
174
+ std_dev = np.std(roi_pixels, ddof=1) # ImageJ uses n-1
175
  min_val = np.min(roi_pixels)
176
  max_val = np.max(roi_pixels)
177
 
178
+ print(f"\nImageJ-compatible Analysis:")
179
  print(f"Position: ({x}, {y})")
180
+ print(f"Diameter: {self.circle_diameter} pixels")
181
  print(f"Pixel count: {n_pixels}")
182
  print(f"Area: {area:.3f} mm²")
183
  print(f"Mean: {mean_value:.3f}")
 
221
  for x, y, diameter in self.marks:
222
  zoomed_x = int(x * self.zoom_factor)
223
  zoomed_y = int(y * self.zoom_factor)
224
+ # Use exact radius without any additions
225
+ zoomed_radius = int((diameter/2.0) * self.zoom_factor)
226
 
227
  # Draw main circle
228
  cv2.circle(zoomed_bgr,