Question

So I was trying to apply a gaussian filter to an image I have using Java. However the image is not at all being effected by the filter. I am using the inbuilt convolution operator. Could some one point to me what is going wrong ?

public static void main(String[] args) {
  File img = new File( "C:\\Users\\Aditya\\Pictures\\20130729_140153.jpg" );
  BufferedImage a = null;
  try{ 
      a= ImageIO.read( img );
  }
  catch( Exception e ){
      System.out.print( e.toString() );
      return;
  }
  BufferedImage a9 = new BufferedImage( a.getWidth() , a.getHeight() , a.getType() ); 
  float[] matrix = {
    1f/273,4f/273,7f/273,4f/273,1f/273,
    4f/273,16f/273,26f/273,16f/273,4f/273,
    7f/273,26f/273,41f/273,26f/273,7f/273,
    4f/273,16f/273,26f/273,16f/273,4f/273,
    1f/273,4f/273,7f/273,4f/273,1f/273
    };
    BufferedImageOp op = new ConvolveOp( new Kernel( 5, 5, matrix ) );
    a9 = op.filter( a, a9 );
    try{
        ImageIO.write( a9 , "jpg" ,new File( "C:\\Users\\Aditya\\Pictures\\jim.jpg" ));
    }catch( Exception e ){
        System.out.println( e );
    }
    }
}

By not working I mean that there is no difference in the images of a and a9 at all. Its like they have been copied !

Was it helpful?

Solution

There is nothing wrong with your code, I ran it and I see a slight blur in the output image. If you want to see a more pronounced effect, change it to an edge-detection kernel, for example by using

    float[] matrix = {
             1,  0, -1,
             0,  0,  0,
            -1,  0,  1
    };
    BufferedImageOp op = new ConvolveOp(new Kernel(3, 3, matrix));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top