Advertisement
gbc921

Java Median image filter

Sep 12th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. private void jMenuItem13ActionPerformed(java.awt.event.ActionEvent evt) {
  2.         int width = imagem1.getWidth();
  3.         int height = imagem1.getHeight();
  4.    
  5.     // create new image with the width of height and height of width
  6.     BufferedImage imagemFiltroMediana = new BufferedImage(imagem1.getWidth(),imagem1.getHeight(),BufferedImage.TYPE_INT_RGB);
  7.    
  8.     // do not use boundaries. Starts at 1
  9.         for (int y = 1; y < height-1; y++) {
  10.             for (int x = 1; x < width-1; x++) {
  11.         // think the position of the vector as starting from (0,0) and
  12.         // going as (1,0),(2,0)...
  13.         int[] mediana = new int[9];
  14.         mediana[0] = imagem1.getRGB(x-1, y-1);
  15.         mediana[1] = imagem1.getRGB(x, y-1);
  16.         mediana[2] = imagem1.getRGB(x+1, y-1);
  17.        
  18.         mediana[3] = imagem1.getRGB(x-1, y);
  19.         mediana[4] = imagem1.getRGB(x, y); //central element
  20.         mediana[5] = imagem1.getRGB(x+1, y);
  21.        
  22.         mediana[6] = imagem1.getRGB(x-1, y+1);
  23.         mediana[7] = imagem1.getRGB(x, y+1);
  24.         mediana[8] = imagem1.getRGB(x+1, y+1);
  25.  
  26.         Arrays.sort(mediana);
  27.         int xy = 255;
  28.         // gets the median value. As 9 is odd, gets middle position
  29.         xy = mediana[mediana.length / 2];
  30. //      if (mediana.length % 2 == 0) {
  31. //          xy = (mediana[(mediana.length / 2) - 1] + mediana[mediana.length / 2]) / 2;
  32. //      } else {
  33. //          xy = mediana[mediana.length / 2];
  34. //      }
  35.        
  36.         // set the new color of the central matrix pixel
  37.                 imagemFiltroMediana.setRGB(x, y, xy);
  38.             }
  39.         }
  40.     imagem1 = imagemFiltroMediana;
  41.     ImageIcon icon = new ImageIcon(imagem1);
  42.     jLabel1.setIcon(icon);
  43.    
  44.         this.setSize(imagem1.getWidth()+75, imagem1.getHeight()+75);
  45.     //this.imageUpdate(imagem1, ALLBITS, 0, 0, width, height);
  46.     System.out.println("Terminou Filtro Mediana!!!");
  47.     }//GEN-LAST:event_jMenuItem13ActionPerformed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement