Skip to content

Commit

Permalink
add scale bars to zoom window
Browse files Browse the repository at this point in the history
  • Loading branch information
bunnie committed Feb 3, 2024
1 parent ab4b879 commit 77327c6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
2 changes: 0 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
UI_MIN_WIDTH = 1000
UI_MIN_HEIGHT = 1000

INITIAL_R = 1

TILES_VERSION = 1

PIEZO_UM_PER_LSB= 0.0058812 # from empirical measurements
Expand Down
8 changes: 8 additions & 0 deletions schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,28 @@ class Schema():
LAPLACIAN_WINDOW = None
FILTER_WINDOW = None
NOM_STEP = None
SCALE_BAR_WIDTH_UM = None
INITIAL_R = 1
@staticmethod
def set_mag(mag):
if mag == 20:
Schema.PIX_PER_UM = PIX_PER_UM_20X
Schema.LAPLACIAN_WINDOW = LAPLACIAN_WINDOW_20X
Schema.NOM_STEP = NOM_STEP_20x
Schema.FILTER_WINDOW = FILTER_WINDOW_20X
Schema.SCALE_BAR_WIDTH_UM = 5.0
elif mag == 5:
Schema.PIX_PER_UM = PIX_PER_UM_5X
Schema.LAPLACIAN_WINDOW = LAPLACIAN_WINDOW_5X
Schema.NOM_STEP = NOM_STEP_5x
Schema.FILTER_WINDOW = FILTER_WINDOW_5X
Schema.SCALE_BAR_WIDTH_UM = 20.0
elif mag == 10:
Schema.PIX_PER_UM = PIX_PER_UM_10X
Schema.LAPLACIAN_WINDOW = LAPLACIAN_WINDOW_10X
Schema.NOM_STEP = NOM_STEP_10x
Schema.FILTER_WINDOW = FILTER_WINDOW_10X
Schema.SCALE_BAR_WIDTH_UM = 10.0
else:
logging.error(f"Unhandled magnification parameter: {mag}")
@staticmethod
Expand All @@ -53,6 +58,9 @@ def set_laplacian(value):
@staticmethod
def set_filter(value):
Schema.FILTER_WINDOW = value
@staticmethod
def set_initial_r(value):
Schema.INITIAL_R = value

