Basic 2D Example Program
This is the standard template for implemetning algorithms that operation on
conventional 2D images.
This program allows the image file names to be specified on the command
line, allows the threshold to be specified on the command line, and also
works on image files that contain more than one image.
See the concept tutorial for a desction of the
v4 image structures.
v2mean.c
#include "VisXV4.h" /* VisionX structure include file */
#include "Vutil.h" /* VisionX utility header files */
VXparam_t par[] = /* command line structure */
{
{ "if=", 0, " input file vthresh: threshold images"},
{ "of=", 0, " output file "},
{ "th=", 0, " Threshold value (default 50)"},
{ 0, 0, 0}
};
#define INIMAGE par[0].val
#define OUTIMAGE par[1].val
#define THRESHOLD par[2].val
int main(int argc, char** argv)
{
Vfstruct (im);
int x, y;
int thresh;
VXparse(&argc, &argv, par); /* parse the command line */
if (THRESHOLD ) {
thresh = atoi (THRESHOLD);
} else {
thresh = 50;
}
while ( Vfread( &im, INIMAGE) ) {
if ( im.type != VX_PBYTE ) {
fprintf (stderr, "error: image not byte type\n");
exit (1);
}
for ( y = im.ylo; y <= im.yhi; y++) {
for ( x = im.xlo; x <= im.xhi; x++) {
im.u[y][x] = im.u[y][x] >= thresh ? 255 : 0;
}
}
Vfwrite( &im, OUTIMAGE);
}
exit(0);
}
Notes:
-
Command line parameters have now been added to this program.
-
To perform exactly the same function as the previous program one
would need to give the command:
vthresh if=infile.vx of=outfile.vx
The “par” structure defines a total of three parameters if= for
the input file name and of= for the output file name; the if= and of=
prefixes are used by convention throughout the VisionX system. A third
optional parameter th= is also specified which allows the image threshold
to be set to any value (not just 50).
-
The VXparse function matches command line parameters where possible to the .val elements. Therefore, after parsing the command line specified above, the par .val elements would have the following contents:
par[0].val “inimage.vx”
par[1].val “outimage.vx
par[2].val “”
The #define statements in the program provide a convenient way to name
the different par .val elements.
-
If the th= option is specified on the command line then the string
given as the threshold will be converted to an integer value by the atoi
function and assigned to thresh.
-
The program as written will correctly operate on images with any
dimensions and any number of channels but will only operate on images
of type unsigned byte. Therefore a check has been added to ensure that
the input image file is of the correct type.
-
Since there are no dependencies in this simple program there is no real
need to create an image structure for the result; it may be written back
into the original image. Also a more succinct c language version or the
if expression in the first program is used in the second program which
performs the same function.
Table 1 base pixel types supported by VisionX and their Vfstruct names
Vfstruct type Name | C-Type | Vfstruct Member | Description
|
VX_PBYTE | unsigned char | .u | most usual byte format
|
VX_PFLOAT | float | .f | 32-bit floating point
|
VX_PDOUBLE | double | .d | 64-bit floating point
|
VX_PBIT | unsigned char | .b | 1-bit (packed 8/byte)
|
VX_PCHAR | char | .c | 8-bit signed integer
|
VX_PSHORT | short | .s | 16-bit signed integer
|
VX_PINT | int | .i | 32-bit signed integer
|
VX_PIDX | unsigned char | .u | 8-bit color index
|
|