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.
(The image is from [Free Image Site](https://www.pakutaso.com/20191228360post-24995.html))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、チュートリアル
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.
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.)
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)
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.
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
From the above, the order as explained above is recommended.
Recommended Posts