Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.45 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import ij.IJ;
  3. import ij.ImageJ;
  4. import ij.ImagePlus;
  5. import ij.gui.GenericDialog;
  6. import ij.plugin.filter.PlugInFilter;
  7. import ij.process.ImageProcessor;
  8.  
  9.  
  10. public class Filter_plugin3 implements PlugInFilter {
  11.  
  12.     private final int R = 16;
  13.     private final int G = 8;
  14.     private final int B = 0;
  15.  
  16.     ImagePlus imp;
  17.    // ImageProcessor ip;
  18.     ImageProcessor ip2;
  19.     int rozmiarSasiedztwa = 0;
  20.     int krok;
  21.  
  22.     public int setup(String arg, ImagePlus imp) {
  23.         this.imp = imp;
  24.         return DOES_ALL;
  25.     }
  26.  
  27.     public void run(ImageProcessor ip) {
  28.         doDialog();
  29.         krok = (rozmiarSasiedztwa - 1) / 2;
  30.         for (int x = krok; x < ip.getHeight() - krok - 1; x++) {
  31.             for (int y = krok; y < ip.getWidth() - krok - 1; y++) {
  32.                 ip.putPixel(x, y, h(x, y));
  33.             }
  34.         }
  35.     }
  36.  
  37.     private int h(int x, int y) {
  38.         double sumaWag[] = { 0, 0, 0 };
  39.         double suma[] = { 0, 0, 0 };
  40.         int element = 0;
  41.         ArrayList<Integer> wagiR = H(x, y, R);
  42.         ArrayList<Integer> wagiG = H(x, y, G);
  43.         ArrayList<Integer> wagiB = H(x, y, B);
  44.  
  45.         for (int m = x - krok; m <= x + krok; m++) {
  46.             for (int n = y - krok; n <= y + krok; n++) {
  47.                 int piksel = ip2.getPixel(m, n);
  48.  
  49.                 int wagaR = wagiR.get(element);
  50.                 suma[0] += (piksel >> R) & 0xff * wagaR;
  51.                 sumaWag[0] += wagaR;
  52.  
  53.                 int wagaG = wagiG.get(element);
  54.                 suma[1] += (piksel >> G) & 0xff * wagaG;
  55.                 sumaWag[1] += wagaG;
  56.  
  57.                 int wagaB = wagiB.get(element);
  58.                 suma[2] += (piksel >> B) & 0xff * wagaB;
  59.                 sumaWag[2] += wagaB;
  60.  
  61.                 element++;
  62.             }
  63.         }
  64.  
  65.         for (Double waga : sumaWag) {
  66.             if (waga == 0) {
  67.                 waga = 1.0;
  68.             }
  69.         }
  70.         return ((int)(suma[0] / sumaWag[0]) << R) | ((int)(suma[1] / sumaWag[1]) << G) |(int) (suma[2] / sumaWag[2]);
  71.     }
  72.  
  73.     private ArrayList<Integer> H (int x, int y, int kanał) {
  74.         ArrayList<Integer> wagi = new ArrayList<Integer>();
  75.         ArrayList<Integer> gradienty = new ArrayList<Integer>();
  76.         int sumaGradientow = 0;
  77.  
  78.         for (int m = x - krok; m <= x + krok; m++) {
  79.             for (int n = y - krok; n <= y + krok; n++) {
  80.                 int temp = gradient(m, n, x, y, kanał);
  81.                 sumaGradientow += temp;
  82.                 gradienty.add(temp);
  83.             }
  84.         }
  85.  
  86.         for (Integer gradient : gradienty) {
  87.             int wartosc = (int) ((0.5 * gradient) / sumaGradientow);
  88.             if (wartosc > 0) {
  89.                 wagi.add(wartosc);
  90.             } else {
  91.                 wagi.add(1);
  92.             }
  93.         }
  94.  
  95.         return wagi;
  96.     }
  97.  
  98.     private int gradient(int i, int j, int n, int m, int kanał) {
  99.  
  100.         if (n == i && m == j) {
  101.             return 2;
  102.         } else {
  103.             int fij = (ip2.getPixel(i, j) >> kanał) & 0xff;
  104.             int fmn = (ip2.getPixel(n, m) >> kanał) & 0xff;
  105.  
  106.             int moduł = Math.abs(fmn - fij);
  107.  
  108.             if (moduł == 0) {
  109.                 moduł++;
  110.             }
  111.  
  112.             return 1 / moduł;
  113.         }
  114.     }
  115.  
  116.     private void doDialog() {
  117.         GenericDialog gd = new GenericDialog("Filtr uśredniający z odwrotnym gradientem");
  118.         gd.addNumericField("Podaj szerokość sasiedztwa", 0, 0);
  119.         gd.showDialog();
  120.         rozmiarSasiedztwa = (int) gd.getNextNumber();
  121.     }
  122.  
  123.     private void checkValue(String param) {
  124.         GenericDialog g = new GenericDialog("Filtr uśredniający z odwrotnym gradientem");
  125.         g.addMessage(param);
  126.         g.showDialog();
  127.     }
  128.  
  129.     public static void main(String[] args) {
  130.         // set the plugins.dir property to make the plugin appear in the Plugins menu
  131.         Class<?> clazz = Filter_Plugin2.class;
  132.         String url = clazz.getResource("/" + clazz.getName().replace('.', '/') + ".class").toString();
  133.         String pluginsDir = url.substring("file:".length(), url.length() - clazz.getName().length() - ".class".length());
  134.         System.setProperty("plugins.dir", pluginsDir);
  135.  
  136.         // start ImageJ
  137.         new ImageJ();
  138.  
  139.         // open the Clown sample
  140.         ImagePlus image = IJ.openImage("http://imagej.net/images/lena.jpg");
  141.         image.show();
  142.  
  143.         // run the plugin
  144.         IJ.runPlugIn(clazz.getName(), "");
  145.     }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement