This demo shows how images from the V4 system are maintained with their meta data in Python
the Vx class is used for images. One element of the class is VX.i that contains the image data in a numpy array; the other elements maintain the image meta data.
A VX class may be created with the contents of vx image file by:
<python-image> = vx.Vx(<image-file-name>)
A VX class can also be copied or cloned from an existing class:
<python-image2> = vx.Vx(<python-image>)
A vx image may also be implicitly created by:
<python-image> = vx.Vx(<pixel-type>, <bounding-box>, <#-channels>)
Where:
<bounding-box> is a 2D or 3D specification (tuple or list) of the image size and
<#-channels> is the number of elements in each pixel; i.e., for a color image <#-channels> = 3
# implicit Vx class creation
from numpy import *
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import vx
x = vx.Vx('int16', (0, 4, 0, 3),1)
# once created pixels in the numpy array x.i may be set
x.i[0][2] = 10
x.i[1][1] = 10
x.i[2][2] = 20
print 'shape is:', shape(x.i)
print 'Number of channels is:', x.c
plt.imshow(x.i,cmap=cm.gray);
The pixel-type may be set to one of: uint8, int8, int16, int32, float32, or float64. The 2D bounding box has the format (xlo, xhi, ylo, yhi). xlo and xhi may be given v4 offset values; however, numpy arrays always have a lowest index value of 0. Therefore, the range of x-index values will be 0 to (xhi - xlo - 1).
A color image with the same dimensions as x may be created by specifying three channels. For traditional v4 commands the channels are interpreted as 0 = Red, 1 = Green, and 2 = blue.
y = vx.Vx('uint8', [0, 4, 0, 3],3)
y.i[0][2][0] = 255
y.i[1][2] = (200, 200, 200)
y.i[2][2] = (200, 150, 50)
plt.imshow(y.i);
3D imges are specified implicitly by providing a 3D bounding box specification: [xlo, xhi, ylo, yhi, zlo, zhi]
z = vx.Vx('uint8', (0, 4, 0, 2, 0, 3), 1)
# once created pixels in the numpy array x.i may be set
z.i[1] = 2
z.i[1][1] = 10
z.i[2][1] = 20
z.i[2][0][1] = 30
print z.i
As with 2D images for 3D images, you can specify color or mutispecrtal pixels by setting the channel parameter
zc = vx.Vx('uint8', (0, 4, 0, 2, 0, 3), 3)
print shape(zc.i)
There is a Vx class utility called embedim that is similar to the C programming language embed function that facilitates padding the border of images with zeros when convenient. Unlike the C version the indexing cannot be matched to be the same as for the initial array as all numpy arrays start at (0,0). It is the responsibility of the programmer to make appropriate offset indexing when necessary. For numpy arrays the pad() function may also be used.
a = vx.Vx('uint8', (0, 2, 0, 2), 1)
a.i[:] = 5
b = vx.Vx(a)
b.embedim((3,4,0,1))
print a.i
print b.i
a = vx.Vx('uint8', (0, 2, 0, 2, 0, 1), 1)
a.i[:] = 5
b = vx.Vx(a)
b.embedim((3,4,0,1, 0, 1))
print a.i
print b.i