📜  Java DIP-基本阈值

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


阈值使您能够以最简单的方式实现图像分割。图像分割是指将整个图像分成一组像素,以使每组中的像素具有一些共同的特征。图像分割在定义对象及其边界时非常有用。

在本章中,我们对图像执行一些基本的阈值操作。

我们使用OpenCV函数阈值。可以在Imgproc软件包下找到。其语法如下-

Imgproc.threshold(source, destination, thresh , maxval , type);

参数说明如下-

Sr.No. Parameter & Description
1

source

It is source image.

2

destination

It is destination image.

3

thresh

It is threshold value.

4

maxval

It is the maximum value to be used with the THRESH_BINARY and THRESH_BINARY_INV threshold types.

5

type

The possible types are THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, and THRESH_TOZERO.

除了这些阈值方法外,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 ddepth, 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;
         Imgproc.threshold(source,destination,127,255,Imgproc.THRESH_TOZERO);
         Highgui.imwrite("ThreshZero.jpg", destination);
         
      } catch (Exception e) {
         System.out.println("error: " + e.getMessage());
      }
   }
}

输出

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

原始图片

基本阈值教程

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

阈值二进制

基本阈值教程

阈值二进制反转

基本阈值教程

脱粒零

基本阈值教程