From 35a9cd40673f780f9bd1c0cb28a62263b9589e04 Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Sat, 28 Sep 2013 16:24:23 -0500 Subject: [PATCH] Fix setter for xy_array and reuse. --- skimage/novice/_novice.py | 36 +++++++++++------------------ skimage/novice/tests/test_novice.py | 8 +++++++ 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/skimage/novice/_novice.py b/skimage/novice/_novice.py index 4a986bf8..f66c634c 100644 --- a/skimage/novice/_novice.py +++ b/skimage/novice/_novice.py @@ -50,15 +50,16 @@ def _verify_picture_index(index): return tuple(index) -def cartesian_image_transform(image): - """Return image transformed between array origin and Cartesian origin. - - This transform converts an RGB image's origin from the top-left to the - bottom-left, and switches from (row, column)-indexes to (x, y)-indexes. - """ +def array_to_xy_origin(image): + """Return view of image transformed from array to Cartesian origin.""" return np.transpose(image[::-1], (1, 0, 2)) +def xy_to_array_origin(image): + """Return view of image transformed from Cartesian to array origin.""" + return np.transpose(image[:, ::-1], (1, 0, 2)) + + class Pixel(object): """A single pixel in a Picture. @@ -252,7 +253,7 @@ class Picture(object): @array.setter def array(self, array): self._array = array - self._xy_array = cartesian_image_transform(array) + self._xy_array = array_to_xy_origin(array) @property def xy_array(self): @@ -261,7 +262,7 @@ class Picture(object): @xy_array.setter def xy_array(self, array): self._xy_array = array - self._array = cartesian_image_transform(array) + self._array = xy_to_array_origin(array) def save(self, path): """Saves the picture to the given path. @@ -434,22 +435,11 @@ class PixelGroup(Picture): ---------- pic : Picture The Picture object that this pixel group references. - key : tuple - tuple with x and y slices or ints for extracting part of raw image data. - + xy_slice : tuple + 2D slice of `pic` in Cartesian coordinates. """ - def __init__(self, pic, key): - # Flip y axis - y_slice = key[1] - start = y_slice.start if y_slice.start is not None else 0 - stop = y_slice.stop if y_slice.stop is not None else pic.height - - start = pic.height - start - 1 - stop = pic.height - stop - - key = (key[0], slice(stop, start + 1, y_slice.step)) - - self.array = pic._array[(key[1], key[0])] + def __init__(self, pic, xy_slice): + self.xy_array = pic.xy_array[xy_slice] def __iter__(self): """Iterates through all pixels in the pixel group. diff --git a/skimage/novice/tests/test_novice.py b/skimage/novice/tests/test_novice.py index 40671d39..6d9ef4d7 100644 --- a/skimage/novice/tests/test_novice.py +++ b/skimage/novice/tests/test_novice.py @@ -4,6 +4,7 @@ import tempfile import numpy as np from numpy.testing import assert_equal, raises, assert_allclose from skimage import novice +from skimage.novice._novice import array_to_xy_origin, xy_to_array_origin from skimage import data_dir @@ -15,6 +16,13 @@ def _array_2d_to_RGB(array): return np.tile(array[:, :, np.newaxis], (1, 1, 3)) +def test_xy_to_array_origin(): + h, w = 3, 5 + array = np.arange(h * w).reshape(h, w, 1) + out = xy_to_array_origin(array_to_xy_origin(array.copy())) + assert np.allclose(out, array) + + def test_pic_info(): pic = novice.open(IMAGE_PATH) assert_equal(pic.format, "png")