Skip to content

BUG: ndinterp.affine_transform() creates an image with black border, in each direction #20038

Open

Description

@LucaMarconato

Describe your issue.

I am trying to upscale an image by 2x using ndinterp.affine_transform() and I am comparing the result with a "ground truth" obtained using napari. What I get is almost correct: the output image is upscaled and correctly aligned, but I get a black border, around direction of the image.

I don't know if I am not using the function correctly or if I stumbled upon a bug of affine_transform().

I show 4 screenshots of napari.

original image

Screenshot 2024-02-06 at 23 26 12

"ground truth" upscaled imaged

overlayed to the original image (the small, semi-transparent one). Notice how the center of the (0, 0) pixels are the same location for both the small and large image. This is how napari operates.
Screenshot 2024-02-06 at 23 26 20

transformed image

Screenshot 2024-02-06 at 23 26 39

transformed image

overlayed to the "ground truth" image; notice that the transformed image is correctly aligned
Screenshot 2024-02-06 at 23 26 46

Reproducing Code Example

import numpy as np
import napari
from scipy.ndimage import affine_transform

viewer = napari.Viewer()

# create image
y = 2
x = 2
c = 3
image = np.random.rand(y, x, c)

# add raw image to napari
viewer.add_image(image, name="original")

# scale factor
k = 2
# add raw image to napari, with an affine scale transformation
scale = np.array(
    [
        [k, 0, 0],
        [0, k, 0],
        [0, 0, 1],
    ]
)
viewer.add_image(image, name="scaled", affine=scale)

# half-pixel offset, since the origin is at the center of the pixel
pre_scale = np.array(
    [
        [1, 0, 0, 0.5],
        [0, 1, 0, 0.5],
        [0, 0, 1, 0],
        [0, 0, 0, 1],
    ]
)
inverse_scale = np.array(
    [
        [1 / k, 0, 0, 0],
        [0, 1 / k, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1],
    ]
)
post_scale = np.array(
    [
        [1, 0, 0, -0.5],
        [0, 1, 0, -0.5],
        [0, 0, 1, 0],
        [0, 0, 0, 1],
    ]
)
affine = post_scale @ inverse_scale @ pre_scale

transformed = affine_transform(image, matrix=affine, output_shape=[y * 2, x * 2, c], order=0, prefilter=False)
# we need to add a translation to napari, since we want that the center of the (0, 0) pixel to coincide 
# for both the original and transformed image, and the pixel sizes are different
offset_for_transformed = np.array(
    [
        [1, 0, -0.5 * k + 0.5],
        [0, 1, -0.5 * k + 0.5],
        [0, 0, 1],
    ]
)
viewer.add_image(transformed, name="transformed", affine=offset_for_transformed)

napari.run()

Error message

No error message

SciPy/NumPy/Python version and system information

1.11.4 1.26.3 sys.version_info(major=3, minor=10, micro=13, releaselevel='final', serial=0)
Build Dependencies:
  blas:
    detection method: pkgconfig
    found: true
    include directory: /Users/macbook/miniconda3/envs/ome/include
    lib directory: /Users/macbook/miniconda3/envs/ome/lib
    name: blas
    openblas configuration: unknown
    pc file directory: /Users/macbook/miniconda3/envs/ome/lib/pkgconfig
    version: 3.9.0
  lapack:
    detection method: pkgconfig
    found: true
    include directory: /Users/macbook/miniconda3/envs/ome/include
    lib directory: /Users/macbook/miniconda3/envs/ome/lib
    name: lapack
    openblas configuration: unknown
    pc file directory: /Users/macbook/miniconda3/envs/ome/lib/pkgconfig
    version: 3.9.0
  pybind11:
    detection method: pkgconfig
    include directory: /Users/macbook/miniconda3/envs/ome/include
    name: pybind11
    version: 2.11.1
Compilers:
  c:
    commands: arm64-apple-darwin20.0.0-clang
    linker: ld64
    name: clang
    version: 16.0.6
  c++:
    commands: arm64-apple-darwin20.0.0-clang++
    linker: ld64
    name: clang
    version: 16.0.6
  cython:
    commands: cython
    linker: cython
    name: cython
    version: 0.29.36
  fortran:
    commands: /Users/runner/miniforge3/conda-bld/scipy-split_1700812700233/_build_env/bin/arm64-apple-darwin20.0.0-gfortran
    linker: ld64
    name: gcc
    version: 12.3.0
  pythran:
    include directory: ../../_build_env/venv/lib/python3.10/site-packages/pythran
    version: 0.14.0
Machine Information:
  build:
    cpu: x86_64
    endian: little
    family: x86_64
    system: darwin
  cross-compiled: true
  host:
    cpu: arm64
    endian: little
    family: aarch64
    system: darwin
Python Information:
  path: /Users/macbook/miniconda3/envs/ome/bin/python
  version: '3.10'

Activity

  1. added
    defectA clear bug or issue that prevents SciPy from being installed or used as expected
    on Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    defectA clear bug or issue that prevents SciPy from being installed or used as expectedscipy.ndimage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions