www.pudn.com > gsnake.rar > image.tex


\rhead {class IMAGE}
\section{IMAGE : The raw image object}

{\tt IMAGE} is a class for manipulating and displaying image data. Image data can be stored, retrieved, copied, cut, and viewed for different purposes. In addition, histogram processing, image correlation and Gaussian kernel generation are also provided for image enhancement and smoothing purpose. An {\tt IMAGE} object has the following structure :

\begin{verbatim}
            CLASS IMAGE {
                  protected :
                     float *data;
                     int row, col;

                  public :
                     XIMAGE *ximg;
            }
\end{verbatim}

Image data is built up of a matrix of size row x col, which can be generated as an X window image.


\subsection{IMAGE Constructor}

\subsubsection*{Synopsis}
\begin{verbatim}	
	IMAGE()
\end{verbatim}

\subsubsection*{Description}
The constructor initializes an image data of matrix size 0 x 0, and sets the X window image pointer to NULL. 

%
\subsection{IMAGE Destructor}

\subsubsection*{Synopsis} 

\begin{verbatim}
	~IMAGE()
\end{verbatim}

\subsubsection*{Description} 
The destructor frees the memory allocated to the image data and the X window image.

%
\subsection{Resetting an IMAGE}

\subsubsection*{Synopsis}
\begin{verbatim}
reset()
\end{verbatim}

\subsubsection*{Description}
{\tt reset} allows the reuse of an {\tt IMAGE} object. The memory allocated previously is freed and the image data of matrix size 0 x 0 is initialized. 

%
\subsection{Initializing image matrix}

\subsubsection*{Synopsis} 
\begin{verbatim}
	int init(int _row, int _col);
\end{verbatim}

\subsubsection*{Arguments} 
\begin{tabular}{ll}
	{\tt \_row} & The number of rows in an image. \\
	{\tt \_col} & The number of columns in an image.
\end{tabular}

\subsubsection*{Returns}
\begin{tabular}{ll}
	{\tt NOERROR} & Memory allocation is successful. \\
	{\tt MEMORYERROR} & Otherwise.
\end{tabular}

\subsubsection*{Description}
{\tt init} allocates memory of type float and size \_row x \_col to image data.

%
\subsection{Initializing Gaussian template}

\subsubsection*{Synopsis} 
\begin{verbatim}
	void initAsGauss()
\end{verbatim}

\subsubsection*{Description} 		
{\tt initAsGauss} initializes the image data into a 3 x 3 Gaussian kernel which can be used for image smoothing. A larger Gaussian kernel can be obtained by correlating the template with itself. The content of kernel is as below :
\begin{table}[thbp]
    \begin{center}
	\begin{tabular}{||c|c|c||} \hline
		0.049997 & 0.122466 & 0.049997 \\ \hline
		0.122466 & 0.299975 & 0.122466 \\ \hline
		0.049997 & 0.122466 & 0.049997 \\ \hline
	\end{tabular}
     \end{center}
\caption{Gaussian generating kernel}
\label{tbl:pe}
\end{table}

%
\subsection{Putting data into image matrix}

\subsubsection*{Synopsis} 
\begin{verbatim}
	void put(int m, int n, float val)
\end{verbatim}

\subsubsection*{Arguments} 
\begin{tabular}{ll}
	{\tt m} & Row coordinate. \\
	{\tt n} & Column coordinate.  \\
	{\tt val} & Floating point data 
\end{tabular}

\subsubsection*{Description} 		
{\tt put} stores the floating point data into location (m,n) of the image matrix.  The user should ensure that the row and column coordinates are within the valid range. 

%
\subsection{Getting data from image matrix}

\subsubsection*{Synopsis} 
\begin{verbatim}
	float get(int m, int n)
\end{verbatim}

\subsubsection*{Arguments}
\begin{tabular}{ll}
	{\tt m} & Row coordinate. \\
	{\tt n} & Column coordinate.
\end{tabular}

\subsubsection*{Description} 
{\tt get} returns the floating point data at location (m,n) of the image data. The validity of the row and column coordinates are not checked.

%
\subsection{Getting IMAGE row and col}

\subsubsection*{Synopsis} 
\begin{verbatim}		
	int getRow()
	int getCol()
\end{verbatim}

