IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit

Modules demonstrating how to mix PyIPSDK images with Numpy array, the fundamental package for scientific computing with Python. More...

Modules demonstrating how to mix PyIPSDK images with Numpy array, the fundamental package for scientific computing with Python.

Numpy array structure, see numpy official web site, can easily be manipulated has PyIPSDK images and vice versa without memory penalty cost. There is no reallocation exchanging data from numpy array to PyIPSDK images and vice versa.

Prerequisite

Numpy library should be installed with used Python distribution (this is already the case if you use Anaconda Python distribution).

Usage

The following script illustrates how to handle usage of numpy array with PyIPSDK library.

We start by importing all necessary libraries:

import os
import PyIPSDK
import PyIPSDK.IPSDKIPLUtility as util
# specific numpy library import
import numpy as np

We can now create a new PyIPSDK image, manipulate it and then access to associated numpy array :

# creation of a 2d image with size 5x7 and with unsigned short data type
img2d1 = PyIPSDK.createImage(PyIPSDK.eImageBufferType.eIBT_UInt16, 5, 7)
# fill this image with random values in range [3, 11]
util.randomImg(PyIPSDK.createRange(3, 11), img2d1)
# access and print of associated numpy array
print("img2d1.array = ")
print(img2d1.array)
# we can then directly modify array data and by the way image data
# note that numpy 2d array uses a y,x ordering
img2d1.array[2, 3] = 25
print("img2d1.array = ")
print(img2d1.array)

We can also create a new numpy array and use it as a PyIPSDK image :

# creation of a 3d numpy array
# note that numpy 3d array uses a z,y,x ordering
array3d1 = np.empty([3, 5, 2], np.int16)
array3d1.fill(5)
print("array3d1 = ")
print(array3d1)
# create a PyIPSDK image from this array
# note that array and image share memory
img3d1 = PyIPSDK.fromArray(array3d1)
# fill this image with random values in range [0, 4]
util.randomImg(PyIPSDK.createRange(0, 4), img3d1)
print("array3d1 = ")
print(array3d1)
print("img3d1.array = ")
print(img3d1.array)
Note
Numpy array and PyIPSDK image in previous examples share memory. Any modification of image modify array and vis versa. Numpy array are so for example a good way to access image pixels.

The following functions allow to interpret various array dimensions and data type according to supported PyIPSDK image types :