Canny edge detection

  1. Gaussian filter is used to smooth the image and filter out noise (noise will affect edge pixels)
  2. Calculate the gradient intensity and direction of each pixel in the image
  3. Non-maximum Supression is used to eliminate the spurious response caused by edge detection
    • Maximum suppression: Take the largest Class box
    • Non-maximum suppression: not fetching
  4. Double-threshold detection is applied to determine the real and potential edges
  5. Edge detection is finally completed by suppressing isolated edge screening

1. Gaussian filter

Below or on the normalization processing (on average) H = H = \ [0.09240.11920.09240.11920.15380.11920.09240.11920.0924] left [\ begin {array} {name ‘LLL} 0.0924 & 0.1192 & 0.0924 \\ 0.1192&0.1538&0.1192 \\ 0.0924&0.1192&0.0924 H = {array} \ \ end right] ⎣ ⎢ ⎡ 0.09240.11920.09240.11920.15380.11920.09240.11920.0924 ⎦ ⎥ ⎤

The following is the calculation process of array multiplication: E =H∗A=[H11 h12 H13 h21 h22 h23 h31 h32 h33]∗[abcdefghi]=sum⁡([A ×h11 b× h12C × H13 D × h21E × h22F ×h23 g×h31 H ×h32i×h33])e=H * A=\left[\begin{array}{lll} \mathrm{h}_{11} & \mathrm{~h}_{12} & \mathrm{~h}_{13} \\ \mathrm{~h}_{21} & \mathrm{~h}_{22} & \mathrm{~h}_{23} \\ \mathrm{~h}_{31} & \mathrm{~h}_{32} & \mathrm{~h}_{33} \end{array}\right] *\left[\begin{array}{ccc} a & b & c \\ d & e & f \\ g & h & i \end{array}\right]=\operatorname{sum}\left(\left[\begin{array}{lll} \mathrm{a} \times \mathrm{h}_{11} & \mathrm{~b} \times \mathrm{h}_{12} & \mathrm{c} \times \mathrm{h}_{13} \\ \mathrm{~d} \times \mathrm{h}_{21} & \mathrm{e} \times \mathrm{h}_{22} & \mathrm{f} \times \mathrm{h}_{23} \\ \mathrm{~g} \times \mathrm{h}_{31} & \mathrm{~h} \times \ Mathrm {h}_{32} &\ mathrm{I}\ times \ Mathrm {h}_{33} \end{array}\right]\right)e= h ∗A=⎣⎢, “H11 h21 h31 h12 h22 h32 h13 h23” H33 ⎦ ⎥ ⎤ ∗ ⎣ ⎢ ⎡ adgbehcfi ⎦ ⎥ ⎤ = sum ⎝ ⎛ ⎣ ⎢ ⎡ * h21 h11 d g * h31 b * h22 h * h12e h32c * h23i * h13f h33 ⎦ ⎥ ⎤ ⎠ ⎞

2. Gradient and direction


G = G x 2 + G y 2 Theta. = arctan ( G y / G x ) S x = [ 1 0 1 2 0 2 1 0 1 ] S y = [ 1 2 1 0 0 0 1 2 1 ] \begin{array}{l} G=\sqrt{G_{x}^{2}+G_{y}^{2}} \\ \theta=\arctan \left(G_{y} / G_{x}\right) \end{array} \quad S_{x}=\left[\begin{array}{rrr} -1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \end{array}\right] \quad S_{y}=\left[\begin{array}{ccc} 1 & 2 & 1 \\ 0 & 0 & 0 \\ -1 & -2 & -1 \end{array}\right]

