Using VisionX-V4 and Python

  1. Currently VisionX V4 is not fully integrated with Python
  2. It is possible to read and write VisionX images with Python
  3. It is possible to give shell commands from python that include Python images and parameters with image and data files
The following requires that the python visionx module be installed (pip install v4) and that a full version of VisionX v4 is installed. Testing was done on python3.6.

VisionX V4 images

The class vx.Vx is used to contain v4 images. There first example shows how such an image may be created within Python. The image is stored in a numpy array accessed by .i

In [1]:
from numpy  import *
from v4 import vx

x = vx.Vx("int16", [0, 6, 0, 4],1)
x.i[0][2] = 10
x.i[1][1] = 10
x.i[2][2] = 20
x.i[3][3] = 30
print (x)
print (x.i)
VisionX V4: Vx
    Image Size: (4, 6)
    Pixel Type: int16
    Number of channels: 1

[[ 0  0 10  0  0  0]
 [ 0 10  0  0  0  0]
 [ 0  0 20  0  0  0]
 [ 0  0  0 30  0  0]]

VisionX images may be read for image files as in:

img = vx.Vx('file.vx')

For images in formats that are other than v4, the imageio package is useful. See below an example of reading a .png image, creating a python v4 structure from it and writing the image in VisionX format.

In [10]:
import imageio
img = imageio.imread('clock.png')
im3 = vx.Vx()
im3.setim(img)
im3.write('clock.vx')

Results from external command executions may be returned to python using the vxsh utility

In [2]:
exec(vx.vxsh('p = vqfile -q nyg2.vx'))
print (p.decode())
Format:     VXB
Size:       512 x 512
Pixel Type: byte

Images may also be returned from external commands using teh vxsh utility

In [12]:
tmp1 = vx.Vx()
exec(vx.vxsh( 'vimag nyg2.vx m=0.3,0.3,1 of=$tmp1' ));
print (tmp1)
print (tmp1.h)
VisionX V4: Vx
    Image Size: (154, 154)
    Pixel Type: uint8
    Number of channels: 1

vpnmtovx 
vxfile of=nyimg.vx f=1 
vclip p=272,228 s=512 nyimg.vx of=nyr1.vx 
vchan -c nyc1.vx of=nyg1.vx 
vibbox nyg1.vx of=nyg2.vx -z 
vgfilt nyg2.vx xs=1.6666666666666667 ys=1.6666666666666667 zs=0 of=/tmp/tm.vimag33603.1 
viscale if=/tmp/tm.vimag33603.1 of=tmp.vxpy.33403.3 m=0.3,0.3,1 
meta=1,0,0

Displaying Images in Jupyter Notebooks

One of the most direct ways od displaying v4 and other images in a python environment is to use the PIL package as shown below: Note, the image is displayed in native resolution which is often useful. However, scaling to fit may be necessary for large images

In [13]:
from PIL import Image
display(Image.fromarray(tmp1.i))

Another approach is to use the matplotlib package. In this case the image is always scaled and you have some control on the scaling; however you cannot show it in its actual native size

In [14]:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
plt.rcParams["figure.figsize"] = (4,4)
plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
plt.imshow(tmp1.i, cmap=cm.gray);
In [15]:
nyg3 = vx.Vx()
exec(vx.vxsh( 'vclip if=nyg2.vx s=128 p=46,160 | vibbox -z of=$nyg3' ));
plt.imshow(nyg3.i, cmap=cm.gray);         
In [6]:
tmp2 = vx.Vx("uint8", [0, 1, 0, 1],1)
exec(vx.vxsh( 'vinsp if=nyg3.vx p=36,56 s=12,8 of=$tmp2' ));
display(Image.fromarray(tmp2.i))