opencv - Sobel operator, Scharr edge detection operator

The basic concept of the sobel operator

sobel operator is mainly used in a discrete differential edge detection operator, and it combines the Gaussian smoothing differential derivation, gradient calculation for approximating the image intensity function.

 

Calculation process sobel operator promoter

(1), respectively, in two directions x and y derivative

derivation x-direction: the source image and the G x convoluted

y-direction derivation: the source image and G y convoluted

(2) For each point, the results of binding two or more images obtained approximately gradient

Can be simplified to:

 

Sobel function

void Sobel(InputArray src, OutputArray dst, int ddepth, int dx, int dy, int ksize = 3, double scale = 1, double delta = 0, int borderType = BORDER_DEFAULT);
  • src, input image to fill Mat type.
  • dst, output image and the source image needs to have the same size and type.
  • ddepth, the depth of the output image, meet the following requirements:

src.depth() = CV_8U, ddepth = -1 / CV_16S / CV_32F / CV_64F

src.depth() = CV_16U / CV_16S, ddepth = -1 / CV_32F / CV_64F

src.depth() = CV_32F, ddepth = -1 / CV_32F / CV_64F

src.depth() = CV_64F, ddepth = -1 / CV_64F

  • Order derivative in dx, x direction is 0 only. When the first derivative in the x-direction seeking, dx = 1, dy = 0.
  • The order number dy, y direction guide, is 0 only. If the first derivative of the direction and y, dx = 0, dy = 1.
  • ksize, the default value is 3, represents a sobel size of the nucleus, 1,3,5,7 must be fetched.
  • scale,计算导数值的缩放因子,默认值为 1,表示默认情况下不进行缩放。
  • delta,输出梯度 = scale * G + delta。
  • borderType,Gx 和 Gy 都有一定大小,边缘会处理不到,需要进行边缘扩展。这个参数指定边缘扩充类型。

 一般情况下,都是使用 ksize×ksize 内核来计算导数的,然而,当 ksize = 1 时,会使用 3×1、1×3 的内核。这种情况下,并没有进行高斯平滑操作。

代码示例:

#include<opencv.hpp>
using namespace cv;
int main() {
    Mat src = imread("C:/Users/齐明洋/Desktop/证件照/7.jpg");
    GaussianBlur(src, src, Size(3, 3), 0, 0);
    imshow("src", src);

    Mat gray, x_img, y_img, sobel_img;
    cvtColor(src, gray, COLOR_BGR2GRAY);
    Sobel(gray, x_img, -1, 1, 0, 3);
    imshow("x_img", x_img);
    Sobel(gray, y_img, -1, 0, 1, 3);
    imshow("y_img", y_img);

    addWeighted(x_img, 1, y_img, 1, 0, sobel_img);
//https://www.cnblogs.com/bjxqmy/p/11986135.html imshow(
"sobel_img", sobel_img); waitKey(0); }

效果演示:

 

补充说明:当内核大小为 3×3 时,sobel 内核可能产生比较明显的误差,毕竟 sobel 算子只是求取了导数的近似值而已。为解决这一问题,OpenCV 提供了 Scharr 函数,但该函数仅作用于 3×3 的内核。该函数的运算与 Sobel 函数一样快,但结果更精确,其内核为

 

借鉴博客:https://www.cnblogs.com/wxl845235800/p/7700887.html

https://www.cnblogs.com/sevenyuan/p/7874344.html

 

Guess you like

Origin www.cnblogs.com/bjxqmy/p/12325852.html