📜  Java DIP-了解卷积

📅  最后修改于: 2020-12-14 05:41:14             🧑  作者: Mango


卷积是对两个函数f和g的数学运算。在这种情况下,函数f和g是图像,因为图像也是二维函数。

执行卷积

为了对图像执行卷积,采取以下步骤-

  • 翻转面罩(水平和垂直)仅一次。
  • 将遮罩滑到图像上。
  • 将相应的元素相乘,然后相加。
  • 重复此过程,直到已计算出图像的所有值。

我们使用OpenCV函数filter2D将卷积应用于图像。可以在Imgproc软件包下找到。其语法如下-

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

函数参数描述如下-

Sr.No. Argument & Description
1

src

It is source image.

2

dst

It is destination image.

3

depth

It is the depth of dst. A negative value (such as -1) indicates that the depth is the same as the source.

4

kernel

It is the kernel to be scanned through the image.

5

anchor

It is the position of the anchor relative to its kernel. The location Point (-1, -1) indicates the center by default.

6

delta

It is a value to be added to each pixel during the convolution. By default it is 0.

7

BORDER_DEFAULT

We let this value by default.

下面的示例演示如何使用Imgproc类对灰度图像执行卷积。

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 = 3;
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         
         Mat source = Highgui.imread("grayscale.jpg",  Highgui.CV_LOAD_IMAGE_GRAYSCALE);
         Mat destination = new Mat(source.rows(),source.cols(),source.type());
         
         Mat kernel = new Mat(kernelSize,kernelSize, CvType.CV_32F) {
            {
               put(0,0,0);
               put(0,1,0);
               put(0,2,0);

               put(1,0,0);
               put(1,1,1);
               put(1,2,0);

               put(2,0,0);
               put(2,1,0);
               put(2,2,0);
            }
         };
         
         Imgproc.filter2D(source, destination, -1, kernel);
         Highgui.imwrite("original.jpg", destination);
         
      } catch (Exception e) {
          System.out.println("Error:" + e.getMessage());
      }
   }
}

输出

在此示例中,我们将图像与以下滤镜(内核)进行卷积。此滤镜可产生原始图像-

0 0 0
0 1 0
0 0 0

原始图片

了解卷积教程

卷积图像

了解卷积教程