Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.*;
- import java.awt.image.*;
- public class ImageAssignment {
- /* First, some utility methods that you will need in the methods you write.
- Do not modify these methods in any way. */
- public static int getRed(int rgb) { return (rgb >> 16) & 0xff; }
- public static int getGreen(int rgb) { return (rgb >> 8) & 0xff; }
- public static int getBlue(int rgb) { return rgb & 0xff; }
- public static int rgbColour(int r, int g, int b) {
- return (r << 16) | (g << 8) | b;
- }
- public static double brightness(int rgb) {
- int r = getRed(rgb);
- int g = getGreen(rgb);
- int b = getBlue(rgb);
- return 0.21*r + 0.72*g + 0.07*b;
- }
- public static BufferedImage convertToGrayscale(BufferedImage img) {
- BufferedImage result = new BufferedImage(
- img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB
- );
- for(int x = 0; x < img.getWidth(); x++) {
- for(int y = 0; y < img.getHeight(); y++) {
- int col = img.getRGB(x, y);
- int gr = (int)brightness(col);
- result.setRGB(x, y, rgbColour(gr, gr, gr));
- }
- }
- return result;
- }
- /* ----------- Methods that you will write in this assignment. */
- public static BufferedImage thresholdImage(BufferedImage img, int threshold)
- {
- BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB) ;
- for (int i = 0 ; i < img.getWidth() ; i++)
- {
- for (int j = 0 ; j < img.getHeight() ; j++)
- {
- int color = img.getRGB(i,j) ;
- double brightness = brightness(color) ;
- if (brightness > threshold)
- {
- result.setRGB(i,j,rgbColour(255,255,255)) ;
- }
- else
- {
- result.setRGB(i,j,rgbColour(0,0,0)) ;
- }
- }
- }
- return result ;
- }
- public static BufferedImage horizontalMirror(BufferedImage img)
- {
- BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB) ;
- for (int x = 0 ; x < img.getWidth() ; x++)
- {
- for (int y = 0 ; y < img.getHeight() ; y++)
- {
- int old = img.getRGB(x,y) ;
- int gnu = img.getRGB(img.getWidth() - x - 1, y) ;
- result.setRGB(x,y,gnu) ;
- result.setRGB(img.getWidth() - x - 1,y,old) ;
- }
- }
- return result ;
- }
- public static BufferedImage splitToFour(BufferedImage img)
- {
- BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB) ;
- int n = 2 ;
- int w = img.getWidth() / n ;
- int h = img.getHeight() / n ;
- for (int x = 0 ; x < w ; x++)
- {
- for (int y = 0 ; y < h ; y++)
- {
- int col = img.getRGB(x+n, y+n) ;
- result.setRGB(x, y, col);
- result.setRGB(x+w, y, col);
- result.setRGB(x, y+h, col);
- result.setRGB(x+w, y+h, col);
- }
- }
- return result ;
- }
- /*public static BufferedImage imageCorrelation(BufferedImage img, double[][] mask)
- {
- // fill this in
- }*/
- public static BufferedImage rowPixelSort(BufferedImage img, int n)
- {
- BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB) ;
- for (int i = 0 ; i < img.getWidth() ; i++)
- {
- for (int j = 0 ; j < img.getHeight() ; j++)
- {
- int pix = img.getRGB(i,j) ;
- result.setRGB(i,j,pix) ;
- }
- }
- for (int z = 0 ; z < n ; z++)
- {
- for (int x = 0 ; x < result.getWidth() - 1 ; x++)
- {
- for (int y = 0 ; y < result.getHeight() ; y++)
- {
- int one = result.getRGB(x,y) ;
- int two = result.getRGB(x+1,y) ;
- int b1 = (int) brightness(one) ;
- int b2 = (int) brightness(two) ;
- if (b1 > b2)
- {
- result.setRGB(x,y,two) ;
- result.setRGB(x+1,y,one) ;
- }
- }
- }
- }
- return result ;
- }
- // ------------------------------------ end of your code
- /* A utility method we need to convert Image objects to BufferedImage, copied from
- * http://stackoverflow.com/questions/13605248/java-converting-image-to-bufferedimage
- */
- public static BufferedImage toBufferedImage(Image img) {
- if (img instanceof BufferedImage) { return (BufferedImage) img;}
- // Create a buffered image with transparency
- BufferedImage bimage = new BufferedImage(
- img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB
- );
- // Draw the image on to the buffered image
- Graphics2D bGr = bimage.createGraphics();
- bGr.drawImage(img, 0, 0, null);
- bGr.dispose();
- // Return the buffered image
- return bimage;
- }
- /* A utility method to create a JPanel instance that displays the given Image. */
- public static JPanel createPanel(Image img) {
- // Define a local class from which we create an object to return as result.
- class ImagePanel extends JPanel {
- private Image img;
- public ImagePanel(Image img) {
- this.img = img;
- this.setPreferredSize(new Dimension(img.getWidth(null), img.getHeight(null)));
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.drawImage(img, 0, 0, this);
- }
- }
- return new ImagePanel(img);
- }
- /* The main method to try out the whole shebang. */
- public static void main(String[] args) {
- Image img = Toolkit.getDefaultToolkit().getImage("ryerson1.jpg");
- MediaTracker m = new MediaTracker(new JPanel());
- m.addImage(img, 0);
- try { m.waitForAll(); } catch(InterruptedException e) { }
- BufferedImage bimg = toBufferedImage(img);
- JFrame f = new JFrame("CCPS 109 Lab 7");
- f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- f.setLayout(new GridLayout(2, 3));
- f.add(createPanel(thresholdImage(bimg, 150)));
- f.add(createPanel(horizontalMirror(bimg)));
- f.add(createPanel(splitToFour(bimg)));
- double wt = 1.0/9;
- double[][] blur = {{wt,wt,wt},{wt,wt,wt},{wt,wt,wt}};
- // f.add(createPanel(imageCorrelation(bimg, blur)));
- double[][] edged ={{-1,-1,-1},{-1,8,-1},{-1,-1,-1}};
- //f.add(createPanel(imageCorrelation(convertToGrayscale(bimg), edged)));
- //double [][] sharpen = {{0,-1,0},{-1,5,-1},{0,-1,0}};
- //f.add(createPanel(imageCorrelation(bimg, sharpen)));
- f.add(createPanel(rowPixelSort(bimg, bimg.getWidth())));
- f.pack();
- f.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement