table of Contents
Knowledge reserve
Before learning custom linear filtering, you need to understand the convolution related concept
image convolution
Common operators (convolution kernel)
Robert operator
The most common operators
Sobel operator
It is often used in edge detection. It is an important step in canny edge detection. The gradient is obtained through the Sobel operator.
Laplance operator
The center of the sharpened mask is 5, and the center of the Laplacian is 4
Laplacian and Sobel operators are used to find the gradient and find the edge of the image
Custom linear filter
It’s actually the same as the mask, but the core used is different
API
void filter2D(
InputArray src,
OutputArray dst,
int ddepth,
InputArray kernel,
Point anchor = Point(-1,-1),
double delta = 0,
int borderType = BORDER_DEFAULT
);
(1) src of InputArray type, input image.
(2) dst of the OutputArray type, the output image, the size of the image, the number of channels are the same as the input image.
(3) ddepth of int type, the required depth of the target image.
(4) InputArray type of kernel, the convolution kernel (or more precisely related kernel) is a single-channel floating-point matrix; if you want to apply different kernels to different channels, please use split to split the image into different Color planes and process them separately. .
(5) The anchor of the Point type represents the anchor point (that is, the point to be smoothed). Note that it has the default value Point (-1, -1). If the coordinate of this point is negative, it means that the center of the core is the anchor point, so the default value Point(-1,-1) means that the anchor point is at the center of the core. .
(6) The delta of the double type is an optional value added to these pixels before storing the filtered pixels in the dst. To say a bit professional is actually to add a value delta to the selected pixel value.
(7) The borderType of int type is used to infer a certain border mode of the external pixels of the image. There is a default value of BORDER_DEFAULT.
Generally speaking, the third parameter uses -1 and the
fifth parameter uses Point(-1,-1) to default the anchor point as the center. The
sixth 0 and the
seventh do not write, use the default
Code
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat src, dstx, dsty;
src = imread("C:/Users/86176/Pictures/pics/camera.tiff");
if (!src.data)
{
cout << "could not load image..." << endl;
return -1;
}
imshow("show image", src);
namedWindow("x img", CV_WINDOW_AUTOSIZE);
namedWindow("y img", CV_WINDOW_AUTOSIZE);
//卷积核可自己定义,这里定义的是Robert算子
Mat kernel_x = (Mat_<int>(2, 2) << 1, 0, 0,-1);
Mat kernel_y = (Mat_<int>(2, 2) << -1, 0, 0, 1);
filter2D(src, dstx, -1, kernel_x, Point(-1,-1), 0.0);
imshow("x img", dstx);
filter2D(src, dsty, -1, kernel_y, Point(-1, -1), 0.0);
imshow("y img", dsty);
waitKey(0);
return 0;
}
effect
The x and y directions pay more attention to the differences in their own directions respectively