Image Processing and Computer vision in java (Image Filtering part1)

Image filtering:

today we are going to learn image filtering. We can apply various kind of effects on our picture by image filtering. Basically it is a neighborhood operation, which means the value of output pixel depends on the neighboring pixels of input pixel. In image filtering we have a 2D filtering matrix and a 2D image. Actually we are multiplying each filter value with a corresponding neighboring pixel of the input pixel and then sum them out. The sum will be the value of output pixel. Some times we have to normalize value of output pixel by averaging. Take a look at the picture below

convolution2

we can call the filtering matrix a kernel. As we can see from the picture above, we apply the filter to the center pixel by multiplying its neighborhood with the kernel. This operation is also called convolution and the kernel is called the convolution matrix . The size of kernel has to be odd so that it has a center like an example 3×3,5×5 and 7×7 kernels are valid.

Blurring:

blurring can be achieved simply by averaging the neighborhood of a pixel. To get a better idea look at the code snippet below below

    //5x5 filtering matrix
    public static final int[][] MEEAN_FILTR_5 = {{  1,1,1,1,1  },
                                                 {  1,1,1,1,1  },
                                                 {  1,1,1,1,1  },
                                                 {  1,1,1,1,1  },
                                                 {  1,1,1,1,1  }};
    public void meanFilter(BufferedImage img){
        int pixel[];
        //looping trough each pixel of the image
        for (int i = 0; i < img.getWidth(); i++) {
            for (int j = 0; j < img.getHeight(); j++) {
                int n=0;
                //looping into filtering matrix
                for(int k =0 ; k<MEEAN_FILTR_5.length;k++){
                    for(int l=0;l<MEEAN_FILTR_5[k].length;l++){
                        //getting the corresponding neighboring pixel
                        // we use modulus of the width or height of the image so that pixels outside the image will be wrapped around
                        int imgX = (i-MEEAN_FILTR_5.length/2+k+img.getWidth())%img.getWidth();
                        int imgY = (j-MEEAN_FILTR_5[k].length/2+l+img.getHeight())%img.getHeight();
                        pixel = img.getRaster().getPixel(imgX,imgY, new int[3]);
                        //get the new value
                        n += pixel[0]*MEEAN_FILTR_5[k][l];

                        
                    }
                }
                //normalize the value
                n = n/25;
                if(n>255)
                    n =255;
                else if(n<0)
                    n=0;
                //set the new pixel value
                Color color = new Color(n, n, n);

                int rgb = color.getRGB();

                img.setRGB(i, j, rgb);

            }

        }
    }

Input Image:
meanfilbeforeOutput Image:
meanafter

Motion Blur:

For motion filter we can use the kernel below

public static final int[][] MOTION_FILTR_5 =   { {  1,0,0,0,0  },
                                                 {  0,1,0,0,0  },
                                                 {  0,0,1,0,0  },
                                                 {  0,0,0,1,0  },
                                                 {  0,0,0,0,1  } };
//here our normalize factor will be 5 because the sum of the kernel values is 5

So the code will be

public void motionFilter(BufferedImage img){
        int pixel[];
        for (int i = 0; i < img.getWidth(); i++) {
            for (int j = 0; j < img.getHeight(); j++) {
                int n=0;
                for(int k =0 ; k<MOTION_FILTR_5.length;k++){
                    for(int l=0;l<MOTION_FILTR_5[k].length;l++){
                        int imgX = (i-MOTION_FILTR_5.length/2+k+img.getWidth())%img.getWidth();
                        int imgY = (j-MOTION_FILTR_5[k].length/2+l+img.getHeight())%img.getHeight();
                        pixel = img.getRaster().getPixel(imgX,imgY, new int[3]);
                        //get the new value
                        n += pixel[0]*MOTION_FILTR_5[k][l];

                        
                    }
                }
                n = n/5;
                if(n>255)
                    n =255;
                else if(n<0)
                    n=0;
                Color color = new Color(n, n, n);

                int rgb = color.getRGB();

                img.setRGB(i, j, rgb);

            }

        }
    }

Input Image:
meanfilbeforeOutput Image:
motion blur

On our next tutorial we will be talking about Image Sharpening and Edge Detection.

Advertisements

One thought on “Image Processing and Computer vision in java (Image Filtering part1)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s