isefire

Sobel

Jul 27th, 2014
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.10 KB | None | 0 0
  1. /* Sobel
  2.  * <Description>
  3.  * Displays result of the Sobel method for image processing run on an image.
  4.  * <Usage>
  5.  * javac Sobel.java Picture.java Luminance.java
  6.  * java Sobel picture.jpg
  7.  * <References>
  8.  * http://introcs.cs.princeton.edu/java/31datatype/Luminance.java.html
  9.  * http://introcs.cs.princeton.edu/java/stdlib/Picture.java.html
  10.  * CIS201L-2014-Summer Dr. Sloan
  11.  * @author
  12.  * Dan Latham
  13.  * @version 0.0.1
  14.  *
  15.  */
  16. import java.awt.Color;
  17. public class Sobel
  18. {
  19.     public static int multiply(int[] filter, Color[] bynine)
  20.     {
  21.         int a = 0;
  22.         for (int i = 0; i < filter.length; i+=1)
  23.         {
  24.             Color z = bynine[i];
  25.             int y = z.getRed();
  26.             y = y*filter[i];
  27.             a += y;
  28.         }
  29.         return a;
  30.     }
  31.     public static Picture sobel(Picture initpicture, Picture resultpicture)
  32.     {
  33.         int[] filter1 = {-1, 0, 1,
  34.                         -2, 0, 2,
  35.                         -1, 0, 1};
  36.         int[] filter2 = {1, 2, 1,
  37.                         0, 0, 0,
  38.                         -1, -2, -1};
  39.         int y0 = initpicture.height();
  40.         int x0 = initpicture.width();
  41.         //get and set color of each pixel
  42.         for (int x = 0; x < x0; x+=1)
  43.         {
  44.             for (int y = 0; y < y0; y+=1)
  45.             {
  46.                 //start at top left, iterate down to bottom right
  47.                 //declare boundary pixels first(default case)
  48.                 Color initcolor1 = new Color(255,255,255);
  49.                 Color initcolor2 = new Color(255,255,255);
  50.                 Color initcolor3 = new Color(255,255,255);
  51.                 Color initcolor4 = new Color(255,255,255);
  52.                 Color initcolor5 = initpicture.get(x,y);
  53.                 Color initcolor6 = new Color(255,255,255);
  54.                 Color initcolor7 = new Color(255,255,255);
  55.                 Color initcolor8 = new Color(255,255,255);
  56.                 Color initcolor9 = new Color(255,255,255);
  57.                 //System.out.println(initcolor5);
  58.                 //top left
  59.                 try
  60.                 {
  61.                     initcolor1 = initpicture.get(x-1,y-1);
  62.                 }
  63.                 catch(IndexOutOfBoundsException A)
  64.                 {
  65.                     continue;
  66.                 }
  67.                 //top middle
  68.                 try
  69.                 {
  70.                     initcolor2 = initpicture.get(x,y-1);
  71.                 }
  72.                 catch(IndexOutOfBoundsException A)
  73.                 {
  74.                     continue;
  75.                 }
  76.                 //top right
  77.                 try
  78.                 {
  79.                     initcolor3 = initpicture.get(x+1,y-1);
  80.                 }
  81.                 catch(IndexOutOfBoundsException A)
  82.                 {
  83.                     continue;
  84.                 }
  85.                 //middle left
  86.                 try
  87.                 {
  88.                     initcolor4 = initpicture.get(x-1,y);
  89.                 }
  90.                 catch(IndexOutOfBoundsException A)
  91.                 {
  92.                     continue;
  93.                 }
  94.                 //middle right
  95.                 try
  96.                 {
  97.                     initcolor6 = initpicture.get(x+1,y);
  98.                 }
  99.                 catch(IndexOutOfBoundsException A)
  100.                 {
  101.                     continue;
  102.                 }
  103.                 //bottom left
  104.                 try
  105.                 {
  106.                     initcolor7 = initpicture.get(x-1,y+1);
  107.                 }
  108.                 catch(IndexOutOfBoundsException A)
  109.                 {
  110.                     continue;
  111.                 }
  112.                 //bottom middle
  113.                 try
  114.                 {
  115.                     initcolor8 = initpicture.get(x,y+1);
  116.                 }
  117.                 catch(IndexOutOfBoundsException A)
  118.                 {
  119.                     continue;
  120.                 }
  121.                 //bottom right
  122.                 try
  123.                 {
  124.                     initcolor9 = initpicture.get(x+1,y+1);
  125.                 }
  126.                 catch(IndexOutOfBoundsException A)
  127.                 {
  128.                     continue;
  129.                 }
  130.                 Color[] bynine = {initcolor1, initcolor2, initcolor3, initcolor4, initcolor5, initcolor6, initcolor7, initcolor8, initcolor9};
  131.                 int Gx = multiply(filter1, bynine);
  132.                 int Gy = multiply(filter2, bynine);
  133.                 int colornum =(int) Math.abs(255-Math.sqrt((Gx*Gx)+(Gy*Gy)));
  134.                 if (colornum > 255) colornum=255;
  135.                 //System.out.println(colornum);
  136.                 Color colorgrey = new Color(colornum,colornum,colornum);
  137.                 resultpicture.set(x,y,colorgrey);
  138.                
  139.             }
  140.         }
  141.         return resultpicture;
  142.     }
  143.    
  144.     public static Picture ggrayscale(Picture picture)
  145.     {
  146.         int y0 = picture.height();
  147.         int x0 = picture.width();
  148.         //get color of each pixel, convert to grayscale, set color to gray
  149.         for (int x = 0; x < x0; x+=1)
  150.         {
  151.             for (int y = 0; y < y0; y+=1)
  152.             {
  153.                 java.awt.Color initcolor = picture.get(x,y);
  154.                 java.awt.Color graycolor = Luminance.toGray(initcolor);
  155.                 int r = graycolor.getRed();
  156.                 int b = graycolor.getBlue();
  157.                 int g = graycolor.getGreen();
  158.                
  159.                 java.awt.Color newcolor = new java.awt.Color(r,g,b);
  160.                 picture.set(x, y, newcolor);
  161.             }
  162.         }
  163.         return picture;
  164.     }
  165.    
  166.     public static void main(String[] args)
  167.     {
  168.         Picture initpicture = new Picture(args[0]);
  169.         Picture resultpicture = new Picture(initpicture.width(),initpicture.height());
  170.         initpicture = ggrayscale(initpicture);
  171.         resultpicture = sobel(initpicture, resultpicture);
  172.         resultpicture.show();
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment