当前位置:网站首页>[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 :
边栏推荐
- Enhance network security of kubernetes with cilium
- 绕过ObRegisterCallbacks需要驱动签名方法
- 哈希表 AcWing 841. 字符串哈希
- Get started REPORT | today, talk about the microservice architecture currently used by Tencent
- Hash table acwing 841 String hash
- Analog to digital converter (ADC) ade7913ariz is specially designed for three-phase energy metering applications
- Input box assembly of the shutter package
- Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
- Ntmfs4c05nt1g N-ch 30V 11.9a MOS tube, pdf
- 8A 同步降压稳压器 TPS568230RJER_规格信息
猜你喜欢
线性DP AcWing 897. 最长公共子序列
Counting class DP acwing 900 Integer partition
区间DP AcWing 282. 石子合并
应用LNK306GN-TL 转换器、非隔离电源
spfa AcWing 852. SPFA judgement negative ring
移动式布局(流式布局)
浏览器存储方案
Dijkstra AcWing 850. Dijkstra求最短路 II
Async/await asynchronous function
Ltc3307ahv meets EMI standard, step-down converter qca7005-al33 phy
随机推荐
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
染色法判定二分图 AcWing 860. 染色法判定二分图
浏览器存储方案
Linear DP acwing 897 Longest common subsequence
线性DP AcWing 895. 最长上升子序列
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
Typora+docsify quick start
Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
趣味 面试题
"As a junior college student, I found out how difficult it is to counter attack after graduation."
绕过ObRegisterCallbacks需要驱动签名方法
Mui WebView down refresh pull-up load implementation
Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
NTMFS4C05NT1G N-CH 30V 11.9A MOS管,PDF
Redis sentinel mechanism and configuration
Analog to digital converter (ADC) ade7913ariz is specially designed for three-phase energy metering applications
[ybtoj advanced training guidance] judgment overflow [error]
Hash table acwing 841 String hash
Js4day (DOM start: get DOM element content, modify element style, modify form element attributes, setinterval timer, carousel Map Case)
堆 AcWing 838. 堆排序