别人没那么重要,我也没那么重要,好好活着,把能做的小事做好,够不到的东西就放弃,承认就好。做一个心情好能睡着的人,你所有事情都会在正轨上。

C#OpenCV算子-滤波

5.1 均值滤波

函数原型:

void Blur(InputArray src, OutputArray dst, Size ksize, Point? anchor = null, BorderTypes borderType = BorderTypes.Reflect101)
/*
参数:
    src: 输入图像.
    dst: 输出图像. 
    ksize:滤波核的大小.
    Point类型的anchor,表示锚点(即被平滑的那个点),注意他有默认值Point(-1,-1)。如果这个点坐标是负值的话,就表示取核的中心为锚点,所以默认值Point(-1,-1)表示这个锚点在核的中心。
    borderType,用于推断图像外部像素的某种边界模式。
功能:
    对图片进行均值滤波处理。
*/

例子:

static void Main(string[] args)
{
    Mat src1 = new Mat(@"C:\Users\Administrator\Desktop\1.jpg");
    Mat dst = new Mat();
    Cv2.Blur(src1, dst, new Size(11, 11));
    Cv2.ImShow("test", dst);
    Cv2.WaitKey();
}

实验样例:

Src1

dst

 

 


5.2 中值滤波

函数原型:

void MedianBlur(InputArray src, OutputArray dst, int ksize)
/*
参数:
    src: 输入图像.
    dst: 输出图像. 
    ksize:滤波核的大小.
功能:
    对图片进行中值滤波处理。
*/

例子:

static void Main(string[] args)
{
    Mat src1 = new Mat(@"C:\Users\Administrator\Desktop\1.jpg");
    Mat dst = new Mat();
    Cv2.MedianBlur(src1, dst, 5);
    Cv2.ImShow("test", dst);
    Cv2.WaitKey();
}

实验样例:

Src1

dst

 

 


5.3 高斯滤波

函数原型:

void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY = 0, BorderTypes borderType = BorderTypes.Reflect101)
/*
参数:
    src: 输入图像.
    dst: 输出图像. 
    ksize:滤波核的大小.
    sigmaX,表示高斯核函数在X方向的的标准偏差.
    sigmaY,表示高斯核函数在Y方向的的标准偏差。若sigmaY为零,就将它设为sigmaX,如果sigmaX和sigmaY都是0,那么就由ksize.width和ksize.height计算出来
    borderType,用于推断图像外部像素的某种边界模式。
功能:
    对图片进行高斯滤波处理。
*/

例子:

static void Main(string[] args)
{
    Mat src1 = new Mat(@"C:\Users\Administrator\Desktop\1.jpg");
    Mat dst = new Mat();
    Cv2.GaussianBlur(src1, dst, new Size(5, 5), 3);
    Cv2.ImShow("test", dst);
    Cv2.WaitKey();
}

实验样例:

Src1

dst

 

 


5.4 形态学处理

函数原型:

void MorphologyEx(InputArray src, OutputArray dst, MorphTypes op, InputArray? element, Point? anchor = null, int iterations = 1, BorderTypes borderType = BorderTypes.Constant, Scalar? borderValue = null)
/*
参数:
    src: 输入图像.
    dst: 输出图像. 
    op:形态操作类型(包括:开运算、闭运算、形态梯度、顶帽、黑帽等)
    element:卷积核的大小.
    Point类型的anchor,表示锚点(即被平滑的那个点),注意他有默认值Point(-1,-1)。如果这个点坐标是负值的话,就表示取核的中心为锚点,所以默认值Point(-1,-1)表示这个锚点在核的中心。
    Iteration:形态学操作次数.
    borderType:用于推断图像外部像素的某种边界模式.
    borderValue:边界值.
功能:
    对图片进行指定的形态学处理
*/

例子:

static void Main(string[] args)
{
    Mat src1 = new Mat(@"C:\Users\Administrator\Desktop\1.jpg");
    Mat dst = new Mat();
    int MorphSize = 3;
    Mat element = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(2 * MorphSize + 1, 2 * MorphSize + 1), new    OpenCvSharp.Point(-1, -1));
    Cv2.MorphologyEx(src1, dst, MorphTypes.Dilate, element);   //膨胀处理为例子
    Cv2.ImShow("test", dst);
    Cv2.WaitKey();
}

实验样例:

Src1

dst(膨胀)

 

 


5.5 阈值化

函数原型

double Threshold(InputArray src, OutputArray dst, double thresh, double maxval, ThresholdTypes type)
/*
参数:
    src: 输入图像.
    dst: 输出图像. 
    thresh:阈值
    maxval:要与阈值二进制和阈值binary_inv阈值类型一起使用的最大值。.
    type:阈值类型(包括:阈值二值化、反二值化阈值、截断阈值、阈值取零、阈值反取零等)
功能:
    对图像进行指定的阈值化处理。
*/

例子:

static void Main(string[] args)
{
    Mat src1 = new Mat(@"C:\Users\Administrator\Desktop\1.jpg");
    Mat dst = new Mat();
    Cv2.Threshold(src1, dst, 125, 255, ThresholdTypes.Binary);   //二值化为例子
    Cv2.ImShow("test", dst);
    Cv2.WaitKey();
}

实验样例:

Src1

dst(二值化)

 

 

 

 

/*-------------------------------------------------------------------------------------------------------

笔者说明:

  该笔记来源于本人学习OpenCvSharp时的资料,

  分享出来只是为了供大家学习,并且为了自己以后想要用的时候方便寻找。

时间:2023年3月14日

------------------------------------------------------------------------------------------------------------*/

 

posted @ 2023-03-14 15:41  一路狂奔的乌龟  阅读(221)  评论(0)    收藏  举报
返回顶部