3D Program Example

The 3D program strucutre is simliar to the 2D example but using 3D versions of the programming tools. Also there is only one 3D image per file.

v3mean.c

/*********************************************************************/
/* v3mean   Compute 3x3x3 3D mean filter on byte images              */
/*********************************************************************/
#include "VisXV4.h"          /* VisionX structure include file       */
#include "Vutil.h"           /* VisionX utility header files         */

VXparam_t par[] =            /* command line structure               */
{
{    "if=",    0,   " input file  v3mean: compute local mean"},
{    "of=",    0,   " output file "},
{     0,       0,    0}
};
#define  IVAL   par[0].val
#define  OVAL   par[1].val

int
main(argc, argv)
int argc;
char *argv[];
{
V3fstruct (im);
V3fstruct (tm);
int        x,y,z;               /* index counters                 */
int        xx,yy,zz;            /* window index counters          */
int        sum;
    VXparse(&argc, &argv, par); /* parse the command line         */

    V3fread( &im, IVAL);        /* read 3D image                  */
    if ( im.type != VX_PBYTE || im.chan != 1) { /* check  format  */
       fprintf (stderr, "image not byte type or single channel\n");
       exit (1);
    }   
   
    V3fembed(&tm, &im, 1,1,1,1,1,1); /* temp image copy with border */

    for (z = im.zlo; z <= im.zhi; z++) {/* for all pixels */
      for (y = im.ylo; y <= im.yhi; y++) {
        for (x = im.xlo; x <= im.xhi; x++) {
             sum = 0;
             for (zz = -1; zz <= 1; zz++) {/* compute the function */
               for (yy = -1; yy <= 1; yy++) {
                 for (xx = -1; xx <= 1; xx++) {
                     sum = sum + tm.u[z + zz][y + yy][x + xx];
                 }   
               }   
             }   
             im.u[z][y][x] = sum/27;
        }
      }
   }
   V3fwrite (&im, OVAL);
   exit(0);
}

Notes:
  1. The program, in general, is similar to 2D except that there is an additional image index (z);
  2. Use V3fread, V3fwrite, V3newim and V3embed instead of the 2D versions. Also use V3fstruct instead of Vfstruct.
  3. The 3D program reads in a whole image file and treats it as a single 3D image. Therefore, there is no need to loop for multiple images as in the previous example. Files containing multiple 3D images are currently not directly supported

The 3D image structure has two additional elements for indexing in the z dimension as shown below. The base pixel types are the same as for 2D structures.

Critical members of the V3fstruct that you must know:

.bbx The bounding box for the image (float[6])
.type The base type of the image pixels; see next example for a list
.chan The number of channels in the image (e.g., color has 3)
.xlo The lowest defined x index
.xhi The highest defined x index
.ylo The lowest defined y index
.yhi The highest defined y index
.zlo The lowest defined z index
.zhi The Highest defined z index