An edge detection method based on Canny operator with high and low thresholds
By using technologies such as switching median filtering and k-means clustering in the Canny edge detection algorithm, the problem of poor noise processing by traditional algorithms is solved, and higher edge detection accuracy and adaptability are achieved, which significantly improves the Noise removal effect.
Patent Information
- Application Number
- CN202210044131.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-01-14
- Publication Date
- 2025-01-03
- Estimated Expiration
- 2042-01-14
AI Technical Summary
The traditional Canny edge detection algorithm has poor denoising effect on noisy images and requires pre-set dual thresholds, resulting in insufficient edge detection accuracy and adaptability.
Switch median filtering is used instead of Gaussian filtering to remove noise, combine the Sobel operator to calculate the gradient, use k-means clustering and OTSU algorithm to determine the high and low thresholds, and use non-maximum suppression and area morphology to process edge images.
It improves the positioning accuracy and adaptability of edge detection, effectively removes interference points, retains image details, and significantly improves the noise removal effect.
Smart Images

Figure CN114399522B_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the field of digital image processing, and specifically provides an edge detection method of a Canny operator based on high and low thresholds. Background Art
[0002] Edge features are basic features of images. They can greatly reduce the information to be processed while retaining the shape information of the object. The accuracy of edge feature extraction affects the accuracy of subsequent processing of the extracted features. Therefore, edge detection is a basic problem in image processing and computer vision. The edge of a grayscale image refers to the part of the local area of the image where the grayscale change is most significant. The grayscale change of the image can be represented by a gradient. At present, the commonly used edge extraction methods are mainly divided into two categories: first-order difference edge detection algorithms (such as Sobel and Prewitt algorithms) and second-order difference edge detection algorithms (such as Laplacian operators). Traditional edge detection algorithms such as Sobel (which can be divided into two operators for detecting horizontal and vertical directions), Canny, Roberts (operators found using local difference methods), Prewitt (operators for edge refinement), Log, etc., but most operators have certain limitations. The Sobel operator is not accurate enough for edge positioning. Roberts has high edge positioning accuracy but is prone to losing edge information of the image, which is not conducive to edge extraction. In addition, the operator does not smooth the input image and is easily affected by noise. The Prewitt operator is easy to detect pseudo edges, which has a good detection effect on grayscale gradient low-noise images, but the processing effect is not good for mixed and complex noise images. The Log algorithm is more sensitive to noise. Compared with the traditional differential algorithm, the Canny algorithm can achieve more accurate edge extraction and positioning.
[0003] However, the traditional Canny edge detection algorithm has problems such as poor denoising effect on noisy images and the need to pre-set dual thresholds. Domestic and foreign scholars have proposed many improvement methods for this. For example, Chen Shun, Meng Qingqing and others proposed an image edge detection algorithm that combines image enhancement and improved Canny operator on the basis of improving the Otsu algorithm, which reduces the influence of noise and enhances the contrast of the image. Duan Jun, Zhang Bo and others proposed an edge detection algorithm that uses statistical filtering denoising and grayscale-based iterative method to calculate the threshold. Li Ling proposed an improved Canny edge detection algorithm, which uses composite morphological filtering to replace Gaussian filtering, maintains edge strength and details, and reduces the influence of noise; uses Sobel operator to add 45° and 135° direction templates to calculate the gradient amplitude, and the edge positioning effect is better; in order to make edge detection more continuous, the threshold is selected according to the grayscale mean and variance of the image. Liu Suxing uses adaptive median filtering for denoising, adds 45° and 135° directions on the original basis, and uses finite differences to calculate the gradient amplitude direction; uses non-maximum suppression to refine the image edge, and uses the Otsu algorithm to calculate the high and low thresholds of the image. Summary of the invention
[0004] The purpose of the present invention is to propose an edge detection method based on a Canny operator with high and low thresholds to solve one or more technical problems existing in the prior art and at least provide a beneficial choice or create conditions.
[0005] The present invention proposes an algorithm framework based on Canny. First, the image is denoised by using a switch median filter instead of a Gaussian filter. The gradient amplitude and direction of each pixel of the denoised image are calculated. Then, the gradient image is subjected to non-maximum suppression to obtain an edge image with a single pixel width. Then, the high and low thresholds are determined and the image edge is processed by using k-means clustering and the OTSU algorithm. Finally, the area morphological opening operation is used to process the interference edge to obtain the final edge image. The algorithm has been experimentally verified to have higher positioning accuracy, stronger adaptability, and better interference point removal effect.
[0006] In order to achieve the above object, according to one aspect of the present invention, the present invention proposes an edge detection algorithm based on a Canny operator with high and low thresholds, comprising the following steps:
[0007] Step 1, perform smoothing filtering on the source image, and use switch median filtering instead of Gaussian filtering to remove noise from the image;
[0008] Step 2, using the Sobel operator to calculate the image gradient amplitude and direction after the image is smoothed and denoised in step 1;
[0009] Step 3: Perform non-maximum suppression on the gradient amplitude obtained in step 2 to obtain an edge image with a single pixel width.
[0010] Step 4, using the k-means clustering algorithm to obtain the cluster centers of high and low gradient values;
[0011] Step 5, using the Otsu algorithm to obtain the optimal threshold of the gradient, and taking the high and low thresholds between the high cluster center, the optimal threshold and the low cluster center;
[0012] Step 6, using high and low thresholds to process the edge image with a single pixel width obtained in step 3, and obtain a binary edge;
[0013] Step 7: Use area morphological opening operation to remove interference edges of the binary edges and obtain the final edge image.
[0014] Specifically, in step 1, the source image is smoothed and filtered, and the specific steps of the method for removing noise from the image by using a switching median filter instead of a Gaussian filter are as follows:
[0015] Step 1.1: Use a two-dimensional Gaussian function to perform smoothing filtering on the image. The calculation formula of the Gaussian function is:
[0016]
[0017] Where H(x, y, σ) is a two-dimensional Gaussian function, σ represents the degree of smoothness, π represents the circumference of a circle, x represents the horizontal coordinate of the center point, and y represents the vertical coordinate of the center point; exp(·) represents an exponential function with the natural constant e as the base.
[0018] Step 1.2: Detect the noise:
[0019] For an image v, the expression for detecting salt and pepper noise is as follows:
[0020]
[0021] Where: G (i,j) represents the gray value of the pixel in the i-th row and j-th column in v; a min and a max They represent the minimum grayscale mean and maximum grayscale mean in the 3X3 window centered at point (i, j); F (i,j) Indicates whether the pixel in the i-th row and j-th column is a noise point.
[0022] Step 1.3: Mark the noise points:
[0023] When G (i,j) Between a min and a max When between (i,j) is 1, otherwise the point is a noise point, marked F (i,j) is 0.
[0024] Step 1.4: Use the switch median filter algorithm to process the image:
[0025] According to the principle of switching median filtering, F (i,j) The pixel marked as 1 keeps its gray value unchanged. (i,j) The gray value of the pixel marked as 0 is replaced by the median value of its location.
[0026] Specifically, in step 2, the specific steps of the method for calculating the image gradient amplitude and direction based on the smoothed image using the Sobel operator are as follows:
[0027] Step 2.1: The Sobel operator combines Gaussian smoothing and differential derivation to calculate the approximate gradient of the image grayscale.
[0028] Step 2.2: The Sobel operator is derived in the horizontal and vertical directions to obtain the gradient images in the X and Y directions:
[0029] Horizontal transformation: transform image I into an odd-sized kernel G x When the kernel size is 3, G x The calculation result is formula (3):
[0030]
[0031] Vertical transformation: transform image I into an odd-sized kernel G. y When the kernel size is 3, G y The calculation result is formula (4):
[0032]
[0033] Step 2.3: At each point of image I, find the approximate gradient:
[0034] Step 2.4: At each point in image I, find the gradient direction:
[0035] Specifically, in step 3, the specific steps of the method for performing non-maximum suppression on the gradient amplitude of the smoothed image to obtain an edge image with a single pixel width are as follows:
[0036] Step 3.1: Detect the gradient magnitude and gradient direction of each pixel in the smoothed image;
[0037] Step 3.2: According to the gradient direction, perform non-maximum suppression on the gradient amplitude to eliminate edge false detection, retain the gradient amplitude of the pixel with the maximum gradient amplitude in the same gradient direction, and set the grayscale value of the non-maximum point to zero, and finally obtain an edge image with a single pixel width.
[0038] Specifically, in step 4, the specific steps of the method of using the k-means clustering algorithm to obtain the cluster centers of high and low gradient values are as follows:
[0039] Step 4.1: Select k cluster centers from the image gradient data to be classified, namely c1, c2, ..., c k , and then the image gradient data to be classified is divided into the classes at the corresponding positions according to the distance from the k cluster centers.
[0040] Step 4.2: Use the error square sum as the objective function to measure the classification effect, the error square sum V SSE The expression is formula (7):
[0041]
[0042] Where: c s represents the value of the sth cluster center; c0 represents the data point of the sth cluster center.
[0043] Step 4.3: Make V SSE The value is minimum, then V SSE Find the partial derivative and set it to 0. The formula is as follows:
[0044]
[0045] From (8), we can get Where m s is the number of data contained in the sth cluster center.
[0046] Step 4.4: When the value of the cluster center is the mean of the data of this type, V SSE The local value of is the smallest, so the mean of each class data is used as the value of the new cluster center and reclassified.
[0047] Then solve the mean of each type of data and repeat the above steps until the mean of each type of data basically does not change. At this time, V SSE The value is the global minimum, and each mean is the best cluster center.
[0048] Specifically, in step 5, the Otsu algorithm is used to obtain the Otsu threshold of the gradient, and the specific steps of the method of taking high and low thresholds between the high cluster center, the Otsu threshold and the low cluster center are as follows:
[0049] Step 5.1: Divide the image into foreground and background, and the segmentation threshold is t;
[0050] Step 5.2: The ratio of the pixels in the foreground to the entire image is marked as v0, and the average grayscale is marked as u0;
[0051] Step 5.3: The ratio of the background pixels to the entire image is marked as v1, and the average grayscale is marked as u1;
[0052] Step 5.3: The total average grayscale of the entire image is marked as u, and the inter-class variance is marked as d; the number of pixels in the image that is less than the threshold t is marked as N0, and the number of pixels that is greater than the threshold t is marked as N1.
[0053] Step 5.4: Find the optimal threshold t for dividing image edge points and non-edge points:
[0054] u=v0*u0+v1*u1 (9)
[0055] d(t)=v0(t)(u0-u) 2 +v1(t)(u1-u) 2 (10)
[0056] Substituting formula (9) into formula (10), we get:
[0057] d(t)=v0(t)v1(t)(u0-u1) 2 (11)
[0058] Traverse the entire graph, when d(t) reaches the global maximum, the t value is the optimal threshold.
[0059] Step 5.5: In step 4, the k-means algorithm is introduced to obtain the cluster center c of high and low gradient values h and c l , then the high threshold t h Must be in [t,c h ] interval, the low threshold t l Must be in [c l ,t] interval.
[0060] Step 5.6: Introduce the low cluster center offset factor u l and the high cluster center offset factor u h , can accurately obtain high and low thresholds to achieve the best separation effect, where u l and u h The value range of is [0,1], then the high and low thresholds are:
[0061] t h =c h -u h (ch -t) (12)
[0062] t l =c l +u l (tc l ) (13)
[0063] Specifically, in step 6, the specific steps of the method of processing the image edge using high and low thresholds and obtaining the binary edge are as follows:
[0064] Step 6.1: According to step 5, the optimal high and low thresholds are obtained, and the pixel points with gradient values greater than the high threshold are set to 1, and the pixel points with gradient values less than the low threshold are set to 0;
[0065] Step 6.2: For the pixel points whose gradient values are between the high and low thresholds, find out whether there is a gradient value greater than the high threshold in its 8-neighborhood. If so, set it to 1, otherwise set it to 0, and finally obtain a binary edge with good connectivity.
[0066] Specifically, in step 7, the method of removing the interference edge of the binary edge by using the area morphological opening operation and obtaining the final edge image has the following specific steps:
[0067] Step 7.1: Connected domain C at position p in the binary image g (p) is
[0068]
[0069] Where: P g (p,q) represents the continuous path between position p and position q in the denoised image g. The connected elements in the path are defined as 8-connected.
[0070] Step 7.2: The expression of area morphological opening operation is:
[0071]
[0072] Where: |C g (p)| represents the area of the connected region with a pixel value of 1; S represents the set area threshold; It represents the opening operation. From formula (15), we can see that the result of the area morphological opening operation is to set the pixel values of the connected domain with a pixel value of 1 and an area smaller than S to 0.
[0073] Step 7.3: During edge detection, since the false edge area is almost less than 100 pixels, which is very different from the real edge area, the edge with a connected domain area that is too small is identified as an interference edge. The area morphological opening operation is used to remove the interference edge of the binary edge and obtain the final edge image.
[0074] The beneficial effects of the present invention are:
[0075] The present invention proposes a framework of an edge detection method based on a Canny operator with high and low thresholds, which can effectively remove noise while retaining image details. Experiments have confirmed that the improved algorithm has high positioning accuracy, strong adaptability and good interference point removal effect. BRIEF DESCRIPTION OF THE DRAWINGS
[0076] By describing in detail the embodiments shown in conjunction with the accompanying drawings, the drawings described below are only some embodiments of the present invention, in which:
[0077] Figure 1 Flow chart of the edge detection method based on the Canny operator with high and low thresholds in this embodiment;
[0078] Figure 2 This is a comparison diagram of denoising of salt and pepper noise using different algorithms in this embodiment;
[0079] Figure 3 The binary edge images are obtained by processing with different algorithms in this embodiment;
[0080] Figure 4 This is the final edge detected image in this embodiment. DETAILED DESCRIPTION
[0081] The present invention will be further explained below in conjunction with the accompanying drawings and specific embodiments. It should be understood that the following specific embodiments are only used to illustrate the present invention and are not used to limit the scope of the present invention.
[0082] In order to achieve the above object, according to one aspect of the present invention, the present invention proposes an edge detection method based on a Canny operator with high and low thresholds. Figure 1 Flow chart of the edge detection method based on the Canny operator with high and low thresholds in this embodiment, as shown in FIG. Figure 1 As shown, the method comprises the following steps:
[0083] Step 1, perform smoothing filtering on the source image, and use switch median filtering instead of Gaussian filtering to remove noise from the image;
[0084] Step 2, using the Sobel operator to calculate the image gradient amplitude and direction after the image is smoothed and denoised in step 1;
[0085] Step 3: Perform non-maximum suppression on the gradient amplitude obtained in step 2 to obtain an edge image with a single pixel width.
[0086] Step 4, using the k-means clustering algorithm to obtain the cluster centers of high and low gradient values;
[0087] Step 5, using the Otsu algorithm to obtain the optimal threshold of the gradient, and taking the high and low thresholds between the high cluster center, the optimal threshold and the low cluster center;
[0088] Step 6, using high and low thresholds to process the edge image with a single pixel width obtained in step 3, and obtain a binary edge;
[0089] Step 7: Use area morphological opening operation to remove interference edges of the binary edges and obtain the final edge image.
[0090] Specifically, in step 1, the source image is smoothed and filtered, and the specific steps of the method for removing noise from the image by using a switching median filter instead of a Gaussian filter are as follows:
[0091] Step 1.1: Use a two-dimensional Gaussian function to smooth the image. The calculation formula of the Gaussian function is:
[0092]
[0093] Where H(x, y, σ) is a two-dimensional Gaussian function, σ represents the degree of smoothness, π represents the circumference of a circle, x represents the horizontal coordinate of the center point, and y represents the vertical coordinate of the center point; exp(·) represents an exponential function with the natural constant e as the base.
[0094] Step 1.2: Detect the noise:
[0095] For an image v, the expression for detecting salt and pepper noise is as follows:
[0096]
[0097] Where: G (i,j) Represents the grayscale value of the pixel in the i-th row and j-th column in v;
[0098] a min and a max Respectively represent the minimum grayscale mean and maximum grayscale mean in the 3X3 window centered at point (i, j);
[0099] F (i,j) Indicates whether the pixel in the i-th row and j-th column is a noise point.
[0100] Step 1.3: Mark the noise points:
[0101] When G (i,j) Between a min and a max When between (i,j) is 1, otherwise the point is a noise point, marked F (i,j) is 0.
[0102] Step 1.4: Use the switch median filter algorithm to process the image:
[0103] According to the principle of switching median filtering, F (i,j) The pixel marked as 1 keeps its gray value unchanged. (i,j) The gray value of the pixel marked as 0 is replaced by the median value of its location.
[0104] Specifically, in step 2, the specific steps of the method for calculating the image gradient amplitude and direction based on the smoothed image using the Sobel operator are as follows:
[0105] Step 2.1: The Sobel operator combines Gaussian smoothing and differential derivation to calculate the approximate gradient of the image grayscale.
[0106] Step 2.2: The Sobel operator is derived in the horizontal and vertical directions to obtain the gradient images in the X and Y directions:
[0107] Horizontal transformation: transform image I into an odd-sized kernel G x When the kernel size is 3, G x The calculation result is formula (3):
[0108]
[0109] Vertical transformation: transform image I into an odd-sized kernel G. y When the kernel size is 3, G y The calculation result is formula (4):
[0110]
[0111] Step 2.3: At each point of image I, find the approximate gradient:
[0112] Step 2.4: At each point in image I, find the gradient direction:
[0113] Specifically, in step 3, the specific steps of the method for performing non-maximum suppression on the gradient amplitude of the smoothed image to obtain an edge image with a single pixel width are as follows:
[0114] Step 3.1: Detect the gradient magnitude and gradient direction of each pixel in the smoothed image;
[0115] Step 3.2: According to the gradient direction, perform non-maximum suppression on the gradient amplitude to eliminate edge false detection, retain the gradient amplitude of the pixel with the maximum gradient amplitude in the same gradient direction, and set the grayscale value of the non-maximum point to zero, and finally obtain an edge image with a single pixel width.
[0116] Specifically, in step 4, the specific steps of the method of using the k-means clustering algorithm to obtain the cluster centers of high and low gradient values are as follows:
[0117] Step 4.1: Select k cluster centers from the image gradient data to be classified, namely c1, c2, ..., c k , and then the image gradient data to be classified is divided into the classes at the corresponding positions according to the distance from the k cluster centers.
[0118] Step 4.2: Use the error square sum as the objective function to measure the classification effect, the error square sum V SSE The expression is:
[0119]
[0120] Where: c s represents the value of the sth cluster center; c0 represents the data point of the sth cluster center.
[0121] Step 4.3: Make V SSE The value is minimum, then V SSE Find the partial derivative and set it to 0. The formula is as follows:
[0122]
[0123] From (8), we can get Where m s is the number of data contained in the sth cluster center.
[0124] Step 4.4: When the value of the cluster center is the mean of the data of this type, V SSE The local value of is the smallest, so the mean of each class data is used as the value of the new cluster center and reclassified.
[0125] Then solve the mean of each type of data and repeat the above steps until the mean of each type of data basically does not change. At this time, V SSE The value is the global minimum, and each mean is the best cluster center.
[0126] Specifically, in step 5, the Otsu algorithm is used to obtain the Otsu threshold of the gradient, and the specific steps of the method of taking high and low thresholds between the high cluster center, the Otsu threshold and the low cluster center are as follows:
[0127] Step 5.1: Divide the image into foreground and background, and the segmentation threshold is t;
[0128] Step 5.2: The ratio of the pixels in the foreground to the entire image is marked as v0, and the average grayscale is marked as u0; the ratio of the pixels in the background to the entire image is marked as v1, and the average grayscale is marked as u1;
[0129] Step 5.3: The total average grayscale of the entire image is marked as u, and the inter-class variance is marked as d; the number of pixels in the image that is less than the threshold t is marked as N0, and the number of pixels that is greater than the threshold t is marked as N1.
[0130] Step 5.4: Find the optimal threshold t for dividing image edge points and non-edge points:
[0131] u=v0*u0+v1*u1 (9)
[0132] d(t)=v0(t)(u0-u) 2 +v1(t)(u1-u) 2 (10)
[0133] Substituting formula (9) into formula (10), we get:
[0134] d(t)=v0(t)v1(t)(u0-u1) 2 (11)
[0135] Traverse the entire graph, when d(t) reaches the global maximum, the t value is the optimal threshold.
[0136] Step 5.5: In step 4, the k-means algorithm is introduced to obtain the cluster center c of high and low gradient values h and c l , then the high threshold t h Must be in [t,c h ] interval, the low threshold t l Must be in [c l ,t] interval.
[0137] Step 5.6: Introduce the low cluster center offset factor u l and the high cluster center offset factor u h , can accurately obtain high and low thresholds to achieve the best separation effect, where u l and u h The value range of is [0,1], then the high and low thresholds are:
[0138] t h =c h -u h (c h -t) (12)
[0139] t l =c l +u l (tc l ) (13)
[0140] Specifically, in step 6, the specific steps of the method of processing the image edge using high and low thresholds and obtaining the binary edge are as follows:
[0141] Step 6.1: According to step 5, the optimal high and low thresholds are obtained, and the pixel points with gradient values greater than the high threshold are set to 1, and the pixel points with gradient values less than the low threshold are set to 0;
[0142] Step 6.2: For the pixel points whose gradient values are between the high and low thresholds, find out whether there is a gradient value greater than the high threshold in its 8-neighborhood. If so, set it to 1, otherwise set it to 0, and finally obtain a binary edge with good connectivity.
[0143] Specifically, in step 7, the method of removing the interference edge of the binary edge by using the area morphological opening operation and obtaining the final edge image has the following specific steps:
[0144] Step 7.1: Connected domain C at position p in the binary image g (p) is
[0145]
[0146] Where: P g (p,q) represents the continuous path between position p and position q in the denoised image g. The connected elements in the path are defined as 8-connected.
[0147] Step 7.2: The expression of area morphological opening operation is:
[0148]
[0149] Where: |C g (p)| represents the area of the connected region with a pixel value of 1; S represents the set area threshold; It represents the opening operation. From formula (15), we can see that the result of the area morphological opening operation is to set the pixel values of the connected domain with a pixel value of 1 and an area smaller than S to 0.
[0150] Step 7.3: During edge detection, since the false edge area is almost less than 100 pixels, which is very different from the real edge area, the edge with a connected domain area that is too small is identified as an interference edge. The area morphological opening operation is used to remove the interference edge of the binary edge and obtain the final edge image.
[0151] Experiment 1: In step 1, the image is denoised by using a switch median filter. The results are compared with those of the Gaussian filter denoising process as follows: Figure 2 middle, Figure 2 (a) in the figure is the original image. Figure 2 (b) is the salt and pepper noise image. Figure 2 (c) is the Gaussian filter denoising image. Figure 2 (d) is the denoising image of the switch median filter.
[0152] Experiment 2: In step 6, high and low thresholds are used to process the image edge, and the binary edge image obtained is compared with the binary edge image obtained directly using the ostu algorithm. Figure 3 (a) and Figure 3 (b) As shown in: Figure 3 It can be seen that the threshold selection of the Otsu algorithm is better than the k-means clustering algorithm used in this embodiment. The method of obtaining cluster centers with high and low gradient values and then taking high and low thresholds between the high cluster center, the Otsu threshold and the low cluster center does not retain enough edge details of the image, resulting in discontinuous target contours and loss of edge detail information. The algorithm proposed in this embodiment not only retains edge details but also has better applicability, such as Figure 3 (a) shown.
[0153] Experiment 3, in step 7, the area morphological opening operation is used to remove the interference edge of the binary edge, and the final edge image is obtained as follows Figure 4 Shown: In Figure 4 middle, Figure 4 (a) is the original image. Figure 4 (b) is the final edge image.
[0154] Depend on Figure 4 and Figure 3 (a) By comparison, it can be seen that removing the interference edges from the binary edge image can achieve better edge detail detection and higher positioning accuracy.
[0155] The above is only an embodiment of the present invention. Although the present invention describes several embodiments, it is not intended to be limited to these embodiments. For those skilled in the art, the present invention can be improved without departing from the principles of the present invention, and all improvements are included in the scope of the claims of the present invention.
Claims
1. An edge detection method based on the Canny operator with high and low thresholds, characterized in that: The method comprises the following steps: Step 1: Perform smoothing filtering on the source image and use switch median filtering instead of Gaussian filtering to remove noise from the image; Step 2: Using the Sobel operator to calculate the image gradient amplitude and direction after the smoothing filtering and denoising in step 1; Step 3: Perform non-maximum suppression on the gradient amplitude obtained in step 2 to obtain an edge image with a single pixel width; Step 4: Use the k-means clustering algorithm to obtain the cluster centers of high and low gradient values; Step 5: Use the Otsu algorithm to obtain the optimal threshold of the gradient, and select high and low thresholds between the high cluster center, the optimal threshold and the low cluster center; Step 6: Use high and low thresholds to process the edge image with a single pixel width obtained in step 3 and obtain a binary edge; Step 7: Use area morphological opening operation to remove interference edges of the binary edges and obtain the final edge image.
2. The edge detection method based on the Canny operator with high and low thresholds according to claim 1 is characterized in that: In step 1, the source image is subjected to smoothing filtering, and the specific steps of the method for removing noise from the image by using a switch median filter instead of a Gaussian filter are as follows: Step 1.1: Use a two-dimensional Gaussian function to perform smoothing filtering on the image. The calculation formula of the Gaussian function is formula (1) Where H(x, y, σ) is a two-dimensional Gaussian function, σ represents the degree of smoothness, π represents the ratio of pi, x represents the horizontal coordinate of the center point, and y represents the vertical coordinate of the center point; exp(·) represents an exponential function with the natural constant e as the base; Step 1.2: Detect the noise: For an image v, the expression for detecting salt and pepper noise is as follows: Where: G (i,j) Represents the grayscale value of the pixel in the i-th row and j-th column of the image v; a min and a max They represent the minimum grayscale mean and maximum grayscale mean in the 3X3 window centered at point (i, j); F (i,j) Indicates whether the pixel in the i-th row and j-th column is a noise point; Step 1.3: Mark the noise points: When G (i,j) Between a min and a max When between (i,j) is 1, otherwise the point is a noise point, marked F (i,j) is 0; Step 1.4: Use the switch median filter algorithm to process the image: According to the principle of switching median filtering, F (i,j) The pixel marked as 1 keeps its gray value unchanged. (i,j) The gray value of the pixel marked as 0 is replaced by the median value of its location.
3. The edge detection method based on the Canny operator with high and low thresholds according to claim 1 is characterized in that: In step 2, the steps of the method for calculating the image gradient amplitude and direction based on the smoothed image using the Sobel operator are: Step 2.1: The Sobel operator combines Gaussian smoothing and differential derivation to calculate the approximate gradient of the image grayscale; Step 2.2: The Sobel operator is derived in the horizontal and vertical directions to obtain the gradient images in the X and Y directions: Horizontal transformation: transform image I into an odd-sized kernel G x For convolution, when the kernel size is 3, G x The calculation result is formula (3): Vertical transformation: transform image I into an odd-sized kernel G. y For convolution, when the kernel size is 3, G y The calculation result is formula (4): Step 2.3: At each point of image I, find the approximate gradient: Step 2.4: At each point in image I, find the gradient direction:
4. The edge detection method based on the Canny operator with high and low thresholds according to claim 1 is characterized in that: In step 3, the steps of performing non-maximum suppression on the gradient amplitude of the smoothed image to obtain an edge image with a single pixel width are as follows: Step 3.1: Detect the gradient magnitude and gradient direction of each pixel in the smoothed image; Step 3.2: According to the gradient direction, perform non-maximum suppression on the gradient amplitude to eliminate edge false detection, retain the gradient amplitude of the pixel with the maximum gradient amplitude in the same gradient direction, and set the grayscale value of the non-maximum point to zero, and finally obtain an edge image with a single pixel width.
5. The edge detection method based on the Canny operator with high and low thresholds according to claim 1 is characterized in that: In step 4, the steps of using the k-means clustering algorithm to obtain the cluster centers of high and low gradient values are: Step 4.1: Select k cluster centers from the image gradient data to be classified, namely c1, c2, ..., c k , and then divide the image gradient data to be classified into the class at the corresponding position according to the distance from the k cluster centers; Step 4.2: Use the error square sum as the objective function to measure the gradient classification effect, the error square sum V SSE The expression is formula (7) Where: c s represents the value of the sth cluster center; c0 represents the data point of the sth cluster center; Step 4.3: Make V SSE The value is minimum, then V SSE Find the partial derivative and set it to 0. The formula is as follows: From (8), we can get Where m s is the number of data contained in the sth cluster center; Step 4.4: When the value of the cluster center is the mean of the data of this type, V SSE The local value of is the smallest, so the mean of each class of data is used as the value of the new cluster center and reclassified, and then the mean of each class of data is solved, and the above steps are repeated until the mean of each class does not change. At this time, V SSE The value is the global minimum, and each mean is the best cluster center.
6. The edge detection method based on the Canny operator with high and low thresholds according to claim 1, characterized in that: In step 5, the Otsu algorithm is used to obtain the Otsu threshold of the gradient. The steps of the method of taking high and low thresholds between the high cluster center, the Otsu threshold and the low cluster center are: Step 5.1: Divide the image into foreground and background, and the segmentation threshold is t; Step 5.2: The ratio of the foreground pixels to the entire image is marked as v0, and the average grayscale is marked as u0; Step 5.3: The ratio of the background pixels to the entire image is marked as v1, and the average grayscale is marked as u1; Step 5.4: The total average grayscale of the entire image is marked as u, and the inter-class variance is marked as d; The number of pixels in the image that are less than the threshold t is marked as N0, and the number of pixels that are greater than the threshold t is marked as N1; Step 5.5: Find the optimal threshold t for dividing image edge points and non-edge points: u=v0*u0+v1*u1 (9) d(t)=v0(t)(u0-u) 2 +v1(t)(u1-u) 2 (10) Substituting formula (9) into formula (10), we get: d(t)=v0(t)v1(t)(u0-u1) 2 (11) Traverse the entire graph, when d(t) reaches the global maximum, the t value is the optimal threshold; Step 5.6: In step 4, the k-means algorithm is introduced to obtain the cluster center c of high and low gradient values h and c l , then the high threshold t h Must be in [t,c h ] interval, the low threshold t l Must be in [c l ,t] interval; Step 5.7: Introduce low cluster center offset factor u l and the high cluster center offset factor u h Only then can we accurately obtain the high and low thresholds to achieve the best separation effect, where u l and u h The value range of is [0,1], and the high and low thresholds are: t h =c h -u h (c h -t) (12) t l =c l +u l (t-c l ) (13)。 7. The edge detection method based on the Canny operator with high and low thresholds according to claim 1, characterized in that: In step 6, the specific steps of the method of processing the image edge using high and low thresholds and obtaining the binary edge are as follows: Step 6.1: According to step 5, the optimal high and low thresholds are obtained, and the pixel points with gradient values greater than the high threshold are set to 1, and the pixel points with gradient values less than the low threshold are set to 0; Step 6.2: For the pixel points whose gradient values are between the high and low thresholds, find out whether there is a gradient value greater than the high threshold in its 8-neighborhood. If so, set it to 1, otherwise set it to 0, and finally obtain a binary edge with good connectivity.
8. The edge detection method based on the Canny operator with high and low thresholds according to claim 1, characterized in that: In step 7, the specific steps of the method of using the area morphological opening operation to remove the interference edge of the binary edge and obtain the final edge image are as follows: Step 7.1: Connected domain C at position p in the binary image g (p) is Where: P g (p,q) represents the continuous path between position p and position q of the denoised image g, and the connected elements in the path are defined as 8-connected; Step 7.2: The expression of area morphological opening operation is: Where: |C g (p)| represents the area of the connected region with a pixel value of 1; S represents the set area threshold; It represents the opening operation. From formula (15), we can see that the result of the area morphological opening operation is to set the pixel values of the connected domain with a pixel value of 1 and an area less than S to 0; Step 7.3: During edge detection, since the false edge area is almost less than 100 pixels, which is very different from the real edge area, the edge with a connected domain area that is too small is identified as an interference edge. The area morphological opening operation is used to remove the interference edge of the binary edge and obtain the final edge image.
Citation Information
Patent Citations
Improved Canny-based automatic threshold edge detection method
CN108109155A
An improved Canny image edge detection method with noise immunity
CN109410230A
