Specifications

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
Part
Part
Part
Part
Part
Part
1:
2,
2,
2,
2,
2,
3:
Question
Question
Question
Question
Question
1:
2:
3:
4:
5:
0 points
15 points
15 points
15 points
25 points
30 points
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
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 to
consider: 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 occurrences
of target from the array. The method should return the new int[ ]. Question to consider: Why is it that
we 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 the
supporting code in imgSupport.java. Implement all the methods in this assignment under this same class
ImageProcessing 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 by
calling the method ”ImgSupport.display” (Ex: imgSupport.display(a,”Greyscale”);). You can save your
grayscale 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 the
following 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 result
is 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
(
255
newA[i][j] =
0
if a[i][j] > t
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 named
f ilename 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
Page 3
Figure 4: Threshold at 100 Images
Question 3: 2D Array Piecewise Multiplication (15 points)
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. Example :
a b c
i j k
a∗i b∗j c∗k
∗
=
d e f
l m n
d∗l e∗m f ∗n
1
3
2
0
4
4
∗
1
5
6
7
0
4
=
2
15
12 0
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 and
an 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) and
an odd number dx, return the sub-array centered at (i,j) extending from i-dx/2 to i+dx/2 and from
j-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.


1 2 3 4 5 7 0
1 5 4 6 8 2 9



For Example: Given a matrix a = 
4 5 3 1 9 7 0 And i = 2, j = 1 and dx = 3, the sub-matrix
2 3 5 6 1 7 9
2 4 5 8 1 0 3


3 4 5 7 0


4 6 8 2 9
1 5 4





would be, 4 5 3 And for i = 2, j = 4 and dx = 5, the sub-matrix would be, 
3 1 9 7 0 .

5 6 1 7 9
2 3 5
5 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 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 dx the convolution operation is:
newA[i][j] =
(dx/2)
(dx/2)
X
X
(kernel[x + (dx/2)][y + (dx/2)] ∗ a[i + x][j + y])
x=(−dx/2) y=(−dx/2)
The math above is given for your understanding of the convolution technique. The steps to follow to
achieve convolution:
1. Create a general method called convolution that takes as inputs a 2D int array representing an
image and a 2D doubles array representing the kernel and returns a new 2D int array. Your method
should 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 gives
you 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 image
centered 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 piecewise
multiplication, let us call this result sum. The method absSum should return the absolute value of
sum. 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 newA
array. 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 or
columns) 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 array
should 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 f ilename 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.0
1.0/9.0 1.0/9.0 1.0/9.0
1.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 f ilename 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 the
file. 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 doing
this assignment. If you point out an error that you know occurs in your problem, it may lead
the TA to give you more partial credit. On the other hand, it also may lead the TA to notice
something 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 one
can do for fun.
1. Make changes to your code such that you can give a non-square kernel matrices for convolution. This
means, now you will have dx and dy separately. In the above assignment, dy = dx. This might need
you 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.


−1 −2 −1
0
0
hKernel =  0
1
2
1


−1 0 1
vKernel = −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 to
255.
Page 6