Class Notes (809,444)
Canada (493,713)
COMP 202 (186)


9 Pages
Unlock Document

McGill University
Computer Science (Sci)
COMP 202
Daniel Pomerantz

ASSIGNMENT 3 COMP-202A, Fall 2012, All Sections Due: November 19th, 2012 (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. Question 1: 2D Array piecewise multiplication (15%) Given two matrices (two dimensional (2D) arrays) with the same dimensions create a method that would perform piecewise multiplication. Please note that this is not the standard matrix multiplication, instead the result is a new matrix in which each element is the product of the respective elements of the other two matrices. The result for multiplying matrices A and B is C. ⏞ ⏞ ⏞ [ ] [ ] [ ] Your method should have the header: public static double [][] multiply(int [][] a, double [][]b) Return null if the dimensions do not agree. In order to check for the first and second dimension use: a.length and a[0].length You can safely assume that the arrays are rectangular. ATTENTION: Because the kernel (later on) is defined as double [][] array, the multiply method should take as argument a double [][] array and also return a double [][] instead of an int[][]. Question 2: Sub-matrices (25%) Create a method that would return a 2D sub-array of a 2D array of integers. Given a location (i,j) and two odd numbers dx and dy, return the sub-array centered at (i,j) extending from i-dx/2 to i+dx/2 and from j-dy/2 to j+dy/2. Note: the previous divisions are integer divisions returning an integer; for dx=dy=5, dx/2=dy/2=2. Your method should have the header: public static int [][] snip(int [][] a,int i, int j, int dx, int dy) If i, j, i-dx, j-dy, i+dx, j+dy are out of bounds return null. For dx=3, dy=3 the indices should be: [ ] [ ] Question 3: Simple Image Manipulation (15%) Using the supporting code in create a new class named ImageProcessing that would test several image processing methods. In the main method of the ImageProcessing class load an image into a two-dimensional array of integers using the helper method “ImgSupport.load”. Then you can display the gray scale version of you image by calling the method “ImgSupport.display”. You can save your grayscale image using the helper function “”. For load, display and save, see the headers in You can test your program using the following provided images: The original images: House.jpg Mandrill.jpg The grayscale images displayed should look like: Grayscale: House.jpg Grayscale: Mandrill.jpg 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 “double.png” using the “save” method. That is newA[i][j] should become 2*a[i][j].If the result is more that 255 set it to 255. The results should look like: Double the values: House.jpg Mandrill.jpg Question 4: Thresholding (15%) 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: [ ][ ] [ ][ ] { [ ][ ] 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 named threshold_t.png where t is the threshold value used. The results should look like: Thresholding at 120: House.jpg Thresholding at 120: Mandrill.jpg Question 5: Convolution (30%) The operation of convolution takes as input a two dimensional array (an image) and replaces every pixel with a weighted sum of its neighbors. The coefficients (weights) for the convolution are stored in another two dimensional array called kernel. The resulting image is stored in a new 2D array. For a kernel of dimension dx by dy the convolution operation is: [ ][ ] | ∑ ∑ ( [ ][ ] [ ][ )| Use the method snip of Question 2 in order to get the dx by dy sub array of the original image centered around the element [i,j]. Use the method multiply of Question 1 to multiply this sub-array with the kernel. Create a new method named absSum which will sum all the elements of the result from the piecewise mult
More Less

Related notes for COMP 202

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.