当前位置:网站首页>[opencv learning] [Canny edge detection]
[opencv learning] [Canny edge detection]
2022-07-02 12:52:00 【A sea of stars】
Let's learn today Canny testing , The specific algorithm is shown in the code .
import cv2
import numpy as np
# edge detection , The characteristic of edge points is that the gradient is large , General non edge points , The gradient is smaller or even 0
# Canny Edge detection process :
# 1: Use Gaussian filter to smooth the image , Remove noise points
# 2: Calculate the gradient and direction of each pixel in the image , This method can be used Sobel operator X and Y Direction get
# 3: Apply non maximum suppression (Non-Maximum-suppression), Eliminate some edge effects of edge detection , Because it is likely that adjacent pixels may be considered edge points , At this time, we have to remove some unnecessary interference points , Take the most likely edge pixels
# 4: Apply double threshold method , Identify real potential boundaries , Remove some unnecessary noise points
# 5: Then suppress the isolated edge ( It is considered as noise or non critical edge points ) Complete the purpose
# Disassembly instructions
# 1: Gauss filter , We are familiar with this , Use the following kernel for convolution
# such as :
# H = [[0.0924, 0.1192, 0.0924],
# [0.1192, 0.1538, 0.1192],
# [0.0924, 0.1192, 0.0924]]
# img = img * H
# 2: Calculate the gradient and direction of pixels
# Use here Sobel operator
# Sx = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]
# Sy = [[-1, -2, -1], [0, 0, 0], [1, 2, 1]]
# Convolute with the image machine respectively , Get the horizontal gradient Gx, And vertical gradient Gy
# Gx = img * Sx
# Gy = img * Sy
# utilize Gx and Gy Calculate the total gradient intensity G And direction θ
# G = (Gx^2 + Gy^2)^0.5
# θ = arctan(Gy / Gx), This is to determine which two adjacent pixels the central pixel is compared with
# 3: Non maximum suppression
# For simplicity of calculation , There are eight directions around a pixel , The angles are (0, 45,90, 135, 180, 225, 270, 315)
# θ And which angle range is closest , It belongs to which angle , In each angle direction, you can find the center point near N A numerical ( Gradient intensity value ),N Depending on kernel size Size
# The gradient intensity value of the center point and Near N Compare the gradient intensity values of points , If the gradient intensity value of the center point is the largest , Then keep the strength value , Otherwise, it will be suppressed , Change to 0.
# 4: Double threshold method
# Set a maximum gradient intensity max, And gradient intensity minimum min
# Filter the strength calculated above ,
# The gradient intensity value is less than min Of , Direct suppression , Change it to 0.
# The gradient intensity value is greater than max Of , Keep... Directly .
# Gradient intensity is between the two , See if this value is connected with other boundaries , If there is a link, keep it , Otherwise, the suppression is 0.
img = cv2.imread('images/saoge.jpg', cv2.IMREAD_GRAYSCALE)
print(img.shape)
can1 = cv2.Canny(img, threshold1=20, threshold2=100)
print(can1.shape)
can2 = cv2.Canny(img, threshold1=80, threshold2=160)
print(can2.shape)
ret = np.hstack((img, can1, can2))
cv2.imshow('Canny', ret)
cv2.waitKey(0)
cv2.destroyAllWindows()
The effect is as follows :
边栏推荐
- Redis sentinel mechanism and configuration
- JDBC 预防sql注入问题与解决方法[PreparedStatement]
- ASP. Net MVC default configuration, if any, jumps to the corresponding program in the specified area
- 应用LNK306GN-TL 转换器、非隔离电源
- Counting class DP acwing 900 Integer partition
- Openssh remote enumeration username vulnerability (cve-2018-15473)
- 3 a VTT terminal regulator ncp51200mntxg data
- Linear DP acwing 898 Number triangle
- 模块化 CommonJS ES Module
- Apply lnk306gn-tl converter, non isolated power supply
猜你喜欢

Interval DP acwing 282 Stone merging

堆 AcWing 839. 模拟堆

ArrayList与LinkedList效率的对比

线性DP AcWing 902. 最短编辑距离

Direct control PTZ PTZ PTZ PTZ camera debugging (c)

Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution

Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime

Sensor adxl335bcpz-rl7 3-axis accelerometer complies with rohs/weee

模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计

C#修饰符
随机推荐
深拷貝 事件總線
VLAN experiment
About wechat enterprise payment to change x509certificate2 read certificate information, publish to the server can not access the solution
堆 AcWing 838. 堆排序
模块化 CommonJS ES Module
Interview questions for software testing - a collection of interview questions for large factories in 2022
bellman-ford AcWing 853. Shortest path with side limit
Wechat official account payment prompt MCH_ ID parameter format error
Js10day (API phased completion, regular expression introduction, custom attributes, filtering sensitive word cases, registration module verification cases)
Mongodb redis differences
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
Package management tools
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
Openssh remote enumeration username vulnerability (cve-2018-15473)
Oracle from entry to mastery (4th Edition)
Obtain file copyright information
正确遍历EntryList方法
应用LNK306GN-TL 转换器、非隔离电源
Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)
C#修饰符