美文网首页
C#:图像二值化

C#:图像二值化

作者: 大龙10 | 来源:发表于2023-08-29 12:56 被阅读0次

一、基本阈值化

  • OpenCV中基本阈值化操作的函数为threshold,该函数的定义如下:

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

  • 参数
    • src,输入图像;
    • dst,输出图像(返回值)。
    • thresh,设定的阈值;
    • maxval
      参数type为THRESH_BINARY或者THRESH_BINARY_INV时的最大值;
    • type
      阈值类型,由ThresholdTypes 定义;
      THRESH_BINARY后,阈值化后输入图像中像素值高于阈值的被设置为最大值maxval,低于阈值的设置为0
      THRESH_BINARY_INV,阈值化后输入图像中像素值高于阈值的被设置为像素值0,低于阈值的设置为maxval
  • 返回结果:
    the computed threshold value when type == OTSU

二、自适应阈值化

  • 基本阈值化需要读者手动设置阈值进行阈值化操作,阈值的设置需要尝试选出最佳值,自适应阈值化则可以变化阈值完成阈值化操作,OpenCV中提供了自适应阈值化操作的函数adaptiveThreshold

三、程序

二值化
       private void uiButton6_Click(object sender, EventArgs e)
        {
            Mat result = src_img.Clone();
            Mat imgLab = new Mat();
            Cv2.CvtColor(src_img, imgLab, ColorConversionCodes.BGR2Lab);
            Mat[] labArray = Cv2.Split(imgLab); //L, a, b
            Mat blur = new Mat();
            Cv2. GaussianBlur(labArray[0], blur, new OpenCvSharp.Size(3, 3), 0); //b通道
            Mat m6 = new Mat();

            string s_yuzhi = uiTextBox2.Text;
            //不用大律法二值化;大律法二值的不好

            Cv2.Threshold(blur, m6, s_yuzhi.ToInt(), 255, ThresholdTypes.BinaryInv);

             m6.CopyTo(dst);
             pictureBox1.Image = m6.ToBitmap();

        }

四、资料

知乎:
https://zhuanlan.zhihu.com/p/473304766

相关文章

网友评论

      本文标题:C#:图像二值化

      本文链接:https://www.haomeiwen.com/subject/srjgmdtx.html