Opencv (C++) learning series---Sobel Sobel operator edge detection

Table of contents

【1】Algorithm introduction

【2】Introduction to operator parameters

【3】Complete code


【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:

6a0c3bdbe6704354a2cf6da18b8a0f77.png

       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.

de7e41baabf84dfbac42fa2ad6510297.png

 For the specific algorithm process, please refer to this article:

https://blog.csdn.net/great_yzl/article/details/119709699?ops_request_misc=&request_id=&biz_id=102&utm_term=opencv%20sobel%E8%BE%B9%E7%BC%98%E6%A3%80%E6% B5%8B&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-4-119709699.nonecase&spm=1018.2226.3001.4187 Contents 1. Basic theory 2. Actual combat 1. Differentiate in x direction 2. In y direction Differential 3. Linear mixed total code reference materials 1. Basic theory Sobel operator: a discrete differential operator used for edge detection. Gradient formula: For an image, it is discrete, so the minimum value of h can only be 1, then this means that the gradient of a pixel position in the image (take the x direction as an example) is equal to two pixels left and right of it The difference in pixels of the points is divided by 2. Example: Suppose there is a row of pixels distributed like this: 123 155 173 Then, the gradient in the x direction of the pixel position with a pixel value of 155 is (173 - 123)/2 = 25Prewit... https://blog.csdn.net/ great_yzl/article/details/119709699?ops_request_misc=&request_id=&biz_id=102&utm_term=opencv%20sobel%E8%BE%B9%E7%BC%98%E6%A3%80%E6%B5%8B&utm_medium=distribute.pc_search_result.none task-blog-2~all~sobaiduweb~default-4-119709699.nonecase&spm=1018.2226.3001.4187

【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:

e0540586ea55488791819b954c8b1c99.png

Guess you like

Origin blog.csdn.net/qiaodahua/article/details/128082272