Image processing and analysis are generally seen as operations on two-dimensional arrays of values. There are however a number of fields where images of higher dimensionality must be analyzed. Good examples of these are medical imaging and biological imaging. ** numpy** is suited very well for this type of applications due its inherent multidimensional nature. The

**packages provides a number of general image processing and analysis functions that are designed to operate with arrays of arbitrary dimensionality. The packages currently includes functions for linear and non-linear filtering, binary morphology, B-spline interpolation, and object measurements.**

`scipy.ndimage`

**Proprietà condivise da tutte le funzioni**

All functions share some common properties. Notably, all functions allow the specification of an output array with the output argument. With this argument you can specify an array that will be changed in-place with the result with the operation. In this case the result is not returned. Usually, using the output argument is more efficient, since an existing array is used to store the result.

The type of arrays returned is dependent on the type of operation, but it is in most cases equal to the type of the input. If, however, the output argument is used, the type of the result is equal to the type of the specified output argument. If no output argument is given, it is still possible to specify what the result of the output should be. This is done by simply assigning the desired ** numpy** type object to the output argument. For example:

**Funzioni filtro**

The functions described in this section all perform some type of spatial filtering of the input array: the elements in the output are some function of the values in the neighborhood of the corresponding input element. We refer to this neighborhood of elements as the filter kernel, which is often rectangular in shape but may also have an arbitrary footprint. Many of the functions described below allow you to define the footprint of the kernel, by passing a mask through the footprint parameter. For example a cross shaped kernel can be defined as follows:

Usually the origin of the kernel is at the center calculated by dividing the dimensions of the kernel shape by two. For instance, the origin of a one-dimensional kernel of length three is at the second element. Take for example the correlation of a one-dimensional array with a filter of length 3 consisting of ones:

Sometimes it is convenient to choose a different origin for the kernel. For this reason most functions support the ** origin** parameter which gives the origin of the filter relative to its center. For example:

The effect is a shift of the result towards the left. This feature will not be needed very often, but it may be useful especially for filters that have an even size. A good example is the calculation of backward and forward differences:

We could also have calculated the forward difference as follows:

However, using the ** origin** parameter instead of a larger kernel is more efficient. For multidimensional kernels

**can be a number, in which case the origin is assumed to be equal along all axes, or a sequence giving the origin along each axis.**

`origin`

Since the output elements are a function of elements in the neighborhood of the input elements, the borders of the array need to be dealt with appropriately by providing the values outside the borders. This is done by assuming that the arrays are extended beyond their boundaries according certain boundary conditions. In the functions described below, the boundary conditions can be selected using the ** mode** parameter which must be a string with the name of the boundary condition. The following boundary conditions are currently supported:

Use the value at the boundary`"nearest"`

`[1 2 3]->[1 1 2 3 3]`

Periodically replicate the array`"wrap"`

`[1 2 3]->[3 1 2 3 1]`

Reflect the array at the boundary`"reflect"`

`[1 2 3]->[1 1 2 3 3]`

Use a constant value, default is 0.0`"constant"`

`[1 2 3]->[0 1 2 3 0]`

The ** "constant"** mode is special since it needs an additional parameter to specify the constant value that should be used.

* Note*: The easiest way to implement such boundary conditions would be to copy the data to a larger array and extend the data at the borders according to the boundary conditions. For large arrays and large filter kernels, this would be very memory consuming, and the functions described below therefore use a different approach that does not require allocating large temporary buffers.

Uhmmm… mi sa che questa cosa dei filtri sarà lunga; pausa 😊