1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland.

14
1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland

Transcript of 1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland.

Page 1: 1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland.

1

ENGI 2420Structured Programming

(Lab Tutorial 8)

Memorial University of Newfoundland

Page 2: 1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland.

2 ENGI 2420 – Structured Programming

Lab Tutorial 8

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)

Page 3: 1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland.

3 ENGI 2420 – Structured Programming

Lab Tutorial 8

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.

Page 4: 1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland.

4 ENGI 2420 – Structured Programming

Lab Tutorial 8

Data Format (II)

a 5x6 image with diagonal stripes:

5 6

123 44 1 99 88 200

44 1 99 88 200 123

1 99 88 200 123 44

99 88 200 123 44 1

88 200 123 44 1 99

Page 5: 1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland.

5 ENGI 2420 – Structured Programming

Lab Tutorial 8

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

Page 6: 1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland.

6 ENGI 2420 – Structured Programming

Lab Tutorial 8

threshold() Function (II)

For example, if thr = 100, then src and dest should be

123 44 1 99 88 200

44 1 99 88 200 123

1 99 88 200 123 44

99 88 200 123 44 1

88 200 123 44 1 99

255 0 0 0 0 255

0 0 0 0 255 255

0 0 0 255 255 0

0 0 255 255 0 0

0 255 255 0 0 0

Page 7: 1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland.

7 ENGI 2420 – Structured Programming

Lab Tutorial 8

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.)

Page 8: 1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland.

8 ENGI 2420 – Structured Programming

Lab Tutorial 8

blur() Function (II) for a pixel not on the edges: dest[r, c] = 0.5 + (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

Page 9: 1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland.

9 ENGI 2420 – Structured Programming

Lab Tutorial 8

threshold() Function (III) For the diagonal stripe image example, the

contents of src and dest should be

123 44 1 99 88 200

44 1 99 88 200 123

1 99 88 200 123 44

99 88 200 123 44 1

88 200 123 44 1 99

53 52 55 96 133 153 52 56 80 110 129 130 55 80 110 129 105 89 96 110 129 105 75 52 119 133 130 89 52 36

Page 10: 1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland.

10 ENGI 2420 – Structured Programming

Lab Tutorial 8

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

Page 11: 1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland.

11 ENGI 2420 – Structured Programming

Lab Tutorial 8

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

Page 12: 1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland.

12 ENGI 2420 – Structured Programming

Lab Tutorial 8

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

Page 13: 1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland.

13 ENGI 2420 – Structured Programming

Lab Tutorial 8

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

Page 14: 1 ENGI 2420 Structured Programming (Lab Tutorial 8) Memorial University of Newfoundland.

14 ENGI 2420 – Structured Programming

Lab Tutorial 8

Running with MMGood2

– Wait patiently for your program to finish

– Observe the output image

– You can save it as a jpeg, if

you like