Update app.py
Browse files
app.py
CHANGED
|
@@ -40,12 +40,15 @@ class DicomAnalyzer:
|
|
| 40 |
|
| 41 |
image = dicom_data.pixel_array.astype(np.float32)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
rescale_slope = getattr(dicom_data, 'RescaleSlope', 1)
|
| 44 |
rescale_intercept = getattr(dicom_data, 'RescaleIntercept', 0)
|
| 45 |
image = (image * rescale_slope) + rescale_intercept
|
| 46 |
|
| 47 |
self.current_image = image
|
| 48 |
-
self.original_image = image.copy()
|
| 49 |
self.dicom_data = dicom_data
|
| 50 |
|
| 51 |
self.display_image = self.normalize_image(image)
|
|
@@ -131,7 +134,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,19 +147,23 @@ class DicomAnalyzer:
|
|
| 144 |
# Get image dimensions
|
| 145 |
height, width = self.original_image.shape[:2]
|
| 146 |
|
| 147 |
-
# Create mask using exact
|
| 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
|
| 154 |
dx = X - x
|
| 155 |
dy = Y - y
|
| 156 |
dist_squared = dx*dx + dy*dy
|
| 157 |
-
mask = dist_squared <= (radius * radius)
|
| 158 |
|
| 159 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
roi_pixels = self.original_image[mask]
|
| 161 |
|
| 162 |
if len(roi_pixels) == 0:
|
|
@@ -165,19 +172,28 @@ class DicomAnalyzer:
|
|
| 165 |
# Get pixel spacing (mm/pixel)
|
| 166 |
pixel_spacing = float(self.dicom_data.PixelSpacing[0])
|
| 167 |
|
| 168 |
-
# Calculate area
|
| 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}")
|
|
|
|
| 40 |
|
| 41 |
image = dicom_data.pixel_array.astype(np.float32)
|
| 42 |
|
| 43 |
+
# Store original pixel values before any scaling
|
| 44 |
+
self.original_image = image.copy()
|
| 45 |
+
|
| 46 |
+
# Apply DICOM scaling for display
|
| 47 |
rescale_slope = getattr(dicom_data, 'RescaleSlope', 1)
|
| 48 |
rescale_intercept = getattr(dicom_data, 'RescaleIntercept', 0)
|
| 49 |
image = (image * rescale_slope) + rescale_intercept
|
| 50 |
|
| 51 |
self.current_image = image
|
|
|
|
| 52 |
self.dicom_data = dicom_data
|
| 53 |
|
| 54 |
self.display_image = self.normalize_image(image)
|
|
|
|
| 134 |
clicked_x = evt.index[0]
|
| 135 |
clicked_y = evt.index[1]
|
| 136 |
|
| 137 |
+
# Transform coordinates
|
| 138 |
x = clicked_x + self.pan_x
|
| 139 |
y = clicked_y + self.pan_y
|
| 140 |
if self.zoom_factor != 1.0:
|
|
|
|
| 147 |
# Get image dimensions
|
| 148 |
height, width = self.original_image.shape[:2]
|
| 149 |
|
| 150 |
+
# Create mask using ImageJ's exact method
|
| 151 |
Y, X = np.ogrid[:height, :width]
|
| 152 |
|
| 153 |
# Use exact 9-pixel diameter
|
| 154 |
radius = self.circle_diameter / 2.0
|
| 155 |
+
r_squared = radius * radius
|
| 156 |
|
| 157 |
+
# Calculate distances exactly as ImageJ does
|
| 158 |
dx = X - x
|
| 159 |
dy = Y - y
|
| 160 |
dist_squared = dx*dx + dy*dy
|
|
|
|
| 161 |
|
| 162 |
+
# Create mask with ImageJ's method
|
| 163 |
+
mask = np.zeros((height, width), dtype=bool)
|
| 164 |
+
mask[dist_squared <= r_squared] = True
|
| 165 |
+
|
| 166 |
+
# Get ROI pixels from original DICOM values
|
| 167 |
roi_pixels = self.original_image[mask]
|
| 168 |
|
| 169 |
if len(roi_pixels) == 0:
|
|
|
|
| 172 |
# Get pixel spacing (mm/pixel)
|
| 173 |
pixel_spacing = float(self.dicom_data.PixelSpacing[0])
|
| 174 |
|
| 175 |
+
# Calculate area (this part is correct)
|
| 176 |
n_pixels = np.sum(mask)
|
| 177 |
area = n_pixels * (pixel_spacing ** 2)
|
| 178 |
|
| 179 |
+
# Calculate statistics using ImageJ's methods
|
| 180 |
mean_value = np.mean(roi_pixels)
|
| 181 |
std_dev = np.std(roi_pixels, ddof=1) # ImageJ uses n-1
|
| 182 |
min_val = np.min(roi_pixels)
|
| 183 |
max_val = np.max(roi_pixels)
|
| 184 |
|
| 185 |
+
# Apply any necessary scaling from DICOM
|
| 186 |
+
rescale_slope = getattr(self.dicom_data, 'RescaleSlope', 1)
|
| 187 |
+
rescale_intercept = getattr(self.dicom_data, 'RescaleIntercept', 0)
|
| 188 |
+
|
| 189 |
+
# Adjust values using DICOM scaling
|
| 190 |
+
mean_value = (mean_value * rescale_slope) + rescale_intercept
|
| 191 |
+
std_dev = std_dev * rescale_slope
|
| 192 |
+
min_val = (min_val * rescale_slope) + rescale_intercept
|
| 193 |
+
max_val = (max_val * rescale_slope) + rescale_intercept
|
| 194 |
+
|
| 195 |
print(f"\nImageJ-compatible Analysis:")
|
| 196 |
print(f"Position: ({x}, {y})")
|
|
|
|
| 197 |
print(f"Pixel count: {n_pixels}")
|
| 198 |
print(f"Area: {area:.3f} mm²")
|
| 199 |
print(f"Mean: {mean_value:.3f}")
|