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

Coordinate Assignment for Random Tiler #665

Open
NeurogliaNerd opened this issue Oct 31, 2023 · 1 comment
Open

Coordinate Assignment for Random Tiler #665

NeurogliaNerd opened this issue Oct 31, 2023 · 1 comment
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@NeurogliaNerd
Copy link

Hello @alessiamarcolini @Christopher22 ,

From my understanding, when I apply the RandomTiler in Histolab, I get a tile with coordinate information for the bottom left and top right vertices in the file name PVALB054_seed16777216_tile_1_level0_1980-700-2280-900 ( I added in the other information that is not bolded). However, I'd like to know how these coordinates are calculated, because the image I'm using in this particular example measures 1767 pixels wide by 1519 pixels high. How is Histolab arriving at the numbers it is exporting? I'd appreciate any clarification you may have. Thanks :)

@NeurogliaNerd NeurogliaNerd added help wanted Extra attention is needed question Further information is requested labels Oct 31, 2023
@alessiamarcolini
Copy link
Collaborator

Hey @NeurogliaNerd,

the random coordinates calculation is performed here:

histolab/histolab/tiler.py

Lines 741 to 781 in 52bb10b

def _random_tile_coordinates(
self, slide: Slide, extraction_mask: BinaryMask = BiggestTissueBoxMask()
) -> CoordinatePair:
"""Return 0-level Coordinates of a tile picked at random within the box.
Parameters
----------
slide : Slide
Slide from which calculate the coordinates. Needed to calculate the box.
extraction_mask : BinaryMask, optional
BinaryMask object defining how to compute a binary mask from a Slide.
Default `BiggestTissueBoxMask`.
Returns
-------
CoordinatePair
Random tile Coordinates at level 0
"""
binary_mask = extraction_mask(slide)
tile_w_lvl, tile_h_lvl = self.tile_size
x_ul_lvl, y_ul_lvl = random_choice_true_mask2d(binary_mask)
# Scale tile dimensions to extraction mask dimensions
tile_w_thumb = (
tile_w_lvl * binary_mask.shape[1] / slide.level_dimensions(self.level)[0]
)
tile_h_thumb = (
tile_h_lvl * binary_mask.shape[0] / slide.level_dimensions(self.level)[1]
)
x_br_lvl = x_ul_lvl + tile_w_thumb
y_br_lvl = y_ul_lvl + tile_h_thumb
tile_wsi_coords = scale_coordinates(
reference_coords=CoordinatePair(x_ul_lvl, y_ul_lvl, x_br_lvl, y_br_lvl),
reference_size=binary_mask.shape[::-1],
target_size=slide.dimensions,
)
return tile_wsi_coords

where

  • we sample a pair of random coordinates for the upper left corner of the tile from the binary extraction mask (which is calculated on the slide thumbnail),
  • then we scale the tile dimensions to the dimension of the binary extraction mask
  • Then we calculate the top left and bottom right coordinates at the requested level of extraction (which in your case is level 0).
  • Last step is to scale those coordinates to level 0 coordinates (which they already are in your case)

All of this is to avoid sampling over huge binary masks. Now, there could be some rounding errors, but I doubt it was SO off.

in this particular example measures 1767 pixels wide by 1519 pixels high

Did you check with slide.dimensions?

If so, could you share a code snippet and the image you used so we can figure it out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants