Programming Assignment 1

Post on 03-Jan-2016

37 views 1 download

Tags:

description

Programming Assignment 1. CS302 Data Structures. Goals. Improve your skills with manipulating dynamic arrays . Improve your understanding of constructors , destructors , and copy-constructors . Improve your skills with operator overloading . - PowerPoint PPT Presentation

Transcript of Programming Assignment 1

Programming Assignment 1

CS302 Data Structures

Goals

• Improve your skills with manipulating dynamic arrays.

• Improve your understanding of constructors, destructors, and copy-constructors.

• Improve your skills with operator overloading.

• Familiarize yourself with reading/writing images from/to a file.

• Familiarize yourself with image processing.

• Improve your skills with documenting and describing your programs.

Implementation

•You should implement:

image.h (class specification)

image.cpp (class implementation)

driver.cpp (application or client)

image.h

class Image { public:

constructor //parameter less

constructor //with parameters

copy_constructor

operator=

getImageInfo

getPixelVal

setPixelVal

getSubImage

meanGray

image.h (cont’d) enlargeImage

shrinkImage

reflectImage

translateImage

rotateImage

operator+

operator-

negateImage

private:

int N; //no of rows

int M; //no of columns

int Q; //no of gray-level values

int **pixelVal;

};

First ...

• Carefully implement the following functions:

– constructor

– destructor

– copy-constructor

– assignment

readImage(fileName, image)(client function – has been provided)

• Reads in an image in PGM format from a file.

• A “NOT-PGM” exception should be raised if the image is not in PGM format.

writeImage(fileName, image) (client function – has been provided)

• Writes out an image to a file in PGM format.

getImageInfo(noRows, noCols, maxVal) (member function – has been provided)

• Returns:– the height (# of rows)

– width (# of columns) of the image

– the max gray-level value allowed (i.e., 255 for 8-bit images)

• These values are “returned” using call by reference.

int getPixelVal(r, c) (member function – has been provided)

• Returns the pixel value at (r, c) location.

• An OUT-OF-BOUNDS exception should be raised if (r,c) falls outside the image.

setPixelVal(r, c, value) (member function – has been provided)

• Sets the pixel value at location (r, c) to value.

• An OUT-OF-BOUNDS exception should be raised if (r,c) falls outside the image.

getSubImage(Ulr, Ulc, LRr, LRc, oldImage)

• It crops a rectangular subimage from the input image.– The subimage can be defined by the coordinates of its upper-left

(UL) and lower-right (LR) corners.

– Useful for limiting the extent of image processing operations to some small part of the image.

int meanGray()

• Computes the average gray-level value of an image.

• Returns the value as “int” (i.e., truncate the result)

enlargeImage(s, oldImage)

• Enlarges the input image by some integer factor s

• It is useful for magnifying small details in an image.

2 times the original size

enlargeImage (cont’d)

• Replicate pixels such that each pixel in the input image becomes an s x s block of identical pixels in the output image.

• Most easily implemented by iterating over pixels of the output image and computing the coordinates of the corresponding input image pixel.

Example: s = 2

shrinkImage(s, oldImage)

• Shrinks the input image by some integer factor s

• It is useful, for example, for reducing a large image in size so it fits on the screen.

1/2 times the original size

shrinkImage (cont’d)• Sample every s-th pixel in the horizontal and vertical

dimensions and ignore the other.

• Most easily implemented by iterating over pixels of the output image and computing the coordinates of the corresponding input image pixel.

Example: s = 2

reflectImage(flag, oldImage)

• Reflects the input image along the horizontal or vertical directions.

vertical reflection

reflectImage (cont’d)• Reflection along either direction can be performed by

simply reversing the order of pixels in the rows or columns of the image

operator+ (image addition)

• Computes the weighted sum of two images.

• You can use it for simple image morphing !

1 2( , ) ( , ) (1 ) ( , )

0 1

O r c aI r c a I r c

a

+ =

operator- (image subtraction)

• Computes the difference of two given images.

• Useful for change detection.

• Compute absolute difference:

1 2( , ) | ( , ) ( , ) |O r c I r c I r c

- =

negateImage()

• Computes the negative of an image.

• This can be done using the following transformation

( , ) 255 ( , )O r c I r c negative

translateImage(t, oldImage)

• Translates the pixel locations by some amount t.

• Translation can be performed using the equations:

32 x 32 translation

'r r t 'c c t

rotateImage(theta, oldImage)• Rotates the input image by some angle theta about (0,0).

• Rotation uses the following equations:

' cos( ) sin( )

' sin( ) cos( )

r r theta c theta

c r theta c theta

if theta > 0: counter- clockwise about (0,0) if theta < 0: clockwise about (0,0)

rotateImage (cont’d)• Equations to rotate an image about any point :

-45 degreesrotation (aboutthe center of theimage)

0 0( , )r c

0 0 0

0 0 0

' ( )cos( ) ( )sin( )

' ( )sin( ) ( )cos( )

r r r r theta c c theta

c c r r theta c c theta

Ignore pixels whose coordinates fall outside the range.

Find nearest neighbors to and 'r 'c

rotateImage (cont’d)• Practical problems:

– consider rotating pixel (0,100) by 90 degrees:

– consider rotating pixel (50,0) by 35 degrees:

' cos(90) cos(90) 100sin(90) 100

' sin(90) sin(90) sin(90) 0

r r c

c r c o

' cos(35) sin(35) 50cos(35) 40.96

' sin(35) cos(35) 50sin(35) 28.68

r r c

c r c

rotateImage(cont’d)

• Serious problem: the rotated image could contain numerous “holes” where no value was computed for a pixel !!

original rotated

Extra Credit

• Usually, each assignment will contain a number of extra credit questions; answering the extra credit questions is optional.

• Your are expected to spend some time thinking about each question and provide a reasonable answer.

• Reasonable ideas, which have been implemented and demonstrated to the TA, will get extra credit !!

Useful Tips

• Learn how to use a debugger– Very important for debugging your programs efficiently!

• Learn how to use makefiles (especially if you use Linux/Unix):– Very useful when your application is split over many files.