V4 Python Programming
A beta version of a python api is available for the development and validation V4 image processing functions. The current first version of this system provides support for single 2D and 3D images in byte pixel format.
The numpy module arrays are currently used as the data structure to support images. Unlike the c programming environment, the index origin must be 0,0 rather than ylo,xlo. Further, the index origin is located at the top left hand corner of the image rather than the lower left hand corner. As a consequence of the indexing limitation, offsets in location between images must be explicitly managed by the program.
The functions available for image programming include: vxparse, vfread, vfwrite, vfembed, vfnewim, v3fread, v3fwrite, v3embed, and v3fnewim. Their function is similar to their c programming language counterparts; however, their arguments differ as outlined below:
array = vfnewim(type, bbx, chan); array = vfread(file-name); array = vfembed(array, xlo, xhi, ylo, yhi); vfwrite(file-name, array); array = v3fnewim(type, bbx, chan); array = v3fread(file-name); array = v3fembed(array, xlo, xhi, ylo, yhi, zlo, zhi); v3fwrite(file-name, array); parameter-list = vxparse(sys.argv, parameter-spec);For documentation of these functions see the c programming language descriptions. For their use see the following example python program and compare to the c example program vtemp.c
Release Notes
- The module vx.py must be imported. If it is not installed in the system then the program example below shows how it may be accessed from v4 itself.
- The standard name for the input file (image) command line prefix for VisionX V4 is "if=" which is translated to a variable name "if". However, python confuses a variable named "if" with the if statement. To accommodate this limitation the vxparse function renames the "if=" parameter to the variable "vxif". All other parameter specifications result in the setting of variables having the same name as for the conventional vxparse.
- Image indexing is from 0,0 at the top left corner of the image with the index increasing in a downward direction.
- Only single byte format images are currently supported.
A template python program to implement a 2D local 5 element max filter is shown below:
#!/usr/bin/python #### vtempy a python version of 2D max filter import sys from numpy import * from v4 import vx of=' ' vxif=' ' clist = vx.vxparse(sys.argv, "if= of= -v - ") exec (clist ) if 'OPT' in locals(): print "vtempy V4 python test program" print "if= input file" print "of= output file" print "[-v] verbose mode" exit(0) if 'OPTv' in locals(): optv=1 else: optv=0 im = vx.vfread ( vxif ) tm = vx.vfembed( im,1,1,1,1) for y in range(im.shape[0] ): for x in range(im.shape[1] ): im[y,x] = max(tm[y+1,x+1],tm[y+1,x+2], tm[y+1,x],tm[y,x+1],tm[y+2,x+1]) #if optv: # print im vx.vfwrite(of, im)
The conventional c language program for this function, for comparison, is as follows:
/* vtemp Compute local max operation on a single byte image */ /*******************************************************************/ #include "VisXV4.h" /* VisionX structure include file */ #include "Vutil.h" /* VisionX utility header files */ VXparam_t par[] = /* command line structure */ { /* prefix, value, description */ { "if=", 0, " input file vtemp: local max filter "}, { "of=", 0, " output file "}, { 0, 0, 0} /* list termination */ }; #define IVAL par[0].val #define OVAL par[1].val main(argc, argv) int argc; char *argv[]; { Vfstruct (im); /* i/o image structure */ Vfstruct (tm); /* temp image structure */ int y,x; /* index counters */ VXparse(&argc, &argv, par); /* parse the command line */ Vfread(&im, IVAL); /* read image file */ Vfembed(&tm, &im, 1,1,1,1); /* image structure with border */ for (y = im.ylo ; y <= im.yhi ; y++) { /* compute the function */ for (x = im.xlo; x <= im.xhi; x++) { im.u[y][x] = MAX(tm.u[y][x], MAX(tm.u[y][x+1], MAX(tm.u[y+1][x], MAX(tm.u[y][x-1], tm.u[y-1][x])))); } } Vfwrite(&im, OVAL); /* write image file */ exit(0); }
SEE ALSO
Vnewim(3),Vembed(3),Vfread(3), Vfwrite(3) |
A. P. Reeves |