canny algorithm of target detection opencv

canny

There are three canny goals

  • Low error rate detected edge is a true edge
  • Pixels on the edge of good positioning and pixel distance on the real edge should be minimal
  • The minimum response can only identify an edge, noise should not be labeled as the edge

canny several steps

  • Filtered Gaussian noise such as
  • Calculating a gradient such as the gradient is calculated by Sobel operator
  • Non-maxima suppression
    in the previous step may be calculated rough edges, an edge is assumed that a very thin line, then the processed result of the above can be understood as you get a relatively thick line, a so-called non-maxima suppression is to to find the most dramatic transformation point in a local pixel point, thus obtaining a finer edge.
  • Dual-threshold detection and edge connector

2 steps in front of us should be very familiar, unfamiliar reference https://www.cnblogs.com/sdu20112013/p/11608469.html and https://www.cnblogs.com/sdu20112013/p/11600436.html

Non-maximal suppression

In solving this step gradient, we can get mold growth and direction of the gradient

This step is done nms (non-maximal suppression) laid the foundation for our next image after Sobel operator processing the resulting edge may be very rough, which is reflected in the image edge is wide, we adopt the non-nms maximum value of the gradation points are set to zero, so that you can filter out many of the non-edge pixels .

As shown below, the point C represents the current value of the non-maximum suppression, it G1-4 communication points in the neighborhood of 8, FIG. Calcd step angle of the image obtained at point C on the blue line indicates, i.e., the gradient direction , the first step determines whether the maximum gradation value C value in the neighborhood of 8, then continue to check the case of a gradient direction in FIG intersection dTmp1, whether the value is greater than dTmp2 C, as point C is greater than dTmp1, gray value dTmp2 point is determined point C is the maximum point, is set to 1, so the final image should be generated under a binary image, the edges of the ideal state are single-pixel edge.

this step one thing to note is that dTmp1, dTmp2, two a pixel does not exist, is calculated by a bilinear interpolation . Canny proposed in John Canny operator of the sub-paper, non-maximal suppression just on the gradient direction in four 0,90,45,135 for each pixel with the gradient direction at a similar level in place of the four directions. the actual detection process, in order to more accurately filter out pixel belonging to the edge, bilinear interpolation do give dTmp1, dTmp2. go nms do aforesaid process to judge a pixel No part of the edge.
Recommended 2 speak better: https://blog.csdn.net/kezunhai/article/details/11620357 https://www.cnblogs.com/techyan1990/p/7291771.html

On pixels how to get the gradient direction , as shown in FIG.

In this case it reached the "thick edge" filter to be more delicate.

After this step, the resulting edge also contains a lot of false edges caused by noise and other reasons.

Dual-threshold detection and edge connector

After later nms, very close to the real edge. But there are some due to noise or some other causes of false edges. Let's further filtered through two thresholds.

Hysteresis: The final step. Canny does use two thresholds (upper and lower): - If a pixel gradient is higher than the upper threshold, the pixel is accepted as an edge .If a pixel gradient value is below the lower threshold, then it is rejected.If the pixel gradient is between the two thresholds, then it will be accepted only if it is connected to a pixel that is above the upper threshold.
Canny recommended a upper:lower ratio between 2:1 and 3:1.

  • For the high point of the gradient is greater than a threshold value, that is on the true edge pixels.
  • For the low point of the gradient is less than the threshold value, that is false edge pixels, due to noise, removing these points.
  • For gradient between a point between the upper and lower thresholds, if the neighborhood pixels around it are "true edge point" (i.e. gradient greater than a point of the high threshold value), it is considered this is also "true edge point."
    Recommended level between 1: 2 ratio threshold value: 1-3

In practical engineering, for these two parameters to your own image data to adjust, too low may cause too many false edge, the edge is too high may cause you want to keep is also filtered out.
Canny API

3,4 parameter indicates a low threshold and a high threshold, L2gradient defaults to false, indicating whether the calculated magnitude of the gradient of the square root manner.

opencv example

from __future__ import print_function
import cv2 as cv
import argparse
max_lowThreshold = 100
window_name = 'Edge Map'
title_trackbar = 'Min Threshold:'
ratio = 3
kernel_size = 3
def CannyThreshold(val):
    low_threshold = val
    #img_blur = cv.blur(src_gray, (3,3))
    detected_edges = cv.Canny(src_gray, low_threshold, low_threshold*ratio, kernel_size)
    mask = detected_edges != 0
    dst = src * (mask[:,:,None].astype(src.dtype))
    cv.imshow(window_name, dst)

src = cv.imread("/home/sc/disk/keepgoing/opencv_test/sidetest.jpeg")
src = cv.GaussianBlur(src, (3, 3), 0)
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
cv.namedWindow(window_name)
cv.createTrackbar(title_trackbar, window_name , 0, max_lowThreshold, CannyThreshold)
CannyThreshold(0)
cv.waitKey()

Note threshold effects caused by different, you can see a very low threshold when more lines, of course, "false edge" More, when a high threshold when the "false edge" reduced, but also lost more details so we need to adjust parameters based on their actual image data.

Guess you like

Origin www.cnblogs.com/sdu20112013/p/11614059.html