Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Sobel
- * <Description>
- * Displays result of the Sobel method for image processing run on an image.
- * <Usage>
- * javac Sobel.java Picture.java Luminance.java
- * java Sobel picture.jpg
- * <References>
- * http://introcs.cs.princeton.edu/java/31datatype/Luminance.java.html
- * http://introcs.cs.princeton.edu/java/stdlib/Picture.java.html
- * CIS201L-2014-Summer Dr. Sloan
- * @author
- * Dan Latham
- * @version 0.0.1
- *
- */
- import java.awt.Color;
- public class Sobel
- {
- public static int multiply(int[] filter, Color[] bynine)
- {
- int a = 0;
- for (int i = 0; i < filter.length; i+=1)
- {
- Color z = bynine[i];
- int y = z.getRed();
- y = y*filter[i];
- a += y;
- }
- return a;
- }
- public static Picture sobel(Picture initpicture, Picture resultpicture)
- {
- int[] filter1 = {-1, 0, 1,
- -2, 0, 2,
- -1, 0, 1};
- int[] filter2 = {1, 2, 1,
- 0, 0, 0,
- -1, -2, -1};
- int y0 = initpicture.height();
- int x0 = initpicture.width();
- //get and set color of each pixel
- for (int x = 0; x < x0; x+=1)
- {
- for (int y = 0; y < y0; y+=1)
- {
- //start at top left, iterate down to bottom right
- //declare boundary pixels first(default case)
- Color initcolor1 = new Color(255,255,255);
- Color initcolor2 = new Color(255,255,255);
- Color initcolor3 = new Color(255,255,255);
- Color initcolor4 = new Color(255,255,255);
- Color initcolor5 = initpicture.get(x,y);
- Color initcolor6 = new Color(255,255,255);
- Color initcolor7 = new Color(255,255,255);
- Color initcolor8 = new Color(255,255,255);
- Color initcolor9 = new Color(255,255,255);
- //System.out.println(initcolor5);
- //top left
- try
- {
- initcolor1 = initpicture.get(x-1,y-1);
- }
- catch(IndexOutOfBoundsException A)
- {
- continue;
- }
- //top middle
- try
- {
- initcolor2 = initpicture.get(x,y-1);
- }
- catch(IndexOutOfBoundsException A)
- {
- continue;
- }
- //top right
- try
- {
- initcolor3 = initpicture.get(x+1,y-1);
- }
- catch(IndexOutOfBoundsException A)
- {
- continue;
- }
- //middle left
- try
- {
- initcolor4 = initpicture.get(x-1,y);
- }
- catch(IndexOutOfBoundsException A)
- {
- continue;
- }
- //middle right
- try
- {
- initcolor6 = initpicture.get(x+1,y);
- }
- catch(IndexOutOfBoundsException A)
- {
- continue;
- }
- //bottom left
- try
- {
- initcolor7 = initpicture.get(x-1,y+1);
- }
- catch(IndexOutOfBoundsException A)
- {
- continue;
- }
- //bottom middle
- try
- {
- initcolor8 = initpicture.get(x,y+1);
- }
- catch(IndexOutOfBoundsException A)
- {
- continue;
- }
- //bottom right
- try
- {
- initcolor9 = initpicture.get(x+1,y+1);
- }
- catch(IndexOutOfBoundsException A)
- {
- continue;
- }
- Color[] bynine = {initcolor1, initcolor2, initcolor3, initcolor4, initcolor5, initcolor6, initcolor7, initcolor8, initcolor9};
- int Gx = multiply(filter1, bynine);
- int Gy = multiply(filter2, bynine);
- int colornum =(int) Math.abs(255-Math.sqrt((Gx*Gx)+(Gy*Gy)));
- if (colornum > 255) colornum=255;
- //System.out.println(colornum);
- Color colorgrey = new Color(colornum,colornum,colornum);
- resultpicture.set(x,y,colorgrey);
- }
- }
- return resultpicture;
- }
- public static Picture ggrayscale(Picture picture)
- {
- int y0 = picture.height();
- int x0 = picture.width();
- //get color of each pixel, convert to grayscale, set color to gray
- for (int x = 0; x < x0; x+=1)
- {
- for (int y = 0; y < y0; y+=1)
- {
- java.awt.Color initcolor = picture.get(x,y);
- java.awt.Color graycolor = Luminance.toGray(initcolor);
- int r = graycolor.getRed();
- int b = graycolor.getBlue();
- int g = graycolor.getGreen();
- java.awt.Color newcolor = new java.awt.Color(r,g,b);
- picture.set(x, y, newcolor);
- }
- }
- return picture;
- }
- public static void main(String[] args)
- {
- Picture initpicture = new Picture(args[0]);
- Picture resultpicture = new Picture(initpicture.width(),initpicture.height());
- initpicture = ggrayscale(initpicture);
- resultpicture = sobel(initpicture, resultpicture);
- resultpicture.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment