Package fr.um2.physique.risa.core.process

Source Code of fr.um2.physique.risa.core.process.ConvolveG

package fr.um2.physique.risa.core.process;

import fr.um2.physique.risa.gui.CoreInterface;
import ij.ImagePlus;
import ij.process.ImageProcessor;

/**
* This class returns a convolved image
*
* @author Ga�l Gonzalez
*/
public class ConvolveG {

  ImagePlus imp;
  String type = "Convolved";
  ImageProcessor imgpr;
  static float[] kernel1d = null;
  static int size = 0;

  /**
   * ConvolveG constructor
   * creates ImagePlus and ImageProcessor.
   */
  public ConvolveG() {
    try {
      imp = new ImagePlus(CoreInterface.getFile().getAbsolutePath());
      imgpr = imp.getProcessor();
    } catch (NullPointerException npe) {
      npe.printStackTrace();
      throw npe;
    }
  }

  /**
   * this method creates a kernel with the given values for sigma and radius
   *
   * @param radius
   * @param sigma ( gaussian width )
   */
  public void CreateKernel(int radius, float sigma) {

    // (2r+1)(2r+1) matrix means (4r�+4r+1) values
    kernel1d = new float[4 * radius * radius + 4 * radius + 1];

    //size is for height and width because this is a square matrix
    size = (2 * radius + 1);

    type = type + " s= " + sigma + " & r= " + radius;

    float gaussianKernelFactor = 0;
    float e = 0;

    // iterator
    int i = 0;

    for (int ky = -radius; ky <= radius; ky++) {
      for (int kx = -radius; kx <= radius; kx++) {
        e = (float) Math
            .exp(-(kx * kx + ky * ky) / (2 * sigma * sigma));

        gaussianKernelFactor += e;

        kernel1d[i++] = e;
      }
    }

    // display the kernel in the console
    /*
     * System.out.println(" gaussianKernelFactor = "+gaussianKernelFactor );
     *
     * System.out.println("\n\n 1d");
     *
     * for (int j=0; j<kernel1d.length;j++){
     *
     * kernel1d[j] = kernel1d[j]/gaussianKernelFactor; if (j % *
     * (2*radius+1) == 0){ System.out.println(""); }
     * System.out.print("\t "+kernel1d[j] );
     *
     * }
     */

  }

  /**
   * This method returns a convolved image with the calculated kernel
   *
   * @return convolved ImagePlus.
   */
  public ImagePlus getConvolvedImage() {

    imgpr.convolve(kernel1d, size, size);
    return imp;
  }

  /**
   * This method returns the calculated kernel
   *
   * @return kernel float[]
   */
  public float[] get1d() {
    return kernel1d;
  }

  /**
   * This method returns the type of process done.
   *
   * @return Type of last processed convolved image.
   */
  public String getType() {
    return type;
  }

}
TOP

Related Classes of fr.um2.physique.risa.core.process.ConvolveG

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.