当前位置:网站首页>[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 :
边栏推荐
- Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)
- JDBC 预防sql注入问题与解决方法[PreparedStatement]
- 移动式布局(流式布局)
- Efficiency comparison between ArrayList and LinkedList
- js4day(DOM开始:获取DOM元素内容,修改元素样式,修改表单元素属性,setInterval定时器,轮播图案例)
- Js4day (DOM start: get DOM element content, modify element style, modify form element attributes, setinterval timer, carousel Map Case)
- Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
- 线性DP AcWing 896. 最长上升子序列 II
- spfa AcWing 852. SPFA judgement negative ring
- 1380. Lucky numbers in the matrix [two-dimensional array, matrix]
猜你喜欢
线性DP AcWing 895. 最长上升子序列
架构师必须了解的 5 种最佳软件架构模式
Five best software architecture patterns that architects must understand
bellman-ford AcWing 853. 有边数限制的最短路
传感器 ADXL335BCPZ-RL7 3轴 加速度计 符合 RoHS/WEEE
Rust search server, rust quick service finding tutorial
js3day(数组操作,js冒泡排序,函数,调试窗口,作用域及作用域链,匿名函数,对象,Math对象)
BOM DOM
Js3day (array operation, JS bubble sort, function, debug window, scope and scope chain, anonymous function, object, Math object)
C#修饰符
随机推荐
Redis sentinel mechanism and configuration
Ltc3307ahv meets EMI standard, step-down converter qca7005-al33 phy
Analog to digital converter (ADC) ade7913ariz is specially designed for three-phase energy metering applications
Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
C#运算符
Variable, "+" sign, data type
上手报告|今天聊聊腾讯目前在用的微服务架构
Redis bloom filter
How can attribute mapping of entity classes be without it?
Package management tools
Interval DP acwing 282 Stone merging
Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
JSON序列化 与 解析
bellman-ford AcWing 853. Shortest path with side limit
C#修饰符
async/await 异步函数
Rust search server, rust quick service finding tutorial
PXE installation UOS prompt NFS over TCP not available from 10 x.x.x
Js1day (input / output syntax, data type, data type conversion, VaR and let differences)
Linear DP acwing 899 Edit distance