Topic "basic"

BASIC			    Monash Image Library			BASIC

These are the most commonly used functions in the image library, both in
C functions and shell commands. They can also be found under other topic 
headings.

The following program illustrates the use of some of these functions:

/*
 * This programs computes the median of an image and thresholds the image
 * at this value. Both images and their histograms are displayed, and
 * the binarized image is saved as a gif file.
 */

#include "image.h"

void main(int argc, char *argv[])
{

    IMAGE *in, *out, *hist_in, *hist_out;

    int size, count, pos, half, hist[256];

    register int i, j;

/*
 * Check and process arguments
 */

    if (argc < 3)
	i_error(I_FATAL,"Usage: %s in out",argv[0]);

    in = i_open(argv[1]);

    if (in->bitsperpixel != 8)
	i_error(I_FATAL,"Image must be 8 bpp");

/*
 * Compute histogram
 */

    for (i=0; i<256; i++)
	hist[i] = 0;

    for (i=0; irows; i++)
	for (j=0; jcols; j++)
	    hist[i_getpix(in, i, j)]++;
	 
    size = in->rows * in->cols;
    half = size / 2;
    count = 0;
    pos = 0;

/*
 * Determine median
 */

    for (i=0; i<256; i++)
    {
	count += hist[i];
	if (count > half)
	{
	    pos = i-1;
	    break;
	}
    }

/*
 * All messages to the screen should be printed to stderr.
 * This permits the piping of images which uses stdin and stdout.
 */

   fprintf(stderr, "\nMedian = %d\n",pos); 

   out = i_dup(in);
   i_binary(out,pos);		/* Pixels in range [0..1] */
   i_mulscalar(out,255);	/* Pixels in range [0..255] */

/*
 * Compute histograms and display images
 */

   i_display(in);
   hist_in = i_hist(in);
   i_display(hist_in);
 
   i_display(out);
   hist_out = i_hist(out);
   i_display(hist_out);

/*
 * Save the binarized image.
 */

   i_dump(out,argv[2],"gif");

/*
 * Free up memory used by images
 */

   i_close(in);
   i_close(out);
   i_close(hist_in);
   i_close(hist_out);
}


To compile this program you first need the standard Makefile:

	ihelp makefile > Makefile

Modify the Makefile for this program. If the program is called test.c,
then modifiy these lines to:

OBJS = test.o
BIN = test

To compile the program just enter:

	make test

The program can then be run with the input image mark.gif which creates the
output image output.gif:

	test mark.gif output

From the command line, images can be created using these simple commands:

	imktemp - 512 512 8 | izero - newimage.gif

This creates a 512x512 8 bits per pixel image called newimage.gif. The image
is created by the imktemp command and then set to zero by the izero command.
The '-' in the imktemp command indicates that the resulting image is to be
piped on stdout. The izero command also specifies a '-' for its input image
so that it is read from stdin. The data that is passed on a pipe is always
in "cif" format, unless a specific extension is given. For example:

	iresize newimage.gif -.gif 256 256 linear | xv -

The resized image is piped as a gif file to xv. Te file type of the piped
input image must always be cif with image library routines, which xv is
not. this should be altered in future versions.

Related Functions

iclose Releases all memory allocated to the image
icopy copy src image to dest image
idump dump image called basename.fmt, fmt: img smg lmg dmg cmg xbm rgb sgi gif ps cps bps cif pbm pgm ppm pnm. N.B. this separation of basename and format arguments non standard, the rest of the image library commands use a combined file name.
idup duplicate image
igetany return double pixel in image at row,col of 8, 16, 32 or 64 bpp
igetpix return integer pixel in image at row,col
ihist produce an intensity histogram (as an image)
iinfo displays information about an image to stderr. Full info includes statistical information (See istats)
imktemp creates new image, pixels are not zerod
iopen find image using paths in the IMAGES enviroment variable and load into memory
iputany put double pixel in image at row,col of 8, 16, 32 or 64 bpp
iputpix put integer pixel in image at row,col of 8, 16 or 32 bpp image
iresize arbitrary resize of an image, may not work on binary images.

Other Topics

index | userguide | full | arithmetic | basic | binary | blocks | colour | compress | display | docs | fft | hist | image | io | makefile | masks | memory | misc | morph | pixel | rgb | stats | transform | error | mapping