A common way that digital images are processed is to selectively blur portions of the image. Blurring can be used to reduce noise, eliminate distracting backgrounds, and smooth a model's skin in images. In this assignment you will create a program that can blur images.
Your program should display a window with an image before and after it is blurred. In order to blur an image, a new value for every pixel is calculated by computing the average of all nearby pixels. The larger the area of pixels that is averaged the more the image is blurred.
In order to accomplish this, you should use the BWImage.java class that represents black-and-white images. The class has two constructors, one that takes a string for an image file name, and one that creates an empty image of specified dimensions. It also has methods for querying the image dimensions, getting and setting pixel values, and drawing an image to a Graphics object. The signatures for these methods are as follows:
BWImage (String filename) - Constructor: creates a black and white image from the specified file BWImage (int width, int height) - Constructor: creates an all white image with the specified dimensions int getPixel (int row, int col) - Returns the intensity of the pixel at position (row, col) in the image void setPixel (int row, int col, int intensity) - Sets the intensity of the pixel at position (row, col) of the image. If row or col is out of bounds of the image, nothing is done. If the intensity is outside the range 0 - 255 it sets the intensity to 0 or 255. int getWidth() - Returns the width of the image (number of pixels) int getHeight() - Returns the height of the image (number of pixels) void draw (int x, int y, Graphics graphics) - Draws the image at the coordinates (x, y) in the Graphics object (The point (x, y) is the upper left corner of the image)
Save BWImage.java to your assignment directory. You can take a look at the code in the java file but you don't need to understand it to use it. All you need to understand is how to use the methods to do what you want to do.
In your program's paintComponent method you should create two BWImage objects, one that contains an image to blur (such as this image) and one that will contain the blurred image. The program should iterate over every pixel in the image to blur (this will be every pixel except for a 3-pixel deep border around the edge), calculate the "blurred" value, then put the blurred value in your image object for the blurred image. Calculate the value for a blurred pixel by computing the average of all pixels in a 7 by 7 window centered around the same pixel in the original image. For example, in order to calculate the blurred value of the pixel (50, 75), sum the value of all pixels in the original image with an x-coordinate between 47 and 53 and a y-coordinate between 72 and 78. Then, divide the sum by the number of pixels in the 7 by 7 square (that would be 49). Be careful around the edges of the image. In order to prevent attempting to access pixels that do not exist, only calculate blurred values for pixels that are more than 3 pixels from the edge of the image. Your loop conditions should make sure you process only pixels that are at least 3 from the edge. No credit if you use continue statements to achieve this.
Your program should draw both the original and blurred images in the Graphics object along with labels (such as "Original Image" and "Blurred Image"). It should also draw your name somewhere on the page.
As usual, use good programming techniques. You should use good variable names, constants where appropriate, whitespace, and correct indentation and alignment of statements. Be sure to put your name, the file name, and a description of the program in the header. The program should be broken up into logical sections and each section should be documented with a brief description of what it does.
By using a weighted average it is possible to produce a blur that is more similar to real camera blur. A frequently used weight function is a Gaussian function (named after the famous mathematician Carl Friedrich Gauss). A Gaussian function when graphed produces the familiar bell curve. In order to compute a weighted average, each value being summed is multiplied by a weight (a value between 0 and 1). (The above blur method uses the same weight for each pixel value (1/49)). In order to compute the weight of each pixel use the Gaussian function:
where G(x, y) is the weight of the pixel that is located x pixels horizontally and y pixels vertically from the pixel that is being blurred. Your program should display all three images appropriately labeled.
Academic Integrity Reminder!!! Programming assignments are to be your own work. You may get help on the specifics of the assignment from no one except the instructor. You may not show your program to anyone or look at anyone else's program or share ideas with anyone about how to write the program or solve the problem.
Submission: cp a tgz file (with your name as the file name) that contains the java file for your program to the directory: