ASSIGNMENT 3 - McGill School Of Computer Sciencecs202/2015-05/web/assignments/assignment3/a3... ·...

6
ASSIGNMENT 3 COMP-202C, Summer 2015 Due: Sunday June 14th, 2015 (23:30) Please read the entire pdf before starting. You must do this assignment individually and, unless otherwise specified, you must follow all the general instructions and regulations for assignments. Graders have the discretion to deduct up to 10% of the value of this assignment for deviations from the general instructions and regulations. These regulations are posted on the course website. Be sure to read them before starting. Part 1: 0 points Part 2, Question 1: 15 points Part 2, Question 2: 15 points Part 2, Question 3: 15 points Part 2, Question 4: 25 points Part 2, Question 5: 30 points Part 3: 0 points 100 points total Before starting on the assignment, you should download from the course website the following files. Make sure to save them in the same working folder. imgSupport.java House1.jpg mickey.jpg This imgSupport.java file will both be used for loading, displaying, and saving the image files. This will be needed throughout the assignment. The .jpg files are used to test your implementations. It is very important that you follow the directions as closely as possible. The directions, while perhaps tedious, are designed to make it as easy as possible for the TAs to mark the assignments by letting them run your assignment through automated tests. While these tests will not determine your entire grade, it will speed up the process significantly, which will allow the TAs to provide better feedback and not waste time on administrative details. Plus, if the TA is in a good mood while he or she is grading, then that increases the chance of them giving out partial marks :) Part 1 (0 points): Warm-up Do NOT submit this part, as it will not be graded. However, doing these exercises might help you to do the second part of the assignment, which will be graded. If you have difficulties with the questions of Part 1, then we suggest that you consult the TAs during their office hours; they can help you and work with you through the warm-up questions. 1

Transcript of ASSIGNMENT 3 - McGill School Of Computer Sciencecs202/2015-05/web/assignments/assignment3/a3... ·...

ASSIGNMENT 3

COMP-202C, Summer 2015

Due: Sunday June 14th, 2015 (23:30)

Please read the entire pdf before starting.

You must do this assignment individually and, unless otherwise specified, you must follow all the generalinstructions and regulations for assignments. Graders have the discretion to deduct up to 10% of the valueof this assignment for deviations from the general instructions and regulations. These regulations are postedon the course website. Be sure to read them before starting.

Part 1: 0 pointsPart 2, Question 1: 15 pointsPart 2, Question 2: 15 pointsPart 2, Question 3: 15 pointsPart 2, Question 4: 25 pointsPart 2, Question 5: 30 pointsPart 3: 0 points

100 points total

Before starting on the assignment, you should download from the course website the following files. Makesure to save them in the same working folder.

• imgSupport.java

• House1.jpg

• mickey.jpg

This imgSupport.java file will both be used for loading, displaying, and saving the image files. This will beneeded throughout the assignment. The .jpg files are used to test your implementations.

It is very important that you follow the directions as closely as possible. The directions, whileperhaps tedious, are designed to make it as easy as possible for the TAs to mark the assignments by lettingthem run your assignment through automated tests. While these tests will not determine your entire grade,it will speed up the process significantly, which will allow the TAs to provide better feedback and not wastetime on administrative details. Plus, if the TA is in a good mood while he or she is grading, then thatincreases the chance of them giving out partial marks :)

Part 1 (0 points): Warm-up

Do NOT submit this part, as it will not be graded. However, doing these exercises might help you to do thesecond part of the assignment, which will be graded. If you have difficulties with the questions of Part 1, thenwe suggest that you consult the TAs during their office hours; they can help you and work with you throughthe warm-up questions.

1

Warm-up Question 1 (0 points)Write a method scalarMultiply which takes as input a double[], and a double scale, and returns void.The method should modify the input array by multiplying each value in the array by scale. Question toconsider: Would this approach work if we had a double as input instead of a double[ ]?

Warm-up Question 2 (0 points)Write a method deleteElement which takes as input an int[ ] and an int target and deletes all occurrencesof target from the array. The method should return the new int[ ]. Question to consider: Why is it thatwe have to return an array and can’t simply change the input parameter array?

Warm-up Question 3 (0 points)Write the same method, except this time it should take as input a String[ ] and a String target.

Part 2

The questions in this part of the assignment will be graded.

Question 1: Simple Image Manipulation (15 points)Create a new class named ImageProcessing that would test several image processing methods using thesupporting code in imgSupport.java. Implement all the methods in this assignment under this same classImageProcessing and save it as ImageProcessing.java. In the main method of the ImageProcessing class,load an image into a two-dimensional array of integers using the helper method ”ImgSupport.load” (Ex:int [][] a = imgSupport.load(”mickey.jpg”);). Then you can display the gray scale version of you image bycalling the method ”ImgSupport.display” (Ex: imgSupport.display(a,”Greyscale”);). You can save yourgrayscale image using the helper function ”ImgSupport.save” (Ex: imgSupport.save(a,”mickey grey.jpg”);).For load, display and save, see the headers in imgSupport.java. You can test your program using thefollowing provided images:

(a) House1.jpg (b) mickey.jpg

Figure 1: Original Images

The grayscale images displayed should look like:

Create a method that will return a new 2D array which will have doubled the values of each element.Display the results by calling the ”display” method. Save the resulting array in a new image called”filename double.png” using the ”save” method. That is newA[i][j] should become 2*a[i][j].If the resultis more that 255 set it to 255. The results should look like:

Question 2: Thresholding (15 points)Add to the ImageProcessing class a method that takes a two dimensional array and a threshold value”t” and returns a new two dimensional array that has the following property:

Page 2

(a) House1 gray.jpg (b) mickey gray.jpg

Figure 2: Grayscale Images

(a) House1 doubled.jpg (b) mickey doubled.jpg

Figure 3: Doubled Images

newA[i][j] =

