Example usage for java.awt.image Kernel Kernel

List of usage examples for java.awt.image Kernel Kernel

Introduction

In this page you can find the example usage for java.awt.image Kernel Kernel.

Prototype

public Kernel(int width, int height, float[] data) 

Source Link

Document

Constructs a Kernel object from an array of floats.

Usage

From source file:view.FramePrincipal.java

@SuppressWarnings("Convert2Diamond")
private void initKernels() {
    kernels = new ArrayList<Kernel>();
    kernelNames = new ArrayList<String>();
    listaDadosEstatisticosJava = new ArrayList<DadosEstatisticos>();
    listaDadosEstatisticosOPENCL = new ArrayList<DadosEstatisticos>();
    listaDadosEstatisticosJavaTHREAD = new ArrayList<DadosEstatisticos>();

    int kernelSizeX = 0;
    int kernelSizeY = 0;
    float kernelData[] = null;

    // Edge detection
    kernelSizeX = 3;/*from   w  w w  .  jav a2  s  .  c o m*/
    kernelSizeY = 3;
    kernelData = new float[] { -1, 0, -1, 0, 4, 0, -1, 0, -1 };
    kernels.add(new Kernel(kernelSizeX, kernelSizeY, kernelData));
    kernelNames.add("Negativar");

    // Sharpen
    kernelSizeX = 3;
    kernelSizeY = 3;
    kernelData = new float[] { -1, 0, -1, 0, 5, 0, -1, 0, -1 };
    kernels.add(new Kernel(kernelSizeX, kernelSizeY, kernelData));
    kernelNames.add("Desbotar");

    // Blur
    for (int i = 3; i <= 21; i += 2) {
        initRedimencionarKernel(i);
    }
}

From source file:org.sbs.util.ImageCompress.java

/**
 * gif/*from  w w w  . ja v  a  2 s .c om*/
 * 
 * @param originalFile
 *            
 * @param resizedFile
 *            ?
 * @param newWidth
 *            
 * @param newHeight
 *             -1?
 * @param quality
 *             ()
 * @throws IOException
 */
public void resize(File originalFile, File resizedFile, int newWidth, int newHeight, float quality)
        throws IOException {
    if (quality < 0 || quality > 1) {
        throw new IllegalArgumentException("Quality has to be between 0 and 1");
    }
    ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
    Image i = ii.getImage();
    Image resizedImage = null;
    int iWidth = i.getWidth(null);
    int iHeight = i.getHeight(null);
    if (newHeight == -1) {
        if (iWidth > iHeight) {
            resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight) / iWidth, Image.SCALE_SMOOTH);
        } else {
            resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight, newWidth, Image.SCALE_SMOOTH);
        }
    } else {
        resizedImage = i.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
    }
    // This code ensures that all the pixels in the image are loaded.
    Image temp = new ImageIcon(resizedImage).getImage();
    // Create the buffered image.
    BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    // Copy image to buffered image.
    Graphics g = bufferedImage.createGraphics();
    // Clear background and paint the image.
    g.setColor(Color.white);
    g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
    g.drawImage(temp, 0, 0, null);
    g.dispose();
    // Soften.
    float softenFactor = 0.05f;
    float[] softenArray = { 0, softenFactor, 0, softenFactor, 1 - (softenFactor * 4), softenFactor, 0,
            softenFactor, 0 };
    Kernel kernel = new Kernel(3, 3, softenArray);
    ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
    bufferedImage = cOp.filter(bufferedImage, null);
    // Write the jpeg to a file.
    FileOutputStream out = FileUtils.openOutputStream(resizedFile);
    // Encodes image as a JPEG data stream
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
    param.setQuality(quality, true);
    encoder.setJPEGEncodeParam(param);
    encoder.encode(bufferedImage);
}

From source file:qupath.lib.algorithms.color.EstimateStainVectors.java

/**
 * Smooth out compression artefacts by running 3x3 filter twice (roughly approximates a small Gaussian filter).
 * //from   w  w w  .  ja  va  2 s  . com
 * @param img
 * @return
 */
public static BufferedImage smoothImage(final BufferedImage img) {
    ConvolveOp op = new ConvolveOp(new Kernel(3, 3,
            new float[] { 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f }),
            ConvolveOp.EDGE_NO_OP, null);
    BufferedImage img2 = op.filter(img, null);
    return op.filter(img2, null);
}

From source file:rega.genotype.ui.util.GenotypeLib.java

public static void scalePNG(File in, File out, double perc) throws IOException {
    Image i = ImageIO.read(in);
    Image resizedImage = null;/*  w w  w.j av  a 2s . c o  m*/

    int newWidth = (int) (i.getWidth(null) * perc / 100.0);
    int newHeight = (int) (i.getHeight(null) * perc / 100.0);

    resizedImage = i.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);

    // This code ensures that all the pixels in the image are loaded.
    Image temp = new ImageIcon(resizedImage).getImage();

    // Create the buffered image.
    BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null),
            BufferedImage.TYPE_INT_RGB);

    // Copy image to buffered image.
    Graphics g = bufferedImage.createGraphics();

    // Clear background and paint the image.
    g.setColor(Color.white);
    g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
    g.drawImage(temp, 0, 0, null);
    g.dispose();

    // Soften.
    float softenFactor = 0.05f;
    float[] softenArray = { 0, softenFactor, 0, softenFactor, 1 - (softenFactor * 4), softenFactor, 0,
            softenFactor, 0 };
    Kernel kernel = new Kernel(3, 3, softenArray);
    ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
    bufferedImage = cOp.filter(bufferedImage, null);

    ImageIO.write(bufferedImage, "png", out);
}