一、阈值
当像素值高于阈值时,我们给这个像素赋予一个新值,否则给他赋予另一个值。
二、 固定阈值/简单阈值
double Threshold(IInputArray src, IOutputArray dst, double threshold, double maxValue, ThresholdType thresholdType)
Threshold方法的第五个参数类型如下
THRESH_BINARY
THRESH_BINARY_INV
THRESH_TRUNC
THRESH_TOZERO
THRESH_TOZERO_INV




// 1. 加载原图1
var image1 = new Image<Bgr, byte>("bird1.png");
PreviewImage1 = new WriteableBitmap(Bitmap2BitmapImage(image1.Bitmap));
// 2. 加载灰度图
var image2 = new Image<Gray, byte>("bird1.png");
PreviewImage2 = new WriteableBitmap(Bitmap2BitmapImage(image2.Bitmap));
var image3 = new Image<Gray, byte>(image1.Size);
CvInvoke.Threshold(image2,image3,100,255,ThresholdType.Binary);
PreviewImage3 = new WriteableBitmap(Bitmap2BitmapImage(image3.Bitmap));
CvInvoke.Threshold(image2, image3, 100, 255, ThresholdType.BinaryInv);
PreviewImage4 = new WriteableBitmap(Bitmap2BitmapImage(image3.Bitmap));
CvInvoke.Threshold(image2, image3, 100, 255, ThresholdType.Trunc);
PreviewImage5 = new WriteableBitmap(Bitmap2BitmapImage(image3.Bitmap));
CvInvoke.Threshold(image2, image3, 100, 255, ThresholdType.ToZero);
PreviewImage6 = new WriteableBitmap(Bitmap2BitmapImage(image3.Bitmap));
CvInvoke.Threshold(image2, image3, 100, 255, ThresholdType.ToZeroInv);
PreviewImage7 = new WriteableBitmap(Bitmap2BitmapImage(image3.Bitmap));
CvInvoke.Threshold(image2, image3, 100, 255, ThresholdType.Mask);
PreviewImage8 = new WriteableBitmap(Bitmap2BitmapImage(image3.Bitmap));
CvInvoke.Threshold(image2, image3, 0, 255, ThresholdType.Otsu);
PreviewImage9 = new WriteableBitmap(Bitmap2BitmapImage(image3.Bitmap));
三、自适应阈值
在前面部分我们使用的是全局阈值,整幅图采用同一个数当做阈值。但这种方法并不适用于所有情况,尤其是当同一副图像上的不同部分的具有不同的亮度的。这时我们可以采用自适应阈值。此时的阈值是根据图像上的每一个小区域计算与其对应的阈值。因此在同一副图像上的不同的区域采用的し不同 阈值,从而し我们能在亮度不同时得到更好的结果。
void AdaptiveThreshold(IInputArray src, IOutputArray dst, double maxValue, AdaptiveThresholdType adaptiveType, ThresholdType thresholdType, int blockSize, double param1)
adaptiveType 类型,二选一 MeanC/GaussianC
ThresholdType 类型,二选一 BinaryInv/Binary


// 1. 加载原图1
var image1 = new Image<Bgr, byte>("bird1.png");
PreviewImage1 = new WriteableBitmap(Bitmap2BitmapImage(image1.Bitmap));
// 2. 加载灰度图
var image2 = new Image<Gray, byte>("bird1.png");
PreviewImage2 = new WriteableBitmap(Bitmap2BitmapImage(image2.Bitmap));
// 3
var image3 = new Image<Gray, byte>(image1.Size);
CvInvoke.AdaptiveThreshold(image2, image3, 255, AdaptiveThresholdType.MeanC, ThresholdType.Binary, 11, 5);
PreviewImage4 = new WriteableBitmap(Bitmap2BitmapImage(image3.Bitmap));
CvInvoke.AdaptiveThreshold(image2, image3, 255, AdaptiveThresholdType.MeanC, ThresholdType.BinaryInv, 11, 5);
PreviewImage5 = new WriteableBitmap(Bitmap2BitmapImage(image3.Bitmap));
CvInvoke.AdaptiveThreshold(image2, image3, 255, AdaptiveThresholdType.GaussianC, ThresholdType.Binary, 11, 5);
PreviewImage7 = new WriteableBitmap(Bitmap2BitmapImage(image3.Bitmap));
CvInvoke.AdaptiveThreshold(image2, image3, 255, AdaptiveThresholdType.GaussianC, ThresholdType.BinaryInv, 11, 5);
PreviewImage8 = new WriteableBitmap(Bitmap2BitmapImage(image3.Bitmap));

本文详细介绍了图像处理中的阈值技术,包括固定阈值(二值化、反向二值化、截断、置零、置零反向)和自适应阈值(均值和高斯阈值),并提供了C#代码示例,展示了如何应用这些技术进行图像二值化,以适应不同亮度区域的图像处理需求。
&spm=1001.2101.3001.5002&articleId=121666002&d=1&t=3&u=91f6729ed10347a785fd079f8bf96e35)
3386

被折叠的 条评论
为什么被折叠?



