Edge detection is also an edge detection method. This article introduces the functions and usage of the edge detection function, and uses the emgucv method to compare the results of the contour detection with the original text.
The principle of image edge detection is to detect all the gray-scale points in the image, and these points are connected to form several lines, which can be called the edge of the image. A multi-level edge detection algorithm developed by John F. Kan in 1986.
Here, the mathematical principles and algorithms of the edge moderation are no longer implemented. Interested readers can refer to professional books.
I. Overview (if you don't want to see it, you can skip it. Conversion from: "principle of the Canny edge detection algorithm and its VC Implementation Details", "Baidu Encyclopedia", and "my opencv Study Notes (19): Detection of contour, straight lines, circles, and straight line fitting")
A multi-level edge detection algorithm developed by John F. Kan in 1986. More importantly, he created the Computational Theory of edge detection to explain how this technology works.
When we detect the contour, we use the Canny edge detection algorithm, which is actually based on the gradient. However, what is different from the traditional Gradient Algorithm in edge search is:
1. It can precisely locate the edge. It detects the maximum value of the Modulus Along the width and angle, that is, the edge vertex, traverses eight image pixels, and compares the partial value of each pixel with the modulus of adjacent pixels, set the max value to the edge point and set the gray value of the pixel to 0. As a result, the edge is very fine. 2. Dual Threshold Detection. Generally, a small threshold value retains many edges, and some of them are useless. A large threshold value retains the main edge, but may lose some edge information. How can we combine them for use? The specific method is as follows: (Image 2 is generated by a large threshold, and image 1 is generated by a small threshold)
• Scans the image. When a non-zero gray pixel p (x, y) is encountered, the contour line starting with p (x, y) is tracked, until the end of the contour line Q (x, y ).
• Examine the eight adjacent regions of the vertex S (x, y) in Image 1 corresponding to the Q (x, y) point in Image 2. If there is a non-zero pixel S (x, y) in the 8 adjacent areas of S (x, y), it is included in Image 2 as R (x, y) point. Repeat the first step from R (x, y) until we are unable to continue in Image 1 and Image 2.
• After you link a contour line containing P (x, y), mark the contour line as accessed. Go back to the first step and find the next line. Repeat steps 1, 2, and 3 until the new contour line is not found in Image 2.
• At this point, the edge detection of the Canny operator is completed.
In opencv, you can use the canny function to detect the edge. The first parameter is the image to be detected, and the second parameter is the detection result. The last two parameters are the two thresholds. Generally, the high/low threshold ratio is between and.
Parameter Adjustment: the Canny algorithm contains many adjustable parameters, which will affect the computing time and effectiveness of the algorithm.
Gaussian filter size: the smoothing filter used in the first step will directly affect the result of the Canny algorithm. A smaller filter produces less blur effects, so that the fine line with small changes can be detected. A large filter produces a lot of blur effects, and applies a large image area to the color value of a specific point. The result is that it is more useful for detecting large and smooth edges, such as the Rainbow edge. Threshold: two thresholds are more flexible than one, but they still have common problems. If the threshold is too high, important information may be missed. If the threshold is too low, branch information is very important. It is difficult to give a general threshold for all images. Currently, there is no verified implementation method.
Disadvantages: the 'EC' algorithm is applicable to different scenarios. Its parameters can be adjusted according to specific requirements of different implementations to identify different edge features. Real-time image processing on PCs may be slow to use, especially when large Gaussian filters are used. However, when we talk about computing power, we also need to consider that as the processor speed continues to increase, it is expected that this will not become a problem in the next few years.
Ii. Program Implementation
1. Key functions
Public static void cvcanny (intptr image, intptr edges, double threshold1, double threshold2, int aperturesize)
The first parameter is image, input image
Indicates the input image, which must be a single channel grayscale image
The second parameter is edges, image to store the edges found by the function
Indicates the output edge image, which is a single-channel black-and-white image.
The third parameter threshold1, the first threshold
The fourth parameter is threshold2, the second threshold.
The third parameter and the fourth parameter indicate the threshold. The small threshold in these two thresholds is used to control edge connections, and the large threshold is used to control the initial segmentation of the strong edge, that is, if the gradient of a pixel is large and the upper threshold value, it is regarded as an edge pixel. If the gradient is smaller than the lower threshold, it is discarded. If the gradient of this point is between the two, we will keep it when this point is connected to a pixel above the upper limit. Otherwise, it will be deleted.
Aperture, aperture parameter for Sobel operator
The size of the Sobel operator. The default value is 3, indicating a 3*3 matrix. Sobel operators and Gaussian Laplace operators are common edge operators. For detailed mathematical principles, refer to professional books.
2. Programming
// Specifies the value of the edge detection intptr cannyimg = cvinvoke. cvcreateimage (cvinvoke. cvgetsize (histimg), emgu. CV. cvenum. ipl_depth.ipl_depth_8u, 1); cvinvoke. cvcanny (histimg, cannyimg, trackbar1.value, trackbar1.value * 3, 3); miplimage cannymi = (miplimage) Marshal. ptrtostructure (cannyimg, typeof (miplimage); image <gray, byte> cannyimage = new image <gray, byte> (cannymi. width, cannymi. height, cannymi. widthstep, cannymi. imagedata );
Picturebox5.image = cannyimage. tobitmap ();
Iii. Result Analysis
Adjust the threshold value to compare it with the result of using opencv in the third article "opencv Getting Started Guide.
For the original and processed images, the threshold is set to 100.
This article uses the emgucv method to calculate the result based on the threshold of 100.
From the comparison of the two images, we can see that the emgucv method has more details and needs to be adjusted according to the needs of the project.
[Reproduced + original] emgu CV on C # (6) -- emgu CV on Canny edge detection