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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -20
app.py CHANGED
@@ -125,55 +125,54 @@ class DicomAnalyzer:
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:
 
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
145
  )
146
  mask[distance_from_center <= radius] = 1
147
 
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}",
165
  'StdDev': f"{stddev:.3f}",
166
  'Min': f"{min_val:.3f}",
167
  'Max': f"{max_val:.3f}",
168
+ 'Point': f"({x}, {y})"
169
  }
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: