OpenCV Learning (Edge extraction using filters)

Source: Internet
Author: User

OpenCV Learning (Edge extraction using filters)

With a low-pass filter, we can smooth the image and, conversely, use a high-pass filter to extract the edges of the image.

Sobel Filter

The Sobel filter is a directional filter that acts in either the X or Y direction.
The theoretical introduction of this filter can be consulted:

Https://en.wikipedia.org/wiki/Sobel_operator

The function prototypes are as follows:

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 );

This filter combines Gaussian smoothing filtering and differential operation, which is not very sensitive to noise and is a very common edge detection operator. DX and DY are the order of difference operations in the X and Y directions.
If 1 order difference is obtained for X direction, this parameter is set to 1, 0. The Y direction is set to 0, 1.

Ksize is the size of the kernel and can only be 1, 3, 5, 7. Ksize = 1 O'Clock the core is 1 rows, 3 columns, or 3 rows and 1 columns, then the Gaussian smoothing step is gone.

Ksize can also be set to Cv_scharr (-1), which is a hidden feature. This is actually calculated as a 3 * 3 ScHARR filter.

Scale is the scaling of the computed result, and the delta is the translation of the calculated result.

Here is an example of the original test image as follows:

Here we have edge detection in the X direction. Because the result of the calculation will be negative, there are also some indenting and panning operations. The code is as follows:

cv::Sobel(image, result, CV_8U, 1, 0, 3, 0.5, 128);

The image is processed as follows:

You can see that the positive and negative edges in the x direction of the image are detected, and the values of the positive and negative edges are different, which is advantageous when we need to extract a particular edge.

If we do not need to distinguish between positive and negative edges, we can take an absolute value operation. This is similar to the following.

cv::Sobel(image, result, CV_16S, 1, 0, 3);result = abs(result);result.convertTo(result, CV_8U);

It is important to note that when the image is a cv_8u type, the result type cannot be cv_8s. This can only be turned into cv_16s and then converted to the cv_8u we need.

The gradient information of the image is obtained by Sobel filtering the X-direction and Y-direction of the image respectively. But this gradient information is in the form of (X, Y). Sometimes we need to use (ρ,θ) Form. You can then use the Cv::carttopolar function to convert. Here's an example:

cv::Sobel(image, resultX, CV_32F, 1, 0, 3);cv::Sobel(image, resultY, CV_32F, 0, 1, 3);cv::Mat norm, dir;cv::cartToPolar(resultX, resultY, norm, dir);

ScHARR Filter

The ScHARR operator is more accurate than the Sobel operator in the calculation of the gradient direction of the image. The usage and Sobel operators are similar.

The specific theory can also be consulted:

Https://en.wikipedia.org/wiki/Sobel_operator

The following is the function prototype of the ScHARR operator.

void Scharr( InputArray src, OutputArray dst, int ddepth,                      int dx, int dy, double scale=1, double delta=0,                      int borderType=BORDER_DEFAULT );

To put it simply:

Scharr(src, dst, ddepth, dx, dy, scale, delta, borderType);

is equivalent to:

Sobel(src, dst, ddepth, dx, dy, CV_SCHARR, scale, delta, borderType);

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

OpenCV Learning (Edge extraction using filters)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.