1

I have an image imported in a ndarray with a shape (1027, 888, 3).

My task is to create a method that returns 2 one dimensional arrays of indexes that select a tile from the image.

ii, jj = tile_cordinates(i,j, tile_size)
imshow(image[ii,jj])

I would like to simulate the same result as using this code:

imshow(image[1:32, 2:32])

I tried to do this:

def tile_coordinates(i, j, tile_size):
    return range(i, i + tile_size), range(j, j + tile_size)
ii, jj = tile_coordinates(1,2,32)
imshow(image[ii,jj])

But the the image is not right. In fact the result return form indexing the image with the two arrays is (32, 3) while using

image[1:32, 2:32].shape

Returns (31, 30, 3)

So how to form the returned arrays form the tile_coordinates method to simulate the same result as the slicing example? Is it even possible?

PS: The specifications are set from a homework assignment. I have already spend a few hours looking at the documentation and other examples of indexing but have not found anything that could do what is required of me. So I'm quite stuck. Any guidance would be really appreciated :)

Thanks!

JustmeVSI
  • 173
  • 2
  • 3
  • 11
  • Are you positive you need "one dimensional" arrays? In general, if you want the output of index operation to be 2d, your indexers should be broadcastable to 2d as well, or otherwise you'll need to do some manual reshaping ([here](http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#advanced-indexing) is the corresponding doc section). – immerrr Oct 19 '14 at 15:56
  • Thanks for the documentation reference. This is the specification of the task: "Tiles can be arranged spatially in rows and columns, similarly to pixels. We can thus ask what pixels the tile at the ith row and the jth column span. Below you must finish the implementation of the function tile_coordinates, which gives the answer to just that question. The function should return two 1-dimensional arrays, the first providing the row coordinates of the given tile, and the second providing the column coordinates." As far as I understand it; yes it needs to be one dimensional. – JustmeVSI Oct 19 '14 at 16:35

2 Answers2

1

You are looking for the slice builtin. image[1:32, 2:32] can be expressed as image[slice(1,32), slice(2,32)]

def tile_coordinates(i, j, tile_size):
    return slice(i, i + tile_size), slice(j, j + tile_size)
Janne Karila
  • 24,266
  • 6
  • 53
  • 94
0

Without worrying about broadcasting, the selection can be done as a two-stage process, which is fairly simple to understand (first, select along axis 0, next select along axis 1). If you care to check, you can see that the first indexing reduces the size of one axis to 32.

image[ii][:,jj] #has shape (32, 32, 3)
mdurant
  • 27,272
  • 5
  • 45
  • 74