Java DIP - 腐蚀和扩张
在本章中,我们学习应用两个非常常见的形态学运算符:扩张和腐蚀。
我们使用OpenCV函数erode和dilate。它们可以在Imgproc包下找到。其语法如下 −
Imgproc.erode(source, destination, element); Imgproc.dilate(source, destination, element);
参数如下所述 −
| Sr.No. | 参数 &描述 |
|---|---|
| 1 |
source 源图像。 |
| 2 |
destination 目标图像。 |
| 3 |
element 用于腐蚀和扩张的结构元素,如果element=Mat(),则使用3 x 3矩形结构元素。 |
除了erode()和dilate()方法外,Imgproc类还提供了其他方法。它们被简要描述 −
| Sr.No. | 方法 &描述 |
|---|---|
| 1 |
cvtColor(Mat src, Mat dst, int code, int dstCn) 它将图像从一个颜色空间转换为另一个颜色空间。 |
| 2 |
dilate(Mat src, Mat dst, Mat kernel) 它使用特定的结构元素来扩大图像。 |
| 3 |
equalizeHist(Mat src, Mat dst) 它均衡直方图灰度图像。 |
| 4 |
filter2D(Mat src, Mat dst, intdepth, Mat kernel, Point anchor, double delta) 它将图像与内核进行卷积。 |
| 5 |
GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX) 它使用高斯滤波器模糊图像。 |
| 6 |
integral(Mat src, Mat sum) 计算图像的积分。 |
示例
以下示例演示了如何使用 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());
}
}
}
输出
执行给定的代码时,将看到以下输出 −
原始图像
在上面的原始图像上,执行了一些腐蚀和扩张操作,这些操作已在下面的输出中显示 −
腐蚀
扩张