The following is the gradient value of the horizontal and vertical directions obtained by using Sobel operator: Gx = Sx ∗ A = [- 101-202-101] ∗ [abcdefghi] = sum ⁡ ([- a0c d02f – 2 – g0i]) G_ {x} = S_ * A = {x} \ left [\ begin & 0 {array} {CCC} – 1 &1 \ \ & 0-2 & 2 \\ -1 & 0 & 1 \end{array}\right] *\left[\begin{array}{ccc} a & b & c \\ d & e & f \\ g & h & i \end{array}\right]=\operatorname{sum}\left(\left[\begin{array}{ccc} -a & 0 & c \\ -2 d & 0 & 2 f \\ -g & 0 & i {array} \ right] \ \ end right) Gx = Sx ∗ A = ⎣ ⎢ ⎡ – 1-2-1000121 ⎦ ⎥ ⎤ ∗ ⎣ ⎢ ⎡ adgbehcfi ⎦ ⎥ ⎤ = sum ⎝ ⎛ ⎣ ⎢ ⎡ – A – 2 d – g000c2fi ⎦ ⎥ ⎤ ⎠ ⎞


G y = S y A = [ 1 2 1 0 0 0 1 2 1 ] [ a b c d e f g h i ] = sum ( [ a 2 b c 0 0 0 g 2 h i ] ) G_{y}=S_{y} * A=\left[\begin{array}{ccc} 1 & 2 & 1 \\ 0 & 0 & 0 \\ -1 & -2 & -1 \end{array}\right] *\left[\begin{array}{ccc} a & b & c \\ d & e & f \\ g & h & i \end{array}\right]=\operatorname{sum}\left(\left[\begin{array}{ccc} a & 2 b & c \\ 0 & 0 & 0 \\ -g & -2 h & -i \end{array}\right]\right)

3. Non-maximum suppression

  1. Method one:

  • The gradient of the unknown point is calculated by using the gradient of the known point
  • After obtaining the gradient of point C, if it is larger than dump1 and DumP2, it will be retained (if it is not the maximum value, it will be suppressed).
  1. Method 2

  • Who is near is compared with who
  • Maximum is left

4. Dual-threshold detection

  • If the threshold value is greater than maxVal or minVal < threshold < maxVal and the point is connected to the boundary, it is reserved

  • How do you tell if you have a boundary? Whether there are pixels in the D8 neighborhood whose gradient exceeds MaxVal

  • The process of double threshold detection:

    1. Double threshold detection & edge connection algorithm is simple: set high threshold and low threshold first (generally, high threshold is 2-3 times of low threshold)
    2. Through the whole gray matrix, if the gradient of a point is higher than the high threshold, set 1 in the result; if the gradient of the point is lower than the low threshold, set 0 in the result
    3. If the gradient value of the point is between the high and low thresholds, the following judgments need to be made: Check the 8 neighborhood points of the point (regarded as the center point) to see whether there are points whose gradient value is higher than the high threshold. If there are points, it means that the center point is connected to the determined edge point, so set 1 in the result, otherwise set 0.
import cv2
import matplotlib
import matplotlib.pyplot as plt # alias (for drawing display)
import numpy as np # take alias, below is notepad special, immediately display image
%matplotlib inline 
Copy the code
def cv_show(name, img) : # define the function to display the image, where name is the window name, img is the value returned by cv2 calling imread
    cv2.imshow(name,img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Copy the code
img = cv2.imread('tgcf.png', cv2.IMREAD_GRAYSCALE)

cv_show("gray", img)
Copy the code
v1 = cv2.Canny(img, 80.150) # Upper and lower limits, filter out noise points, but less edge information
v2 = cv2.Canny(img, 20.50) # More edge information, but more intrusive hand noise point

res = np.hstack([v1, v2])
cv_show('Compare', res)
Copy the code
p = cv2.imread('Park.jpg',cv2.IMREAD_GRAYSCALE)

cv_show('Park', p)
Copy the code
v1 = cv2.Canny(p, 100.200)  #Canny directly integrates all 5 of the above steps into one
v2 = cv2.Canny(p, 25.50)

res = np.hstack([p, v1, v2])
cv_show('Compare', res)
Copy the code