Docsity

Simple Edge Detection - Modifying Pixels - Lab | CPSC 1301, Lab Reports of Computer Science

Toggle sidebar
bg1
LAB 18 – Conditionally Modifying Pixels (part 2 – Simple Edge Detection)
Topics
To do simple edge detection.
To replace several colors in a picture: sepia-toned.
To use a conditional with two possible results if and else.
6.1 Conditional Pixel Changes
Type in the following code in the Program window (changing the comments):
import java.awt.Color;
/* Program that transforms pictures using loops and conditionals
*
* Picture and Pixel classes are defined in bookClasses developed
* at Georgia Tech by Mark Guzdial / Barbara Ericson
*
* @author Wayne Summers
* @date Sept. 23, 2007
*/
public class TransformPictures
{
public static void main(String[] args)
{
String fileName1, fileName2;
System.out.println(“Find the source file”);
fileName1 = FileChooser.pickAFile(); // note that this pops up a window to select a jpg file
Picture sourcePicture;
sourcePicture = new Picture(fileName1);
// calls to methods that manipulate images go here
sourcePicture.show();
}
}
6.2 Simple Edge Detection: Conditionals with Two Options
i) Type in the following code in the Program window below the main method and before the last }:
/**
* Method that does simple edge detection by comparing the absolue value of the difference between the color
* intensities between a pixel and the pixel below it. If the absolute value of the difference is less than
* the passed value, the top pixel color will be set to white, otherwise it will set to black
* @param sourcePic - picture Object to be modified
* @param distance – distance allowed between adjacent pixels
*/
public static void edgeDetection (Picture sourcePic, double distance)
{
Pixel topPixel = null;
Pixel bottomPixel = null;
double topAverage = 0.0;
double bottomAverage = 0.0;
int width = sourcePic.getWidth();
pf3

Partial preview of the text

Download Simple Edge Detection - Modifying Pixels - Lab | CPSC 1301 and more Lab Reports Computer Science in PDF only on Docsity!

LAB 18 – Conditionally Modifying Pixels (part 2 – Simple Edge Detection)

Topics  To do simple edge detection.  To replace several colors in a picture: sepia-toned.  To use a conditional with two possible results if and else.

6.1 Conditional Pixel Changes

Type in the following code in the Program window (changing the comments): import java.awt.Color; /* Program that transforms pictures using loops and conditionals

  • Picture and Pixel classes are defined in bookClasses developed
  • at Georgia Tech by Mark Guzdial / Barbara Ericson
  • @author Wayne Summers
  • @date Sept. 23, 2007 / public class TransformPictures { public static void main(String[] args) { String fileName1, fileName2; System.out.println(“Find the source file”); fileName1 = FileChooser.pickAFile(); // note that this pops up a window to select a jpg file Picture sourcePicture; sourcePicture = new Picture(fileName1); // calls to methods that manipulate images go here sourcePicture.show(); } } 6.2 Simple Edge Detection: Conditionals with Two Options i) Type in the following code in the Program window below the main method and before the last }: /*
  • Method that does simple edge detection by comparing the absolue value of the difference between the color
  • intensities between a pixel and the pixel below it. If the absolute value of the difference is less than
  • the passed value, the top pixel color will be set to white, otherwise it will set to black
  • @param sourcePic - picture Object to be modified
  • @param distance – distance allowed between adjacent pixels */ public static void edgeDetection (Picture sourcePic, double distance) { Pixel topPixel = null; Pixel bottomPixel = null; double topAverage = 0.0; double bottomAverage = 0.0; int width = sourcePic.getWidth();

int height = sourcePic.getHeight(); // loop through the y values from 0 to (height-1) since we compare to pixel below for (int y = 0; y < (height-1); y++) { // loop through the x values from 0 to width for (int x = 0; x < width; x++) { // get the top and bottom pixels topPixel = sourcePic.getPixel(x,y); bottomPixel = sourcePic.getPixel(x,y+1); // compute the color averages for the two pixels topAverage = topPixel.getAverage(); bottomAverage = bottomPixel.getAverage(); // check if absolute value of the difference is less than distance if (Math.abs(topAverage - bottomAverage) < distance) topPixel.setColor(Color.WHITE); else topPixel.setColor(Color.BLACK); } //end inner loop } //end outer loop } ii) Type in the following code in the Program window in the main method and before the pic.show(); edgeDetection (sourcePicture, 10); iii) Test your program with different pictures and values for the distance (e.g. butterfly1.jpg, 10). EXTRA (if there is time)

6. 3 Sepia-tinted and Posterized Pictures: Using Multiple Conditionals to Choose a Color iv) Type in the following code in the Program window below the main method and before the last }: /** * Method to convert sourcePic to a sepia tint (modify the middle colors to a light brown * and the light colors to a light yellow and make the shadows darker * @param sourcePic - picture Object to be modified */ public static void sepiaTint(Picture sourcePic) { Pixel pixelObj = null; double redValue = 0; double greenValue = 0; double blueValue = 0; int width = sourcePic.getWidth(); int height = sourcePic.getHeight(); // first change the current picture to grayscale grayscale(sourcePic);