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.
In addition to strings and numerical values, the parameters passed to vxsh commands may be Python vx images.
import vx as 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;
exec(vx.vxsh( 'vmean if=$x of=$y xy=$window' ));
print 'result image y'
print y.i
Note, the image to be returned to Python is identified by the prefix 'of=' The above exec-vx.vxsh statemet 'vmean if=$x of=$y xy=$window' is equivalent to the following:
x.write('temp1')
os.system('vmean if=tmp1 of=tmp2 xy=' + str(window) );
y = vx.Vx('temp2')
os.system('rm -f tmp1 tmp2' );
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.
exec(vx.vxsh('p = vps if=$x'))
print p
In summary, vxsh provides a convenient command line syntax for executing system commands on both system files and Python objexts.