CS 1110/1111 - Spring 2015 - Homework Assignments

Important Links: Submit POTDs to the Automated Grading System and Join the TA Office Hour Queue

Team Projects

Program of the Day

POTD 20 - BlurImage.java - Due: Friday, April 24, 11:00 AM

We would like to finish a library that will take a Picture and display a blurred version of that image, based upon a particular radius. We have written part of this class for you. You need to add two methods to this class to make it work properly.

To get started, download this project and import it into Eclipse: ImageManipulation.zip

If the project doesn’t work for you, here’s a jar file that you can add to your build path (ask a TA for assistance) along with some pictures to work with:

This project has the Picture class preloaded in it, along with some very basic sample code with three images you can play with.

The Algorithm

One way to blur an image is to take each Pixel from the original picture and then replace that picture with a Pixel of the average color of all the Pixels around it in a certain radius. For example, if you want to find the blurred value of Pixel at position (10,12) with radius 4, you would find the average of the red, blue, and green values of all of the Pixels found in the area between (6,8) and (14,16).

getSubImage

Write a method with the following header:

public static Picture getSubImage(Picture s, int sx, int sy, int w, int h)

where s is the source Picture, sx and sy is the upper-left corner of the sub image, and w and h are the width and height of the sub image.

Your method needs to handle boundary checking. If the desired sub image would try to pull an image that falls outside the boundaries of the source image, you should shift the sub image back onto the image, keeping the width and height of the sub image the same.

For example, assume the source image is 400x300.

  • If the sub image’s upper-left corner is (400,300) and the sub image should be 100x100, you should adjust sx and sy to be (300,200)
  • If the sub image’s upper-left corner has negative values, those values should be set to 0.
getAverageColor

Write a method with the following header:

public static Color getAverageColor(Picture p)

where p is the Picture that should have its Pixel values averaged.

This method should add up the red, green, and blue values of each Pixel and divide by the total number of Pixels in the source image. Create a new instance of the Color class with these averaged values.

blur

Here is the blur method you should use. Note that it uses the two methods above in its implementation:

blur
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static Picture blur(Picture pic, int interval) {
  int oldWidth = pic.getWidth();
  int oldHeight = pic.getHeight();
  
  Picture newImg = new Picture((int)(oldWidth), (int)(oldHeight));
  
  for (int y = 0; y < newImg.getHeight(); y += 1) {
      for (int x = 0; x < newImg.getWidth(); x += 1) {
          newImg.getPixel(x,y).setColor(getAverageColor(getSubImage(pic, x-interval, y-interval, 2*interval, 2*interval)));
      }
  }
  
  return newImg;
}
Example
Example Usage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Picture pict1 = new Picture("picture1.jpg");
Picture blue = new Picture("blue.png");
Picture blueorange = new Picture("blueandorange.png");

Picture subImage = getSubImage(pict1, 100, 100, 50, 50);
subImage.show();
System.out.println(getAverageColor(subImage)); // Should output java.awt.Color[r=155,g=177,b=208]
System.out.println(getAverageColor(blue)); // Should output java.awt.Color[r=0,g=93,b=179]
System.out.println(getAverageColor(blueorange)); // java.awt.Color[r=118,g=66,b=70]
      
Picture blurred = blur(pict1, 4);
blurred.setTitle("Interval: 4");
blurred.show();
      
Picture blurred2 = blur(pict1, 10);
blurred2.setTitle("Interval: 10");
blurred2.show();

The blurred image with radius 4 should look like this:

blur4.jpg

blur4.jpg

The blurred image with radius 10 should look like this:

blur10.jpg

blur10.jpg

If you would like to do a pixel by pixel comparison with your resulting image with ours, please use these reference images. You can write code to load in these images and compare with your own results!

Submission: Please submit BlurImage.java to the grading system.

Upcoming POTDs

Previous POTDs