📜  Java DIP-侵蚀和扩张

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


在本章中,我们学习应用两个非常常见的形态运算符:“膨胀”和“侵蚀”。

我们使用OpenCV函数腐蚀扩张。它们可以在Imgproc软件包下找到。其语法如下-

Imgproc.erode(source, destination, element);
Imgproc.dilate(source, destination, element);                

参数说明如下-

Sr.No. Parameter & Description
1

source

It is Source image.

2

destination

It is destination image.

3

element

It is a structuring element used for erosion and dilation, if element=Mat(), a 3 x 3 rectangular structuring element is used.

除了erode()和dilate()方法外,Imgproc类还提供其他方法。他们简要描述-

Sr.No. Method & Description
1

cvtColor(Mat src, Mat dst, int code, int dstCn)

It converts an image from one color space to another.

2

dilate(Mat src, Mat dst, Mat kernel)

It dilates an image by using a specific structuring element.

3

equalizeHist(Mat src, Mat dst)

It equalizes the histogram of a grayscale image.

4

filter2D(Mat src, Mat dst, int depth, Mat kernel, Point anchor, double delta)

It convolves an image with the kernel.

5

GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)

It blurs an image using a Gaussian filter.

6

integral(Mat src, Mat sum)

It calculates the integral of an image.

以下示例演示了如何使用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 main {
   public static void main( String[] args ) {
   
      try{    
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         Mat source = Highgui.imread("digital_image_processing.jpg",  Highgui.CV_LOAD_IMAGE_COLOR);
         Mat destination = new Mat(source.rows(),source.cols(),source.type());
         
         destination = source;

         int erosion_size = 5;
         int dilation_size = 5;
         
         Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new  Size(2*erosion_size + 1, 2*erosion_size+1));
         Imgproc.erode(source, destination, element);
         Highgui.imwrite("erosion.jpg", destination);

         source = Highgui.imread("digital_image_processing.jpg",  Highgui.CV_LOAD_IMAGE_COLOR);
         
         destination = source;
         
         Mat element1 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new  Size(2*dilation_size + 1, 2*dilation_size+1));
         Imgproc.dilate(source, destination, element1);
         Highgui.imwrite("dilation.jpg", destination);
         
      } catch (Exception e) {
         System.out.println("error:" + e.getMessage());
      } 
   }
}

输出

当您执行给定的代码时,将看到以下输出-

原始图片

侵蚀与扩张教程

在上面的原始图像上,已经执行了一些腐蚀和膨胀操作,这些输出显示在下面的输出中-

侵蚀

侵蚀与扩张教程

扩张

侵蚀与扩张教程