Example usage for java.awt.image ConvolveOp filter

List of usage examples for java.awt.image ConvolveOp filter

Introduction

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

Prototype

public final WritableRaster filter(Raster src, WritableRaster dst) 

Source Link

Document

Performs a convolution on Rasters.

Usage

From source file:paintbasico2d.VentanaPrincipal.java

private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
    int index = jComboBox2.getSelectedIndex();
    Kernel k;// w w  w.j  a  va2 s.com

    VentanaInterna vi = (VentanaInterna) (escritorio.getSelectedFrame());
    if (vi != null) {
        BufferedImage ImgSource = vi.getLienzo().getImage();
        if (ImgSource != null) {
            try {
                switch (index) {
                case 0:
                    k = KernelProducer.createKernel(KernelProducer.TYPE_MEDIA_3x3);
                    break;
                case 1:
                    k = KernelProducer.createKernel(KernelProducer.TYPE_BINOMIAL_3x3);
                    break;
                case 2:
                    k = KernelProducer.createKernel(KernelProducer.TYPE_ENFOQUE_3x3);
                    break;
                case 3:
                    k = KernelProducer.createKernel(KernelProducer.TYPE_RELIEVE_3x3);
                    break;
                case 4:
                    k = KernelProducer.createKernel(KernelProducer.TYPE_LAPLACIANA_3x3);
                    break;
                default:
                    k = null;

                }

                ConvolveOp cop = new ConvolveOp(k);

                BufferedImage imgdest = cop.filter(ImgSource, null);
                vi.getLienzo().setImage(imgdest);
                vi.getLienzo().repaint();

            } catch (IllegalArgumentException e) {
                System.err.println(e.getLocalizedMessage());
            }
        }
    }
}

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 .  j av a2s  .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;//from   w  w  w  . j a v  a2s.  com

    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);
}