Edge Detection Based on OpenCv-Laplacian operator, scharr filter, and laplacianscharr
Laplacian Operator Edge Detection
Original graph
Grayscale chart
Scharr filter Edge Detection
Original graph
X direction
Y direction
Scharr merge chart
1. Introduction to Laplacian Operators
Laplacian operator is a second-order differential operator in the n-dimensional Euclidean space. It is defined as the divergence p of gradid. You can use an operation template to calculate this theorem.
If f is a second-order real function, the Laplace operator of f is defined:
(1) The Laplace operator of f is also the sum of all non-mixed second-order partial derivatives in the Cartesian coordinate system:
(2) As a second-order differential operator, the Laplace operator maps C functions to C functions, for k ≥ 2. Expression (1) (or (2) defines an operator delta: C (R) → C (R), or, more generally, defines an operator delta: C (Ω) → C (Ω), for any open set Ω.
For a step edge, the derivative has zero crossover at the edge point, that is, the second-order derivative of the edge points has an abnormal number. Based on this, for each pixel of the numeric image {f (I, j)}, the sum of its second-order difference on the X axis and Y axis is expressed
2. Operation Template
The Laplace operator of the function is also the trace of the matrix of the function. It can be proved that it has the same direction, that is, it has nothing to do with the direction of the coordinate axis, and the gradient result remains unchanged after the coordinate axis is rotated. If the neighbor system is a 4-neighbor system, the Laplacian operator template is:
If the neighbor system is an 8-neighbor system, the Laplacian operator template is:
As mentioned above, the Laplacian operator is sensitive to noise. Therefore, the image is first processed smoothly, because the smoothing operation is also performed using a template. Therefore, generally, the splitting algorithm combines the Laplacian operator and the smoothing operator to generate a new template.
3. Laplacian operator code reference
# Include
# Include
# Include
Using namespace cv; int On_Laplacian () {// [1] Create a matrix Mat gray, dst, a_dst; // [2] load the original graph Mat src = imread ("F: // 5.png"); // [3] display the original image imshow ("Original Image", src); // [4] Use Gaussian filter to eliminate noise GaussianBlur (src, src, size (3, 3), 0, 0, BORDER_DEFAULT); // [5] convert to grayscale cvtColor (src, gray, COLOR_RGB2GRAY); imshow ("grayscale", gray ); // [6] use the Laplace function Laplacian (src, dst, CV_16S, 3, 1, 0, BORDER_DEFAULT); // [7] to calculate the absolute value, convert the result to 8-bit convertScaleAbs (dst, a_dst); // [8] displays imshow ("", a_dst); return 0 ;}
4. Introduction to scharr Filters
Use the Scharr filter operator to calculate the image difference in the x or y direction. In fact, its parameter variables are basically the same as those of Sobel, except for the size of the ksize kernel.
Void Scharr (InputArray src, // source image OutputArray dst, // destination image int ddepth, // int dx in image depth, // int dy in the x direction, // differential order in the y direction double scale = 1, // scaling factor double delta = 0, // delta value intborderType = BORDER_DEFAULT) // boundary Mode
The first parameter, "src" of the InputArray type, is the input image. Enter "Mat.
The second parameter is OutputArray type dst, which is the output parameter of the target image and function. It must have the same size and type as the source image.
The third parameter is ddepth of the int type, indicating the depth of the output image. It supports the combination of src. depth () and ddepth as follows:
If src. depth () = CV_8U, ddepth =-1/CV_16S/CV_32F/CV_64F
If src. depth () = CV_16U/CV_16S, ddepth =-1/CV_32F/CV_64F
If src. depth () = CV_32F, take ddepth =-1/CV_32F/CV_64F
If src. depth () = CV_64F, take ddepth =-1/CV_64F
The fourth parameter is the differential order in the x direction of dx of the int type.
The fifth parameter is the difference order in the y direction of the int type dy.
The sixth parameter is the scale of the double type. It is an optional scaling factor for the calculation of the export value. The default value is 1, indicating that scaling is not applied by default. For more information about this parameter, see getDerivKernels.
The seventh parameter, delta of the double type, indicates the delta value available before the result is saved to the target graph (the second parameter dst). The default value is 0.
The eighth parameter is the borderType of the int type. It is an old friend (Wannian is the last parameter) and the boundary mode. The default value is BORDER_DEFAULT.
5. Use the scharr filter code
Int On_Scharr () {// [1] Create the matrix Mat g_x, g_y; Mat a_x, a_y, dst; // [2] load the original graph Mat src = imread ("F: // 5.png"); // [3] display the original graph imshow ("Scharr original graph", src); // [4] calculate the gradient Scharr (src, g_x, CV_16S, 1, 0, 1, 0, BORDER_DEFAULT); convertScaleAbs (g_x, a_x); imshow ("x direction", a_x ); // [5] calculate the gradient Scharr (src, g_y, CV_16S, 1, 0, 1, 0, BORDER_DEFAULT) in the Y direction; convertScaleAbs (g_y, a_y ); imshow ("y direction", a_y); // [6] merge gradient addWeighted (a_x, 0.5, a_y, 0.5, 0, dst ); // [7] display imshow ("scharr merge Chart", dst); return 0 ;}
: