What is masking an image in OpenCV?


In mask operations the value of each pixel of an image is recalculated based on a given mask matrix, this is known as the kernel. Masking is otherwise known as filtering.

The filter2D() method of the Imgproc class accepts a source, destination and kernel matrices and convolves the source matrix with the kernel matrix. Using this method you can mask or filter an image.

Example

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class MaskingExample {
   public static void main( String[] args ) {
      //Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      //Reading the input image
      String file = "D://images//boy.jpg";
      Mat src = Imgcodecs.imread(file);
      //Creating an empty matrix to store the result
      Mat dst = new Mat();
      //Creating kernel1
      Mat kernel1 = new Mat(3, 3, CvType.CV_8S);
      int row = 0, col = 0;
      kernel1.put(row, col, 0, -1, 0, -1, 5, -1, 0, -1, 0);
      //Creating kernel2
      Mat kernel2 = Mat.ones(2,2, CvType.CV_32F);
      for(int i = 0; i<kernel2.rows(); i++) {
         for(int j = 0; j<kernel2.cols(); j++) {
            double[] m = kernel2.get(i, j);
            for(int k = 1; k<m.length; k++) {
               m[k] = m[k]/(2 * 2);
            }
            kernel2.put(i,j, m);
         }
      }
      //Filtering the image using kernel1
      Imgproc.filter2D(src, dst, -1, kernel1);
      HighGui.imshow("Mask Example1", dst);
      dst = new Mat();
      //Filtering the image using kernel2
      Imgproc.filter2D(src, dst, -1, kernel2);
      HighGui.imshow("Mask Example2", dst);
      HighGui.waitKey();
   }
}

Input Image

Output

On executing, the above program generates the following windows −

Kernel1 − 

Kernel2 − 

Updated on: 13-Apr-2020

262 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements