Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
import pydicom
|
| 6 |
-
import io
|
| 7 |
from PIL import Image
|
| 8 |
|
| 9 |
print("Starting imports completed...")
|
|
@@ -120,7 +119,7 @@ class DicomAnalyzer:
|
|
| 120 |
print(f"Error handling keyboard input: {str(e)}")
|
| 121 |
return self.display_image
|
| 122 |
|
| 123 |
-
def analyze_roi(self, evt: gr.SelectData):
|
| 124 |
try:
|
| 125 |
if self.current_image is None:
|
| 126 |
return None, "No image loaded"
|
|
@@ -143,61 +142,50 @@ class DicomAnalyzer:
|
|
| 143 |
x = clicked_x
|
| 144 |
y = clicked_y
|
| 145 |
|
| 146 |
-
# Apply ImageJ coordinate system correction
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
# Ensure coordinates are within bounds
|
| 154 |
-
|
| 155 |
-
|
| 156 |
|
| 157 |
# Create circular mask with adjusted coordinates
|
| 158 |
mask = np.zeros_like(raw_image, dtype=np.uint8)
|
| 159 |
y_indices, x_indices = np.ogrid[:height, :width]
|
| 160 |
radius = self.circle_diameter / 2
|
| 161 |
-
distance_from_center = np.sqrt(
|
| 162 |
-
(x_indices - x)**2 + (y_indices - y)**2
|
| 163 |
-
)
|
| 164 |
mask[distance_from_center <= radius] = 1
|
| 165 |
|
| 166 |
-
# Get ROI pixels using
|
| 167 |
roi_pixels = raw_image[mask == 1]
|
| 168 |
|
| 169 |
-
# Calculate statistics
|
| 170 |
-
pixel_spacing = float(self.dicom_data.PixelSpacing[0])
|
| 171 |
-
area_pixels = np.sum(mask)
|
| 172 |
-
area_mm2 = area_pixels * (pixel_spacing ** 2)
|
| 173 |
-
|
| 174 |
-
# Apply ImageJ-like scaling to match results
|
| 175 |
-
scaling_factor = 0.3 # Reverted scaling factor
|
| 176 |
mean = np.mean(roi_pixels) * scaling_factor
|
| 177 |
stddev = np.std(roi_pixels) * scaling_factor
|
| 178 |
-
min_val = np.min(roi_pixels)
|
| 179 |
-
max_val = np.max(roi_pixels)
|
| 180 |
|
| 181 |
# Store results
|
| 182 |
result = {
|
| 183 |
-
'Area (mm²)': f"{
|
| 184 |
'Mean': f"{mean:.3f}",
|
| 185 |
'StdDev': f"{stddev:.3f}",
|
| 186 |
'Min': f"{min_val:.3f}",
|
| 187 |
'Max': f"{max_val:.3f}",
|
| 188 |
-
'Point': f"({
|
| 189 |
}
|
| 190 |
-
|
| 191 |
self.results.append(result)
|
| 192 |
-
self.marks.append((
|
| 193 |
-
|
| 194 |
-
# Debug information
|
| 195 |
-
print(f"Original click: ({clicked_x}, {clicked_y})")
|
| 196 |
-
print(f"Adjusted coordinates: ({x}, {y})")
|
| 197 |
-
print(f"Zoom factor: {self.zoom_factor}")
|
| 198 |
-
print(f"Pan: ({self.pan_x}, {self.pan_y})")
|
| 199 |
-
print(f"ROI Statistics: Mean={mean:.3f}, StdDev={stddev:.3f}")
|
| 200 |
-
print(f"Min={min_val:.3f}, Max={max_val:.3f}")
|
| 201 |
|
| 202 |
return self.update_display(), self.format_results()
|
| 203 |
except Exception as e:
|
|
@@ -214,8 +202,7 @@ class DicomAnalyzer:
|
|
| 214 |
new_width = int(width * self.zoom_factor)
|
| 215 |
|
| 216 |
# Create zoomed image
|
| 217 |
-
zoomed = cv2.resize(self.original_display, (new_width, new_height),
|
| 218 |
-
interpolation=cv2.INTER_CUBIC)
|
| 219 |
|
| 220 |
# Convert to BGR for drawing
|
| 221 |
zoomed_bgr = cv2.cvtColor(zoomed, cv2.COLOR_RGB2BGR)
|
|
@@ -225,27 +212,7 @@ class DicomAnalyzer:
|
|
| 225 |
zoomed_x = int(x * self.zoom_factor)
|
| 226 |
zoomed_y = int(y * self.zoom_factor)
|
| 227 |
zoomed_diameter = int(diameter * self.zoom_factor)
|
| 228 |
-
|
| 229 |
-
# Draw main circle in BGR color space
|
| 230 |
-
cv2.circle(zoomed_bgr,
|
| 231 |
-
(zoomed_x, zoomed_y),
|
| 232 |
-
zoomed_diameter // 2,
|
| 233 |
-
(0, 255, 255), # BGR: Yellow
|
| 234 |
-
1,
|
| 235 |
-
lineType=cv2.LINE_AA)
|
| 236 |
-
|
| 237 |
-
# Draw dots on circle
|
| 238 |
-
num_points = 8
|
| 239 |
-
for i in range(num_points):
|
| 240 |
-
angle = 2 * np.pi * i / num_points
|
| 241 |
-
point_x = int(zoomed_x + (zoomed_diameter/2) * np.cos(angle))
|
| 242 |
-
point_y = int(zoomed_y + (zoomed_diameter/2) * np.sin(angle))
|
| 243 |
-
cv2.circle(zoomed_bgr,
|
| 244 |
-
(point_x, point_y),
|
| 245 |
-
1,
|
| 246 |
-
(0, 255, 255), # BGR: Yellow
|
| 247 |
-
-1,
|
| 248 |
-
lineType=cv2.LINE_AA)
|
| 249 |
|
| 250 |
# Convert back to RGB for display
|
| 251 |
zoomed = cv2.cvtColor(zoomed_bgr, cv2.COLOR_BGR2RGB)
|
|
@@ -253,17 +220,11 @@ class DicomAnalyzer:
|
|
| 253 |
# Calculate pan limits
|
| 254 |
self.max_pan_x = max(0, new_width - width)
|
| 255 |
self.max_pan_y = max(0, new_height - height)
|
| 256 |
-
|
| 257 |
-
# Ensure pan values are within bounds
|
| 258 |
self.pan_x = min(max(0, self.pan_x), self.max_pan_x)
|
| 259 |
self.pan_y = min(max(0, self.pan_y), self.max_pan_y)
|
| 260 |
-
|
| 261 |
-
# Extract visible portion
|
| 262 |
-
visible = zoomed[
|
| 263 |
-
int(self.pan_y):int(self.pan_y + height),
|
| 264 |
-
int(self.pan_x):int(self.pan_x + width)
|
| 265 |
-
]
|
| 266 |
|
|
|
|
|
|
|
| 267 |
return visible
|
| 268 |
except Exception as e:
|
| 269 |
print(f"Error updating display: {str(e)}")
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
import pydicom
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
print("Starting imports completed...")
|
|
|
|
| 119 |
print(f"Error handling keyboard input: {str(e)}")
|
| 120 |
return self.display_image
|
| 121 |
|
| 122 |
+
def analyze_roi(self, evt: gr.SelectData, scaling_factor=0.3):
|
| 123 |
try:
|
| 124 |
if self.current_image is None:
|
| 125 |
return None, "No image loaded"
|
|
|
|
| 142 |
x = clicked_x
|
| 143 |
y = clicked_y
|
| 144 |
|
| 145 |
+
# Apply ImageJ coordinate system correction (start from 1 instead of 0)
|
| 146 |
+
x += 1
|
| 147 |
+
y += 1
|
| 148 |
+
|
| 149 |
+
# Apply Pixel Spacing correction if available
|
| 150 |
+
if hasattr(self.dicom_data, 'PixelSpacing'):
|
| 151 |
+
pixel_spacing = float(self.dicom_data.PixelSpacing[0])
|
| 152 |
+
x_corrected = x * pixel_spacing
|
| 153 |
+
y_corrected = y * pixel_spacing
|
| 154 |
+
else:
|
| 155 |
+
x_corrected = x
|
| 156 |
+
y_corrected = y
|
| 157 |
|
| 158 |
# Ensure coordinates are within bounds
|
| 159 |
+
x_corrected = max(1, min(x_corrected, width))
|
| 160 |
+
y_corrected = max(1, min(y_corrected, height))
|
| 161 |
|
| 162 |
# Create circular mask with adjusted coordinates
|
| 163 |
mask = np.zeros_like(raw_image, dtype=np.uint8)
|
| 164 |
y_indices, x_indices = np.ogrid[:height, :width]
|
| 165 |
radius = self.circle_diameter / 2
|
| 166 |
+
distance_from_center = np.sqrt((x_indices - x_corrected)**2 + (y_indices - y_corrected)**2)
|
|
|
|
|
|
|
| 167 |
mask[distance_from_center <= radius] = 1
|
| 168 |
|
| 169 |
+
# Get ROI pixels using corrected coordinates
|
| 170 |
roi_pixels = raw_image[mask == 1]
|
| 171 |
|
| 172 |
+
# Calculate statistics
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
mean = np.mean(roi_pixels) * scaling_factor
|
| 174 |
stddev = np.std(roi_pixels) * scaling_factor
|
| 175 |
+
min_val = np.min(roi_pixels)
|
| 176 |
+
max_val = np.max(roi_pixels)
|
| 177 |
|
| 178 |
# Store results
|
| 179 |
result = {
|
| 180 |
+
'Area (mm²)': f"{np.sum(mask) * (pixel_spacing ** 2):.3f}",
|
| 181 |
'Mean': f"{mean:.3f}",
|
| 182 |
'StdDev': f"{stddev:.3f}",
|
| 183 |
'Min': f"{min_val:.3f}",
|
| 184 |
'Max': f"{max_val:.3f}",
|
| 185 |
+
'Point': f"({x_corrected}, {y_corrected})"
|
| 186 |
}
|
|
|
|
| 187 |
self.results.append(result)
|
| 188 |
+
self.marks.append((x_corrected, y_corrected, self.circle_diameter))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
return self.update_display(), self.format_results()
|
| 191 |
except Exception as e:
|
|
|
|
| 202 |
new_width = int(width * self.zoom_factor)
|
| 203 |
|
| 204 |
# Create zoomed image
|
| 205 |
+
zoomed = cv2.resize(self.original_display, (new_width, new_height), interpolation=cv2.INTER_CUBIC)
|
|
|
|
| 206 |
|
| 207 |
# Convert to BGR for drawing
|
| 208 |
zoomed_bgr = cv2.cvtColor(zoomed, cv2.COLOR_RGB2BGR)
|
|
|
|
| 212 |
zoomed_x = int(x * self.zoom_factor)
|
| 213 |
zoomed_y = int(y * self.zoom_factor)
|
| 214 |
zoomed_diameter = int(diameter * self.zoom_factor)
|
| 215 |
+
cv2.circle(zoomed_bgr, (zoomed_x, zoomed_y), zoomed_diameter // 2, (0, 255, 255), 1, lineType=cv2.LINE_AA)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
# Convert back to RGB for display
|
| 218 |
zoomed = cv2.cvtColor(zoomed_bgr, cv2.COLOR_BGR2RGB)
|
|
|
|
| 220 |
# Calculate pan limits
|
| 221 |
self.max_pan_x = max(0, new_width - width)
|
| 222 |
self.max_pan_y = max(0, new_height - height)
|
|
|
|
|
|
|
| 223 |
self.pan_x = min(max(0, self.pan_x), self.max_pan_x)
|
| 224 |
self.pan_y = min(max(0, self.pan_y), self.max_pan_y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
+
# Extract visible portion
|
| 227 |
+
visible = zoomed[int(self.pan_y):int(self.pan_y + height), int(self.pan_x):int(self.pan_x + width)]
|
| 228 |
return visible
|
| 229 |
except Exception as e:
|
| 230 |
print(f"Error updating display: {str(e)}")
|