Opencv3 C++ VS2017 Study Notes 12 Convolution and Convolution Operator

  • Convolution is a mathematical concept. It is an operation in image processing. It is the operation of the kernel (filter, convolution kernel, operator) on each pixel of the image

    • The kernel is essentially a small matrix of fixed size, the center point is the anchor point, which is equivalent to anchoring in the ocean of the picture---->operation, that is, anchor point
    • How convolution works
      • The kernel is placed on the pixel array, and the product of the coverage pixels around the anchor point is calculated, and then added up, replacing the pixel value under the anchor point coverage, that is, convolution processing
      • Sum = 8x1+6x1+6x1+2x1+8x1+6x1+2x1+2x1+8x1

        New pixel = sum / (m*n)

    • Convolution operation has two cores, one is the design of the kernel, and the other is the way the operator traverses the pixels

      • There are some classic operators in general, such as:

        •  

          Robert operator
        • Sobel operator
           
        • Laplance operator
      • Custom operator
        • Is to create a small matrix. I learned that you can use Mat to create it.
          int main(int argc, char ** argv)
          {
          	Mat src, gray, bin, dst;
          	src = imread("C:\\Users\\xujin\\Desktop\\test1.JPG");
          	if (!src.data)
          	{
          		cout << "no image";
          		return -1;
          	}
          	namedWindow("src_image", WINDOW_AUTOSIZE);
          	imshow("src_image", src);
          	Mat dst_x, dst_y;
          	Mat dstSobel_x, dstSobel_y, dstLap;
          	src.copyTo(dst_x);
          	src.copyTo(dst_y);
          	src.copyTo(dstSobel_x);
          	src.copyTo(dstSobel_y);
          	src.copyTo(dstLap);
          	Mat kernel_x = (Mat_<int>(2, 2) << 1, 0, 0, -1);
          	Mat kernel_y = (Mat_<int>(2, 2) << 0,-1,-1,0);
          	Mat kernelSobel_x = (Mat_<int>(3, 3) << -1, 0, -1, -2, 0, -2, -1, 0, -1);
          	Mat kernelSobel_y = (Mat_<int>(3, 3) << -1,-2,-1, 0, 0, 0, 1, 2, 1);
          	Mat kernelLap = (Mat_<int>(2, 2) << 0, -1, 0, -1, -4, -1, 0, -1, 0);
          	filter2D(src, dst_x,-1, kernel_x, Point(-1, -1));
          	filter2D(src, dst_y, -1, kernel_x, Point(-1, -1));  //filter2D()是进行2D卷积操作的函数, 卷积核是一个小型矩阵,卷积核是设计核心
          	filter2D(src, dst_y, -1, kernelSobel_x, Point(-1, -1));
          	filter2D(src, dst_y, -1, kernelSobel_y, Point(-1, -1));
          	filter2D(src, dst_y, -1, kernelLap, Point(-1, -1));
          	namedWindow("dst_x_image", WINDOW_AUTOSIZE);
          	imshow("dst_x_image", dst_x);
          	namedWindow("dst_y_image", WINDOW_AUTOSIZE);
          	imshow("dst_y_image", dst_y);
          	namedWindow("dstSobel_x_image", WINDOW_AUTOSIZE);
          	imshow("dstSobel_x_image", dstSobel_x);
          	namedWindow("dstSobel_y_image", WINDOW_AUTOSIZE);
          	imshow("dstSobel_y_image", dstSobel_y);
          	namedWindow("dstLap_image", WINDOW_AUTOSIZE);
          	imshow("dstLap_image", dstLap);
          
          
          	waitKey(0);
          	return 0;
          }
          
          • 例如Mat kernel = (Mat_<double>(3,3)<<-1,0,-1,-2,0,2,-1,0,1);
      • Convolution operation method
        • API: filter2D(src, dst, depth, kernel, anchor, delta);
          • Parameters (original image, output image, image depth, operator, anchor point, delta);

Guess you like

Origin blog.csdn.net/Mrsherlock_/article/details/104551325
Recommended