{255 if a[i][j] > t

0 if a[i][j] <= t

Your method should have the header:

public static int [ ][ ] threshold(int [ ][ ] a,int t)

Use the display method to see the results. Save the results using the save method in a file namedfilename threshold t.jpg, where t is the threshold value used. The results should look like:

(a) House1 threshold 100.jpg (b) mickey threshold 100.jpg

Figure 4: Threshold at 100 ImagesPage 3

Question 3: 2D Array Piecewise Multiplication (15 points)Given two matrices (two dimensional (2D) arrays) with the same dimensions create a method that wouldperform piecewise multiplication. Please note that this is not the standard matrix multiplication, insteadthe result is a new matrix in which each element is the product of the respective elements of the othertwo matrices. Example : [

a b cd e f

]∗[i j kl m n

]=

[a ∗ i b ∗ j c ∗ kd ∗ l e ∗m f ∗ n

][1 2 43 0 1

]∗[4 6 05 7 2

]=

[4 12 015 0 2

]Your method should have the header:

public static double [ ][ ] multiply(int [ ][ ] a, double [ ][ ]b)

Return null if the dimensions do not agree. You can safely assume that the arrays are rectangular.

ATTENTION: For a later use, the multiply method should take as argument a double [ ][ ] array andan int [ ][ ] array, and return a double [ ][ ] instead of an int[ ][ ].

Question 4: Sub-matrices (25 points)Create a method that would return a 2D sub-array of a 2D array of integers. Given a location (i,j) andan odd number dx, return the sub-array centered at (i,j) extending from i-dx/2 to i+dx/2 and fromj-dx/2 to j+dx/2. Note: The previous division (dx/2) is integer division returning an integer; for dx=5,dx/2=2.

Your method should have the header: public static int [ ][ ] snip(int [ ][ ] a,int i, int j, int dx) If i, j,i-(dx/2), j-(dx/2), i+(dx/2), and j+(dx/2) are out of bounds, return null.

For Example: Given a matrix a =

1 2 3 4 5 7 01 5 4 6 8 2 94 5 3 1 9 7 02 3 5 6 1 7 92 4 5 8 1 0 3

And i = 2, j = 1 and dx = 3, the sub-matrix

would be,

1 5 44 5 32 3 5

And for i = 2, j = 4 and dx = 5, the sub-matrix would be,

3 4 5 7 04 6 8 2 93 1 9 7 05 6 1 7 95 8 1 0 3

.

Question 5: Convolution (30 points)The operation of convolution takes as input a two dimensional array (an image) and replaces every pixelwith a weighted sum of its neighbors. The coefficients (weights) for the convolution are stored in anothertwo dimensional array called kernel. The resulting image is stored in a new 2D array. For a kernel ofdimension dx by dx the convolution operation is:

newA[i][j] =

(dx/2)∑x=(−dx/2)

(dx/2)∑y=(−dx/2)

(kernel[x + (dx/2)][y + (dx/2)] ∗ a[i + x][j + y])

The math above is given for your understanding of the convolution technique. The steps to follow toachieve convolution:

1. Create a general method called convolution that takes as inputs a 2D int array representing animage and a 2D doubles array representing the kernel and returns a new 2D int array. Your methodshould have the following header:

public static int [][] convolution(int [][] a, double [][] kernel)

Page 4

2. You are given a kernel matrix, which will be a square matrix in our case. The kernel matrix givesyou the value for dx.

3. Use the method snip of Question 2 in order to get the dx by dx sub array of the original imagecentered around the element [i,j].

4. Use the method multiply of Question 1 to multiply this sub-array with the kernel.

5. Create a new method named absSum which will sum all the elements of the result from the piecewisemultiplication, let us call this result sum. The method absSum should return the absolute value ofsum. The value returned from the method absSum will be assigned to the new array at position[i,j]. If the result is above 255 set the value to 255. Do this operation for all elements of the newAarray. The method absSum should have the following header:

public static double absSum(double [][] a)

NOTE: In the convolution the neighbors of boundary pixels (pixels that are in the first, last rows orcolumns) need special treatment. In your program start calculating the pixels from dx/2, instead of 0,and go up sizeX-dx/2, (same for y), in order to avoid out of bounds exceptions. The resulting arrayshould have the same dimensions as the input array.

Edge Detection

For Edge detection, you will be using an ”edge” kernel. A square edge kernel looks like,−1 −1 −1−1 8 −1−1 −1 −1

Use this kernel matrix as input to your convolution method and view the results using display methods.Save the result as filename edge.jpg. The results of edge detection should look like,

(a) House1 edge.jpg (b) mickey edge.jpg

Figure 5: Result of Edge detection

Smoothing

For Smoothing, you will be using a kernel called ”mean”. A square mean kernel looks like,1.0/9.0 1.0/9.0 1.0/9.01.0/9.0 1.0/9.0 1.0/9.01.0/9.0 1.0/9.0 1.0/9.0

Use this kernel matrix as input to your convolution method and view the results using display methods.Save the result as filename smooth.jpg. The results of smoothing should look like,

Page 5

(a) House1 smooth.jpg (b) mickey smooth.jpg

Figure 6: Result of Smoothing

What To Submit

You should submit your assignment on MyCourses. In order to do this, you will need to make a zip of thefile. You can do this on windows by following the instructions at this link: http://condor.depaul.edu/

slytinen/instructions/zip.html. On a mac or linux, you can find instructions at http://osxdaily.

com/2012/01/10/how-to-zip-files-in-mac-os-x/

You should submit a zip file called Assignment3.zip with the following files inside of it.

ImageProcessing.java

Confession.txt (optional) In this file, you can tell the TA about any issues you ran into doingthis assignment. If you point out an error that you know occurs in your problem, it may leadthe TA to give you more partial credit. On the other hand, it also may lead the TA to noticesomething that otherwise he or she would not.

Part 3 (0 points): Enhancements

The questions in this part of the assignment will be NOT be marked. This is just an enhancement which onecan do for fun.

1. Make changes to your code such that you can give a non-square kernel matrices for convolution. Thismeans, now you will have dx and dy separately. In the above assignment, dy = dx. This might needyou to change the snip( ) method and convolution( ) method.

2. Edge Enhancement : It is like adding the edges detected by the edges kernel to the original image.−1 −1 −1−1 9 −1−1 −1 −1

3. Sobel Edge Detector : The Sobel edge operators have a smoothing effect, so they are less affected

to noise. In this case there are two convolutions, a horizontal component and a vertical component.

hKernel =

−1 −2 −10 0 01 2 1

vKernel =

−1 0 1−2 0 2−1 0 1

The results of the two convolutions are added together and if any value is above 255 then it is set to255. Page 6