1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland
ENGI 2420 – Structured Programming Lab Tutorial 8 2 Assignment-8 Purpose of this assignment implement functions for image processing Structure of C++ files - assign8.h (downloadable): contains the declarations for the functions - a8main.cpp (downloadable): main function that can be used as test code - assign8.cpp: contains your implemented functions (only submit this one via web-submit) - you may want to create your own test code in a separate.cpp file (do NOT submit your test code)
ENGI 2420 – Structured Programming Lab Tutorial 8 3 Data Format (I) We provide readImage and writeImage functions in a8main.cpp These both use the same data format The data consists of a sequence of integers. The first pair is to be interpreted as the number of rows and columns, respectively, in the image (i.e., the size of the data). The remaining numbers are the data, row by row.
ENGI 2420 – Structured Programming Lab Tutorial 8 4 Data Format (II) a 5x6 image with diagonal stripes:
ENGI 2420 – Structured Programming Lab Tutorial 8 5 threshold() Function (I) void threshold(int src[], int rows, int cols, int dest[], int thr) Convert the image in src to a binary (black and white) storing the resulting image in dest, with pixel values of only either 0 or 255. If an image pixel value is greater than thr (the threshold), then set it to 255, otherwise set it to 0 Do not alter the data in src
ENGI 2420 – Structured Programming Lab Tutorial 8 6 threshold() Function (II) For example, if thr = 100, then src and dest should be
ENGI 2420 – Structured Programming Lab Tutorial 8 7 blur() Function (I) void blur(int src[], int rows, int cols, int dest[]) Make a blurred copy of the image in src into dest. The value of each pixel in dest is calculated as the average of the corresponding pixel in src with its adjacent neighbours (normally a total of 9 pixels). Round to the nearest integer. (When the average is exactly half way between two integers, round up.)
ENGI 2420 – Structured Programming Lab Tutorial 8 8 blur() Function (II) for a pixel not on the edges: dest[r, c] = (src[r-1,c-1] + src[r-1,c] + src[r-1,c+1] + src[r,c-1] + src[r,c] + src[r,c+1] + src[r+1,c-1] + src[r+1,c] + src[r+1,c+1])/9. Note that, for pixels on an edge of the image or in the corner, the number of neighbours is fewer so the summation and the divisor will be different
ENGI 2420 – Structured Programming Lab Tutorial 8 9 threshold() Function (III) For the diagonal stripe image example, the contents of src and dest should be
ENGI 2420 – Structured Programming Lab Tutorial 8 10 Running the Program Our main program uses a "program argument" to select the transformation (threshold or blur). 0 is for threshold and 1 is for blur The default is threshold, but it is easy to change by changing the line int functionSelector = FUNC_THRESHOLD ; to int functionSelector = FUNC_BLUR ; You can also select the function using the "program argument" feature of Eclipse's Run Dialog
ENGI 2420 – Structured Programming Lab Tutorial 8 11 Running with MMGood2 MMGood2 is a java program that will execute your program on JPEG, PNG, or GIF files so you can see it working For windows –Download and uncompress mmgood2.zip –Double click on mmgood2.bat from a Window's Explorer window
ENGI 2420 – Structured Programming Lab Tutorial 8 12 Running with MMGood2 Once mmgood's main window has appeared, and you have compiled your project, you can –Load an input JPEG, PNG, or GIF file –Observe the input image
ENGI 2420 – Structured Programming Lab Tutorial 8 13 Running with MMGood2 –Select your assignment 8 executable. (It is in the Debug subdirectory of your project directory and, in Windows, its name will end with ".exe".) –Set the program argument to be 0 (threshold) or 1 (blur) –Run the executable
ENGI 2420 – Structured Programming Lab Tutorial 8 14 Running with MMGood2 –Wait patiently for your program to finish –Observe the output image –You can save it as a jpeg, if you like