1 图像阈值化简介
2 固定阈值
Ostu自己选择阈值(较重要)
3 自适应阈值(用于提取轮廓)
4 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using Emgu.Util;
using System.Drawing;
namespace Lesson14
{
class Program
{
static void Main(string[] args)
{
Mat srcImg = CvInvoke.Imread("2.png");
Mat grayImg = new Mat();
CvInvoke.CvtColor(srcImg, grayImg, ColorConversion.Bgr2Gray);
CvInvoke.Imshow("gray", grayImg);
//CvInvoke.Threshold(grayImg, grayImg, 100, 255, ThresholdType.BinaryInv);//固定阈值
//CvInvoke.Threshold(grayImg, grayImg, 100, 255, ThresholdType.Trunc);
//CvInvoke.Threshold(grayImg, grayImg, 100, 255, ThresholdType.ToZeroInv);
//CvInvoke.Threshold(grayImg, grayImg, 0, 255, ThresholdType.Otsu);//应用较多,效果较好
//自适应阈值
//CvInvoke.AdaptiveThreshold(grayImg, grayImg, 255, AdaptiveThresholdType.GaussianC, ThresholdType.Binary, 11, 5);
CvInvoke.AdaptiveThreshold(grayImg, grayImg, 255, AdaptiveThresholdType.MeanC, ThresholdType.BinaryInv, 11, 5);
CvInvoke.Imshow("threshold", grayImg);
CvInvoke.WaitKey(0);
}
}
}