Welcome to DOFBOT-Ultra repository
15. Edge Detection
Edge Detection
The purpose of edge detection is to significantly reduce the data size of an image while preserving the original image attributes. Currently, there are multiple algorithms for edge detection. Although the Canny algorithm is older, it can be considered a standard algorithm for edge detection and is still widely used in research. Canny edge detection is a technique that extracts useful structural information from different visual objects and greatly reduces the amount of data to be processed. It is now widely used in various computer vision systems. Canny found that the requirements for edge detection across different visual systems are relatively similar, thus enabling an edge detection technique with broad application significance. The general criteria for edge detection include:
Detecting edges with a low error rate, meaning it needs to capture as many edges in the image as accurately as possible. The detected edges should be precisely positioned at the center of the true edges. A given edge in the image should be marked only once, and where possible, image noise should not produce false edges. To meet these requirements, Canny used the variational method. The optimal function in the Canny detector is described by the sum of four exponential terms, which can be approximated by the first derivative of a Gaussian function.
Among the commonly used edge detection methods, the Canny edge detection algorithm is strictly defined and is one of the methods that can provide good and reliable detection. Due to its advantages of meeting the three criteria of edge detection and having a simple implementation process, it has become one of the most popular algorithms for edge detection.
The Canny edge detection algorithm can be divided into the following 5 steps:
- Use a Gaussian filter to smooth the image and remove noise.
- Calculate the gradient intensity and direction for each pixel in the image.
- Apply Non-Maximum Suppression to eliminate spurious responses caused by edge detection.
- Apply Double-Threshold detection to determine real and potential edges.
- Complete edge detection by suppressing isolated weak edges.
Code path:
~/dofbot_opencv/scripts/3.draw_picture/03_Edge_detection_1.ipynb
x#Method 1import cv2import numpy as npimport randomimport matplotlib.pyplot as pltimg = cv2.imread('image0.jpg',1)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]#cv2.imshow('src',img)#canny 1 gray 2 gaussian 3 cannygray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)imgG = cv2.GaussianBlur(gray,(3,3),0)dst = cv2.Canny(img,50,50) # image convolution -> th# cv2.imshow('dst',dst) # cv2.waitKey(0)Comparing two images
xxxxxxxxxximg_bgr2rgb1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)plt.imshow(img_bgr2rgb1) plt.show()xxxxxxxxxximg_bgr2rgb1 = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)plt.imshow(img_bgr2rgb1)plt.show()
Code path:
xxxxxxxxxx~/dofbot_opencv/scripts/3.draw_picture/03_Edge_detection_2.ipynb
xxxxxxxxxx#Method 2import cv2import numpy as npimport randomimport mathimg = cv2.imread('image0.jpg',1)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]# cv2.imshow('src',img)# sobel 1 operator template 2 image convolution 3 threshold judgment# [1 2 1 [ 1 0 -1# 0 0 0 2 0 -2# -1 -2 -1 ] 1 0 -1 ]# [1 2 3 4] [a b c d] a*1+b*2+c*3+d*4 = dst# sqrt(a*a+b*b) = f>thgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)dst = np.zeros((height,width,1),np.uint8)for i in range(0,height-2): for j in range(0,width-2): gy = gray[i,j]*1+gray[i,j+1]*2+gray[i,j+2]*1-gray[i+2,j]*1-gray[i+2,j+1]*2-gray[i+2,j+2]*1 gx = gray[i,j]+gray[i+1,j]*2+gray[i+2,j]-gray[i,j+2]-gray[i+1,j+2]*2-gray[i+2,j+2] grad = math.sqrt(gx*gx+gy*gy) if grad>50: dst[i,j] = 255 else: dst[i,j] = 0# cv2.imshow('dst',dst) # cv2.waitKey(0)