卷积:是核函数kerenl对图像中每个像素进行操作。kernel是固定矩阵,矩阵中心点成为锚点。 用一个模板和一幅图像进行卷积,对于图像上的一个点,让模板的原点和该点重合,然后模板上的点和图像上对应的点相乘,然后各点的积相加,就得到了该点的卷积值。对图像上的每个点都这样处理。由于大多数模板都是对称的,所以模板不旋转。卷积是一种积分运算,用来求两个曲线重叠区域面积。可以看作加权求和,可以用来消除噪声、特征增强。 H ( x , y ) = ∑ i = 0 M i − 1 ∑ j = 0 M j − 1 I ( x + i − a i , y + j − a j ) K ( i , j ) H(x,y)=\sum_{i=0}^{M_i-1}\sum_{j=0}^{M_j-1}I(x+i-a_i,y+j-a_j)K(i,j) H(x,y)=i=0∑Mi−1j=0∑Mj−1I(x+i−ai,y+j−aj)K(i,j) 假设想知道图像中某个特定位置的结果值。卷积的值以下列方式计算: 1) 将核的锚点(中心点)放在要计算像素上,卷积核剩余的部分对应在图像相应的像素上。 2) 用卷积核中的系数和图像中相应的像素值相乘,并求和。 3) 将最终结果赋值给锚点对应的像素。 4) 通过将核在整个图像滑动,重复以上计算过程直到处理完所有的像素。
常见的算子:
Robert算子、Sobel算子、拉普拉斯算子 // 摘要: // Convolves an image with the kernel // // 参数: // src: // The source image // // dst: // The destination image. It will have the same size and the same number of channels // as src // // ddepth: // The desired depth of the destination image. If it is negative, it will be the // same as src.depth() // // kernel: // Convolution kernel (or rather a correlation kernel), a single-channel floating // point matrix. If you want to apply different kernels to different channels, split // the image into separate color planes using split() and process them individually // // anchor: // The anchor of the kernel that indicates the relative position of a filtered point // within the kernel. The anchor should lie within the kernel. The special default // value (-1,-1) means that the anchor is at the kernel center // // delta: // The optional value added to the filtered pixels before storing them in dst // // borderType: // The pixel extrapolation method public static void Filter2D(InputArray src, OutputArray dst, MatType ddepth, InputArray kernel, Point? anchor = null, double delta = 0, BorderTypes borderType = BorderTypes.Reflect101);实现:
// Creating kernel matrix var kernel1 = new int[] { -1, 0, 0,-1}; Mat kernel = new Mat(2, 2,MatType.CV_16U,kernel1);//MatType.CV_16U 不确定??? Cv2.Filter2D(srt,dst,-1,kernel);参考: opencv学习(七)之图像卷积运算函数filter2D()