Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import ij.IJ;
- import ij.ImageJ;
- import ij.ImagePlus;
- import ij.gui.GenericDialog;
- import ij.plugin.filter.PlugInFilter;
- import ij.process.ImageProcessor;
- public class Filter_plugin3 implements PlugInFilter {
- private final int R = 16;
- private final int G = 8;
- private final int B = 0;
- ImagePlus imp;
- // ImageProcessor ip;
- ImageProcessor ip2;
- int rozmiarSasiedztwa = 0;
- int krok;
- public int setup(String arg, ImagePlus imp) {
- this.imp = imp;
- return DOES_ALL;
- }
- public void run(ImageProcessor ip) {
- doDialog();
- krok = (rozmiarSasiedztwa - 1) / 2;
- for (int x = krok; x < ip.getHeight() - krok - 1; x++) {
- for (int y = krok; y < ip.getWidth() - krok - 1; y++) {
- ip.putPixel(x, y, h(x, y));
- }
- }
- }
- private int h(int x, int y) {
- double sumaWag[] = { 0, 0, 0 };
- double suma[] = { 0, 0, 0 };
- int element = 0;
- ArrayList<Integer> wagiR = H(x, y, R);
- ArrayList<Integer> wagiG = H(x, y, G);
- ArrayList<Integer> wagiB = H(x, y, B);
- for (int m = x - krok; m <= x + krok; m++) {
- for (int n = y - krok; n <= y + krok; n++) {
- int piksel = ip2.getPixel(m, n);
- int wagaR = wagiR.get(element);
- suma[0] += (piksel >> R) & 0xff * wagaR;
- sumaWag[0] += wagaR;
- int wagaG = wagiG.get(element);
- suma[1] += (piksel >> G) & 0xff * wagaG;
- sumaWag[1] += wagaG;
- int wagaB = wagiB.get(element);
- suma[2] += (piksel >> B) & 0xff * wagaB;
- sumaWag[2] += wagaB;
- element++;
- }
- }
- for (Double waga : sumaWag) {
- if (waga == 0) {
- waga = 1.0;
- }
- }
- return ((int)(suma[0] / sumaWag[0]) << R) | ((int)(suma[1] / sumaWag[1]) << G) |(int) (suma[2] / sumaWag[2]);
- }
- private ArrayList<Integer> H (int x, int y, int kanał) {
- ArrayList<Integer> wagi = new ArrayList<Integer>();
- ArrayList<Integer> gradienty = new ArrayList<Integer>();
- int sumaGradientow = 0;
- for (int m = x - krok; m <= x + krok; m++) {
- for (int n = y - krok; n <= y + krok; n++) {
- int temp = gradient(m, n, x, y, kanał);
- sumaGradientow += temp;
- gradienty.add(temp);
- }
- }
- for (Integer gradient : gradienty) {
- int wartosc = (int) ((0.5 * gradient) / sumaGradientow);
- if (wartosc > 0) {
- wagi.add(wartosc);
- } else {
- wagi.add(1);
- }
- }
- return wagi;
- }
- private int gradient(int i, int j, int n, int m, int kanał) {
- if (n == i && m == j) {
- return 2;
- } else {
- int fij = (ip2.getPixel(i, j) >> kanał) & 0xff;
- int fmn = (ip2.getPixel(n, m) >> kanał) & 0xff;
- int moduł = Math.abs(fmn - fij);
- if (moduł == 0) {
- moduł++;
- }
- return 1 / moduł;
- }
- }
- private void doDialog() {
- GenericDialog gd = new GenericDialog("Filtr uśredniający z odwrotnym gradientem");
- gd.addNumericField("Podaj szerokość sasiedztwa", 0, 0);
- gd.showDialog();
- rozmiarSasiedztwa = (int) gd.getNextNumber();
- }
- private void checkValue(String param) {
- GenericDialog g = new GenericDialog("Filtr uśredniający z odwrotnym gradientem");
- g.addMessage(param);
- g.showDialog();
- }
- public static void main(String[] args) {
- // set the plugins.dir property to make the plugin appear in the Plugins menu
- Class<?> clazz = Filter_Plugin2.class;
- String url = clazz.getResource("/" + clazz.getName().replace('.', '/') + ".class").toString();
- String pluginsDir = url.substring("file:".length(), url.length() - clazz.getName().length() - ".class".length());
- System.setProperty("plugins.dir", pluginsDir);
- // start ImageJ
- new ImageJ();
- // open the Clown sample
- ImagePlus image = IJ.openImage("http://imagej.net/images/lena.jpg");
- image.show();
- // run the plugin
- IJ.runPlugIn(clazz.getName(), "");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement