1. Homepage
  2. Programming
  3. CS 0007 INTRODUCTION TO COMPUTER PROGRAMMING Project 2: Image Lab

CS 0007 INTRODUCTION TO COMPUTER PROGRAMMING Project 2: Image Lab

Engage in a Conversation
USPittsburgh UniversityCS 0007INTRODUCTION TO COMPUTER PROGRAMMINGJava

CS 0007 Project 2

Overview

In this assignment, you’ll use your coding skills that you’ve developed over the semester to write a variety of image effects. You may use any code from lecture, recitation, or the textbook as a reference, but this is an individual assignment. You may not share code with or copy code from any other person (whether they are in the class or not). CourseNana.COM

Requirements

You will modify the starter code, ImageLab.java, to implement several different image effects. You may also create as many helper methods as you’d like (these will not be graded, but may help you write cleaner code). You will build your code as normal, using javac ImageLab.java. When you run it, you’ll provide an input image filename. For example, to run it on a file called ”cathy.jpg”, use java ImageLab cathy.jpg. The code will output a series of images (each corresponding to one effect). CourseNana.COM

Background: Java Image Manipulation

1 BufferedImage

Java provides the BufferedImage class to manipulate images. I have provided two methods, readImage() and writeImage(), which simplify the usage of BufferedImages for the purposes of this project. Aside from those, you’ll be using BufferedImages like any other Java programmer would. CourseNana.COM

1.1 Opening and Creating Images

To open an image file as a BufferedImage, you can simply say BufferedImage my image = readImage("image filename.jpg"); The starter code handles loading and saving images, so you shouldn’t have to use this method directly. To create a blank BufferedImage, BufferedImage empty image = new BufferedImage(width, height, BufferedImage.TYPE INT RGB); In this example, width and height are the width and height of the new image (as ints). The third argument, BufferedImage.TYPE INT RGB, tells Java that we want to specify colors using their red, green, and blue values. You will not need to change this for any of the effects. CourseNana.COM

1.2 Saving Images

I have provided the writeImage() method. If img is an image, you can save it as a jpg file by using writeImage(img, "filename.jpg"). The starter code handles loading and saving images, so you shouldn’t have to use this method directly. CourseNana.COM

2 Pixels and Colors

As demonstrated in class, you can think of an image as a 2D array (grid) of pixels. Each pixel is a single color, composed of its component red, green, and blue values. In class we represented those as a third dimension on the array. Java instead represents each pixel as a Color object (imported from the java.awt.Color package). You don’t need to know much about Colors aside from these basics: CourseNana.COM

• If img is a BufferedImage object, you can get the value of a pixel at a position (x, y) by saying Color my color = new Color(img.getRGB(x, y)); – Be sure to check that (x, y) is a valid pixel! If x is not between 0 and the image width (img.getWidth()), or if y is not between 0 and the image height (img.getHeight()), you’ll either get an exception or weird results. • Once you have a Color object, you can extract the red, green, and blue components as integers using – int red = my color.getRed(); – int green = my color.getGreen(); – int blue = my color.getBlue(); • To create a new Color object from a given red, green, and blue value, use Color my new color = new Color(red, green, blue);. – Important: Your values for red, green, and blue must be integers in the range 0 255. If you provide a value that is negative, or that’s greater than 255, Java will cause an IllegalArgumentException. Be sure to check your values before creating a new Color. • If my color is a Color object and img is a BufferedImage object, you can set the color at position (x, y) using img.setRGB(x, y, my color.getRGB()); CourseNana.COM

[0 pts] Converting an Image to Grayscale I have provided an example of a very basic image effect, conversion to grayscale. This demonstrates basic image operations. CourseNana.COM

