Edge Detection with OpenCV (Part1)

Tram Ho

1. Preamble

Edge Detection or edge detection problem is used a lot in the computer vision problem. For example, in problems such as extract information, recognize object, etc. Edges are places that encode many semantics information and shapes in an image. Here I would like to introduce two common algorithms used in edge detection: sobel edge detection and canny edge detection .

2. What is a good edge detector?

To make it easy to compare and visualize when considering between many different operators, we need to identify what is a good effective detector (good edge detector). So the question is what is the Good Edge Detector ? We have three criteria:

  1. Good Detection : minimizes the probability of false positive ( false positive edge detection) and false negatives (probability of missing an edge).
  2. Good Localization : the predicted edge position must be closest to the true edge (true edges).
  3. Single response : Edges (true edges) are made up of many points. A single response is a criterion that evaluates how many points are predicted for a true point. The more points that are predicted for a true point, the better the detector will be.

3. Sobel Edge Detection

Sobel often uses a Gaussian filter to eliminate noise, smoothing the image first so that edge detection algorithms work better. We can always use the function cv2.GaussianBlur () provided by OpenCV. Sobel then uses a 3×3 kernel multiplied by the original image to calculate the derivative in the horizontal (Gx) or vertical (Gy) so that the average of the magnitude of the gradient (magnitude) is calculated. The derivative in the horizontal and vertical direction is the change of pixel value in the horizontal or vertical direction. Finally, based on the calculated intensity, the edges are highlighted and some pixels are blurred. We use skipping_threshold to remove these low light points to make the edges sharper.

3.1. Sobel Edge Detection detect an edge

First we convert the image from BGR format to Gray for processing. Then use a Gaussian filter to smooth out the image, partly eliminating the false edges caused by noise. Then use the sobel function to calculate the gradient in the x and y directions. Finally use skipping_threshold as mentioned above. Notice here we use the parameter ddepth = cv2.CV_8U in the function cv2.Sobel ().

And here is the result of the above algorithm:

3.2. Sobel Edge Detection detects two edges

As we saw in the example above we only identified one side of the grain. The reason is because we use ddepth = cv2.CV_8U . cv2.CV_8U or np.unint8 only get positive values ​​corresponding to Black-to-White Transition so the White-to-Black direction is negative because receiving negative values, so np.uint8 will get 0 values ​​so We will miss the edge

So to detect both sides, we use cv2.CV_64F to get both positive and negative values ​​instead of cv2.CV_8U .

Here are the results using cv2.CV_64F :

Part 2 of the lesson is Canny Edge Detetion, I will leave it to another post to be able to explain more closely the post using opencv to identify this edge. Thank you for watching.


  1. Image Gradient Opencv Python Tutorials
Share the news now

Source : Viblo