Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix AffineWSITransformer.read_rect() #670

Merged
merged 7 commits into from
Jul 31, 2023

Large diffs are not rendered by default.

40 changes: 25 additions & 15 deletions tiatoolbox/tools/registration/wsi_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,7 @@ def get_patch_dimensions(
y = np.array(list(itertools.chain.from_iterable(y)))

points = np.array([x, y]).transpose()
transform = transform * [[1, 1, 0], [1, 1, 0], [1, 1, 1]] # remove translation
transform_points = self.transform_points(points, transform)

width = np.max(transform_points[:, 0]) - np.min(transform_points[:, 0]) + 1
Expand Down Expand Up @@ -1616,9 +1617,9 @@ def read_rect(
"""
(
read_level,
_,
_,
_post_read_scale,
_level_location,
level_size,
post_read_scale,
_baseline_read_size,
) = self.wsi_reader.find_read_rect_params(
location=location,
Expand All @@ -1628,19 +1629,28 @@ def read_rect(
)
transformed_location, max_size = self.get_transformed_location(
location,
size,
level_size,
read_level,
)
patch = self.wsi_reader.read_rect(
transformed_location,
max_size,
resolution=resolution,
units=units,
)

# Read at optimal level and corrected read size
patch = self.wsi_reader.read_region(transformed_location, read_level, max_size)
patch = np.array(patch)

# Apply transformation
transformed_patch = self.transform_patch(patch, max_size)

start_row = int(max_size[1] / 2) - int(size[1] / 2)
end_row = int(max_size[1] / 2) + int(size[1] / 2)
start_col = int(max_size[0] / 2) - int(size[0] / 2)
end_col = int(max_size[0] / 2) + int(size[0] / 2)
return transformed_patch[start_row:end_row, start_col:end_col, :]
# Crop to get rid of black borders due to rotation
start_row = int(max_size[1] / 2) - int(level_size[1] / 2)
end_row = int(max_size[1] / 2) + int(level_size[1] / 2)
start_col = int(max_size[0] / 2) - int(level_size[0] / 2)
end_col = int(max_size[0] / 2) + int(level_size[0] / 2)
transformed_patch = transformed_patch[start_row:end_row, start_col:end_col, :]

# Resize to desired size
return imresize(
img=transformed_patch,
scale_factor=post_read_scale,
output_size=size,
interpolation="optimise",
)
Loading