Demonstration of vx vxsh

Executing operating system commands from within python

Consider that we wish to execute the following comand in the operating system; i.e., outside of python:

      vmean if=image1 of=image2 xy=3

This is a VisionX V4 mean filter command that will filter the image file 'image1' with a mean filter of size 3x3 and store the result in the image file 'image2'.

In the sh command language the paramter values are often located in variables to facilitae script progamming:

     imin="image1"
     imres='image2'
     window=3
     vmean if=$imin of=$imres xy=$window

This may also be accomplished in Python with a less convenient syntax:

     imin = 'image1';
     imres = 'image2';
     window = 3;
     os.system( 'vmean if=' + imin + ' of=' + imres + ' xy=' + str(window) );

An alternative is to construct the string to be executed with a format statement; however, both of these options are much less convenient than the sh counterpart. The v4 utilites contain a parser called 'vxsh' that simplifies the specification of this command to:

     exec(vx.vxsh( 'vmean if=$imin of=$imres xy=$window' ));

which may be compared to the sh example. The values prefixed with a $ are replced by their Python variable values using a symilar syntax to sh.

Executing operating system commands on python vx images:

In addition to strings and numerical values, the parameters passed to vxsh commands may be Python vx images.

In [1]:
#from v4 import vx
import vx
#create a vx image in python
x = vx.Vx("int16", [0, 6, 0, 4],1);
x.i[0][2] = 100;
x.i[2][4] = 200;
print('input image x')
print(x.i)
window = 3;
y = vx.Vx()
#print(vx.vdovar(window))
#print(type(x).__name__)
#print(isinstance(x,vx.Vx))
#print(vx.vdovar(x))

exec(vx.vxsh( 'vmean if=$x of=$y xy=$window' ));

print('result image y')
print(y.i)
input image x
[[  0   0 100   0   0   0]
 [  0   0   0   0   0   0]
 [  0   0   0   0 200   0]
 [  0   0   0   0   0   0]]
result image y
[[ 0 11 11 11  0  0]
 [ 0 11 11 33 22 22]
 [ 0  0  0 22 22 22]
 [ 0  0  0 22 22 22]]

Note, the image to be returned to Python is identified by the prefix 'of=' The above exec-vx.vxsh statement 'vmean if=$x of=$y xy=$window' is equivalent to the following:

In [2]:
import os
x.write('temp1')
os.system('vmean if=temp1 of=temp2 xy=' + str(window) );
y = vx.Vx('temp2')
os.system('rm -f temp1 temp2' );

Returning a text response from an external system command with vxsh

Text output that is generated by system commands can also be returned to Python using an assignment statement syntax as shown below. This syntax is to be used for all text retuns while the 'of=' syntax may only be used for images. Commands that produce csv or other text files may also be read by Python using this syntax.

In [3]:
exec(vx.vxsh('p = vps if=$x'))
print(p.decode())
Vx

 Global Statistics Summary

1 images in file    6 x 4 (0,0)

24         (2         ) number of pixels
200        (200       ) maximum pixel value
0          (100       ) minimum pixel value
200        (100       ) range
12.5       (150       ) mean pixel value
1927.08    (2500      ) variance
43.8986    (50        ) standard deviation
3.55549    (0         ) skewness
14.531     (1.00002   ) kurtosis

In summary, vxsh provides a convenient command line syntax for executing system commands on both system files and Python objexts.