0

I have a 6D numpy array, say it has shape (N1,N2,N3,N4,NY,NX). It's holding many images, each separate image is held in the last 2 dimensions (NY,NX). The other dimensions are just a way to organise the images.

I have a function that takes an image and outputs an image of the same shape. It requires the full 2D image, i.e. it won't work if we break the image into 1D arrays.

Is there a numpy function for applying this function over the whole array, to return an array with the same shape (N1,N2,N3,N4,NY,NX)?

I can do it with python loops of course, but I was wondering if there's a numpy way which will be faster thanks to vectorisation?

numpy.apply_along_axis() almost does this but I don't think it can work with a function that requires 2D arrays, I think it always applies the function to 1D arrays.

Similarly numpy.apply_over_axes() seems close but the function doesn't take an axis parameter and numpy.apply_over_axes() always seems to want to pass this.

Thanks!

womtyatt
  • 1
  • 1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Aug 11 '23 at 12:39
  • even if it worked numpy.apply_along_axis() still loops – hpaulj Aug 11 '23 at 15:16
  • As long as your function only works on one 2d array, it has to be run separately for each of them. True speed improving 'vectorization' requires rewriting the function itself. – hpaulj Aug 11 '23 at 15:23
  • `apply_over-axis` is not all applicable. Take it's `sum` example seriously. I think you searched for some "apply' functions, hoping that one of these would magically apply your function to the array without "loops". `numpy` doesn't have such a tool. To get speedup, you have to use compiled code, for the iteration and processing of the 2d argument. That can't be done (with just `numpy`) with python functios such as yours. – hpaulj Aug 11 '23 at 17:55

1 Answers1

0

if you want to work with numpy array and pictures very probably openCV is your friend.

see hereenter link description here for an exaple how to convert an image to an array

user3732793
  • 1,699
  • 4
  • 24
  • 53