I want to do convolve operation on an image using java. But the program shows a runtime error like cannot do Convolve operation on the image. I am attaching the code I have done. Please help...

import java.awt.image.*;

public class ImageOperations {

    private BufferedImage image, blurredImage = null;
    private int w, h;
    private int rgb[];
    private int red[], green[], blue[];
    //private int x[],freq[];
    private double prob[];

    public final float [] FILTER_3x3 =
    {
        0.1 , 0.1 , 0.1,
	0.1 , 0.4 , 0.1,
	0.1 , 0.1 , 0.1
    };

    public ImageOperations()
    {
        this( null );
    }

    public ImageOperations( BufferedImage image )
    {
        this.image = image;
    }

    public BufferedImage getImage()
    {
       return image;
    }

    public void setImageDetails()
    {
        w = image.getWidth();
        h = image.getHeight();
    }
    public void getImagePixelData( BufferedImage im )
    {
        rgb = new int[ w * h ];
        red = new int[ w * h ];
        green = new int[ w * h ];
        blue = new int[ w * h ];

        rgb = im.getRGB(0, 0, w, h, rgbs, 0, w);
    }

    public void showImageData()
    {

        for(int i = 0; i < rgb.length; i++)
        {
            int tmp = rgb[i];

            red[i] = (tmp & 0x00ff0000) >> 16;
            green[i] = (tmp & 0x0000ff00) >> 8;
            blue[i] = (tmp & 0x000000ff);

            //System.out.printf ( "Red[%d] = %d, Green[%d] = %d, Blue[%d] = %d \n" , i, red[i], i , green[i], i, blue[i] );
        }

        System.out.println( "Image Width & Height is :: " +w+ " " +h );
    }

    public BufferedImage getBlurredImage()
    {
        ConvolveOp cop = new ConvolveOp( new Kernel( 3, 3, FILTER_3x3), ConvolveOp.EDGE_NO_OP, null) );

        if(getImage() == null)
            System.out.println( "Image is null");
        else
        System.out.println( "Image is not null");

        blurredImage = cop.filter( image, blurredImage);   // I'm a bit confused here

        return blurredImage;
    }
}

hmmmm, I'm not your debuger (sure maybe someone), but at 1st. sight

public final double[][] FILTER_3x3 = {
        {0.1, 0.1, 0.1},
        {0.1, 0.4, 0.1},
        {0.1, 0.1, 0.1}
    };

luck with that

I recommend examples from the book:http://filthyrichclients.org/
download Examples
Chapter 8, Image Processing
go to
CustomImageOp and GraphicsUtilities - very useful methods

public final float[] FILTER_3x3 = {
        0.1f, 0.1f, 0.1f,
        0.1f, 0.4f, 0.1f,
        0.1f, 0.1f, 0.1f
    };

Thank you quuba, it was very much helpful. Actually the problem was the image was not compatible to do a convolve operation on. I had to get the Color Model of the image, and had to create a compatible writable raster. Then I am able to do a convolve operation....

So, the code is....

import java.awt.image.*;

public class ImageOperations {

    private BufferedImage image;
    private int w, h;
    private int rgb[], blurredRGBS[];
    private int red[], green[], blue[];
    //private int x[],freq[];
    //private double prob[];

    public static final float [] BLUR_3x3 =
    {
        0.1f , 0.1f , 0.1f,
	0.1f , 0.2f , 0.1f,
	0.1f , 0.1f , 0.1f
    };

    public ImageOperations()
    {
        this( null );
    }

    public ImageOperations( BufferedImage image )
    {
        this.image = image;
    }

    public BufferedImage getImage()
    {
       return image;
    }

    public void setImageDetails()
    {
        w = image.getWidth();
        h = image.getHeight();
    }
    public void getImagePixelData( BufferedImage im )
    {
        rgb = new int[ w * h ];
        red = new int[ w * h ];
        green = new int[ w * h ];
        blue = new int[ w * h ];

        rgb = im.getRGB(0, 0, w, h, rgb, 0, w);
    }

    public void showImageData()
    {

        for(int i = 0; i < rgb.length; i++)
        {
            int tmp = rgb[i];

            red[i] = (tmp & 0x00ff0000) >> 16;
            green[i] = (tmp & 0x0000ff00) >> 8;
            blue[i] = (tmp & 0x000000ff);

            //System.out.printf ( "Red[%d] = %d, Green[%d] = %d, Blue[%d] = %d \n" , i, red[i], i , green[i], i, blue[i] );
        }

        System.out.println( "Image Width & Height is :: " +w+ " " +h );
    }

    public BufferedImage getBlurredImage()
    {
        
        BufferedImage blurredImage = null;

        ColorModel cm = image.getColorModel();
        
        image = new BufferedImage( cm, cm.createCompatibleWritableRaster( w, h ),
                                   cm.isAlphaPremultiplied(), null );

        ConvolveOp cop = new ConvolveOp( new Kernel( 3, 3, BLUR_3x3), ConvolveOp.EDGE_NO_OP, null );

        if(getImage() == null)
            System.out.println( "Image is null");
        else
        System.out.println( "Image is not null");
        
        blurredImage = cop.filter( image, null );

        System.out.println( "Blurring is done");

        return blurredImage;
    }
}

Edited 5 Years Ago by Ezzaral: Removed unnecessary quote tags. Quote tags are for quoting others. Please do not use them on your own comments, as it makes them difficult to read.

This question has already been answered. Start a new discussion instead.