当前位置:网站首页>[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 :
边栏推荐
- Async/await asynchronous function
- 基于STM32的OLED 屏幕驱动
- Redis bloom filter
- Browser storage scheme
- 线性DP AcWing 895. 最长上升子序列
- Linear DP acwing 896 Longest ascending subsequence II
- 三面阿里,有惊无险成功拿到offer定级P7,只能说是真的难
- NTMFS4C05NT1G N-CH 30V 11.9A MOS管,PDF
- Modular commonjs es module
- Five best software architecture patterns that architects must understand
猜你喜欢
![[ybtoj advanced training guide] similar string [string] [simulation]](/img/eb/acfefc7f85018fe9365d13502e2b0a.jpg)
[ybtoj advanced training guide] similar string [string] [simulation]

上手报告|今天聊聊腾讯目前在用的微服务架构

Redis bloom filter

Js1day (syntaxe d'entrée / sortie, type de données, conversion de type de données, Var et let différenciés)

Dijkstra AcWing 850. Dijkstra求最短路 II

区间DP AcWing 282. 石子合并

Linear DP acwing 898 Number triangle

Interval DP acwing 282 Stone merging

架构师必须了解的 5 种最佳软件架构模式

async/await 异步函数
随机推荐
Hundreds of web page special effects can be used. Don't you come and have a look?
The redis development document released by Alibaba covers all redis operations
Openssh remote enumeration username vulnerability (cve-2018-15473)
About wechat enterprise payment to change x509certificate2 read certificate information, publish to the server can not access the solution
Enhance network security of kubernetes with cilium
spfa AcWing 852. spfa判断负环
一些突然迸发出的程序思想(模块化处理)
Package management tools
Typora+docsify quick start
Variable, "+" sign, data type
JDBC prevent SQL injection problems and solutions [preparedstatement]
哈希表 AcWing 840. 模拟散列表
Linear DP acwing 895 Longest ascending subsequence
Dijkstra AcWing 850. Dijkstra求最短路 II
Heap acwing 839 Simulated reactor
BOM DOM
bellman-ford AcWing 853. Shortest path with side limit
Shutter encapsulated button
FBX import under ue4/ue5 runtime
Modular commonjs es module