29
Programming Assignment 1 CS308 Data Structures

Programming Assignment 1

  • Upload
    galvin

  • View
    22

  • Download
    0

Embed Size (px)

DESCRIPTION

Programming Assignment 1. CS308 Data Structures. Goals. Familiarize yourself with reading images from a file. Familiarize yourself with writing images to a file. Familiarize yourself with displaying/printing images. Improve your skills with manipulating arrays . - PowerPoint PPT Presentation

Citation preview

Page 1: Programming Assignment 1

Programming Assignment 1

CS308 Data Structures

Page 2: Programming Assignment 1

Goals

• Familiarize yourself with reading images from a file.

• Familiarize yourself with writing images to a file.

• Familiarize yourself with displaying/printing images.

• Improve your skills with manipulating arrays.

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

• Improve your skills with operator overloading.

• Learn to document and describe your programs.

Page 3: Programming Assignment 1

Image Datatype Functions

• Read an image from a file

• Save an image to a file.

• Get the info of an image.

• Set the value of a pixel.

• Get the value of a pixel.

• Extract a region of interest (ROI) from an image.

• Compute the mean gray-level value of an image.

• Enlarge an image by some factor ss

• Shrink an image by sine factor ss

• Reflect an image in the horizontal or vertical derections.

Page 4: Programming Assignment 1

Image Type Functions (cont’d)

• Translate an image by some amount tt

• Rotate an image by some angle thetatheta..

• Compute the sum of two images.

• Compute the difference of two images.

• Compute the negative of an image.

Page 5: Programming Assignment 1

Image Type Specification class Image {

public:

constructor //default - no parameters

constructor //with parameters

copy_constructor

operator= //overload assignment

getImageInfo

getPixelVal

setPixelVal

inBounds

getSubImage

meanGray

enlargeImage

Page 6: Programming Assignment 1

Image Type Specification (cont’d)

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;

};

Page 7: Programming Assignment 1

readImage(fileName, image)

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

• Not a member function

Page 8: Programming Assignment 1

writeImage(fileName, image)

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

• Not a member function

Page 9: Programming Assignment 1

getImageInfo(noRows, noCols, maxVal)

• It returns the height (no of rows) and width (no of columns) of the image.

• Also, it returns the max gray-level value allowed (i.e.,255)

• These values should be returned using “call by reference”

Page 10: Programming Assignment 1

int getPixelVal(r, c)

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

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

Page 11: Programming Assignment 1

setPixelVal(r, c, value)

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

Page 12: Programming Assignment 1

bool inBounds(r, c)

• Returns true if the pixel (r, c) is inside the image..

Page 13: Programming Assignment 1

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

• It crops a rectangular subimage from the input image.

• It is useful for limiting the extent of image processing operations to some small part of the image.

• The subimage can be defined by the coordinates of its upper-left (UL) and lower-right (LR) corners

newImage.getSubimage( xxx )

Page 14: Programming Assignment 1

int meanGray()

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

• Returns the value as “int” (truncate the result)

Page 15: Programming Assignment 1

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

Page 16: Programming Assignment 1

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.

Page 17: Programming Assignment 1

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

Page 18: Programming Assignment 1

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

dimensions and ignore the others.

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

Page 19: Programming Assignment 1

reflectImage(flag, oldImage)

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

vertical reflection

Page 20: Programming Assignment 1

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

Page 21: Programming Assignment 1

operator+ (image addition)

• Computes the sum of two given images.

• It is used to combine the information in two images.

• You can implement a simple image morphing algorithm !

1 2( , ) (1 )O r c aI a I formula:

+ =

Page 22: Programming Assignment 1

operator- (image subtraction)

• Computes the difference of two given images.

• Useful for change detection.

• Consider absolute difference:

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

- =

Page 23: Programming Assignment 1

negateImage()

• Computes the negative of an image.

• This can be done using the following transformation

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

Page 24: Programming Assignment 1

translateImage(t, oldImage)

• Translates the input image by some amount t.

• The translation process is performed with the following equations:

32 x 32 translation

'r r t 'c c t

Page 25: Programming Assignment 1

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

• The rotation process requires the use of the following equations:

-45 degreesrotation (aboutthe center of theimage)

' cos( ) sin( )

' sin( ) cos( )

r r theta c theta

c r theta c theta

theta>0:counter-clockwiseabout (0,0)

Page 26: Programming Assignment 1

rotateImage(cont’d)

• Equations to rotate an image about its center :

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

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

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

0 0( , )r c

' 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

Page 27: Programming Assignment 1

rotateImage(cont’d)

• Possible solutions: – ignore pixels whose coordinates fall outside the range.

– find nearest neighbors to and

• The rotated image can contain numerous “holes” where no value was computed for a pixel !!

original rotated

'r 'c

Page 28: Programming Assignment 1

Questions accompanying the assignments

• Each assignment contains a number of questions that you must answer.

• These questions are worth 10% of the assignment grade.

• Answering these questions does not require prior knowledge of image processing.

• Your are expected to spend some time thinking about these questions.

• Reasonable answers will receive full points …

• Interesting ideas which are implemented and demonstrated will get extra credit !!

Page 29: Programming Assignment 1

Useful Tips

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

– There is information in the course’s webpage

• Learn how to use makefiles:– Very useful when your application is split over many files.

– There is information in the course’s webpage.