Programming Assignment 1
-
Upload
idona-preston -
Category
Documents
-
view
37 -
download
1
description
Transcript of Programming Assignment 1
![Page 1: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/1.jpg)
Programming Assignment 1
CS302 Data Structures
![Page 2: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/2.jpg)
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.
![Page 3: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/3.jpg)
Implementation
•You should implement:
image.h (class specification)
image.cpp (class implementation)
driver.cpp (application or client)
![Page 4: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/4.jpg)
image.h
class Image { public:
constructor //parameter less
constructor //with parameters
copy_constructor
operator=
getImageInfo
getPixelVal
setPixelVal
getSubImage
meanGray
![Page 5: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/5.jpg)
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;
};
![Page 6: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/6.jpg)
First ...
• Carefully implement the following functions:
– constructor
– destructor
– copy-constructor
– assignment
![Page 7: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/7.jpg)
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.
![Page 8: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/8.jpg)
writeImage(fileName, image) (client function – has been provided)
• Writes out an image to a file in PGM format.
![Page 9: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/9.jpg)
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.
![Page 10: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/10.jpg)
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.
![Page 11: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/11.jpg)
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.
![Page 12: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/12.jpg)
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.
![Page 13: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/13.jpg)
int meanGray()
• Computes the average gray-level value of an image.
• Returns the value as “int” (i.e., truncate the result)
![Page 14: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/14.jpg)
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 15: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/15.jpg)
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
![Page 16: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/16.jpg)
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 17: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/17.jpg)
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
![Page 18: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/18.jpg)
reflectImage(flag, oldImage)
• Reflects the input image along the horizontal or vertical directions.
vertical reflection
![Page 19: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/19.jpg)
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 20: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/20.jpg)
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
+ =
![Page 21: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/21.jpg)
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
- =
![Page 22: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/22.jpg)
negateImage()
• Computes the negative of an image.
• This can be done using the following transformation
( , ) 255 ( , )O r c I r c negative
![Page 23: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/23.jpg)
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
![Page 24: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/24.jpg)
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)
![Page 25: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/25.jpg)
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
![Page 26: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/26.jpg)
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
![Page 27: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/27.jpg)
rotateImage(cont’d)
• Serious problem: the rotated image could contain numerous “holes” where no value was computed for a pixel !!
original rotated
![Page 28: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/28.jpg)
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 !!
![Page 29: Programming Assignment 1](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813691550346895d9e1a06/html5/thumbnails/29.jpg)
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.