pr28416 commited on
Commit
354b84f
·
1 Parent(s): 7fc3883

Add detailed debugging for minimum radius calculation

Browse files
Files changed (1) hide show
  1. streamlit_app.py +24 -10
streamlit_app.py CHANGED
@@ -341,10 +341,11 @@ if uploaded is not None:
341
  preview_h, preview_w = pil_img.size[1], pil_img.size[0]
342
  scale_factor = orig_w / preview_w # How much the preview is downsampled
343
 
 
344
  slice_img = st_cropper(pil_img, aspect_ratio=None, box_color="#00FF00")
345
  snp = np.array(slice_img)
346
  h, w = snp.shape[:2]
347
-
348
  if h > 0 and w > 0:
349
  # Get original physical dimensions to maintain pixel-to-micron ratio
350
  s = st.session_state.get("_settings", {})
@@ -359,13 +360,15 @@ if uploaded is not None:
359
  orig_crop_w = w * scale_factor
360
  orig_crop_h = h * scale_factor
361
 
362
- # Save the slice image (keep it at preview resolution for fast processing)
 
363
  actual_h, actual_w = h, w
364
- # Note: We keep the slice at preview resolution but calculate correct physical dimensions
365
-
366
- # Calculate physical dimensions based on ORIGINAL scale crop dimensions
367
- slice_width_um = orig_crop_w * true_px_size_x_um
368
- slice_height_um = orig_crop_h * true_px_size_y_um
 
369
 
370
  roi_path = os.path.join(prev_dir, "slice.png")
371
  iio.imwrite(roi_path, snp)
@@ -426,6 +429,17 @@ if uploaded is not None:
426
  st.write(
427
  f"🔧 Debug: Function will calculate px_size: {calc_px_size_x:.4f}×{calc_px_size_y:.4f} µm/px"
428
  )
 
 
 
 
 
 
 
 
 
 
 
429
 
430
  slice_count, _ = _count_dots_on_preview(
431
  preview_png_path=roi_path,
@@ -435,9 +449,9 @@ if uploaded is not None:
435
  threshold=0.03,
436
  overlap=0.5,
437
  downsample=downsample,
438
- width_um=slice_width_um, # Use calculated slice dimensions
439
- height_um=slice_height_um, # Use calculated slice dimensions
440
- min_diam_um=min_diam_um,
441
  threshold_mode=threshold_mode,
442
  thresh_percent=float(thresh_percent),
443
  threshold_scale=float(threshold_scale),
 
341
  preview_h, preview_w = pil_img.size[1], pil_img.size[0]
342
  scale_factor = orig_w / preview_w # How much the preview is downsampled
343
 
344
+ # Get crop coordinates from the cropper (working on preview image)
345
  slice_img = st_cropper(pil_img, aspect_ratio=None, box_color="#00FF00")
346
  snp = np.array(slice_img)
347
  h, w = snp.shape[:2]
348
+
349
  if h > 0 and w > 0:
350
  # Get original physical dimensions to maintain pixel-to-micron ratio
351
  s = st.session_state.get("_settings", {})
 
360
  orig_crop_w = w * scale_factor
361
  orig_crop_h = h * scale_factor
362
 
363
+ # SIMPLE CORRECT APPROACH:
364
+ # We save h×w pixels from the preview, so physical size = h×w * true_pixel_size
365
  actual_h, actual_w = h, w
366
+ slice_width_um = (
367
+ actual_w * true_px_size_x_um
368
+ ) # e.g. 1229 * 0.203 = 249.7 µm
369
+ slice_height_um = (
370
+ actual_h * true_px_size_y_um
371
+ ) # e.g. 1228 * 0.203 = 249.5 µm
372
 
373
  roi_path = os.path.join(prev_dir, "slice.png")
374
  iio.imwrite(roi_path, snp)
 
429
  st.write(
430
  f"🔧 Debug: Function will calculate px_size: {calc_px_size_x:.4f}×{calc_px_size_y:.4f} µm/px"
431
  )
432
+
433
+ # Calculate what the function will compute for min_radius_px
434
+ avg_calc_px_size = np.sqrt(calc_px_size_x * calc_px_size_y)
435
+ expected_min_radius_full = (min_diam_um / avg_calc_px_size) / 2.0
436
+ expected_min_radius_final = expected_min_radius_full / downsample if downsample > 1 else expected_min_radius_full
437
+ st.write(
438
+ f"🔧 Debug: Min radius calculation: {min_diam_um}µm / {avg_calc_px_size:.4f}µm/px / 2 = {expected_min_radius_full:.1f}px"
439
+ )
440
+ st.write(
441
+ f"🔧 Debug: After downsample ÷{downsample}: {expected_min_radius_final:.1f}px final threshold"
442
+ )
443
 
444
  slice_count, _ = _count_dots_on_preview(
445
  preview_png_path=roi_path,
 
449
  threshold=0.03,
450
  overlap=0.5,
451
  downsample=downsample,
452
+ width_um=slice_width_um, # Correct physical dimensions
453
+ height_um=slice_height_um, # Correct physical dimensions
454
+ min_diam_um=min_diam_um, # Use original min diameter
455
  threshold_mode=threshold_mode,
456
  thresh_percent=float(thresh_percent),
457
  threshold_scale=float(threshold_scale),