c# OpenCvSharp Cv2.Threshold() and Cv2.AdaptiveThreshold parameter description

1. Function parameter description of Cv2.Threshold() binarization

Cv2.Threshold() is a function used for image binarization. Specifically, it compares the gray value of each pixel in the image with a threshold. Pixels greater than the threshold will be assigned the maximum gray value (i.e. 255), and pixels less than the threshold will be assigned the value The minimum gray value (i.e. 0). This divides all pixels in the image into two categories: black and white.

function call

  public static double Threshold(InputArray src, OutputArray dst, double thresh, double maxval, ThresholdTypes type)

Parameter Description

 The type parameter is used to specify the type of threshold processing

 Code demonstration

using OpenCvSharp;

public void ThresholdExample()
{
    // 读取图像
    Mat src = Cv2.ImRead("image.jpg", ImreadModes.GrayScale);

    // 应用阈值处理
    Mat dst = new Mat();
    Cv2.Threshold(src, dst, 120, 255, ThresholdTypes.Binary);

    // 显示结果
    Cv2.ImShow("Thresholded Image", dst);
    Cv2.WaitKey(0);
    Cv2.DestroyAllWindows();
}

 This sample code uses Cv2.Thresholda function to threshold a grayscale image. The parameters of the function include input image, output image, threshold, maximum pixel value, and threshold type. In this example, the threshold is 120, the maximum pixel value is 255, and the threshold type is binary threshold.

2. Cv2.AdaptiveThreshold adaptive threshold processing.

The Cv2.AdaptiveThreshold function is a function used in OpenCV for adaptive threshold processing.

Adaptive thresholding is a method of image binarization. Different from global thresholding, it does not use a fixed threshold to binarize the entire image, but binarizes it based on local areas of the image. This method can effectively handle images with uneven lighting.

function call

  public static void AdaptiveThreshold(InputArray src, OutputArray dst, double maxValue, AdaptiveThresholdTypes adaptiveMethod, ThresholdTypes thresholdType, int blockSize, double c)

Parameter Description

 Code demonstration

using OpenCvSharp;

class Program
{
    static void Main(string[] args)
    {
        // 读入图像并转为灰度图
        Mat img = Cv2.ImRead("image.jpg", ImreadModes.GrayScale);

        // 使用自适应阈值处理图像
        Mat thresh = new Mat();
        Cv2.AdaptiveThreshold(img, thresh, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 11, 2);

        // 显示结果
        Cv2.ImShow("Adaptive Threshold", thresh);
        Cv2.WaitKey(0);
        Cv2.DestroyAllWindows();
    }
}

Guess you like

Origin blog.csdn.net/hb_ljj/article/details/135353551