sunpy.image.resample.reshape_image_to_4d_superpixel(img, dimensions, offset)[source] [edit on github]

Re-shape the two dimension input image into a a four dimensional array whose first and third dimensions express the number of original pixels in the “x” and “y” directions that form one superpixel. The reshaping makes it very easy to perform operations on superpixels.

An application of this reshaping is the following. Let’s say you have an array:

x = np.array([[0, 0, 0, 1, 1, 1],
              [0, 0, 1, 1, 0, 0],
              [1, 1, 0, 0, 1, 1],
              [0, 0, 0, 0, 1, 1],
              [1, 0, 1, 0, 1, 1],
              [0, 0, 1, 0, 0, 0]])

and you want to sum over 2x2 non-overlapping sub-arrays. For example, you could have a noisy image and you want to increase the signal-to-noise ratio. Summing over all the non-overlapping 2x2 sub-arrays will create a superpixel array of the original data. Every pixel in the superpixel array is the sum of the values in a 2x2 sub-array of the original array.

This summing can be done by reshaping the array:

y = x.reshape(3,2,3,2)

and then summing over the 1st and third directions:

y2 = y.sum(axis=3).sum(axis=1)

which gives the expected array:

array([[0, 3, 2],
      [2, 0, 4],
      [1, 2, 2]])
  • img (numpy.ndarray) – A two-dimensional numpy.ndarray of the form (y, x).

  • dimensions (array-like) – A two element array-like object containing integers that describe the superpixel summation in the (y, x) directions.

  • offset (array-like) – A two element array-like object containing integers that describe where in the input image the array reshaping begins in the (y, x) directions.


  • A four dimensional numpy.ndarray that can be used to easily create

  • two-dimensional arrays of superpixels of the input image.