public static BufferedImage imageToGrayscale ( BufferedImage input ) {
// store the image width and height in variables for later use
int img_width = input . getWidth () ;
int img_height = input . getHeight () ;
// create a new BufferedImage with the same width and height as the input
// image . TYPE_INT_RGB means we want to specify pixel values using their
// red , green , and blue components .
BufferedImage output_img = new BufferedImage (
img_width , img_height , BufferedImage . TYPE_INT_RGB ) ;
// Loop over all pixels
for ( int x = 0; x < img_width ; x ++) {
for ( int y = 0; y < img_height ; y ++) {
// Get the color of the input image as a java . awt . Color object
Color color_at_pos = new Color ( input . getRGB (x , y ) ) ;
// Call the getRed () , getGreen () , and getBlue () methods on the color
// object to store the red , green , and blue values into separate integer
// variables .
int red = color_at_pos . getRed () ;
int green = color_at_pos . getGreen () ;
int blue = color_at_pos . getBlue () ;
// to get the grayscale version , we just average the red , green , and
// blue channels , then use that average as the red , green , and blue
// values in the output image .
int average = ( red + green + blue ) / 3;

// You ’ ll get an I l l e g a l A r g u m e n t E x c e p t i o n if you try to specify red ,
// green , or blue values greater than 255 or less than 0. While it ’s
// mathematically impossible to average three values in that range and
// get a value outside the range , the calculations in the other tasks
// will sometimes produce values outside the range . This code " clamps "
// the value to the range 0 - 255
if( average < 0) {
average = 0;
} else if ( average > 255) {
average = 255;
}
// make a new Color object , which has the average intensity (( r + g + b ) /3)
// for each of its channels .
Color average_color = new Color ( average , average , average ) ;
// in the output image , set the color at position (x , y ) to our
// calculated average color
output_img . setRGB (x , y , average_color . getRGB () ) ;
}
}
// return the output image .
return output_img ;
}

[15pts] Effect 1: Thresholding CourseNana.COM

(a) Original image. CourseNana.COM

(b) Thresholded (level 128). CourseNana.COM

Figure 1: An image, before and after thresholding CourseNana.COM

Thresholding is perhaps the most basic image operation: for each pixel, consider its intensity (for our purposes, intensity will be the average of the red, green, and blue channels). If the intensity is greater than a given threshold level, set the corresponding pixel in the output image to white. If the intensity is less than the given level, set the corresponding pixel in the output image to black. Your code will probably be something like CourseNana.COM

  1. Create a new BufferedImage that’s the same size as the input image. This will be the output image.
  2. Loop over each pixel at (x, y) (a) Extract the red, green, and blue components of the pixel at (x, y) (b) Average the red, green, and blue components ((r+g+b)/3) (c) If the computed average is greater than the level, set the output image to white at location (x, y). White is the color with 255 as its red, green, and blue values. (d) If the computed average is not greater than the level, set the output image to black at location (x, y). Black is the color with 0 as its red, green, and blue values.
  3. Return the output image

[15pts] Effect 2: Color Rotation CourseNana.COM

(a) Original image CourseNana.COM

(b) Color Rotated CourseNana.COM

Figure 2: An image, before and after color rotation CourseNana.COM

Color rotation is a simple effect that can produce some cool results. It works by breaking the image down into its red, green, and blue channels, then by creating a new image with the values swapped around. For each pixel in the output, the red value will be the blue value from the input, the green value will be the red value from the input, and the blue value will be the green value from the input. CourseNana.COM

Your code will probably be something like CourseNana.COM

  1. Create a new BufferedImage that’s the same size as the input image. This will be the output image.
  2. Loop over each pixel at (x, y) (a) Extract the red, green, and blue components of the pixel at (x, y) (b) Create a new color. Its red value should be the blue value from the input, its green value should be the red value from the input, and its blue value should be the green value from the input. (c) Set the output image at position (x, y) to the created color
  3. Return the output image ..... [25pts] Effect 6: Box Blur

(a) Original image CourseNana.COM

(b) Blurred image CourseNana.COM

Figure 6: An image, before and after blurring CourseNana.COM

Blurring an image simply averages each pixel with its eight neighbors (add them up and divide by CourseNana.COM

9). Do this independently for the red, green, and blue channels. Be Careful!: On high-resolution images, it can be hard to see the results of box blur. Try it on section.jpg or other small images for the clearest results. CourseNana.COM

Your code might look something like this: CourseNana.COM

  1. Create a new BufferedImage that’s the same size as the input image. This will be the output image.
  2. Loop over each pixel at (x, y) (a) Extract the red, green, and blue components of the pixel at (x, y) (b) Consider each of the eight neighboring pixels in the input image. If the neighbor is not outside the bounds of the image, get its red, green, and blue values. Add those to the new red, green, and blue values computed above. If the neighbor is not valid, you may assume its values are (0, 0, 0). (c) Divide the red, green and blue values stored by 9. (d) Create a new color using the computed red, green, and blue values. (e) Ensure the computed red, green, and blue values are between 0 and 255. (f) Set the output image at position (x, y) to the computed color.
  3. Return the output image

[25pts] Effect 7: Simple Edge Detection CourseNana.COM

(a) Original image CourseNana.COM

(b) Simple edge detection CourseNana.COM

Figure 7: An image, before and after edge detection CourseNana.COM

You can perform simple edge detection by using the above convolution matrix on the intensity of a pixel value. Note that this is not like the blur or sharpen operations, which work on the red, green, and blue components individually. CourseNana.COM

Your code might look something like this: CourseNana.COM

  1. Create a new BufferedImage that’s the same size as the input image. This will be the output image.
  2. Loop over each pixel at (x, y) (a) Extract the red, green, and blue components of the pixel at (x, y). Average them together to compute an intensity value, then multiply by 4.

(b) Consider each of the neighboring pixels in the input image ((x-1, y), (x+1, y), (x, y-1), (x, y+1)). If the neighbor is not outside the bounds of the image, average its red, green, and blue values to compute its intensity. Subtract that intensity from the stored intensity. If the neighbor is not valid, you may assume its intensity is zero. (c) Ensure the computed intensity is between 0 and 255. (d) Create a new color using the computed intensity as the red, green, and blue values. (e) Set the output image at position (x, y) to the computed color. CourseNana.COM

  1. Return the output image

[30pts] Effect 8: Sobel Edge Detection CourseNana.COM

(a) Original image CourseNana.COM

(b) Sobel edge detection CourseNana.COM

Figure 8: An image, before and after Sobel edge detection . Sobel edge detection is more complicated than the simple algorithm above, but produces more dramatic and clearer results. Like the edge detection above, it works on the intensity of a pixel value. Note that this is not like the blur or sharpen operations, which work on the red, green, and blue components individually. CourseNana.COM

For each pixel, the sobel algorithm effectively computes two convolutions: then computes the output intensity as Gx 2 + Gy 2 . Your code might look something like this: CourseNana.COM

  1. Create a new BufferedImage that’s the same size as the input image. This will be the output image.
  2. Loop over each pixel at (x, y) (a) Compute Gx and Gy using the matrices described above. If Ix,y is the intensity at pixel (x, y), then Gx = (2 ∗ Ix+1,y + Ix+1,y−1 + Ix+1,y+1 ) − (2 ∗ Ix−1,y + Ix−1,y−1 + Ix−1,y+1 ) Gy = (2 ∗ Ix,y−1 + Ix−1,y−1 + Ix+1,y−1 ) − (2 ∗ Ix,y+1 + Ix−1,y+1 + Ix+1,y+1 ) q (b) Compute the new intensity as Gx 2 + Gy 2 . You can use Math.sqrt() to take the square root of a number. Note that it returns a double, so you’ll have to cast it to an int before using it. You can do this with int result = (int)Math.sqrt(number). (c) Ensure the computed intensity is between 0 and 255. (d) Create a new color using the computed intensity as the red, green, and blue values. (e) Set the output image at position (x, y) to the computed color.
  3. Return the output image

Get in Touch with Our Experts

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
US代写,Pittsburgh University代写,CS 0007代写,INTRODUCTION TO COMPUTER PROGRAMMING代写,Java代写,US代编,Pittsburgh University代编,CS 0007代编,INTRODUCTION TO COMPUTER PROGRAMMING代编,Java代编,US代考,Pittsburgh University代考,CS 0007代考,INTRODUCTION TO COMPUTER PROGRAMMING代考,Java代考,UShelp,Pittsburgh Universityhelp,CS 0007help,INTRODUCTION TO COMPUTER PROGRAMMINGhelp,Javahelp,US作业代写,Pittsburgh University作业代写,CS 0007作业代写,INTRODUCTION TO COMPUTER PROGRAMMING作业代写,Java作业代写,US编程代写,Pittsburgh University编程代写,CS 0007编程代写,INTRODUCTION TO COMPUTER PROGRAMMING编程代写,Java编程代写,USprogramming help,Pittsburgh Universityprogramming help,CS 0007programming help,INTRODUCTION TO COMPUTER PROGRAMMINGprogramming help,Javaprogramming help,USassignment help,Pittsburgh Universityassignment help,CS 0007assignment help,INTRODUCTION TO COMPUTER PROGRAMMINGassignment help,Javaassignment help,USsolution,Pittsburgh Universitysolution,CS 0007solution,INTRODUCTION TO COMPUTER PROGRAMMINGsolution,Javasolution,