CN114943744A - Edge detection method based on local Otsu thresholding - Google Patents
Edge detection method based on local Otsu thresholding Download PDFInfo
- Publication number
- CN114943744A CN114943744A CN202210457562.3A CN202210457562A CN114943744A CN 114943744 A CN114943744 A CN 114943744A CN 202210457562 A CN202210457562 A CN 202210457562A CN 114943744 A CN114943744 A CN 114943744A
- Authority
- CN
- China
- Prior art keywords
- image
- edge
- edge detection
- threshold
- pixel
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 238000003708 edge detection Methods 0.000 title claims abstract description 45
- 238000000034 method Methods 0.000 title claims abstract description 31
- 230000008569 process Effects 0.000 claims description 7
- 230000001629 suppression Effects 0.000 claims description 5
- 238000009499 grossing Methods 0.000 claims 1
- 238000000605 extraction Methods 0.000 abstract description 2
- 238000000638 solvent extraction Methods 0.000 abstract description 2
- 238000001514 detection method Methods 0.000 description 5
- 230000000694 effects Effects 0.000 description 5
- 230000000717 retained effect Effects 0.000 description 5
- 238000004364 calculation method Methods 0.000 description 4
- 238000005286 illumination Methods 0.000 description 3
- 238000010191 image analysis Methods 0.000 description 3
- 238000007781 pre-processing Methods 0.000 description 3
- 238000012986 modification Methods 0.000 description 2
- 230000004048 modification Effects 0.000 description 2
- 238000006467 substitution reaction Methods 0.000 description 2
- 230000004075 alteration Effects 0.000 description 1
- 238000004458 analytical method Methods 0.000 description 1
- 230000009286 beneficial effect Effects 0.000 description 1
- 230000006835 compression Effects 0.000 description 1
- 238000007906 compression Methods 0.000 description 1
- 230000008094 contradictory effect Effects 0.000 description 1
- 238000005516 engineering process Methods 0.000 description 1
- 238000005457 optimization Methods 0.000 description 1
- 238000005192 partition Methods 0.000 description 1
- 230000004044 response Effects 0.000 description 1
- 238000009394 selective breeding Methods 0.000 description 1
- 230000009466 transformation Effects 0.000 description 1
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06T—IMAGE DATA PROCESSING OR GENERATION, IN GENERAL
- G06T7/00—Image analysis
- G06T7/10—Segmentation; Edge detection
- G06T7/13—Edge detection
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F17/00—Digital computing or data processing equipment or methods, specially adapted for specific functions
- G06F17/10—Complex mathematical operations
- G06F17/18—Complex mathematical operations for evaluating statistical data, e.g. average values, frequency distributions, probability functions, regression analysis
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06T—IMAGE DATA PROCESSING OR GENERATION, IN GENERAL
- G06T5/00—Image enhancement or restoration
- G06T5/70—Denoising; Smoothing
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06T—IMAGE DATA PROCESSING OR GENERATION, IN GENERAL
- G06T7/00—Image analysis
- G06T7/10—Segmentation; Edge detection
- G06T7/11—Region-based segmentation
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06T—IMAGE DATA PROCESSING OR GENERATION, IN GENERAL
- G06T7/00—Image analysis
- G06T7/10—Segmentation; Edge detection
- G06T7/136—Segmentation; Edge detection involving thresholding
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06T—IMAGE DATA PROCESSING OR GENERATION, IN GENERAL
- G06T7/00—Image analysis
- G06T7/60—Analysis of geometric attributes
- G06T7/62—Analysis of geometric attributes of area, perimeter, diameter or volume
Landscapes
- Engineering & Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Theoretical Computer Science (AREA)
- Computer Vision & Pattern Recognition (AREA)
- Data Mining & Analysis (AREA)
- Mathematical Analysis (AREA)
- Mathematical Physics (AREA)
- Pure & Applied Mathematics (AREA)
- Mathematical Optimization (AREA)
- Computational Mathematics (AREA)
- Geometry (AREA)
- Evolutionary Biology (AREA)
- Bioinformatics & Computational Biology (AREA)
- Operations Research (AREA)
- Probability & Statistics with Applications (AREA)
- Bioinformatics & Cheminformatics (AREA)
- Algebra (AREA)
- Life Sciences & Earth Sciences (AREA)
- Databases & Information Systems (AREA)
- Software Systems (AREA)
- General Engineering & Computer Science (AREA)
- Image Analysis (AREA)
Abstract
Description
技术领域technical field
本发明涉及基于局部Otsu阈值化的边缘检测方法,属于边缘检测技术领域。The invention relates to an edge detection method based on local Otsu thresholding, and belongs to the technical field of edge detection.
背景技术Background technique
广义的数字图像处理技术由低级到高级分为三个层次。首先就是狭义上的图像处理,输入和输出都是一副完整的图像,只是在原图像的基础上强化特定的细节,或是改善图像的质量,为后续的处理提供基础。其次是图像分析,它仅对图像中的部分区域进行分析和处理,通常图像的重要信息都包含在这部分区域之中,因此既要尽可能多的提取关键信息,又要减少周边区域对其的影响。最后是图像的识别和理解,而这要基于图像分析。The generalized digital image processing technology is divided into three levels from low level to high level. The first is image processing in a narrow sense. Both the input and output are a complete image, and only specific details are enhanced on the basis of the original image, or the quality of the image is improved to provide a basis for subsequent processing. The second is image analysis, which only analyzes and processes a part of the image. Usually, the important information of the image is contained in this part of the area. Therefore, it is necessary to extract as much key information as possible and reduce the surrounding areas. Impact. The last is image recognition and understanding, which is based on image analysis.
图像边缘检测就属于图像分析这一层次。在实际应用中处理绝大部分的图像时,我们往往不需要关注一整幅图像,通常会利用一些特性,把我们不关注的部分也就是将背景区域忽略,而仅仅关注特征区域,也就是前景区域。边缘检测在数字图像处理中是提取图像特征的关键步骤之一。这与人的视觉是相通的:当人的视线范围内出现一个未知的物体时,通常是通过这个未知物体的轮廓来进行判断的。而轮廓可以由多个边缘片段连接而成,因此在数字图像处理中也可以通过成功地检测图像的边缘,使图像识别变得容易,来简化在后续的数字图像处理中所要处理的信息。通常将边缘检测的结果应用于更高级别的图像处理技术,例如特征识别、图像识别以及图像压缩等,以便进一步分析和理解。Image edge detection belongs to the level of image analysis. When processing most of the images in practical applications, we often do not need to pay attention to the entire image, and usually use some features to ignore the part we don't pay attention to, that is, the background area, and only focus on the feature area, that is, the foreground. area. Edge detection is one of the key steps to extract image features in digital image processing. This is similar to human vision: when an unknown object appears in the range of human sight, it is usually judged by the outline of the unknown object. The contour can be formed by connecting a plurality of edge segments, so in digital image processing, the image recognition can be made easy by successfully detecting the edge of the image, and the information to be processed in the subsequent digital image processing can be simplified. The results of edge detection are usually applied to higher-level image processing techniques, such as feature recognition, image recognition, and image compression, for further analysis and understanding.
从边缘检测算法的优化历程来看,边缘检测的难点始终在于:既要正确地检测出有效的边缘,确保其结果的精准性和细节的完整性,也要保证检测的响应最好为单像素宽,将噪声的影响最小化。但如果要保证最大限度地减少边缘漏检,就不可避免地会受噪声的影响,造成边缘线过宽,甚至产生伪边缘,难以辨别真假边缘,但如果要将噪声的影响最小化,那又难以保证不损失图像的细节。因此,在实际情况中,这两点要求通常是矛盾的,尤其当图像较为复杂时,是难以仅用一个简单的边缘检测算法处理就得到较好的边缘的。From the perspective of the optimization process of edge detection algorithms, the difficulty of edge detection has always been that it is necessary to correctly detect valid edges to ensure the accuracy of the results and the integrity of details, and to ensure that the detection response is preferably a single pixel. wide to minimize the effect of noise. However, if you want to minimize missed edge detection, it will inevitably be affected by noise, resulting in too wide edge lines and even false edges, making it difficult to distinguish true and false edges, but if you want to minimize the impact of noise, then It is difficult to ensure that the details of the image are not lost. Therefore, in practical situations, these two requirements are usually contradictory, especially when the image is complex, it is difficult to obtain a better edge with only a simple edge detection algorithm.
基于这个难点,边缘检测算子在使用前常常需要有一个预处理的步骤。在这个预处理的步骤中,我们力求减少图像中的所有噪声,但同时又不损失图像的边缘信息。而通过对边缘检测算子的研究,我们不难发现阈值的概念贯穿始终。选取好的阈值多为边缘点的判定条件,可以很好地平衡有效边缘和噪声之间的矛盾之处。如果阈值选择合适,可以十分有效地减少图像中的噪声,但如果阈值选择不当,就很容易导致图像细节大面积丢失。但如何选取合适的阈值也是其中的难点。Based on this difficulty, edge detection operators often require a preprocessing step before they are used. In this preprocessing step, we strive to reduce all noise in the image without losing the edge information of the image. Through the study of edge detection operators, it is not difficult to find that the concept of threshold runs through. The selected threshold is mostly the judgment condition of edge points, which can well balance the contradiction between effective edge and noise. If the threshold is properly selected, the noise in the image can be reduced very effectively, but if the threshold is not selected properly, it is easy to cause a large area of loss of image details. But how to choose a suitable threshold is also a difficulty.
提前使用算法自动计算的阈值将图像阈值化作为边缘检测的预处理步骤,可以有效避免人为选取阈值导致的效率低下这个问题。最大类间方差(Otsu)法作为最为经典的阈值算法之一。它可以自行计算最佳阈值,并且因为其相比其他阈值算法,具有实用性强、计算简单的特点,得到了极为广泛的应用。Using the threshold value automatically calculated by the algorithm in advance to threshold the image as a preprocessing step of edge detection can effectively avoid the problem of low efficiency caused by artificial selection of the threshold value. The maximum between-class variance (Otsu) method is one of the most classic thresholding algorithms. It can calculate the optimal threshold by itself, and because it has the characteristics of strong practicability and simple calculation compared with other threshold algorithms, it has been widely used.
但是当图像存在多个目标、光照不均匀等情况时,单个阈值显然不能满足后续边缘检测的需求。而最大类间方差法的缺陷在于:它是通过遍历图像,以类间方差和类内方差来求取阈值实现的,通常只产生一个最佳阈值。However, when there are multiple targets in the image, uneven illumination, etc., a single threshold obviously cannot meet the needs of subsequent edge detection. The disadvantage of the maximum inter-class variance method is that it is realized by traversing the image and obtaining the threshold based on the inter-class variance and the intra-class variance, and usually only produces an optimal threshold.
发明内容SUMMARY OF THE INVENTION
本发明为解决上述的问题,提供了基于局部Otsu阈值化的边缘检测方法算法。In order to solve the above problems, the present invention provides an edge detection method algorithm based on local Otsu thresholding.
基于局部Otsu阈值化的边缘检测方法,具体步骤如下:The edge detection method based on local Otsu thresholding, the specific steps are as follows:
S1,输入图像可以是灰度图像,也可以是彩色图像。将输入图像根据面积大小等分为四个区域;S1, the input image can be a grayscale image or a color image. Divide the input image into four regions according to the size of the area;
S2,采用Otsu(最大类间方差法)算法计算每一块区域的阈值,得到四个阈值。S2, the Otsu (maximum inter-class variance method) algorithm is used to calculate the threshold of each area, and four thresholds are obtained.
S3,利用每个阈值对各自的区域进行阈值化处理,得到二值图像;S3, using each threshold to perform thresholding processing on the respective regions to obtain a binary image;
S4,最后使用边缘检测算子对二值图像进行边缘检测,得到边缘图像。S4, finally use an edge detection operator to perform edge detection on the binary image to obtain an edge image.
进一步,S2中Otsu算法的步骤具体为Further, the steps of the Otsu algorithm in S2 are as follows:
(1)设图像的灰度值为G={0,1,…,255},将灰度值为i的所有像素点的数量用fi表示,总的像素点用表示N,则(1) Set the gray value of the image to G={0, 1, .
(2)用Pi表示图像中灰度值为i的像素点出现的概率,则有(2) Use P i to represent the probability of the occurrence of a pixel whose gray value is i in the image, then we have
(3)选定一个阈值t,将图像中的像素点分为两类和C1,C0由图像中灰度值在[0,t]之间的所有像素点组成,C1由图像中灰度值在[t+1,255]之间所有的像素点组成;(3) Select a threshold t, and divide the pixels in the image into two categories and C1, C0 is composed of all pixels in the image whose gray value is between [0, t], and C1 is composed of the gray value in the image. All pixels between [t+1, 255] are composed;
(4)计算两类像素点集合出现的概率P0和P1,P1=1-P0;(4) Calculate the probability P 0 and P 1 of the two types of pixel point sets appearing, P 1 =1-P 0 ;
(5)计算两类像素点的平均灰度值μ0、μ1和整幅图像的平均灰度值μ:(5) Calculate the average gray value μ 0 and μ 1 of the two types of pixels and the average gray value μ of the entire image:
(6)计算C0和C1的类间方差:(6) Calculate the between-class variance of C0 and C1:
σ2=P0(μ-μ0)2+P1(μ-μ1)2=P0P1(μ0-μ1)2 (14)σ 2 =P 0 (μ-μ 0 ) 2 +P 1 (μ-μ 1 ) 2 =P 0 P 1 (μ 0 -μ 1 ) 2 (14)
(7)遍历图像的灰度值,将每一个灰度值都设定为阈值,然后求取类间方差,使得类间方差为最大值的阈值,就是最佳阈值。(7) Traverse the gray value of the image, set each gray value as a threshold, and then calculate the inter-class variance, so that the threshold with the maximum inter-class variance is the optimal threshold.
进一步,S3中阈值化处理具体为Further, the thresholding process in S3 is specifically as follows
设原图像为f(x,y),阈值化处理后的图像为g(x,y),则Let the original image be f(x, y) and the thresholded image be g(x, y), then
进一步,S4中边缘检测算子具体为Sobel算子和Canny算子。Further, the edge detection operators in S4 are specifically the Sobel operator and the Canny operator.
进一步Sobel算子具体为Further Sobel operator is specifically
Sobel算子加强了中心像素四个领域像素的权重,把待处理像素的上、下、左、右领域的灰度值加权差,在边缘处达到极值,突出图像的边缘信息从而检测边缘,其表达式为:The Sobel operator strengthens the weights of the pixels in the four areas of the center pixel, and weights the gray values of the upper, lower, left and right areas of the pixel to be processed to reach the extreme value at the edge, highlighting the edge information of the image to detect the edge, Its expression is:
其中in
Gx=[f(x+1,y-1)+2f(x+1,y)+f(x+1,y+1)]-[f(x-1,y-1)+2f(x-1,y)+f(x-1,y+1)] (16)G x =[f(x+1,y-1)+2f(x+1,y)+f(x+1,y+1)]-[f(x-1,y-1)+2f( x-1, y)+f(x-1, y+1)] (16)
Gy=[f(x-1,y+1)+2f(x,y+1)+f(x+1,y+1)]-[f(x-1,y-1)+2f(x,y-1)+f(x+1,y-1)] (17)G y =[f(x-1, y+1)+2f(x, y+1)+f(x+1, y+1)]-[f(x-1, y-1)+2f( x, y-1)+f(x+1, y-1)] (17)
两个模板分别为:The two templates are:
以上两个模板分别代表图像的水平梯度和垂直梯度。The above two templates represent the horizontal and vertical gradients of the image, respectively.
进一步Canny算子具体分为以下4步:The Canny operator is further divided into the following four steps:
(1)首先用二维高斯函数的一阶导数,对图像进行平滑。(1) First, use the first derivative of the two-dimensional Gaussian function to smooth the image.
设二维高斯函数为:Let the two-dimensional Gaussian function be:
其中,σ是高斯函数的方差参数。与前面LOG算子在边缘检测中的应用一样,方差参数的大小也直接影响图像的平滑程度,因此这里也需要根据实际情况适当地选取方差参数。where σ is the variance parameter of the Gaussian function. Like the previous application of the LOG operator in edge detection, the size of the variance parameter also directly affects the smoothness of the image, so it is also necessary to appropriately select the variance parameter according to the actual situation.
(2)然后,计算图像中所有像素点的梯度大小和梯度的方向。设高斯函数在某一方向上的一阶方向导数:(2) Then, calculate the gradient size and gradient direction of all pixels in the image. Let the first-order directional derivative of the Gaussian function in a certain direction:
其中,n为方向矢量;为梯度矢量。Among them, n is the direction vector; is the gradient vector.
Canny算子建立在二维的基础上,图像的边缘强度可以用平滑后的图像在待测像素点梯度的幅值来表示:The Canny operator is built in two dimensions On the basis of , the edge strength of the image can be represented by the magnitude of the gradient of the smoothed image at the pixel to be measured:
边缘的方向表示为:The direction of the edge is expressed as:
(3)对梯度的幅值应用“非极大值抑制”。由于在图像中全局梯度并不能确定边缘,所以需要遍历整幅图像,若某个像素的灰度值与其梯度方向上前后两个像素的灰度值相比不是最大的,那么将这个像素值置为0,也就是保留图像梯度中的局部极大值点,把其他非局部极大值置零以得到细化的边缘。而该像素点的梯度方向,应该根据8-连通性将其量化,如果中心像素的梯度方向指向C区域方向,那么在进行局部非极大值抑制时需要比较的邻近像素就是左上和右下的两个像素点。(3) Apply "non-maximum suppression" to the magnitude of the gradient. Since the global gradient in the image cannot determine the edge, it is necessary to traverse the entire image. If the gray value of a pixel is not the largest compared with the gray values of the two pixels before and after its gradient direction, then set the pixel value to It is 0, that is, the local maxima points in the image gradient are retained, and other non-local maxima are set to zero to obtain refined edges. The gradient direction of the pixel should be quantified according to 8-connectivity. If the gradient direction of the central pixel points to the direction of the C area, the adjacent pixels that need to be compared when performing local non-maximum suppression are the upper left and lower right. two pixels.
(4)对梯度进行“迟滞阈值化”以及边缘连接。所谓“迟滞阈值化”就是对梯度取双阈值,一个强阈值Th1,一个弱阈值Th2且两个阈值满足Th1=0.4·Th2。凡是梯度幅值小于弱阈值的像素点都被确定地认为不是边缘点,由于弱阈值的值较低,这就能保留较多的边缘信息;凡是梯度幅值大于强阈值的像素点都被确定地认为是边缘点。这时因为强阈值的值较高,所以尽管除去了大部分的噪声,但是也损失了一些边缘信息,使得边缘点有缺失图像不能形成完整的轮廓。这时梯度幅值幅值介于两者之间的像素点,就可以通过考察它们与其他边缘点的8-连通性来以此判断是不是边缘点。如果一个像素点的梯度幅值大于弱阈值,小于强阈值,并且与另一个边缘点存在连通关系,则该像素可以被认为是边缘点;但如果不与任何边缘点连通的像素就不能被认定为像素点。这样就可以将缺失的边缘点连接起来形成轮廓。(4) "Hysteresis thresholding" of gradients and edge connections. The so-called "hysteresis thresholding" is to take two thresholds for the gradient, one strong threshold Th 1 and one weak threshold Th 2 , and the two thresholds satisfy Th 1 =0.4·Th 2 . Any pixel with a gradient magnitude smaller than the weak threshold is definitely not considered an edge point. Since the weak threshold is low, more edge information can be retained; all pixels with a gradient magnitude greater than the strong threshold are determined. considered as an edge point. At this time, because the value of the strong threshold is high, although most of the noise is removed, some edge information is also lost, so that the image with missing edge points cannot form a complete outline. At this time, the pixels whose gradient amplitude is between the two can be judged as edge points by examining their 8-connectivity with other edge points. If the gradient magnitude of a pixel point is greater than the weak threshold and less than the strong threshold, and there is a connected relationship with another edge point, the pixel can be regarded as an edge point; but a pixel that is not connected to any edge point cannot be identified as for pixel points. This makes it possible to connect missing edge points to form contours.
使用Canny算子的检测的边缘,可以减少模板检测中的边缘中断现象,有利于得到较完整的边缘,定位精度高,信噪比大,对于图像背景中一些较弱的边缘也可以被检测出来。Using the edge detected by the Canny operator can reduce the edge interruption phenomenon in template detection, which is conducive to obtaining more complete edges, high positioning accuracy, and large signal-to-noise ratio. Some weak edges in the image background can also be detected. .
本发明采用以上技术方案与现有技术相比,具有以下技术效果:本发明首先对原始图像进行均匀分区;然后采用Otsu算法计算每一块区域的阈值;接着利用每个阈值对各自的区域进行阈值化处理;最后使用边缘检测算子对二值图像进行边缘检测。通过本发明方法使得边缘检测质量得到提高,可以有效的滤除噪声,保留关键的边缘,并且能得到连续性良好的单像素闭合边缘线,为之后的特征物提取、目标识别等提供了良好的基础。Compared with the prior art, the present invention adopts the above technical scheme, and has the following technical effects: the present invention firstly performs uniform partitioning on the original image; then adopts the Otsu algorithm to calculate the threshold value of each area; Finally, the edge detection operator is used to detect the edge of the binary image. Through the method of the invention, the quality of edge detection is improved, noise can be effectively filtered out, key edges can be retained, and a single-pixel closed edge line with good continuity can be obtained. Base.
附图说明Description of drawings
图1是本发明的基于局部Otsu阈值化的边缘检测方法的流程图;1 is a flowchart of an edge detection method based on local Otsu thresholding of the present invention;
图2计算全局阈值(左)和计算局部阈值(右);Figure 2 Calculates the global threshold (left) and calculates the local threshold (right);
图3全局阈值化(左)和局部阈值化(右);Figure 3 Global thresholding (left) and local thresholding (right);
图4采用Sobel算子(左)和Canny算子(右)对原始图像检测结果;Figure 4 uses Sobel operator (left) and Canny operator (right) to detect the original image results;
图5采用Sobel算子(左)和Canny算子(右)对全局阈值化图像检测结果;Figure 5 uses the Sobel operator (left) and the Canny operator (right) to detect the results of the global thresholded image;
图6采用Sobel算子(左)和Canny算子(右)对局部阈值化图像检测结果;Fig. 6 adopts Sobel operator (left) and Canny operator (right) to detect the result of local thresholding image;
图7采用Canny算子对原始图像检测结果(左)和局部图(右);Figure 7 uses Canny operator to detect the original image results (left) and local map (right);
图8采用Canny算子对局部阈值化图像检测结果(左)和局部图(右)。Figure 8. Detection results (left) and local maps (right) of local thresholded images using Canny operator.
具体实施方式Detailed ways
下面结合附图和各个具体实施方式,对本发明及其有益技术效果进行详细说明。The present invention and its beneficial technical effects will be described in detail below with reference to the accompanying drawings and various specific embodiments.
如图1所示,本发明的基于局部Otsu阈值化的边缘检测方法包括:As shown in Figure 1, the edge detection method based on local Otsu thresholding of the present invention includes:
第一步,获取图像,并将图像根据面积大小等分为四个区域,图像可以是彩色图像,也可以是灰度图像。灰度图像其实就是使彩色图像的R,G,B分量值相等的过程。例如使用MATLAB自带的rgb2gray函数对获得的彩色图像进行处理,得到灰度图像。还可以采用其他公知方法实现图像灰度化,这里不再举例说明。如图2所示,图像根据面积大小等分为四个区域。The first step is to acquire an image, and divide the image into four regions according to the size of the area. The image can be a color image or a grayscale image. A grayscale image is actually the process of making the R, G, and B components of a color image equal in value. For example, use the rgb2gray function that comes with MATLAB to process the obtained color image to obtain a grayscale image. Other well-known methods can also be used to realize the grayscale of the image, which will not be illustrated here. As shown in Figure 2, the image is equally divided into four regions according to the size of the area.
第二步,使用Otsu算法计算局部阈值。为了对比局部Otsu计算阈值的效果,图4中左图为采用全局Otsu算法计算的全局阈值。可以看到全局阈值为170,而分区计算的局部阈值分别为177、166、172、146。右下区域由于图像光照不均匀,而导致该区域的局部阈值明显比全局阈值低。In the second step, the local threshold is calculated using the Otsu algorithm. In order to compare the effect of the local Otsu calculation threshold, the left picture in Figure 4 shows the global threshold calculated by the global Otsu algorithm. It can be seen that the global threshold is 170, while the local thresholds for partition calculation are 177, 166, 172, 146, respectively. The lower right region has a significantly lower local threshold than the global threshold due to the uneven illumination of the image.
所述的Otsu计算阈值的具体步骤如下:The specific steps of the Otsu calculation threshold are as follows:
(1)设图像的灰度值为G={0,1,…,255},将灰度值为i的所有像素点的数量用fi表示,总的像素点用N表示,则(1) Set the gray value of the image as G={0, 1, .
(2)用Pi表示图像中灰度值为i的像素点出现的概率,则有(2) Use P i to represent the probability of the occurrence of a pixel whose gray value is i in the image, then we have
(3)选定一个阈值t,将图像中的像素点分为两类C0和C1,C0由图像中灰度值在之间的所有像素点组成,C1由图像中灰度值在之间所有的像素点组成;(3) Select a threshold t, and divide the pixels in the image into two categories C 0 and C 1 , C 0 is determined by the gray value in the image All pixels in between, C 1 consists of the gray value in the image at All the pixels in between are composed;
(4)计算两类像素点集合出现的概率P0和P1:P1=1-P0;(4) Calculate the probabilities P 0 and P 1 of the two types of pixel point sets appearing: P 1 =1-P 0 ;
(5)计算两类像素点的平均灰度值μ0、μ1和整幅图像的平均灰度值μ:(5) Calculate the average gray value μ 0 and μ 1 of the two types of pixels and the average gray value μ of the entire image:
(6)计算C0和C1的类间方差:(6) Calculate the between-class variance of C 0 and C 1 :
σ2=P0(μ-μ0)2+P1(μ-μ1)2=P0P1(μ0-μ1)2 (26)σ 2 =P 0 (μ-μ 0 ) 2 +P 1 (μ-μ 1 ) 2 =P 0 P 1 (μ 0 -μ 1 ) 2 (26)
(7)遍历图像的灰度值,将每一个灰度值都设定为阈值,然后求取类间方差,使得类间方差为最大值的阈值,就是最佳阈值。(7) Traverse the gray value of the image, set each gray value as a threshold, and then calculate the inter-class variance, so that the threshold with the maximum inter-class variance is the optimal threshold.
第三步,利用每个阈值对各自的区域进行阈值化处理,得到二值图像。图3中左图为采用全局Otsu阈值化的输出图像,右图为采用局部Otsu阈值化,可以看出由于图像光照不均匀,采用全局Otsu阈值化将阴影区也误判为前景区域,而局部Otsu阈值化效果良好。In the third step, each threshold is used to threshold the respective regions to obtain a binary image. The left image in Figure 3 is the output image using global Otsu thresholding, and the right image is using local Otsu thresholding. It can be seen that due to the uneven illumination of the image, the shadow area is also misjudged as the foreground area by using the global Otsu thresholding, while the local Otsu thresholding is used. Otsu thresholding works well.
所述的阈值化处理具体为The thresholding process is specifically as follows
阈值化处理实际上是一种极端的但十分有用的灰度变换。它将图像中每一个像素点的灰度值与某个预先设定的数值,也就是阈值进行比较,并根据比较结果将该点的像素变换为两个可能的输出灰度级之一,通常用黑和白表示。因此通过阈值化处理之后的图像实际上是一幅二值图像。可以设原图像为f(x,y),阈值化处理后的图像为g(x,y),则Thresholding is actually an extreme but very useful grayscale transformation. It compares the gray value of each pixel in the image with a preset value, that is, a threshold, and transforms the pixel of the point into one of two possible output gray levels according to the comparison result, usually Shown in black and white. Therefore, the image after thresholding is actually a binary image. You can set the original image to be f(x, y) and the thresholded image to be g(x, y), then
第四步,使用边缘检测算子对二值图像进行边缘检测,得到边缘图像。如图4所示,直接使用Sobel算子对图像检测出来的边缘,存在三个问题:(1)孤立的边缘点;(2)伪边缘;(3)边缘点不连续。而直接使用Canny算子检测出来的边缘虽然连续性较好,但是伪边缘过多。如图5所示,使用全局Otsu阈值化的方法来检测边缘,对图像目标物误判,直接导致后期的边缘检测效果也出现误差。如图6所示,采用局部Otsu阈值化的边缘检测结果,极大程度地改善了Sobel算子和Canny算子的不足,能较为准确地提取边缘线,并且边缘连续性好,抗噪声性能强。The fourth step is to use the edge detection operator to perform edge detection on the binary image to obtain an edge image. As shown in Figure 4, there are three problems with the edge detected by the Sobel operator directly: (1) isolated edge points; (2) pseudo-edges; (3) discontinuous edge points. However, although the edges detected directly by the Canny operator have good continuity, there are too many false edges. As shown in Figure 5, using the global Otsu thresholding method to detect edges, misjudging the image target directly leads to errors in the later edge detection effect. As shown in Figure 6, the edge detection result using local Otsu thresholding greatly improves the shortcomings of Sobel operator and Canny operator, and can extract edge lines more accurately, with good edge continuity and strong anti-noise performance. .
所述的边缘检测算子具体为Sobel算子和Canny算子。The edge detection operators are specifically Sobel operators and Canny operators.
具体地,Sobel算子为Specifically, the Sobel operator is
Sobel算子是3×3大小的模板,是一个奇数大小的模板,因此可以将待处理的像素点放在模板的中心位置计算其梯度值。Sobel算子加强了中心像素四个领域像素的权重,把待处理像素的上、下、左、右领域的灰度值加权差,在边缘处达到极值,突出图像的边缘信息从而检测边缘,其表达式为:The Sobel operator is a 3×3 template, which is an odd-sized template, so the pixel to be processed can be placed in the center of the template to calculate its gradient value. The Sobel operator strengthens the weights of the pixels in the four areas of the center pixel, and weights the gray values of the upper, lower, left and right areas of the pixel to be processed to reach the extreme value at the edge, highlighting the edge information of the image to detect the edge, Its expression is:
其中in
Gx=[f(x+1,y-1)+2f(x+1,y)+f(x+1,y+1)]-[f(x-1,y-1)+2f(x-1,y)+f(x-1,y+1)] (29)G x =[f(x+1,y-1)+2f(x+1,y)+f(x+1,y+1)]-[f(x-1,y-1)+2f( x-1,y)+f(x-1,y+1)] (29)
Gy=[f(x-1,y+1)+2f(x,y+1)+f(x+1,y+1)]-[f(x-1,y-1)+2f(x,y-1)+f(x+1,y-1)] (30)G y =[f(x-1,y+1)+2f(x,y+1)+f(x+1,y+1)]-[f(x-1,y-1)+2f( x,y-1)+f(x+1,y-1)] (30)
两个模板分别为:The two templates are:
以上两个模板分别代表图像的水平梯度和垂直梯度。The above two templates represent the horizontal and vertical gradients of the image, respectively.
Sobel算子加强了中心像素点。因此如果处理一幅噪点均匀的图像,用Sobel算子容易出现伪边缘。但如果图像的噪点分布在边缘附近,那么Sobel算子进行检测的效果将会好。The Sobel operator strengthens the center pixel point. Therefore, if an image with uniform noise is processed, false edges are prone to appear with the Sobel operator. However, if the noise of the image is distributed near the edge, the Sobel operator will perform well for detection.
具体地,Canny算子主要分为以下4步:Specifically, the Canny operator is mainly divided into the following four steps:
(1)首先用二维高斯函数的一阶导数,对图像进行平滑。设二维高斯函数为:(1) First, use the first derivative of the two-dimensional Gaussian function to smooth the image. Let the two-dimensional Gaussian function be:
其中,σ是高斯函数的方差参数。与前面LOG算子在边缘检测中的应用一样,方差参数的大小也直接影响图像的平滑程度,因此这里也需要根据实际情况适当地选取方差参数。where σ is the variance parameter of the Gaussian function. Like the previous application of the LOG operator in edge detection, the size of the variance parameter also directly affects the smoothness of the image, so it is also necessary to appropriately select the variance parameter according to the actual situation.
(2)然后,计算图像中所有像素点的梯度大小和梯度的方向。设高斯函数在某一方向上的一阶方向导数:(2) Then, calculate the gradient size and gradient direction of all pixels in the image. Let the first-order directional derivative of the Gaussian function in a certain direction:
其中,n为方向矢量;为梯度矢量。Among them, n is the direction vector; is the gradient vector.
Canny算子建立在二维的基础上,图像的边缘强度可以用平滑后的图像在待测像素点梯度的幅值来表示:The Canny operator is built in two dimensions On the basis of , the edge strength of the image can be represented by the magnitude of the gradient of the smoothed image at the pixel to be measured:
边缘的方向表示为:The direction of the edge is expressed as:
(3)对梯度的幅值应用“非极大值抑制”。由于在图像中全局梯度并不能确定边缘,所以需要遍历整幅图像,若某个像素的灰度值与其梯度方向上前后两个像素的灰度值相比不是最大的,那么将这个像素值置为0,也就是保留图像梯度中的局部极大值点,把其他非局部极大值置零以得到细化的边缘。而该像素点的梯度方向,应该根据8-连通性将其量化,如下图所示,如果中心像素的梯度方向指向C区域方向,那么在进行局部非极大值抑制时需要比较的邻近像素就是左上和右下的两个像素点。(3) Apply "non-maximum suppression" to the magnitude of the gradient. Since the global gradient in the image cannot determine the edge, it is necessary to traverse the entire image. If the gray value of a pixel is not the largest compared with the gray values of the two pixels before and after its gradient direction, then set the pixel value to It is 0, that is, the local maxima points in the image gradient are retained, and other non-local maxima are set to zero to obtain refined edges. The gradient direction of the pixel should be quantified according to 8-connectivity, as shown in the figure below, if the gradient direction of the central pixel points to the direction of the C area, then the adjacent pixels that need to be compared when performing local non-maximum suppression are Two pixels on the top left and bottom right.
(4)对梯度进行“迟滞阈值化”以及边缘连接。所谓“迟滞阈值化”就是对梯度取双阈值,一个强阈值Th1,一个弱阈值Th2且两个阈值满足Th1=0.4·Th2。凡是梯度幅值小于弱阈值的像素点都被确定地认为不是边缘点,由于弱阈值的值较低,这就能保留较多的边缘信息;凡是梯度幅值大于强阈值的像素点都被确定地认为是边缘点。这时因为强阈值的值较高,所以尽管除去了大部分的噪声,但是也损失了一些边缘信息,使得边缘点有缺失图像不能形成完整的轮廓。这时梯度幅值幅值介于两者之间的像素点,就可以通过考察它们与其他边缘点的8-连通性来以此判断是不是边缘点。如果一个像素点的梯度幅值大于弱阈值,小于强阈值,并且与另一个边缘点存在连通关系,则该像素可以被认为是边缘点;但如果不与任何边缘点连通的像素就不能被认定为像素点。这样就可以将缺失的边缘点连接起来形成轮廓。(4) "Hysteresis thresholding" of gradients and edge connections. The so-called "hysteresis thresholding" is to take two thresholds for the gradient, one strong threshold Th 1 and one weak threshold Th 2 , and the two thresholds satisfy Th 1 =0.4·Th 2 . Any pixel with a gradient magnitude smaller than the weak threshold is definitely not considered an edge point. Since the weak threshold is low, more edge information can be retained; all pixels with a gradient magnitude greater than the strong threshold are determined. considered as an edge point. At this time, because the value of the strong threshold is high, although most of the noise is removed, some edge information is also lost, so that the image with missing edge points cannot form a complete outline. At this time, the pixels whose gradient amplitude is between the two can be judged as edge points by examining their 8-connectivity with other edge points. If the gradient magnitude of a pixel point is greater than the weak threshold and less than the strong threshold, and there is a connected relationship with another edge point, the pixel can be regarded as an edge point; but a pixel that is not connected to any edge point cannot be identified as for pixel points. This makes it possible to connect missing edge points to form contours.
使用Canny算子的检测的边缘,可以减少模板检测中的边缘中断现象,有利于得到较完整的边缘,定位精度高,信噪比大,对于图像背景中一些较弱的边缘也可以被检测出来。Using the edge detected by the Canny operator can reduce the edge interruption phenomenon in template detection, which is conducive to obtaining more complete edges, high positioning accuracy, and large signal-to-noise ratio. Some weak edges in the image background can also be detected. .
综上,本发明提出的算法使得边缘检测质量得到提高,可以有效的滤除噪声,保留关键的边缘,并且能得到连续性良好的单像素闭合边缘线,为之后的特征物提取、目标识别等提供了良好的基础。In summary, the algorithm proposed in the present invention improves the quality of edge detection, can effectively filter out noise, retain key edges, and obtain a single-pixel closed edge line with good continuity for subsequent feature extraction, target recognition, etc. provides a good foundation.
最后需要指出的是:以上实施例仅用以说明本发明的技术方案,而非对其限制。尽管参照前述实施例对本发明进行了详细的说明,本领域的普通技术人员应当理解:其依然可以对前述各实施例所记载的技术方案进行修改,或者对其中部分技术特征进行等同替换;而这些修改或者替换,并不使相应技术方案的本质脱离本发明各实施例技术方案的精神和范围尽管已经示出和描述了本发明的实施例,对于本领域的普通技术人员而言,可以理解在不脱离本发明的原理和精神的情况下可以对这些实施例进行多种变化、修改、替换和变型,本发明的范围由所附权利要求及其等同物限定。Finally, it should be pointed out that the above embodiments are only used to illustrate the technical solutions of the present invention, but not to limit them. Although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that: it is still possible to modify the technical solutions described in the foregoing embodiments, or perform equivalent replacements to some of the technical features; and these Modifications or substitutions do not make the essence of the corresponding technical solutions depart from the spirit and scope of the technical solutions of the embodiments of the present invention. Although the embodiments of the present invention have been shown and described, those of ordinary skill in the art can understand that Various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principle and spirit of the invention, the scope of which is defined by the appended claims and their equivalents.
Claims (7)
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202210457562.3A CN114943744A (en) | 2022-04-27 | 2022-04-27 | Edge detection method based on local Otsu thresholding |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202210457562.3A CN114943744A (en) | 2022-04-27 | 2022-04-27 | Edge detection method based on local Otsu thresholding |
Publications (1)
Publication Number | Publication Date |
---|---|
CN114943744A true CN114943744A (en) | 2022-08-26 |
Family
ID=82907236
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202210457562.3A Pending CN114943744A (en) | 2022-04-27 | 2022-04-27 | Edge detection method based on local Otsu thresholding |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN114943744A (en) |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115861307A (en) * | 2023-02-21 | 2023-03-28 | 深圳市百昌科技有限公司 | Fascia gun power supply drive plate welding fault detection method based on artificial intelligence |
CN116416268B (en) * | 2023-06-09 | 2023-08-18 | 浙江双元科技股份有限公司 | Method and device for detecting edge position of lithium battery pole piece based on recursion dichotomy |
-
2022
- 2022-04-27 CN CN202210457562.3A patent/CN114943744A/en active Pending
Cited By (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115861307A (en) * | 2023-02-21 | 2023-03-28 | 深圳市百昌科技有限公司 | Fascia gun power supply drive plate welding fault detection method based on artificial intelligence |
CN115861307B (en) * | 2023-02-21 | 2023-04-28 | 深圳市百昌科技有限公司 | Fascia gun power supply driving plate welding fault detection method based on artificial intelligence |
CN116416268B (en) * | 2023-06-09 | 2023-08-18 | 浙江双元科技股份有限公司 | Method and device for detecting edge position of lithium battery pole piece based on recursion dichotomy |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN106407917B (en) | Retinal Vessel Extraction Method and System Based on Dynamic Scale Allocation | |
CN111640157B (en) | Checkerboard corner detection method based on neural network and application thereof | |
CN107301661B (en) | High-resolution remote sensing image registration method based on edge point features | |
CN112819772B (en) | High-precision rapid pattern detection and recognition method | |
CN107194937B (en) | Traditional Chinese medicine tongue picture image segmentation method in open environment | |
CN110415208B (en) | An adaptive target detection method and its device, equipment and storage medium | |
CN103955949B (en) | Moving target detecting method based on Mean-shift algorithm | |
EP2085928B1 (en) | Detection of blobs in images | |
CN108898610A (en) | A kind of object contour extraction method based on mask-RCNN | |
CN108022233A (en) | A kind of edge of work extracting method based on modified Canny operators | |
CN109978848B (en) | Method for detecting hard exudation in fundus image based on multi-light-source color constancy model | |
CN114118144A (en) | Anti-interference accurate aerial remote sensing image shadow detection method | |
CN104794502A (en) | Image processing and mode recognition technology-based rice blast spore microscopic image recognition method | |
CN102324099B (en) | Step edge detection method oriented to humanoid robot | |
EP2888718A1 (en) | Methods and systems for automatic location of optic structures in an image of an eye, and for automatic retina cup-to-disc ratio computation | |
CN109102004A (en) | Cotton-plant pest-insects method for identifying and classifying and device | |
CN111353371A (en) | Shoreline extraction method based on spaceborne SAR images | |
CN114943744A (en) | Edge detection method based on local Otsu thresholding | |
CN111369570A (en) | Multi-target detection tracking method for video image | |
Bora et al. | A new efficient color image segmentation approach based on combination of histogram equalization with watershed algorithm | |
CN117593540A (en) | Pressure injury staged identification method based on intelligent image identification technology | |
Chandra et al. | A survey on advanced segmentation techniques in image processing applications | |
CN109101985A (en) | It is a kind of based on adaptive neighborhood test image mismatch point to elimination method | |
Liu et al. | Bowstring-based dual-threshold computation method for adaptive Canny edge detector | |
CN112967304A (en) | Edge detection algorithm for multi-edge window collaborative filtering |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |