CS 112 - Lab 3
Recursion for Image Processing Applications


Section 1: Introduction

The goal of this lab is to:

If you have not already done so, create a directory for this lab and copy the lab 3 files into it. If you start off in your home directory you can do the following:

	mkdir ~/cs112/lab3
	cd ~/cs112/lab3
	cp ~cs112/lab3/* .

Section 2: Topics for Lecture

This week's lecture will include

Section 3: Image Processing

Image processing is used in a wide variety of applications such as the computerized analysis of microscopic slides, CAT scan, MRI imaging, digital mammography, transportation systems, communicable diseases, and in environmental science. In many of these applications, the fundamental data structure of interest is a two-dimensional array (or matrix) of numbers which can represent properties such as brightness, colors, or structure. A sample 10 by 10 grid is illustrated below.
*     *     *     *  *     *
*  *  *     *  *  *  *  *  *
*           *  *  *     *  *
      *     *  *     *
*  *  *  *  *  *  *        *
*  *  *  *     *  *     *  *
   *  *     *  *     *     *
*  *  *     *  *  *  *     *
*     *     *  *  *  *     *
*     *     *  *  *  *     *
Each element of the two-dimensional grid is considered either occupied or not occupied (FILLED or EMPTY). From the small example above you can see that the occupied elements (or cells) form a variety of sizes and shapes. A cluster is defined to be a set of connected occupied grid cells. Here, connected means that for any pair of grid elements (or cells) within a cluster, you can trace an unbroken path of occupied grid cells between the pair. In the sample 10 by 10 grid above, you can see that the cluster containing the cell in position (0,0), i.e., in the 1st row and 1st column, spans a total of 6 cells.

Natural questions that arise in imaging processing applications include: How large is a particular cluster? What is the relative location of a cluster? Such features or properties of the grid cells can be used to classify the clusters into various categories (e.g., normal cells and malignant cells).

The goal of this lab is to use recursion to determine the size (i.e., number of grid cells) of a particular cluster. The image of interest will be 50 by 50 grid cells with random assignment of occupied cells. Realize that the computer cannot see the cluster you are analyzing - it only knows that for any given grid cell that there are eight elements immediately connected to it which may either be FILLED or EMPTY.

You are to implement a recursive algorithm to determine the size (in cells) of a cluster (given a starting cell) according to the following specifications:

Note: It is permissible to have redundant visits to a grid cell to check whether it is FILLED or EMPTY but you cannot count a FILLED cell more than once.

Section 4: Code Development

You can copy the following main( ) function stored in the file main.c from the ~cs112/lab3 subdirectory:
                #include <math.h>
                #include <stdio.h>
                #include <stdlib.h>

                #define MAXROW 50
                #define MAXCOL 50
                #define MAXPOW 31

                typedef enum { EMPTY, FILLED } STATUS;
                typedef STATUS GRID_TYPE [MAXROW] [MAXCOL];

                void    load_grid( GRID_TYPE, unsigned int, FILE * );
                int     rand(void);
                int     srand( unsigned int);
                float   timer(void);  /* returns elapsed CPU time */
                double  exp2(double);
                int  clus_cnt ( GRID_TYPE, int, int);

                main ( )
                {
                    GRID_TYPE grid;
                    int row,col;
                    unsigned int seed;
                    float time_start, time_end;
                    char *out_file;
                    FILE *out_file_ptr;

                    out_file="clus_output";
                    if (!(out_file_ptr = fopen(out_file, "w"))) {
                      printf("cannot open file %s for writing\n", out_file);
                      exit(-1);
                    }

                    /* Fill grid w/ clusters */
                    printf("\nEnter seed for srand -> ");
                    scanf("%d", &seed);
                    load_grid(grid,seed,out_file_ptr);

                    /* Load cluster coordinates */
                    printf("\nEnter row col for starting point-> ");
                    scanf("%d %d", &row, &col);
                    time_start=timer();
                    printf("\n Cluster originating from row=%d and ",row);
                    printf("col=%d is connected to %d elements\n",
                            col, clus_cnt(grid,row,col) );
                    time_end=timer();
                    printf("\n Elapsed CPU time (in seconds) -> %7.3f\n",time_end);
                    return(0);

                 }
The function timer ( ) is used to record the elapsed CPU time from a previous call to timer This function is available in the file timer.c from the ~cs112/lab3 subdirectory.

The function load_grid ( ) will produce a 50 by 50 grid of random assignments of FILLED and EMPTY. You can also find it stored in the load_grid.c file from the ~cs112/lab3 subdirectory.

Your primary job is to produce the recursive function clus_cnt ( ) Which has a function header similar to:

int clus_cnt ( GRID_TYPE grid, int row, int col)
That is, the function clus_cnt is passed 3 parameters: the grid itself, followed by the row and column dimension of the grid. This is the only function that you will submit to the TA's for grading.

A complete C program with all the function described below will produce an output file clus_out which should contain a schematic diagram of the random grid generated. This grid will be composed of blank spaces and asterisks to illustrate the grid.

You will supply an integer seed from stdin in the main ( ) function as well as starting row and column (both integers ranging from 0 to 49) grid coordinates for the cluster size computation.

Be sure to test your program with a variety of seeds and try to verify by hand that your cluster size is correct using the clus_out ) files generated each time. Keep in mind that yor program must work correctly for any seed value retrieved from stdin.

Note: For this program, you must link in the pre-compiled math libraries via the -lm option for gcc. In your makefile you might have a line similar to the following:

       clus.x:	main.o load_grid.o clus_cnt.o timer.o
          	gcc -o clus.x main.o load_grid.o clus_cnt.o timer.o -lm

Section 5: Handing in your C code and answers

When you have completed this lab assignment, you should have the following program in your ~/cs112/lab3 directory: Then, to submit your program(s) for grading, issue the command
 ~cs112/submit lab3 
at your UNIX prompt. This utility program will copy your programs for grading and an electronic mail message will be sent to you to acknowledge your submission. You may check the status of your submission at any time by typing the command
 ~cs112/query lab3 
at your UNIX prompt. You will automatically receive an electronic mail message confirming the status of your lab submission.

You should also answer the questions below and submit them in hardcopy to the TAs at the beginning of Lab 4 (no late submissions accepted).

This week's questions

For credit on this lab, your software must be received by midnight February 13 and your answers to the lab questions must be received at the beginning of your lab section (8:00am or 11:10am) on Friday, February 14.

Note: Always bring your ID with you to lab.