def __init__(self, use_cache=True):
self.schema = {
Expand Down
23 changes: 5 additions & 18 deletions stitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
from utils import *
from config import *

SCALE_BAR_WIDTH_UM = None

# TODO:
# - colorize the preview based on deviation from plane (triggered by button)
# - make a "heat map" for MSE post-autostitch? (triggered by button)
Expand Down Expand Up @@ -70,7 +68,7 @@ def check_thumbnails(args):

# Load based on filenames, and finalize the overall area
for file in files:
if '_r' + str(INITIAL_R) in file.stem: # filter image revs by the initial default rev
if '_r' + str(Schema.INITIAL_R) in file.stem: # filter image revs by the initial default rev
if force_generate or not (thumb_path / file.name).is_file:
img = cv2.imread(str(file), cv2.IMREAD_GRAYSCALE)
thumb = cv2.resize(img, None, None, fx=THUMB_SCALE, fy=THUMB_SCALE)
Expand All @@ -82,11 +80,11 @@ class MainWindow(QMainWindow):
from mse_stitch import stitch_one_mse
from template_stitch import stitch_one_template, stitch_auto_template_linear, restitch_one
from blend import blend
from zoom import update_zoom_window, on_cv_zoom, get_centered_and_scaled_image
from zoom import update_zoom_window, on_cv_zoom, get_centered_and_scaled_image, draw_scale_bar
from overview import redraw_overview, rescale_overview, update_selected_rect,\
centroid_to_tile_bounding_rect_mm, snap_range, check_res_bounds,\
pix_to_um_absolute, um_to_pix_absolute, preview_selection, get_coords_in_range,\
compute_selection_overlay, draw_rect_at_center, rect_at_center, on_focus_visualize
compute_selection_overlay, draw_rect_at_center, rect_at_center, on_focus_visualize, generate_fullres_overview

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -358,7 +356,7 @@ def new_schema(self, args):

# Load based on filenames, and finalize the overall area
for file in files:
if '_r' + str(INITIAL_R) in file.stem: # filter image revs by the initial default rev
if '_r' + str(Schema.INITIAL_R) in file.stem: # filter image revs by the initial default rev
self.schema.add_tile(file, max_x = args.max_x, max_y = args.max_y)
self.schema.finalize()
self.schema.set_undo_checkpoint()
Expand Down Expand Up @@ -460,19 +458,8 @@ def main():
raise ValueError('Invalid log level: %s' % args.loglevel)
logging.basicConfig(level=numeric_level)

global SCALE_BAR_WIDTH_UM
if args.mag == 20:
SCALE_BAR_WIDTH_UM = 5.0
elif args.mag == 5:
SCALE_BAR_WIDTH_UM = 20.0
elif args.mag == 10:
SCALE_BAR_WIDTH_UM = 10.0
else:
logging.error("Magnification parameters not defined")
exit(0)
global INITIAL_R
INITIAL_R = args.initial_r
Schema.set_mag(args.mag) # this must be called before we create the main window, so that the filter/laplacian values are correct by default
Schema.set_initial_r(args.initial_r)

if False: # run unit tests
from prims import Rect
Expand Down
33 changes: 30 additions & 3 deletions zoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from schema import Schema
import logging
from prims import Rect, Point
from config import *

WINDOW_SIZE_X = 2000
WINDOW_SIZE_Y = 2000
Expand All @@ -16,12 +17,14 @@ def on_cv_zoom(self, raw_value):
self.zoom_scale = ((1.0 - 0.1) / (20 - 0)) * raw_value + 0.1

img = self.get_centered_and_scaled_image()
cv2.imshow('zoom', cv2.resize(img, None, None, self.zoom_scale, self.zoom_scale))
rescaled = cv2.resize(img, None, None, self.zoom_scale, self.zoom_scale)
self.draw_scale_bar(rescaled)
cv2.imshow('zoom', rescaled)

def get_centered_and_scaled_image(self):
# ensure that the fullres dataset is pulled in. Warning: could take a while.
if self.overview_fullres is None:
self.redraw_overview(blend=False, force_full_res=True)
self.generate_fullres_overview(blend=False)

(x_c, y_c) = self.um_to_pix_absolute(self.roi_center_ums)
# scale of 2.0 means we are zooming in by 2x; 0.5 means we are zooming out by 2x
Expand All @@ -48,7 +51,31 @@ def get_centered_and_scaled_image(self):

def update_zoom_window(self):
img = self.get_centered_and_scaled_image()
cv2.imshow('zoom', cv2.resize(img, None, None, self.zoom_scale, self.zoom_scale))
rescaled = cv2.resize(img, None, None, self.zoom_scale, self.zoom_scale)
self.draw_scale_bar(rescaled)
cv2.imshow('zoom', rescaled)
if not self.trackbar_created:
cv2.createTrackbar('scale', 'zoom', 20, 100, self.on_cv_zoom)
self.trackbar_created = True

def draw_scale_bar(self, img):
base_width = Schema.SCALE_BAR_WIDTH_UM
scale_adjusted_width = base_width / self.zoom_scale
# draw scale bar
cv2.rectangle(
img,
(50, 50),
(int(50 + scale_adjusted_width * (Schema.PIX_PER_UM * self.zoom_scale)), 60),
(255, 255, 255),
thickness = 1,
lineType = cv2.LINE_4,
)
cv2.putText(
img,
f"{scale_adjusted_width:0.1f} um",
(50, 45),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(255, 255, 255),
bottomLeftOrigin=False
)

0 comments on commit 77327c6

Please sign in to comment.