c# OpenCvSharp 二值化 Threshold Binary BinaryInv Trunc Tozero TozeroInv

c# OpenCvSharp 二值化 Threshold Binary BinaryInv Trunc Tozero TozeroInv

using OpenCvSharp;
using OpenCvSharp.Extensions;
using DlibDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace app
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 二值化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnThreshold_Click(object sender, EventArgs e)
        {
            Mat src = Cv2.ImRead(@"img\jzh.png", ImreadModes.Grayscale);
            Cv2.ImShow("src", src);

            // 图像二值化

            // 1#
            // Binary(二进制阈值化)
            // 二进制阈值化是指将大于阀值的像素点设置为最大值,而小于阀值的像素点设置为0,即:
            // value = value > thresh ? maxval : 0
            Mat binaryMat0 = new Mat();
            Cv2.Threshold(src, binaryMat0, 100, 255, ThresholdTypes.Binary);
            Cv2.ImShow("binaryImg0", binaryMat0);

            // 2#
            // BinaryInv(反向二进制阈值化)
            // 与二进制阈值化正好相反,反向二进制阈值化是在像素点的值大于阀值时像素点设置为0,反之则设置为最大值。
            // value = value > thresh ? 0 : maxval
            Mat binaryMat1 = new Mat();
            Cv2.Threshold(src, binaryMat1, 100, 255, ThresholdTypes.BinaryInv);
            Cv2.ImShow("binaryImg1", binaryMat1);

            // 3#
            // Trunc(截断阈值化)
            // 截断阈值化是指当像素点的值大于阀值时,像素点的值设置为阀值,反之则保留像素值本身。
            // value = value > thresh ? thresh : value
            Mat binaryMat2 = new Mat();
            Cv2.Threshold(src, binaryMat2, 100, 255, ThresholdTypes.Trunc);
            Cv2.ImShow("binaryMat2", binaryMat2);

            // 4#
            // Tozero(超阈值归零化)
            // 超阈值归零化是指当像素点的值大于阀值时,像素点保留原值,反之则设置为0。
            // value = value > thresh ? value : 0
            Mat binaryMat3 = new Mat();
            Cv2.Threshold(src, binaryMat3, 100, 255, ThresholdTypes.Tozero);
            Cv2.ImShow("binaryMat3", binaryMat3);

            // 5#
            // TozeroInv(低于阈值归零化)
            // 与超阈值归零化相反,低于阈值归零化是指当像素点的值大于阀值时,像素点值被设置为0,反之则保留像素点原值。
            // value = value > thresh ? 0 : value
            Mat binaryMat4 = new Mat();
            Cv2.Threshold(src, binaryMat4, 100, 255, ThresholdTypes.TozeroInv);
            Cv2.ImShow("binaryMat4", binaryMat4);
        }
    }
}

 

发表回复

您的电子邮箱地址不会被公开。