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.

The package vx.py contains a class Vx for VisionX images. This Class contains three main data elements Vx.i which is the pixel data stored as a numpy array Vx.c which is the number of channels in the image and Vx.h which is text that contains file meta data including the VisionX history. The following VisionX pixel types are supported:

Format Python typeVisionX Type
unsigned byteuint8 VX_PBYTE
signed byteint8 VX_PCHAR
16-bit signed integerint16 VX_PSHORT
32-bit signed integerint32 VX_PINT
32-bit float float32 VX_PFLOAT
64-bit double float64 VX_PDOUBLE

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.

Visionx V4 Tutorials: Python and Jupyter Notebook Examples  web

The following class functions are currently available:

  1. Vx structure creation: Read a Visionx File, if the file name is "" then read from stdin (pipe).
    x = Vx('file-name')
    Copy a structure from another structure
    x = Vx(Vx-instance)
    Create a "blank" structure by image specification
    x = Vx('data-type',bbx-tupple)
    x = Vx('data-type',bbx-tupple,num-channels)
  2. write a Visionx File; if the file name is "" the write to stdout (pipe).
    x.write(file-name)
  3. Set the image component of a structure to a new image
    x.setim(numpy-array)
  4. Embed the image x.i in zeros as specified by the embed-tupple.
    x.embedim(embed-tupple)
  5. parameter-list = vxparse(sys.argv, parameter-spec);
    parse and set variable values to command line arguments
  6. python-expression = vxsh( "sh command with $ variables");
    parse an external command for sh using a convenient $ variable syntax

Release Notes

  1. Th v4 python package must be installed. Otherwise vx.py may be indirectly imported from the v4 distribution.
  2. 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.
  3. Image indexing is from 0,0 at the top left corner of the image with the index increasing in a downward direction.

A template python program to implement a 2D local 5 element max filter is shown below:

#!/usr/bin/env python

#### vtempy a python version of 2D max filter

import sys
from numpy  import *
from v4 import vx

of=''
vxif=''
exec(vx.vxparse(sys.argv,  "if= of= -v  - "))

if 'OPT' in locals():
   print "vtempy: local max filter"
   print "if= input file"
   print "of= output file"
   exit(0)

im = vx.Vx(vxif)
tm = vx.Vx(im)
tm.embedim((1,1,1,1))    
for y in range(im.i.shape[0] ):
    for x in range(im.i.shape[1]  ):
        im.i[y,x] = max(tm.i[y+1,x+1],tm.i[y+1,x+2],
                  tm.i[y+1,x],tm.i[y,x+1],tm.i[y+2,x+1])

vx.write(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

Vxparse(3)

AUTHOR

A. P. Reeves