\subsubsection*{Returns}
Row or column size of the image data.

\subsubsection*{Description} 
{\tt get} returns the row and column size of the image matrix.

%
\subsection{Printing image data} 

\subsubsection*{Synopsis} 
\begin{verbatim}
	void print()
\end{verbatim}

\subsubsection*{Description} 
{\tt print} displays onto the screen all the values of the image matrix.

%
\subsection{Displaying image} 

\subsubsection*{Synopsis} 
\begin{verbatim}
void show(unsigned char blowup = 1, int num, 
          int h_offset = 0, int v_offset)
\end{verbatim}

\subsubsection*{Arguments}
\begin{tabular}{ll}
	{\tt blowup} & Image magnification factor. \\
	{\tt num} & The number of times the length of the window to 
		   be increased. \\
	{\tt h\_offset} & Horizontal offset of the image against the window 	
			 origin. \\
	{\tt v\_offset} & Vertical offset of the image against the window 	
			 origin. \\
\end{tabular}
	
\subsubsection*{Description} 
{\tt show} displays the image data on an X window. When {\tt h\_offset} and {\tt v\_offset} are not specified or set to 0, an X window of the image size will be created. Otherwise, no window will be generated and the image will be shown on an existing window. The parameter {\tt num} allows the user to create a larger window so that more than one image can be displayed on the same window. For instance, a second image can be shown next to the original image by calling the method {\tt show} with {\tt num} = 1 and {\tt h\_offset} = column of the original image.

%
\subsection{Reading image data from file}

\subsubsection*{Synopsis} 
\begin{verbatim}	
	int read(char *filename)
\end{verbatim}

\subsubsection*{Arguments}
\begin{tabular}{ll}
	{\tt filename} & Image file name.  
\end{tabular}

\subsubsection*{Returns}
\begin{tabular}{ll}
	{\tt NOERROR} & Successful read operation. \\
	{\tt MEMORYERROR} & Memory allocation failure. \\
	{\tt FILEIOERROR} & File I/O error. 
\end{tabular}

\subsubsection*{Description} 
By using the appropriate filter, {\tt read} can access the file of type SUN raster (\_ras) or binary (\_bin). Memory allocation is done automatically. However, reading of incorrect file format will cause memory allocation error. For a SUN raster colour image file, the image will probably need to be conditioned before it can be viewed clearly.

%
\subsection{Writing image data to file} 

\subsubsection*{Synopsis} 
\begin{verbatim}	
	int write(char *filename,int filetype = _ras)
\end{verbatim}

\subsubsection*{Arguments}
\begin{tabular}{ll}
	{\tt filename} & Image file name. \\
	{\tt filetype} & File type, either \_bin or \_ras.
\end{tabular}
	
\subsubsection*{Returns}
\begin{tabular}{ll}
	{\tt NOERROR} & Write is successful. \\
	{\tt MEMORYERROR} & Unable to create write buffers in memory. \\
	{\tt FILEIOERROR} & File interface error.
\end{tabular}

\subsubsection*{Description} 
	
{\tt write} converts the floating point image data to unsigned characters and linearly mapped them to the full range of 0 to 255 before they are written to the file specified. If the file is of SUN raster format, the raster file header, consisting of 8 bytes of integers, will be written as below :
\begin{quote}

\begin{verbatim}
		ras_magic    = 0x59a66a95    (Magic number of sun raster file) 
		ras_width    = column size   (Image column)
		ras_height   = row size	     (Image row)
		ras_depth    = 8             (Depth of colour plane)
		ras_length   = row * col     (Size in bytes of image)
		ras_type     = 1             (Old or new format raster file)
		ras_maptype  = 0             (Colour Map type) 	
		ras_maplength= 0             (Colour Map length)
\end{verbatim}     
\end{quote}

%
\subsection{Histogram conditioning of image}

\subsubsection*{Synopsis} 
\begin{verbatim}
void condition(double low_pct=0.9, double high_pct=0.95,
               double low_val=0.2, double high_val=0.9,
               double imm_pow=1.0)
\end{verbatim}

\subsubsection*{Arguments}
\begin{tabular}{ll}
	{\tt low\_pct, high\_pct} & Percentage range which the image 
				  pixels are to be mapped from.  \\
	{\tt low\_val, high\_val} & Intensity range over which
				  the pixels are to be mapped to. \\
	{\tt imm\_pow} & Exponential power used by the transformation
			 function.
