HeshamAI commited on
Commit
bfb3785
·
verified ·
1 Parent(s): 457e977

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -98
app.py CHANGED
@@ -59,24 +59,21 @@ class DicomAnalyzer:
59
  return None, f"Error loading DICOM file: {str(e)}"
60
 
61
  def normalize_image(self, image):
62
- try:
63
- # Improve image normalization
64
- normalized = cv2.normalize(
65
- image,
66
- None,
67
- alpha=0,
68
- beta=255,
69
- norm_type=cv2.NORM_MINMAX,
70
- dtype=cv2.CV_8U
71
- )
72
- if len(normalized.shape) == 2:
73
- normalized = cv2.cvtColor(normalized, cv2.COLOR_GRAY2RGB)
74
- else:
75
- normalized = cv2.cvtColor(normalized, cv2.COLOR_BGR2RGB)
76
- return normalized
77
- except Exception as e:
78
- print(f"Error normalizing image: {str(e)}")
79
- return None
80
 
81
  def reset_view(self):
82
  self.zoom_factor = 1.0
@@ -97,23 +94,28 @@ class DicomAnalyzer:
97
  return self.update_display()
98
 
99
  def handle_keyboard(self, key):
100
- try:
101
- print(f"Handling key press: {key}")
102
- pan_amount = int(10 * self.zoom_factor)
103
-
104
- if key == 'ArrowLeft':
105
- self.pan_x = max(0, self.pan_x - pan_amount)
106
- elif key == 'ArrowRight':
107
- self.pan_x = min(self.max_pan_x, self.pan_x + pan_amount)
108
- elif key == 'ArrowUp':
109
- self.pan_y = max(0, self.pan_y - pan_amount)
110
- elif key == 'ArrowDown':
111
- self.pan_y = min(self.max_pan_y, self.pan_y + pan_amount)
112
-
113
- return self.update_display()
114
- except Exception as e:
115
- print(f"Error handling keyboard input: {str(e)}")
116
- return self.display_image
 
 
 
 
 
117
 
118
  def analyze_roi(self, evt: gr.SelectData):
119
  try:
@@ -165,75 +167,68 @@ class DicomAnalyzer:
165
  return self.display_image, f"Error analyzing ROI: {str(e)}"
166
 
167
  def update_display(self):
168
- try:
169
- if self.original_display is None:
170
- return None
171
-
172
- # Calculate zoomed size
173
- height, width = self.original_display.shape[:2]
174
- new_height = int(height * self.zoom_factor)
175
- new_width = int(width * self.zoom_factor)
176
-
177
- # Create zoomed image
178
- zoomed = cv2.resize(self.original_display, (new_width, new_height),
179
- interpolation=cv2.INTER_CUBIC)
180
-
181
- # Convert to BGR for drawing
182
- zoomed_bgr = cv2.cvtColor(zoomed, cv2.COLOR_RGB2BGR)
183
 
184
- # Draw marks with ImageJ-like yellow circle
185
- for x, y, diameter in self.marks:
186
- # Calculate zoomed coordinates correctly
187
- zoomed_x = int(x * self.zoom_factor)
188
- zoomed_y = int(y * self.zoom_factor)
189
- zoomed_diameter = int(diameter * self.zoom_factor)
190
-
191
- # Draw main circle - Pure yellow
192
- cv2.circle(zoomed_bgr,
193
- (zoomed_x, zoomed_y),
194
- zoomed_diameter // 2,
195
- (255, 255, 0), # BGR: Pure yellow
196
- 1, # Thin line
197
- lineType=cv2.LINE_AA)
198
-
199
- # Add small points around circle perimeter
200
- num_points = 8
201
- for i in range(num_points):
202
- angle = 2 * np.pi * i / num_points
203
- point_x = int(zoomed_x + (zoomed_diameter/2) * np.cos(angle))
204
- point_y = int(zoomed_y + (zoomed_diameter/2) * np.sin(angle))
205
- cv2.circle(zoomed_bgr,
206
- (point_x, point_y),
207
- 1,
208
- (255, 255, 0), # BGR: Pure yellow
209
- -1,
210
- lineType=cv2.LINE_AA)
211
 
212
- # Convert back to RGB for display
213
- zoomed = cv2.cvtColor(zoomed_bgr, cv2.COLOR_BGR2RGB)
 
214
 
215
- # Extract visible portion considering pan
216
- visible_height = min(height, new_height)
217
- visible_width = min(width, new_width)
218
-
219
- # Calculate pan bounds
220
- self.max_pan_x = max(0, new_width - width)
221
- self.max_pan_y = max(0, new_height - height)
222
 
223
- # Ensure pan values don't exceed bounds
224
- self.pan_x = min(self.pan_x, self.max_pan_x)
225
- self.pan_y = min(self.pan_y, self.max_pan_y)
 
 
 
 
226
 
227
- # Extract correct portion of zoomed image
228
- visible = zoomed[
229
- self.pan_y:self.pan_y + visible_height,
230
- self.pan_x:self.pan_x + visible_width
231
- ]
 
 
 
 
 
 
 
