scverse / scverse/spatialdata

Reported issue with bounding box query and rotated images

Open
#949 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

method: query
Dominant language
Python
Stars
394
Forks
95
Avg merge
4d 3h
Merged PRs (30d)
7

Description

Reported via email by a user.

I contact you because of a minor issue, though. I have an image of a tissue section and I would like to crop an area of interest. The best way for me to select that area is actually by rotating the image and then just drawing a rectangle around. However, I find that the cropping is not performed on the rotated, new coordinates. Could you help me with any suggestion about how to address this issue?

I write down below the key code snippets, but happy to share with you a jupyter notebook with a reproducible example.

# Using data from registry transcriptomics/resolve/mouse/20272_slide1_A1-1_DAPI.tiff


OUTPUT_DIR = tempfile.gettempdir()
img_layer = "raw_image"
path = None
registry = get_registry(path=path)
path_image = registry.fetch("transcriptomics/resolve/mouse/20272_slide1_A1-1_DAPI.tiff")
zarr_path = os.path.join(OUTPUT_DIR, f"sdata_{uuid.uuid4()}.zarr")

sdata = sp.io.create_sdata(
   input=path_image,
   output_path=zarr_path,
   img_layer=img_layer,
   chunks=1024,

# Rotate
import math

import matplotlib.pyplot as plt
import spatialdata
import spatialdata_plot
from spatialdata.transformations import (
   Affine,
   MapAxis,
   Scale,
   Sequence,
   Translation,
   get_transformation,
   set_transformation,
)

theta = math.radians(45)
rotation = Affine(
   [
       [math.cos(theta), -math.sin(theta), 0],
       [math.sin(theta), math.cos(theta), 0],
       [0, 0, 1],
   ],
   input_axes=("x", "y"),
   output_axes=("x", "y"),
)

set_transformation(sdata.images["raw_image"], rotation, to_coordinate_system="rotation_test")

sdata.pl.render_images().pl.render_labels().pl.render_shapes().pl.show()

# Bounding box
bb_xmin = -4000
bb_ymin = 5000
bb_w = 2500
bb_h = 7000
bb_xmax = bb_xmin + bb_w
bb_ymax = bb_ymin + bb_h

f, ax = plt.subplots(figsize=(7, 7))
sdata.pl.render_images('raw_image').pl.show(ax=ax, coordinate_systems=['rotation_test'])
#sdata.pl.render_shapes().pl.show(ax=ax)
rect = patches.Rectangle((bb_xmin, bb_ymin), bb_w, bb_h, linewidth=5, edgecolor="red", facecolor="none")
ax.add_patch(rect)

sdata_cropped = sdata.query.bounding_box(
min_coordinate=[bb_xmin, bb_ymin],
max_coordinate=[bb_xmax, bb_ymax],
axes=("x", "y"),
target_coordinate_system="rotation_test")

# Cropping not performed as intended:
sdata_cropped.pl.render_images('raw_image').pl.show(coordinate_systems=['global', 'rotation_test'])

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example using set_transformation on sdata.images["raw_image"], render_images with coordinate system "rotation_test", and sdata.query.bounding_box targeting that system. Trace how the bounding box coordinates are interpreted relative to the rotation; done means the queried dataset contains the intended rotated rectangular region and renders consistently in the shown coordinate systems.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.