Advertisement
Guest User

a2

a guest
Nov 30th, 2015
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.28 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.image.*;
  4.  
  5. public class ImageAssignment {
  6.  
  7.     /* First, some utility methods that you will need in the methods you write.
  8.        Do not modify these methods in any way. */
  9.  
  10.     public static int getRed(int rgb) { return (rgb >> 16) & 0xff; }
  11.     public static int getGreen(int rgb) { return (rgb >> 8) & 0xff; }
  12.     public static int getBlue(int rgb) { return rgb & 0xff; }
  13.     public static int rgbColour(int r, int g, int b) {
  14.         return (r << 16) | (g << 8) | b;
  15.     }
  16.     public static double brightness(int rgb) {
  17.         int r = getRed(rgb);
  18.         int g = getGreen(rgb);
  19.         int b = getBlue(rgb);
  20.         return 0.21*r + 0.72*g + 0.07*b;
  21.     }
  22.  
  23.     public static BufferedImage convertToGrayscale(BufferedImage img) {
  24.         BufferedImage result = new BufferedImage(
  25.                 img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB
  26.             );
  27.         for(int x = 0; x < img.getWidth(); x++) {
  28.             for(int y = 0; y < img.getHeight(); y++) {
  29.                 int col = img.getRGB(x, y);
  30.                 int gr = (int)brightness(col);
  31.                 result.setRGB(x, y, rgbColour(gr, gr, gr));
  32.             }
  33.         }
  34.         return result;
  35.     }
  36.  
  37.    
  38.     /* ----------- Methods that you will write in this assignment. */
  39.  
  40.     public static BufferedImage thresholdImage(BufferedImage img, int threshold)
  41.     {
  42.         BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB) ;
  43.         for (int i = 0 ; i < img.getWidth() ; i++)
  44.         {
  45.             for (int j = 0 ; j < img.getHeight() ; j++)
  46.             {
  47.                 int color = img.getRGB(i,j) ;
  48.                 double brightness = brightness(color) ;
  49.                
  50.                 if (brightness > threshold)
  51.                 {
  52.                     result.setRGB(i,j,rgbColour(255,255,255)) ;
  53.                 }
  54.                 else
  55.                 {
  56.                     result.setRGB(i,j,rgbColour(0,0,0)) ;
  57.                 }
  58.             }
  59.         }
  60.         return result ;
  61.     }
  62.    
  63.     public static BufferedImage horizontalMirror(BufferedImage img)
  64.     {
  65.         BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB) ;
  66.         for (int x = 0 ; x < img.getWidth() ; x++)
  67.         {
  68.             for (int y = 0 ; y < img.getHeight() ; y++)
  69.             {
  70.                 int old = img.getRGB(x,y) ;
  71.                 int gnu = img.getRGB(img.getWidth() - x - 1, y) ;
  72.                
  73.                 result.setRGB(x,y,gnu) ;
  74.                 result.setRGB(img.getWidth() - x - 1,y,old) ;
  75.             }
  76.         }
  77.         return result ;
  78.     }
  79.  
  80.     public static BufferedImage splitToFour(BufferedImage img)
  81.     {
  82.         BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB) ;
  83.        
  84.         int n = 2 ;
  85.         int w = img.getWidth() / n ;
  86.         int h = img.getHeight() / n ;
  87.        
  88.         for (int x = 0 ; x < w ; x++)
  89.         {
  90.             for (int y = 0 ; y < h ; y++)
  91.             {
  92.                 int col = img.getRGB(x+n, y+n) ;
  93.                
  94.                 result.setRGB(x, y, col);
  95.                 result.setRGB(x+w, y, col);
  96.                 result.setRGB(x, y+h, col);
  97.                 result.setRGB(x+w, y+h, col);
  98.             }
  99.         }
  100.        
  101.         return result ;
  102.     }
  103.  
  104.     /*public static BufferedImage imageCorrelation(BufferedImage img, double[][] mask)
  105.     {
  106.         // fill this in
  107.     }*/
  108.    
  109.     public static BufferedImage rowPixelSort(BufferedImage img, int n)
  110.     {
  111.         BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB) ;
  112.         for (int i = 0 ; i < img.getWidth() ; i++)
  113.         {
  114.             for (int j = 0 ; j < img.getHeight() ; j++)
  115.             {
  116.                 int pix = img.getRGB(i,j) ;
  117.                 result.setRGB(i,j,pix) ;
  118.             }
  119.         }
  120.         for (int z = 0 ; z < n ; z++)
  121.         {
  122.             for (int x = 0 ; x < result.getWidth() - 1 ; x++)
  123.             {
  124.                 for (int y = 0 ; y < result.getHeight() ; y++)
  125.                 {
  126.                     int one = result.getRGB(x,y) ;
  127.                     int two = result.getRGB(x+1,y) ;
  128.                     int b1 = (int) brightness(one) ;
  129.                     int b2 = (int) brightness(two) ;
  130.                     if (b1 > b2)
  131.                     {
  132.                         result.setRGB(x,y,two) ;
  133.                         result.setRGB(x+1,y,one) ;
  134.                     }
  135.                 }
  136.             }
  137.         }
  138.         return result ;
  139.     }
  140.     // ------------------------------------ end of your code
  141.  
  142.    
  143.    
  144.    
  145.     /* A utility method we need to convert Image objects to BufferedImage, copied from
  146.      * http://stackoverflow.com/questions/13605248/java-converting-image-to-bufferedimage
  147.      */
  148.     public static BufferedImage toBufferedImage(Image img) {
  149.         if (img instanceof BufferedImage) { return (BufferedImage) img;}
  150.         // Create a buffered image with transparency
  151.         BufferedImage bimage = new BufferedImage(
  152.                 img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB
  153.             );
  154.         // Draw the image on to the buffered image
  155.         Graphics2D bGr = bimage.createGraphics();
  156.         bGr.drawImage(img, 0, 0, null);
  157.         bGr.dispose();
  158.         // Return the buffered image
  159.         return bimage;
  160.     }
  161.  
  162.     /* A utility method to create a JPanel instance that displays the given Image. */
  163.     public static JPanel createPanel(Image img) {
  164.         // Define a local class from which we create an object to return as result.
  165.         class ImagePanel extends JPanel {
  166.             private Image img;
  167.             public ImagePanel(Image img) {
  168.                 this.img = img;
  169.                 this.setPreferredSize(new Dimension(img.getWidth(null), img.getHeight(null)));
  170.             }
  171.  
  172.             public void paintComponent(Graphics g) {
  173.                 super.paintComponent(g);
  174.                 g.drawImage(img, 0, 0, this);
  175.             }
  176.         }
  177.         return new ImagePanel(img);
  178.     }
  179.  
  180.     /* The main method to try out the whole shebang. */
  181.     public static void main(String[] args) {
  182.         Image img = Toolkit.getDefaultToolkit().getImage("ryerson1.jpg");
  183.         MediaTracker m = new MediaTracker(new JPanel());
  184.         m.addImage(img, 0);
  185.         try { m.waitForAll(); } catch(InterruptedException e) { }
  186.         BufferedImage bimg = toBufferedImage(img);
  187.         JFrame f = new JFrame("CCPS 109 Lab 7");
  188.         f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  189.         f.setLayout(new GridLayout(2, 3));
  190.         f.add(createPanel(thresholdImage(bimg, 150)));
  191.         f.add(createPanel(horizontalMirror(bimg)));
  192.         f.add(createPanel(splitToFour(bimg)));
  193.         double wt = 1.0/9;
  194.         double[][] blur = {{wt,wt,wt},{wt,wt,wt},{wt,wt,wt}};
  195.        // f.add(createPanel(imageCorrelation(bimg, blur)));
  196.         double[][] edged ={{-1,-1,-1},{-1,8,-1},{-1,-1,-1}};
  197.         //f.add(createPanel(imageCorrelation(convertToGrayscale(bimg), edged)));
  198.         //double [][] sharpen = {{0,-1,0},{-1,5,-1},{0,-1,0}};
  199.         //f.add(createPanel(imageCorrelation(bimg, sharpen)));
  200.         f.add(createPanel(rowPixelSort(bimg, bimg.getWidth())));
  201.         f.pack();
  202.         f.setVisible(true);
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement