How to use python for image edge detection

王林
Release: 2023-06-03 21:22:01
forward
1956 people have browsed it

Edge Detection

Image edges refer to the set of pixels in the image that express step changes in the grayscale of the surrounding pixels of the object.

At the junction of two adjacent areas with different grayscales in the image, there must be a rapid transition of grayscales, or jumps. They correspond to the positions of the edges of each area in the image, and the edges contain rich The intrinsic information, such as direction, step properties, shape, etc., the pixels along the edge change slowly, while the pixels perpendicular to the edge direction change drastically.

Most of the information of the image is concentrated in the edge part. After the edge is determined, the segmentation of different areas is actually achieved.

Edge detection operator

Finding edges often requires the help of some edge detection operators. Some of these operators are based on first-order derivatives, and some are second-order differential operators

Roberts operator, Prewitt operator, and Sobel operator include templates in the x and y directions. Each template is only sensitive to the corresponding direction and has obvious output in this direction, while it is not sensitive to other directions. Little response to change. The following are some common first-order differential operators and their characteristics:

Operator name Features
Simple differential operator is sensitive to noise and has a certain amplification effect on noise
Roberts operator removes noise The effect is small, and the edge detection ability is better than the simple differential operator
Prewitt operator can effectively suppress the influence of noise and can detect edge points
Sobel operator The edge obtained is wider and the noise suppression effect is stronger
Canny operator Detected The edge position is accurate and the edge is narrow

1, Roberts operator

How to use python for image edge detection

2, Prewitt operator

How to use python for image edge detection

3. Sobel operator

The edges detected by the Sobel operator are more continuous than the detection results of the Roberts operator, and have better detection capabilities for image details. Better, and the Sobel edge detector introduces local averaging, which has less impact on noise and has better effect.

How to use python for image edge detection

4. Canny operator

The detection results obtained by Canny are better than those of Roberts and Sobel operators, with richer edge details and accurate edge positioning. Continuity is good, there are few false edges and the edges are all single pixel wide.
The algorithm implementation is divided into the following 4 steps:

  • Use Gaussian filter to smooth the image

  • Use the finite derivative of first-order partial derivative Difference to calculate the magnitude and direction of the gradient

  • Perform non-maximum suppression of the gradient amplitude

  • Use dual threshold algorithm to detect and connect edges

5. Laplace operator

Common second-order differential operators include the Laplace operator, which is a second-order tutor operator , is quite sensitive to the noise in the image, and the detected edges are often double pixels wide and have no direction information, so the Laplacian operator is rarely used to detect edges directly, but is mainly used to determine the edge pixels after the edge pixels are known. Whether the pixel is in a dark or bright area of ​​the image. In addition, the first-order difference operator will form a large gradient value in a wide range, so it cannot be accurately positioned, while the zero-crossing point of the second-order difference operator can be used to accurately locate the edge.
The noise of the Laplace operator is obviously larger than that of the Sobel operator, but its edges are much thinner than Sobel, and the Laplace transform, as a second-order differential operator, is particularly sensitive to noise and will produce double edges and cannot detect the edge direction. .

How to use python for image edge detection

Effect experiment

1. Roberts edge detection

Prewitt operator code:

Roberts_kernel_x = np.array([[-1, 0], [0, 1]], dtype=int)
Roberts_kernel_y = np.array([[0, -1], [1, 0]], dtype=int)
Copy after login
Copy after login

How to use python for image edge detection

2. Prewitt edge detection

Prewitt operator code:

Roberts_kernel_x = np.array([[-1, 0], [0, 1]], dtype=int)
Roberts_kernel_y = np.array([[0, -1], [1, 0]], dtype=int)
Copy after login
Copy after login

How to use python for image edge detection

3 , Sobel edge detection

Sobel function:

edges = cv2.Sobel(img, -1, 1, 1)
Copy after login

How to use python for image edge detection

4, Canny edge detection

Canny Function:

edges = cv2.Canny(img, 5, 100)
Copy after login

How to use python for image edge detection

5. Laplacian edge detection

Laplacian function:

edges = cv2.Laplacian(img, -1)
Copy after login

How to use python for image edge detection

The above is the detailed content of How to use python for image edge detection. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template