HeshamAI commited on
Commit
ba7fac0
·
verified ·
1 Parent(s): 7339e86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -59
app.py CHANGED
@@ -96,7 +96,7 @@ class DicomAnalyzer:
96
  def handle_keyboard(self, key):
97
  try:
98
  print(f"Handling key press: {key}")
99
- pan_amount = int(5 * self.zoom_factor) # Reduced pan amount for smoother movement
100
 
101
  original_pan_x = self.pan_x
102
  original_pan_y = self.pan_y
@@ -110,7 +110,6 @@ class DicomAnalyzer:
110
  elif key == 'ArrowDown':
111
  self.pan_y = min(self.max_pan_y, self.pan_y + pan_amount)
112
 
113
- # Print debug info
114
  print(f"Pan X: {self.pan_x} (was {original_pan_x})")
115
  print(f"Pan Y: {self.pan_y} (was {original_pan_y})")
116
  print(f"Max Pan X: {self.max_pan_x}")
@@ -126,11 +125,9 @@ class DicomAnalyzer:
126
  if self.current_image is None:
127
  return None, "No image loaded"
128
 
129
- # Convert clicked coordinates considering zoom and pan
130
  x = int((evt.index[0] + self.pan_x) / self.zoom_factor)
131
  y = int((evt.index[1] + self.pan_y) / self.zoom_factor)
132
 
133
- # Ensure coordinates are within image bounds
134
  height, width = self.current_image.shape[:2]
135
  x = max(0, min(x, width-1))
136
  y = max(0, min(y, height-1))
@@ -171,69 +168,69 @@ class DicomAnalyzer:
171
  return self.display_image, f"Error analyzing ROI: {str(e)}"
172
 
173
  def update_display(self):
174
- try:
175
- if self.original_display is None:
176
- return None
177
 
178
- height, width = self.original_display.shape[:2]
179
- new_height = int(height * self.zoom_factor)
180
- new_width = int(width * self.zoom_factor)
181
 
182
- # Create zoomed image
183
- zoomed = cv2.resize(self.original_display, (new_width, new_height),
184
- interpolation=cv2.INTER_CUBIC)
185
 
186
- # Convert to BGR for drawing
187
- zoomed_bgr = cv2.cvtColor(zoomed, cv2.COLOR_RGB2BGR)
188
 
189
- # Draw marks
190
- for x, y, diameter in self.marks:
191
- zoomed_x = int(x * self.zoom_factor)
192
- zoomed_y = int(y * self.zoom_factor)
193
- zoomed_diameter = int(diameter * self.zoom_factor)
194
-
195
- # Draw main circle in BGR color space
196
- cv2.circle(zoomed_bgr,
197
- (zoomed_x, zoomed_y),
198
- zoomed_diameter // 2,
199
- (0, 255, 255), # BGR: Yellow
200
- 1,
201
- lineType=cv2.LINE_AA)
202
-
203
- # Draw dots on circle
204
- num_points = 8
205
- for i in range(num_points):
206
- angle = 2 * np.pi * i / num_points
207
- point_x = int(zoomed_x + (zoomed_diameter/2) * np.cos(angle))
208
- point_y = int(zoomed_y + (zoomed_diameter/2) * np.sin(angle))
209
  cv2.circle(zoomed_bgr,
210
- (point_x, point_y),
211
- 1,
212
  (0, 255, 255), # BGR: Yellow
213
- -1,
214
  lineType=cv2.LINE_AA)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
 
216
- # Convert back to RGB for display
217
- zoomed = cv2.cvtColor(zoomed_bgr, cv2.COLOR_BGR2RGB)
218
-
219
- # Calculate pan limits
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 are within bounds
224
- self.pan_x = min(max(0, self.pan_x), self.max_pan_x)
225
- self.pan_y = min(max(0, self.pan_y), self.max_pan_y)
226
-
227
- # Extract visible portion
228
- visible = zoomed[
229
- int(self.pan_y):int(self.pan_y + height),
230
- int(self.pan_x):int(self.pan_x + 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:
 
96
  def handle_keyboard(self, key):
97
  try:
98
  print(f"Handling key press: {key}")
99
+ pan_amount = int(5 * self.zoom_factor)
100
 
101
  original_pan_x = self.pan_x
102
  original_pan_y = self.pan_y
 
110
  elif key == 'ArrowDown':
111
  self.pan_y = min(self.max_pan_y, self.pan_y + pan_amount)
112
 
 
113
  print(f"Pan X: {self.pan_x} (was {original_pan_x})")
114
  print(f"Pan Y: {self.pan_y} (was {original_pan_y})")
115
  print(f"Max Pan X: {self.max_pan_x}")
 
125
  if self.current_image is None:
126
  return None, "No image loaded"
127
 
 
128
  x = int((evt.index[0] + self.pan_x) / self.zoom_factor)
129
  y = int((evt.index[1] + self.pan_y) / self.zoom_factor)
130
 
 
131
  height, width = self.current_image.shape[:2]
132
  x = max(0, min(x, width-1))
133
  y = max(0, min(y, height-1))
 
168
  return self.display_image, f"Error analyzing ROI: {str(e)}"
169
 
170
  def update_display(self):
171
+ try:
172
+ if self.original_display is None:
173
+ return None
174
 
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
+ # Convert to BGR for drawing
184
+ zoomed_bgr = cv2.cvtColor(zoomed, cv2.COLOR_RGB2BGR)
185
 
186
+ # Draw marks
187
+ for x, y, diameter in self.marks:
188
+ zoomed_x = int(x * self.zoom_factor)
189
+ zoomed_y = int(y * self.zoom_factor)
190
+ zoomed_diameter = int(diameter * self.zoom_factor)
191
+
192
+ # Draw main circle in BGR color space
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  cv2.circle(zoomed_bgr,
194
+ (zoomed_x, zoomed_y),
195
+ zoomed_diameter // 2,
196
  (0, 255, 255), # BGR: Yellow
197
+ 1,
198
  lineType=cv2.LINE_AA)
199
+
200
+ # Draw dots on circle
201
+ num_points = 8
202
+ for i in range(num_points):
203
+ angle = 2 * np.pi * i / num_points
204
+ point_x = int(zoomed_x + (zoomed_diameter/2) * np.cos(angle))
205
+ point_y = int(zoomed_y + (zoomed_diameter/2) * np.sin(angle))
206
+ cv2.circle(zoomed_bgr,
207
+ (point_x, point_y),
208
+ 1,
209
+ (0, 255, 255), # BGR: Yellow
210
+ -1,
211
+ lineType=cv2.LINE_AA)
212
+
213
+ # Convert back to RGB for display
214
+ zoomed = cv2.cvtColor(zoomed_bgr, cv2.COLOR_BGR2RGB)
215
+
216
+ # Calculate pan limits
217
+ self.max_pan_x = max(0, new_width - width)
218
+ self.max_pan_y = max(0, new_height - height)
219
+
220
+ # Ensure pan values are within bounds
221
+ self.pan_x = min(max(0, self.pan_x), self.max_pan_x)
222
+ self.pan_y = min(max(0, self.pan_y), self.max_pan_y)
223
+
224
+ # Extract visible portion
225
+ visible = zoomed[
226
+ int(self.pan_y):int(self.pan_y + height),
227
+ int(self.pan_x):int(self.pan_x + width)
228
+ ]
229
 
230
+ return visible
231
+ except Exception as e:
232
+ print(f"Error updating display: {str(e)}")
233
+ return self.original_display
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
 
235
  def format_results(self):
236
  if not self.results: