Jump to content
smack_dead

efeito blur

Recommended Posts

smack_dead

Boas amigos neste site há um exercício para aplicar o efeito  Gaussian blur filter mas nao estou a conseguir ir buscar o pretendido aqui esta o meu código podem-me ajudar??

http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/an-introduction-to-digital-image-processing-r2007

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package a.m.imagem;

import java.awt.BorderLayout;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
*
* @author fnac
*/
public class AMImagem {

    
    public static Color[][] blur(Color[][] image, int blurLevel)
{
	Color[][] blurred = image.clone();
	for (int t = 0; t < blurLevel; t++)
	{
		for (int i = 1; i < blurred.length - 1; i++)
		{
			for (int j = 1; j < blurred[0].length - 1; j++)
			{
				int blue = image[i - 1][j - 1].getBlue()
						+ image[i - 1][j].getBlue()
						+ image[i - 1][j + 1].getBlue()
						+ image[i][j - 1].getBlue() + image[i][j].getBlue()
						+ image[i][j + 1].getBlue()
						+ image[i + 1][j - 1].getBlue()
						+ image[i + 1][j].getBlue()
						+ image[i + 1][j + 1].getBlue();
				blue /= 9;
				int red = image[i - 1][j - 1].getRed()
						+ image[i - 1][j].getRed()
						+ image[i - 1][j + 1].getRed()
						+ image[i][j - 1].getRed() + image[i][j].getRed()
						+ image[i][j + 1].getRed()
						+ image[i + 1][j - 1].getRed()
						+ image[i + 1][j].getRed()
						+ image[i + 1][j + 1].getRed();
				red /= 9;
				int green = image[i - 1][j - 1].getGreen()
						+ image[i - 1][j].getGreen()
						+ image[i - 1][j + 1].getGreen()
						+ image[i][j - 1].getGreen()
						+ image[i][j].getGreen()
						+ image[i][j + 1].getGreen()
						+ image[i + 1][j - 1].getGreen()
						+ image[i + 1][j].getGreen()
						+ image[i + 1][j + 1].getGreen();
				green /= 9;
				blurred[i][j] = new Color(red, green, blue);
			}
		}
		image = blurred.clone();
	}
	return blurred;
}
    
    private static RenderedImage ni;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here

        BufferedImage image = null;
        File f1 = new File("cao.jpg");
        image = ImageIO.read(f1);
        //lançar imaguem
        JFrame frame = new JFrame();
        JLabel label = new JLabel(new ImageIcon(image));
        frame.getContentPane().add(label, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        int H;
        int W;
        H = image.getHeight(null);
        W = image.getWidth(null);
        int nb = image.getSampleModel().getNumBands();
        int[] pixel = new int[nb];
        Raster inputRaster = image.getData();
        //defenir um vextor para correr tudo
        int[] pixels = new int[nb * W * H];
        //vai buscar todos pixeis
        inputRaster.getPixels(0, 0, W, H, pixels);

        //Criar nova imaguem com novas dimensoes!
        BufferedImage ni = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);

        WritableRaster outputRaster = ni.getRaster();


        //float[] matrix = new float[400];
       // for (int i = 0; i < 400; i++) {
          //  matrix[i] = 1.0f / 400.0f;
        //}

       // BufferedImageOp op = new ConvolveOp(new Kernel(20, 20, matrix), ConvolveOp.EDGE_NO_OP, null);
        //blurredImage = op.filter(sourceImage, destImage);

        
        ni.setData(outputRaster);
        ImageIO.write(ni, "JPEG", new File("image1.jpg"));
        JFrame frame0 = new JFrame();
        JLabel label0 = new JLabel(new ImageIcon(ni));
        frame0.getContentPane().add(label0,BorderLayout.CENTER);
        frame0.pack();
        frame0.setVisible(true);

    }
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.