Java DIP - 基本阈值处理
阈值处理可以以最简单的方式实现图像分割。图像分割意味着将整个图像分成一组像素,使得每组中的像素具有一些共同的特征。图像分割在定义对象及其边界方面非常有用。
在本章中,我们对图像执行一些基本的阈值处理操作。
我们使用 OpenCV 函数 threshold。它可以在 Imgproc 包下找到。它的语法如下 −
Imgproc.threshold(source, destination, thresh , maxval , type);
参数说明如下 −
Sr.No. | 参数 &描述 |
---|---|
1 |
source 源图像。 |
2 |
destination 目标图像。 |
3 |
thresh 阈值。 |
4 |
maxval THRESH_BINARY 使用的最大值和THRESH_BINARY_INV 阈值类型。 |
5 |
type 可能的类型有 THRESH_BINARY、THRESH_BINARY_INV、THRESH_TRUNC 和 THRESH_TOZERO。 |
除了这些阈值方法之外,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, int ddepth, 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; Imgproc.threshold(source,destination,127,255,Imgproc.THRESH_TOZERO); Highgui.imwrite("ThreshZero.jpg", destination); } catch (Exception e) { System.out.println("error: " + e.getMessage()); } } }
输出
执行给定的代码时,将看到以下输出 −
原始图像

在上面的原始图像上,执行了一些阈值操作,如下面的输出所示 −
阈值二元

阈值二元反转

阈值零