232
 
233
- return visible
234
- except Exception as e:
235
- print(f"Error updating display: {str(e)}")
236
- return self.original_display
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
 
238
  def format_results(self):
239
  if not self.results:
 
59
  return None, f"Error loading DICOM file: {str(e)}"
60
 
61
  def normalize_image(self, image):
62
+ try:
63
+ normalized = cv2.normalize(
64
+ image,
65
+ None,
66
+ alpha=0,
67
+ beta=255,
68
+ norm_type=cv2.NORM_MINMAX,
69
+ dtype=cv2.CV_8U
70
+ )
71
+ if len(normalized.shape) == 2:
72
+ normalized = cv2.cvtColor(normalized, cv2.COLOR_GRAY2BGR)
73
+ return normalized
74
+ except Exception as e:
75
+ print(f"Error normalizing image: {str(e)}")
76
+ return None
 
 
 
77
 
78
  def reset_view(self):
79
  self.zoom_factor = 1.0
 
94
  return self.update_display()
95
 
96
  def handle_keyboard(self, key):
97
+ try:
98
+ print(f"Handling key press: {key}")
99
+ # Smaller pan amount for smoother movement
100
+ pan_amount = int(5 * self.zoom_factor)
101
+
102
+ if key == 'ArrowLeft':
103
+ self.pan_x = max(0, self.pan_x - pan_amount)
104
+ print(f"Pan X: {self.pan_x}")
105
+ elif key == 'ArrowRight':
106
+ self.pan_x = min(self.max_pan_x, self.pan_x + pan_amount)
107
+ print(f"Pan X: {self.pan_x}")
108
+ elif key == 'ArrowUp':
109
+ self.pan_y = max(0, self.pan_y - pan_amount)
110
+ print(f"Pan Y: {self.pan_y}")
111
+ elif key == 'ArrowDown':
112
+ self.pan_y = min(self.max_pan_y, self.pan_y + pan_amount)
113
+ print(f"Pan Y: {self.pan_y}")
114
+
115
+ return self.update_display()
116
+ except Exception as e:
117
+ print(f"Error handling keyboard input: {str(e)}")
118
+ return self.display_image
119
 
120
  def analyze_roi(self, evt: gr.SelectData):
121
  try:
 
167
  return self.display_image, f"Error analyzing ROI: {str(e)}"
168
 
169
  def update_display(self):
170
+ try:
171
+ if self.original_display is None:
172
+ return None
 
 
 
 
 
 
 
 
 
 
 
 
173
 
174
+ # Calculate zoomed size
175
+ height, width = self.original_display.shape[:2]
176
+ new_height = int(height * self.zoom_factor)
177
+ new_width = int(width * self.zoom_factor)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
 
179
+ # Create zoomed image
180
+ zoomed = cv2.resize(self.original_display, (new_width, new_height),
181
+ interpolation=cv2.INTER_CUBIC)
182
 
183
+ # Draw marks
184
+ for x, y, diameter in self.marks:
185
+ zoomed_x = int(x * self.zoom_factor)
186
+ zoomed_y = int(y * self.zoom_factor)
187
+ zoomed_diameter = int(diameter * self.zoom_factor)
 
 
188
 
189
+ # Draw yellow circle (BGR format)
190
+ cv2.circle(zoomed,
191
+ (zoomed_x, zoomed_y),
192
+ zoomed_diameter // 2,
193
+ (0, 255, 255), # BGR: Yellow
194
+ 1,
195
+ lineType=cv2.LINE_AA)
196
 
197
+ # Draw yellow dots
198
+ num_points = 8
199
+ for i in range(num_points):
200
+ angle = 2 * np.pi * i / num_points
201
+ point_x = int(zoomed_x + (zoomed_diameter/2) * np.cos(angle))
202
+ point_y = int(zoomed_y + (zoomed_diameter/2) * np.sin(angle))
203
+ cv2.circle(zoomed,
204
+ (point_x, point_y),
205
+ 1,
206
+ (0, 255, 255), # BGR: Yellow
207
+ -1,
208
+ lineType=cv2.LINE_AA)
209
 
210
+ # Extract visible portion
211
+ visible_height = min(height, new_height)
212
+ visible_width = min(width, new_width)
213
+
214
+ # Update max pan values
215
+ self.max_pan_x = new_width - width
216
+ self.max_pan_y = new_height - height
217
+
218
+ # Ensure pan values are within bounds
219
+ self.pan_x = min(max(0, self.pan_x), self.max_pan_x)
220
+ self.pan_y = min(max(0, self.pan_y), self.max_pan_y)
221
+
222
+ # Extract visible portion with pan
223
+ start_y = int(self.pan_y)
224
+ start_x = int(self.pan_x)
225
+ visible = zoomed[start_y:start_y + visible_height,
226
+ start_x:start_x + visible_width]
227
+
228
+ return visible
229
+ except Exception as e:
230
+ print(f"Error updating display: {str(e)}")
231
+ return self.original_display
232
 
233
  def format_results(self):
234
  if not self.results: