EEL-5820 Libraries

Table of Contents

1. Introduction 2

2. Sample Images 2

3. FlipUpDown Image Processing Program in C++ 2

4. Compiling and Linking with C++ Libraries 4

5. Tutorial: Compiling FlipUpDown Program 6

EEL-5820 Image Processing Fall 1999

1.  Introduction

As an example of image processing, the sample program FlipUpDown is presented. This program contains basic routines for manipulating images, specifically black and white IRIS image files. This type of file stores 1 byte per pixel in which the pixel value is an unsigned short integer (unsigned char in C++) that ranges from 0 to 255. The FlipUpDown program takes an input image file, flips it up side down, and saves it in an output image file.

2.  Sample Images

Several directories containing sample images can be found under the /cate/cate/Images directory. These images can be displayed using the SGI X Windows file manager by double clicking on the image file. Other commands available to display images are ipaste, imgview, and imgworks.

3.  FlipUpDown Image Processing Program in C++

The FlipUpDown C++ program shows how to load an image file into a two-dimensional array, implement an image processing algorithm that stores the results into another two-dimensional array, and writes the results into another image file. The example also shows how to time the whole process. This file is available to all CATE users, and is located in the /cate/cate/EEL5820/FlipUpDown_Example directory with the name FlipUpDown.c++.

The Source Code of FlipUpDown.c++

// FlipUpDown.cpp

// Author Peterjohn Hugh

// Center for Advanced Technology and Education

// Modified August 24, 1998

// EEL5820 - Image Processing

//

// This program is an image processing example.

// It takes an input image and flips it upside

// down.


#include <iostream.h>

#include <Array2D.h>

#include <ImageIO.h>

#include <Timer.h>

int

main(int argc, char **argv)

{

// *********************

// * Declare Variables *

// *********************

// Declaring empty Array2D objects of integers

Array2D<int> InputImage,OutputImage;

Timer Watch;

int x,y;

// Start Timer

Watch.Start();

// ************************

// * Check Program Syntax *

// ************************

// The argc variable contains the number of command line

// arguments including the program name. The argv variable

// is an array of strings containing each command line argument

if(argc<3)

{

cerr<"The correct syntax is:\n";

cerr<argv[0]<" [InputFile] [OutputFile]"<endl;

exit(1);

}

// *******************

// * Load Image File *

// *******************

// Loads an input image file into an Array2D<T> (Matrix)

if(!ImgBWLoad(InputImage,argv[1]))

{

cerr<"File: "<argv[1]<" not loaded"<endl;

exit(1);

}


// ******************************

// * Image Processing Algorithm *

// ******************************

// Allocate Array2D Object for the output image

// with the same size as the input image

OutputImage=Array2D<int>(InputImage.Size2(),InputImage.Size1());

// Flip Upside Down Algorithm

// x scans through the columns

// y scans through the rows

for(x=0; x<InputImage.Size1(); x++)

for(y=0; y<InputImage.Size2(); y++)

{

OutputImage[y][x]=InputImage[InputImage.Size2()-1-y][x];

}

// *******************

// * Save Image File *

// *******************

// Saves an Array2D<T> into an output image file

if(!ImgBWSave(OutputImage,argv[2]))

{

cerr<"File: "<argv[2]<" not saved"<endl;

exit(1);

}

// Stop Timer

Watch.Stop();

cout<"Running time for process is: "<Watch<endl;

return 1;

}

4.  Compiling and Linking with C++ Libraries

The above program can be compiled and linked using commands from the UNIX shell; however, that method tends to be inefficient especially while debugging. The best method is to utilize a compiler/linker script with the program make. The program make when issued alone, by default, looks in the current directory for a compiler/linker script file called Makefile. It compiles and links the source code specified by the environment variables within the file into an executable file.


The Makefile of FlipUpDown.c++

#!smake

# Makefile for FlipUpDown Program

# Peterjohn Hugh, August 24, 1998

# Default macros

include $(ROOT)/usr/include/make/commondefs

# Libraries path

LIBPATH = /cate/cate/EEL5820/libraries

# Executable Name

TARGETS = FlipUpDown

# Compile Files

C++FILES = FlipUpDown.c++

# Compiler Include Path

LC++INCS = -I$(LIBPATH)/include

# Compiler Flags

LC++DEFS = -g

# Linker Library and Include Path

LLDINCS = -L$(LIBPATH)/lib -I$(LIBPATH)/include

# Linker Libraries

LLDLIBS = -limage -lTimer

# Linker Flags

LLDFLAGS = -o

default all: $(TARGETS)

targets: $(TARGETS)

$(TARGETS): $(OBJECTS)

$(C++) $(LLDFLAGS) $(TARGETS) $(OBJECTS) $(LLDINCS) $(LLDLIBS)

include $(COMMONRULES)

The following is a brief explanation of the changeable environment variables in the Makefile:

TARGETS is the name of the executable file generated.

TARGETS = FlipUpDown

C++FILES is the list of files that need to be compiled separated by spaces.

C++FILES = FlipUpDown.cpp Flip.cpp Down.cpp Up.cpp

LC++DEFS is the list of compiler flags separated by spaces. Execute man CC for an explanation of each compiler flag.

LC++DEFS = -g –O2

LLDLIBS is the list of libraries separated by spaces that are linked to the program.

LLDLIBS = -limage –lm -lTimer

The sample Makefile is located in the /cate/cate/EEL5820/FlipUpDown directory and is available to all CATE users. A suggestion for project development is to create a directory for each project. Create a Makefile for each project so that compiling and linking each project only involves issuing the command make.

5.  Tutorial: Compiling FlipUpDown Program

a)  Login into an SGI Indy Machine.

b)  Create a directory in the home directory where the programs will be stored:

> cd ~

> mkdir [name]

> cd [name]

c)  Copy the program files to the directory:

> cp /cate/cate/EEL5820/FlipUpDown_Example/* .

d)  Compile the program:

> make

e)  Run the program:

> FlipUpDown /cate/cate/Images/cat256.bw out.bw

f)  Display the images

> ipaste /cate/cate/Images/cat256.bw

> ipaste out.bw

Center for Advanced Technology and Education Page 3

EEL-5820 Image Processing Fall 1998

cat256.bw

out.bw

Center for Advanced Technology and Education Page 6