Update app.py
Browse files
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
|
| 148 |
Y, X = np.ogrid[:height, :width]
|
| 149 |
|
| 150 |
-
#
|
| 151 |
-
radius =
|
| 152 |
-
r_squared = radius * radius
|
| 153 |
|
| 154 |
-
# Calculate distances
|
| 155 |
dx = X - x
|
| 156 |
dy = Y - y
|
| 157 |
dist_squared = dx*dx + dy*dy
|
|
|
|
| 158 |
|
| 159 |
-
#
|
| 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
|
| 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"\
|
| 180 |
print(f"Position: ({x}, {y})")
|
| 181 |
-
print(f"
|
| 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
|
| 226 |
-
zoomed_radius = int((
|
| 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,
|