\end{tabular}

\subsubsection*{Description} 
{\tt condition} performs image conditioning based on the histogram specification of an image. The transformation function is as following :
\eq
	T_{k} = \left\{
	    \begin{array}{ll}
		\frac{C_{k} low\_val}{low\_pct} & ;C_{k} < low\_val \\
		(high\_val -low\_val)(\frac{C_{k} - low\_pct}{high\_pct -
		low\_pct})^{imm\_pow} + low\_val & 
		;low\_val< C_{k} < high\_val\\
		(1 - high\_val) (\frac{C_{k} - high\_pct}{1 - 
		high\_pct})^{imm\_pow}
		 + high\_val & ;\mbox{Otherwise}
	    \end{array}
	    \right.
\en
where $C_{k}$ is the culmulative distribution function of histogram. Notice that histogram equalization can be achieved by setting {\tt low\_pct} $= 0$, {\tt high\_pct} $= 1$, {\tt low\_val} $= 0$, {\tt high\_val} $= 1$ and {\tt imm\_pow} $= 1$.


%
\subsection{Image correlation}

\subsubsection*{Synopsis} 
\begin{verbatim}
IMAGE *correlate (IMAGE *InputImg, int RowStep, int ColStep,
                  char verbose=0)
\end{verbatim}

\subsubsection*{Arguments}
\begin{tabular}{ll}
{\tt *InputImg} & Pointer to the correlating image template. \\
{\tt RowStep,}
{\tt ColStep} & Pixel spacing where the correlating template will be shifted
		each step.
\end{tabular}

\subsubsection*{Returns}
Pointer to a new correlated image. Returns NULL if memory allocation failed.

\subsubsection*{Description}
{\tt correlate} performs pixel-to-pixel correlation by calculating the inner product between the image template and data. The template will be shifted horizontally and vertically by {\tt ColStep} and {\tt Rowstep} each time.
	
%
\subsection{X window image generation}

\subsubsection*{Synopsis} 
\begin{verbatim}
void generateX(unsigned char blowup=1)
\end{verbatim}

\subsubsection*{Arguments}
\begin{tabular}{ll}
{\tt blowup} & Image magnification factor.
\end{tabular}

\subsubsection*{Description} 
{\tt generateX} creates an X image without displaying it on the screen.

%
\subsection{Cutting an image segment}

\subsubsection*{Synopsis}
\begin{verbatim}
IMAGE *IMAGE::cut(int sx, int sy, int height, int length)
\end{verbatim}

\subsubsection*{Arguments}
\begin{tabular}{ll}
	{\tt sx, sy} & Top left corner of the new copied image.\\
	{\tt height} & Height of the new copied image.\\
	{\tt length} & Length of the new copied image.
\end{tabular}

\subsubsection*{Returns}
Pointer to a new copied image. Returns NULL if memory allocation fails.	

\subsubsection*{Description}
{\tt cut} allows an image segment to be cut out from the image data. If the dimensions given are larger than the original image, {\tt cut} will select the maximun possible dimension which originated at ({\tt sx, sy}).

%
\subsection{Copying an image}

\subsubsection*{Synopsis}
\begin{verbatim}
	IMAGE *IMAGE::copy()
\end{verbatim}

\subsubsection*{Returns}
Pointer to a new copied {\tt IMAGE} object. Returns NULL if memory allocation fails.	

\subsubsection*{Description}
{\tt copy} duplicates the {\tt IMAGE} object.

%
\subsection{Filling of an area of image}

\subsubsection*{Synopsis}
\begin{verbatim}
int fill(float val, int sx=0, int sy=0, int length=0, int height=0)
\end{verbatim}

\subsubsection*{Arguments}
\tb
%\begin{tabular}{ll}
	{\tt val } & Pixel value.\\
	{\tt sx, sy} & Top left corner of the filled image segment.\\
	{\tt height} & Height of the filled image segment.\\
	{\tt length} & Length of the filled image segment.
\te
%\end{tabular}

\subsubsection*{Returns}
\tb
{\tt NOERROR} & Segment filled successfully. \\
{\tt PARAMERROR} & Illegal parameter values used.
\te


\subsubsection*{Description}
{\tt fill} initializes an image area to the given pixel value.



\subsection{Example : Reading, writing and correlation of images}
This program performs correlation between the input image and template. The correlated image will be displayed on an X window and written to an output file.

\begin{verbatim}
void testmain( char *imgfile,   /* input image file */ 
               char *tmpfile,   /* image template */
               char *outfile,   /* output image file */
               int mag )        /* magnification factor */
{ 
 
        IMAGE test1,test2;
        IMAGE *test3;    
 
        /* Read test images */
        if (test1.read(imgfile))  exit(-1);
         
        printf("Showing test image : %s",imgfile);
        test1.show(mag);
         
        /* If user did not specify template image, use a gaussian template */
        if (tmpfile) {
           if( test2.read(tmpfile) )  exit( -1 );
        }
        else
           test2.initAsGauss();
        
        test2.show(mag);
         
        printf("\nCorrelating image..");
        test3=test1.correlate(&test2,2,2,1);
  
        /* Show all three images horizontally */        
        /* Allocate window space for three images */
        test1.show(mag,3,0,0);
         
        /* Show images with offsets */
        test2.show(mag,1,test1.getCol());
        test3->show(mag,1,test2.getCol()+test1.getCol());
        printf("\nPress enter to continue ");
        getchar();
        
        /* write result to file */
        if (outfile)
           test3->write(outfile) ;
        else
           test3->write("out.bin");
         
        printf("End of test.");
        xwin_close();  
}
\end{verbatim}

First, an input image will be read. If there is no template image, {\tt initAsGauss} will create a 3 by 3 Gausian kernal. The resulting image, {\tt test3}, is returned by {\tt correlate} and displayed together with the input image and template by {\tt show}. 


\subsection{Example : Histogram specification of an image}

\begin{verbatim}
void testmain( char *imgfile,        /* input image file */
               int blowup,           /* magnification factor */
               float lp , float hp,  /* percentage range */
               float lv, float hv,   /* intensity range */
               float ep  )           /* exponential power */
{
        IMAGE myimage;
 
        if  ( !(myimage.read(imgfile)) ) {
        
           printf("Displaying image .... \n\n");
           printf("Histogram Specification parameters :\n");
           printf("\n    Low Percent:%f     High Percent:%f",lp,hp);
           printf("\n    Low Value  :%f     High Value  :%f",lv,hv);
           printf("\n    Exponent   :%f     Magnification:%d",ep,blowup);

           myimage.show(blowup,2);
           myimage.condition(lp,hp,lv,hv,ep);
           printf("Displaying conditioned image ... Press enter to continue\n");
           myimage.show(blowup,1,myimage.getCol()*blowup);
           getchar();
           xwin_close();
        }
}
\end{verbatim}

Histogram specification is done by calling {\tt condition}. With appropriate input parameters, it can either perform noise reduction or image enhancement. In this program, the conditioned and input image will be displayed side by side on an X window.


\subsection{Example : Cutting and copying of image segments}

\begin{verbatim}
void testmain( char *imgfile,   /* input image file */
               char *outfile,   /* output image file */
               int mag )        /* magnification factor */
{
        IMAGE test1;
        IMAGE *test2,*test3;
         
        int sx, sy, length, height;
         
        /* Read image file */
        if  (test1.read(imgfile) ) exit(-1);
         
        printf("Showing test1 : %s",imgfile);
        test1.show(mag); 
         
        /* Using X windows to select an image segment */ 
        xwin_SelRegion( &sx,&sy,&length,&height);
        test2 = test1.cut(sx/mag ,sy/mag ,length/mag, height/mag);
         
        /* Copy image*/  
        test3 = test2->copy();   
         
        /* Show images in line */
        printf("\nShowing cut image and copied image "); 
        test2->show(mag,2);
        test3->show(mag,1,test2->getCol());
        printf("\nPress enter to continue...");
        getchar();
         
        if (outfile) {
                printf("\nWriting to file %s",outfile);
                test3->write(outfile); 
        }
 
        printf("End of test..");
        xwin_close();
}
\end{verbatim}
 
This program uses X window interface, {\tt xwin\_SelRegion}, to select an image segment and then cut it. This segment is finally copied to another image segment.