Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0c0b315

Browse filesBrowse files
committed
Fix an off-by-half-pixel bug when resampling under a nonaffine transform
1 parent 759765c commit 0c0b315
Copy full SHA for 0c0b315

File tree

2 files changed

+42
-5
lines changed
Filter options

2 files changed

+42
-5
lines changed

‎lib/matplotlib/tests/test_image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_image.py
+37-2Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import urllib.request
1010

1111
import numpy as np
12-
from numpy.testing import assert_array_equal
12+
from numpy.testing import assert_allclose, assert_array_equal
1313
from PIL import Image
1414

1515
import matplotlib as mpl
@@ -18,7 +18,7 @@
1818
from matplotlib.image import (AxesImage, BboxImage, FigureImage,
1919
NonUniformImage, PcolorImage)
2020
from matplotlib.testing.decorators import check_figures_equal, image_comparison
21-
from matplotlib.transforms import Bbox, Affine2D, TransformedBbox
21+
from matplotlib.transforms import Bbox, Affine2D, Transform, TransformedBbox
2222
import matplotlib.ticker as mticker
2323

2424
import pytest
@@ -1641,6 +1641,41 @@ def test__resample_valid_output():
16411641
resample(np.zeros((9, 9)), out)
16421642

16431643

1644+
@pytest.mark.parametrize("data, interpolation, expected",
1645+
[(np.array([[0.1, 0.3, 0.2]]), mimage.NEAREST,
1646+
np.array([[0.1, 0.1, 0.1, 0.3, 0.3, 0.3, 0.3, 0.2, 0.2, 0.2]])),
1647+
(np.array([[0.1, 0.3, 0.2]]), mimage.BILINEAR,
1648+
np.array([[0.1, 0.1, 0.15078125, 0.21096191, 0.27033691,
1649+
0.28476562, 0.2546875, 0.22460938, 0.20002441, 0.20002441]])),
1650+
]
1651+
)
1652+
def test__resample_nonaffine(data, interpolation, expected):
1653+
# Test that equivalent affine and nonaffine transforms resample the same
1654+
1655+
# Create a simple affine transform for scaling the input array
1656+
affine_transform = Affine2D().scale(sx=expected.shape[1] / data.shape[1], sy=1)
1657+
1658+
affine_result = np.empty_like(expected)
1659+
mpl._image.resample(data, affine_result, affine_transform,
1660+
interpolation=interpolation)
1661+
assert_allclose(affine_result, expected)
1662+
1663+
# Create a nonaffine version of the same transform
1664+
# by compositing with a nonaffine identity transform
1665+
class NonAffineIdentityTransform(Transform):
1666+
input_dims = 2
1667+
output_dims = 2
1668+
1669+
def inverted(self):
1670+
return self
1671+
nonaffine_transform = NonAffineIdentityTransform() + affine_transform
1672+
1673+
nonaffine_result = np.empty_like(expected)
1674+
mpl._image.resample(data, nonaffine_result, nonaffine_transform,
1675+
interpolation=interpolation)
1676+
assert_allclose(nonaffine_result, expected, atol=5e-3)
1677+
1678+
16441679
def test_axesimage_get_shape():
16451680
# generate dummy image to test get_shape method
16461681
ax = plt.gca()

‎src/_image_wrapper.cpp

Copy file name to clipboardExpand all lines: src/_image_wrapper.cpp
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ _get_transform_mesh(const py::object& transform, const py::ssize_t *dims)
5454
/* TODO: Could we get away with float, rather than double, arrays here? */
5555

5656
/* Given a non-affine transform object, create a mesh that maps
57-
every pixel in the output image to the input image. This is used
57+
every pixel center in the output image to the input image. This is used
5858
as a lookup table during the actual resampling. */
5959

6060
// If attribute doesn't exist, raises Python AttributeError
@@ -66,8 +66,10 @@ _get_transform_mesh(const py::object& transform, const py::ssize_t *dims)
6666

6767
for (auto y = 0; y < dims[0]; ++y) {
6868
for (auto x = 0; x < dims[1]; ++x) {
69-
*p++ = (double)x;
70-
*p++ = (double)y;
69+
// The convention for the supplied transform is that pixel centers
70+
// are at 0.5, 1.5, 2.5, etc.
71+
*p++ = (double)x + 0.5;
72+
*p++ = (double)y + 0.5;
7173
}
7274
}
7375

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.