Table of contents
【2】Introduction to operator parameters
【1】Algorithm introduction
The Sobel operator is a linear filter used for edge detection. If the image is regarded as a two-dimensional function, the Sobel operator is the speed at which the image changes in the vertical and horizontal directions (that is, the gradient). The Sobel operator performs the difference of pixel values in the horizontal and vertical directions, and can obtain the approximate value of the image gradient, and can reduce the influence of noise when performing calculations around pixels.
The corresponding kernel function is:
The Sobel operator detection method is better for image processing with gray scale changes and more noise. The sobel operator is not very accurate for edge positioning, and the edge of the image is more than one pixel; when the accuracy requirement is not very high, it is a relatively Commonly used edge detection methods. For those with high requirements for small edge detection, the scharr operator can be used, which is improved on the basis of the sobel operator, and improves the accuracy of edge detection without affecting the detection speed.
The convolution kernel sizes of the sobel operator and the scharr operator are the same. This means that their workload is calculated to be the same.
For the specific algorithm process, please refer to this article:
【2】Introduction to operator parameters
cv::Sobel(image, // 输入的图像可以是三通道,也可是单通道
sobel, // 输出
image_depth, // 图像类型
xorder,yorder, // 内核规格
kernel_size, // 正方形内核的尺寸
alpha, beta); // 比例和偏移量
Parameter introduction:
The pixel type of the output image is selectable: unsigned char, signed integer or floating point. For the sobel operator, the generally selected type is CV_16S. If the result exceeds the range of the pixel value range, the saturation operation will be performed. Before the final image is generated, the result can be scaled (multiplied) by alpha times, and the offset beta can be added.
Each Sobel mask is a derivative in one direction, so two parameters are used to indicate the kernel to be applied, that is, the order of the derivative in the x direction and the y direction. For example, if xorder and yorder are 1 and 0 respectively, you get a horizontal Sobel kernel; if they are 0 and 1 respectively, you get a vertical kernel. The size of the kernel can also be larger than 3×3. Available sizes are 1, 3, 5 and 7. Kernel size is 1, which represents a one-dimensional Sobel filter (1×3 or 3×1).
【3】Complete code
#include<opencv2\opencv.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\highgui\highgui.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
//载入原始图
Mat src = imread("E:\\乔大花进度\\11-28\\边缘检测\\3.jpg");
//【1】创建abs_grad_x,abs_grad_y,dst矩阵;
Mat grad_x, grad_y;
Mat abs_grad_x,abs_grad_y,dst;
//【2】显示原始图
imshow("原始图",src);
//【3】求x方向梯度
Sobel(src,grad_x,CV_16S,1,0,3);
convertScaleAbs(grad_x,abs_grad_x);
imshow("x方向sobel",abs_grad_x);
Scharr(src, grad_y, CV_16S, 0, 1, 9);
convertScaleAbs(grad_y,abs_grad_y);
imshow("y方向sobel",abs_grad_y);
//【5】合并梯度(近似)
addWeighted(abs_grad_x,0.5,abs_grad_y,0.5,0,dst);
cout << "Sobel输出图像的类型为:" << dst.type() << endl;
cout << "Sobel输出图像的通道数" << dst.channels() << endl;
imshow("整体效果",dst);
waitKey(0);
system("pause");
destroyAllWindows();
return 0;
}
operation result: