cv2.Canny (): Makes the adjustment of edge detection by the Canny method nice

Edge refers to the boundary between objects and the background, and edge detection generally refers to image processing that detects edges by detecting changes in pixel values and areas with a large brightness gradient in the image.

スクリーンショット 2020-02-09 3.24.50.png (The image is from [Free Image Site](https://www.pakutaso.com/20191228360post-24995.html))

What is cv2.Canny ()?

Edge detection function implemented and provided by opencv. Edge images can be created easily, but (mainly) two parameters need to be adjusted for proper use.

Official page: Documentチュートリアル

Target person

I tried using cv2.Canny () while adjusting the threshold somehow, but it didn't work as I expected. I don't know if it's the limit of the image or the adjustment is bad. Let's understand the meaning of the parameters properly.

Two adjustment parameters

cv2.Canny(gray_img, threshold1, threshold2)

Simply, both threshold1 and threshold2 represent the thresholds for determining whether an edge is present. ** The larger the value, the harder it is to detect the edge, and the smaller the value, the easier it is to detect the edge. ** ** (If the threshold value is large, it will be judged as an edge only when the brightness changes larger.)

import cv2
gray_img = cv2.imread('sample.jpg', cv2.IMREAD_GRAYSCALE)

threshold1 = 0
threshold2 = 360
edge_img = cv2.Canny(gray_img, threshold1, threshold2)
cv2.imwrite('sample_edge.jpg', edge_img)

threshold2: maxVal threshold2 is the more intuitive value, ** the threshold itself for determining whether it is an edge **.

If you actually reduce threshold 2 gradually, it will be as follows. (For easy understanding, threshold1 has the same value as threshold2.)

threshold2.gif

threshold1: minVal As a premise of the argument explanation, the Canny method considers the edge to be a long line, and ** the part adjacent to the edge tends to be an edge **. (Refer to the figure below)

スクリーンショット 2020-02-09 3.42.44.png

From this idea, we have introduced a second threshold (threshold1), and in simple terms, the role is "** A threshold that is loose in determining whether or not it is an edge in the part adjacent to another edge (the part that tends to become an edge) * * ”.

In other words, even if you try to make threshold 1 looser (smaller), edges will not be detected in places where nothing was originally detected.

By loosening it, the adjacent part of the edge originally detected by threshold2 tends to become an edge, that is, intuitively, it feels like ** extending the line of the edge originally detected by threshold2 **. ..

If you actually reduce threshold 1 gradually, it will be as follows.

threshold1.gif

Summary

cv2.Canny(gray_img, threshold1, threshold2)

--threshold 1: A loose threshold for determining whether or not an edge is adjacent to another edge (a part that tends to become an edge) --threshold2: Threshold itself for judging whether it is an edge

Make edge detection parameter adjustments nice

From the above, the order as explained above is recommended.

  1. Make threhsold1 the same value as threshold2.
  2. Adjust threshold2 so that the edge appears where you want it to be detected.
  3. Use threshold1 to grow edges.

Recommended Posts

cv2.Canny (): Makes the adjustment of edge detection by the Canny method nice
The copy method of pandas.DataFrame is deep copy by default
Reuse the behavior of the @property method by using a descriptor [16/100]
Edge detection (Laplacian, Sobel, Canny)
Feature extraction by TF method using the result of morphological analysis
[Anomaly detection] Try using the latest method of deep distance learning
Find the ratio of the area of Lake Biwa by the Monte Carlo method