Applying Weighted Average Filter Tutorial


In weighted average filter, we gave more weight to the center value. Due to which the contribution of center becomes more then the rest of the values. Due to weighted average filtering , we can actually control the blurring.

We are going to use OpenCV function filter2D to apply weighted average filter to images. It can be found under Imgproc package. Its syntax is given below:

filter2D(src, dst, ddepth , kernel, anchor, delta, BORDER_DEFAULT );

The arguments of the functions and their description is listed below:

Sr.NoArgument & Description
Source image.
Destiantion image.
The depth of dst. A negative value (such as -1) indicates that the depth is the same as the source.
The kernel to be scanned through the image
The position of the anchor relative to its kernel. The location Point(-1, -1) indicates the center by default.
A value to be added to each pixel during the convolution. By default it is 0.
We let this value by default.

Apart from the filter2D method, there are other methods provide by the Imgproc class. They are listed below:

Sr.NoMethod & Description
1cvtColor(Mat src, Mat dst, int code, int dstCn)
Converts an image from one colour space to another.
2dilate(Mat src, Mat dst, Mat kernel)
Dilates an image by using a specific structuring element.
3equalizeHist(Mat src, Mat dst)
Equalizes the histogram of a grayscale image.
4filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta)
Convolves an image with the kernel.
5GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)
Blurs an image using a Gaussian filter.
6integral(Mat src, Mat sum)
Calculates the integral of an image.


The following example demonstrates the use of Imgproc class to apply weighted average filter to an image of GrayScale.

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

public class convolution {
   public static void main( String[] args )
   try {
      int kernelSize = 9;
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      Mat source = Highgui.imread("grayscale.jpg",
      Mat destination = new Mat(source.rows(),source.cols(),source.type());
      Mat kernel = Mat.ones(kernelSize,kernelSize, CvType.CV_32F){	      
      for(int i=0; i<kernel.rows(); i++){
         for(int j=0; j<kernel.cols(); j++){
            double[] m = kernel.get(i, j);
            for(int k =0; k<m.length; k++){
               if(i==1 && j==1){
                  m[k] = 10/18;
                  m[k] = m[k]/(18);
           kernel.put(i,j, m);
    Imgproc.filter2D(source, destination, -1, kernel);
    Highgui.imwrite("output.jpg", destination);
    } catch (Exception e) {
       System.out.println("Error: " + e.getMessage());


Original Image

Applying Weighted Average Filter Tutorial

This original image has also been convolved with the weighted average filter, which is given below:

Weighted Average Filter


Convolved Image.

Applying Weighted Average Filter Tutorial