CN110309687B - A two-dimensional code image correction method and correction device - Google Patents
A two-dimensional code image correction method and correction device Download PDFInfo
- Publication number
- CN110309687B CN110309687B CN201910602805.6A CN201910602805A CN110309687B CN 110309687 B CN110309687 B CN 110309687B CN 201910602805 A CN201910602805 A CN 201910602805A CN 110309687 B CN110309687 B CN 110309687B
- Authority
- CN
- China
- Prior art keywords
- dimensional code
- code image
- target
- boundary
- vertex positions
- 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.)
- Active
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06K—GRAPHICAL DATA READING; PRESENTATION OF DATA; RECORD CARRIERS; HANDLING RECORD CARRIERS
- G06K7/00—Methods or arrangements for sensing record carriers, e.g. for reading patterns
- G06K7/10—Methods or arrangements for sensing record carriers, e.g. for reading patterns by electromagnetic radiation, e.g. optical sensing; by corpuscular radiation
- G06K7/14—Methods or arrangements for sensing record carriers, e.g. for reading patterns by electromagnetic radiation, e.g. optical sensing; by corpuscular radiation using light without selection of wavelength, e.g. sensing reflected white light
- G06K7/1404—Methods for optical code recognition
- G06K7/1408—Methods for optical code recognition the method being specifically adapted for the type of code
- G06K7/1417—2D bar codes
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06K—GRAPHICAL DATA READING; PRESENTATION OF DATA; RECORD CARRIERS; HANDLING RECORD CARRIERS
- G06K7/00—Methods or arrangements for sensing record carriers, e.g. for reading patterns
- G06K7/10—Methods or arrangements for sensing record carriers, e.g. for reading patterns by electromagnetic radiation, e.g. optical sensing; by corpuscular radiation
- G06K7/14—Methods or arrangements for sensing record carriers, e.g. for reading patterns by electromagnetic radiation, e.g. optical sensing; by corpuscular radiation using light without selection of wavelength, e.g. sensing reflected white light
- G06K7/1404—Methods for optical code recognition
- G06K7/1439—Methods for optical code recognition including a method step for retrieval of the optical code
- G06K7/1452—Methods for optical code recognition including a method step for retrieval of the optical code detecting bar code edges
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06K—GRAPHICAL DATA READING; PRESENTATION OF DATA; RECORD CARRIERS; HANDLING RECORD CARRIERS
- G06K7/00—Methods or arrangements for sensing record carriers, e.g. for reading patterns
- G06K7/10—Methods or arrangements for sensing record carriers, e.g. for reading patterns by electromagnetic radiation, e.g. optical sensing; by corpuscular radiation
- G06K7/14—Methods or arrangements for sensing record carriers, e.g. for reading patterns by electromagnetic radiation, e.g. optical sensing; by corpuscular radiation using light without selection of wavelength, e.g. sensing reflected white light
- G06K7/1404—Methods for optical code recognition
- G06K7/146—Methods for optical code recognition the method including quality enhancement steps
Landscapes
- Engineering & Computer Science (AREA)
- Physics & Mathematics (AREA)
- Toxicology (AREA)
- Health & Medical Sciences (AREA)
- Electromagnetism (AREA)
- General Health & Medical Sciences (AREA)
- Artificial Intelligence (AREA)
- Computer Vision & Pattern Recognition (AREA)
- General Physics & Mathematics (AREA)
- Theoretical Computer Science (AREA)
- Quality & Reliability (AREA)
- Image Analysis (AREA)
- Image Processing (AREA)
Abstract
Description
技术领域technical field
本发明属于二维码识别技术领域,更具体地,涉及一种二维码图像的校正方法及校正装置。The invention belongs to the technical field of two-dimensional code identification, and more particularly, relates to a two-dimensional code image correction method and correction device.
背景技术Background technique
二维码是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形,以记录数据符号信息;在代码编制上巧妙地利用构成计算机内部逻辑基础的“0”、“1”比特流的概念,使用若干个与二进制相对应的几何形体来表示文字数值信息,通过图像输入设备或光电扫描设备自动识读以实现信息自动处理。二维码具有条码技术的一些共性:每种码制有其特定的字符集;每个字符占有一定的宽度;具有一定的校验功能等。同时,还具有对不同行的信息自动识别功能及处理图形旋转变化等特点。A two-dimensional code is a black and white pattern distributed on a plane (two-dimensional direction) with a certain specific geometric pattern according to a certain rule to record data symbol information; in the code compilation, the "0" which constitutes the internal logic foundation of the computer is cleverly used. ”, “1” bit stream concept, using several geometric shapes corresponding to binary to represent text and numerical information, which can be automatically read by image input device or photoelectric scanning device to realize automatic information processing. Two-dimensional code has some common features of barcode technology: each code system has its specific character set; each character occupies a certain width; has a certain verification function, etc. At the same time, it also has the characteristics of automatic identification of information of different lines and processing of graphics rotation changes.
常用的二维码码制有:PDF417,Data Matrix,QR Code,Code 49,Code16K,Codeone等。其具有信息容量大、编码范围广、容错能力强、译码可靠性高、成本低、保密性高和防伪性好等特点。在信息获取、网站跳转、广告推送、手机电商、防伪溯源、优惠促销、会员管理和手机支付等领域发挥着重要的作用。Commonly used two-dimensional code systems are: PDF417, Data Matrix, QR Code, Code 49, Code16K, Codeone, etc. It has the characteristics of large information capacity, wide coding range, strong fault tolerance, high decoding reliability, low cost, high confidentiality and good anti-counterfeiting performance. It plays an important role in information acquisition, website redirection, advertisement push, mobile e-commerce, anti-counterfeiting traceability, preferential promotion, membership management and mobile payment.
不过,在打印二维码过程中,由于设备精度等原因,会引起二维码边缘模糊以及不同程度的形变,较难实现定位与识别,导致识别时间过长,甚至无法识别,影响二维码的使用。However, in the process of printing the QR code, due to equipment accuracy and other reasons, the edges of the QR code will be blurred and deformed to varying degrees. It is difficult to achieve positioning and recognition, resulting in too long recognition time or even inability to recognize, affecting the QR code. usage of.
鉴于此,克服该现有技术所存在的缺陷是本技术领域亟待解决的问题。In view of this, overcoming the defects of the prior art is an urgent problem to be solved in the technical field.
发明内容SUMMARY OF THE INVENTION
针对现有技术的以上缺陷或改进需求,本发明提供了一种二维码图像的校正方法及校正装置,其目的在于通过两种不同的算法得到二维码图像的两组顶点位置,并将两组顶点位置中的同类顶点进行调和,得到目标顶点位置,从而确定二维码图像的边界点,通过两种算法可以提高顶点位置确定的准确性,最后再根据目标顶点位置进行校正,可以校正二维码图像的形变,以便于对二维码进行识别。In view of the above defects or improvement requirements of the prior art, the present invention provides a two-dimensional code image correction method and correction device, the purpose of which is to obtain two sets of vertex positions of the two-dimensional code image through two different algorithms, and to The similar vertices in the two sets of vertex positions are reconciled to obtain the target vertex position, thereby determining the boundary point of the two-dimensional code image. The accuracy of vertex position determination can be improved through two algorithms, and finally the correction is performed according to the target vertex position, which can be corrected. The deformation of the two-dimensional code image to facilitate the identification of the two-dimensional code.
为实现上述目的,按照本发明的一个方面,提供了二维码图像的校正方法,所述校正方法包括:In order to achieve the above object, according to an aspect of the present invention, a method for calibrating a two-dimensional code image is provided, and the method for calibrating includes:
对待校正二维码图像进行处理,得到所述待校正二维码图像的边界轮廓;processing the two-dimensional code image to be corrected to obtain the boundary contour of the two-dimensional code image to be corrected;
采用第一算法对所述边界轮廓进行处理,得到第一组顶点位置,采用第二算法对所述边界轮廓进行处理,得到第二组顶点位置;The first algorithm is used to process the boundary contour to obtain a first set of vertex positions, and the second algorithm is used to process the boundary contour to obtain a second set of vertex positions;
将所述第一组顶点位置与所述第二组顶点位置中的同类顶点位置进行调和,得到目标顶点位置;Reconcile the vertex positions of the first group with the same vertex positions in the second set of vertex positions to obtain the target vertex position;
基于所述目标顶点位置对所述待校正二维码图像进行校正,得到目标二维码图像。Correcting the to-be-corrected two-dimensional code image based on the target vertex position to obtain a target two-dimensional code image.
优选地,所述采用第一算法对所述边界轮廓进行处理,得到第一组顶点位置,采用第二算法对所述边界轮廓进行处理,得到第二组顶点位置之后,还包括:Preferably, after using the first algorithm to process the boundary contour to obtain the first group of vertex positions, and using the second algorithm to process the boundary contour to obtain the second group of vertex positions, the method further includes:
获取所述第一组顶点位置和所述第二组顶点位置之间的相似度;obtaining the similarity between the first set of vertex positions and the second set of vertex positions;
当所述第一组顶点位置与所述第二组顶点位置的相似度不小于预设的相似度阈值时,执行将所述第一组顶点位置与所述第二组顶点位置中的同类顶点位置进行调和,得到目标顶点位置的步骤。When the similarity between the first group of vertex positions and the second group of vertex positions is not less than a preset similarity threshold, perform a comparison between the first group of vertex positions and the same type of vertices in the second group of vertex positions The steps of reconciling the positions to get the target vertex position.
优选地,所述第一组顶点位置中包括四个顶点位置,所述第二组顶点位置中包括四个顶点位置,获取所述第一组顶点位置和所述第二组顶点位置之间的相似度包括:Preferably, the first set of vertex positions includes four vertex positions, the second set of vertex positions includes four vertex positions, and the distance between the first set of vertex positions and the second set of vertex positions is obtained. Similarities include:
分别获取所述第一组顶点位置所形成的第一四边形和所述第二组顶点所形成的第二四边形;respectively acquiring the first quadrilateral formed by the first group of vertex positions and the second quadrilateral formed by the second group of vertices;
分别获取所述第一四边形和所述第二四边形所形成的交区域的面积,以及所述第一四边形和所述第二四边形所形成的并区域的面积;respectively acquiring the area of the intersection area formed by the first quadrilateral and the second quadrilateral, and the area of the union area formed by the first quadrilateral and the second quadrilateral;
计算所述交区域的面积与所述并区域的面积的交并比,通过所述交并比标记所述第一组顶点位置和所述第二组顶点位置之间的相似度。The intersection ratio of the area of the intersection area and the area of the merge area is calculated, and the similarity between the first set of vertex positions and the second set of vertex positions is marked by the intersection ratio.
优选地,所述第一算法为LSD线段检测算法,所述第一组顶点位置包括四个顶点位置,所述采用第一算法对所述边界轮廓进行处理,得到第一组顶点位置包括:Preferably, the first algorithm is an LSD line segment detection algorithm, the first set of vertex positions includes four vertex positions, and the use of the first algorithm to process the boundary contour to obtain the first set of vertex positions includes:
采用所述LSD线段检测算法对所述边界轮廓进行处理,得到多条边界线;Using the LSD line segment detection algorithm to process the boundary contour to obtain a plurality of boundary lines;
判断所述边界线的数目是否大于四条,若所述边界线的数目大于四条,则将多条边界线中最长的边界线标记为目标边界线;Determine whether the number of the boundary lines is greater than four, and if the number of the boundary lines is greater than four, then mark the longest boundary line among the plurality of boundary lines as the target boundary line;
依次遍历剩余边界线的长度,选取最长的一条作为待验证边界线,计算待验证边界线的中点与目标边界线的中点之间的距离是否小于预设的距离阈值;Traverse the lengths of the remaining boundary lines in turn, select the longest one as the boundary line to be verified, and calculate whether the distance between the midpoint of the boundary line to be verified and the midpoint of the target boundary line is less than a preset distance threshold;
若不小于,则标记所述待验证边界线为目标边界线,若小于,则剔除待验证边界线,直至筛选出四条目标边界线;If it is not less than, mark the boundary line to be verified as the target boundary line, if it is less than, remove the boundary line to be verified until four target boundary lines are screened;
获取四条目标边界线的交点,得到四个顶点位置,从而得到第一组顶点位置。Obtain the intersection of the four target boundary lines, and obtain the four vertex positions, thereby obtaining the first set of vertex positions.
优选地,所述第二算法为霍夫变换,所述第二组顶点位置包括四个顶点位置,所述采用第二算法对所述边界轮廓进行处理,得到第二组顶点位置包括:Preferably, the second algorithm is Hough transform, the second set of vertex positions includes four vertex positions, and the second algorithm is used to process the boundary contour to obtain the second set of vertex positions including:
对所述边界轮廓进行霍夫变换,得到边界线段簇;Hough transform is performed on the boundary contour to obtain boundary line segment clusters;
对边界线段簇进行斜率和截距二分聚类,得到上边界线段簇、下边界线段簇、左边界线段簇和右边界线段簇;Perform slope and intercept binary clustering on the boundary line segment clusters to obtain upper boundary line segment clusters, lower boundary line segment clusters, left boundary line segment clusters and right boundary line segment clusters;
分别对上边界线段簇、下边界线段簇、左边界线段簇和右边界线段簇进行直线拟合,得到四条目标边界线;Perform straight line fitting on the upper boundary line segment cluster, the lower boundary line segment cluster, the left boundary line segment cluster and the right boundary line segment cluster respectively, and obtain four target boundary lines;
获取四条目标边界线的交点,得到四个顶点位置,从而得到第二组顶点位置。Obtain the intersection points of the four target boundary lines, and obtain the four vertex positions, thereby obtaining the second set of vertex positions.
优选地,所述目标顶点的数目为四个,基于所述目标顶点位置对所述待校正二维码图像进行校正,得到目标二维码图像包括:Preferably, the number of the target vertices is four, and the two-dimensional code image to be corrected is corrected based on the position of the target vertex, and obtaining the target two-dimensional code image includes:
在所述待校正二维码图像上确定所述目标顶点位置,以确定所述待校正二维码图像的边界点;determining the position of the target vertex on the two-dimensional code image to be corrected to determine the boundary point of the two-dimensional code image to be corrected;
对所述待校正二维码图像进行透视变换,校正所述待校正二维码图像的形变,得到目标二维码图像。Perform perspective transformation on the to-be-corrected two-dimensional code image, correct the deformation of the to-be-corrected two-dimensional code image, and obtain a target two-dimensional code image.
优选地,所述校正方法还包括:Preferably, the calibration method further includes:
采用第三算法对所述目标二维码图像进行边缘检测得到第一边缘轮廓图,采用第四算法对所述目标二维码图像进行边缘检测得到第二边缘轮廓图;Use the third algorithm to perform edge detection on the target two-dimensional code image to obtain a first edge contour map, and use the fourth algorithm to perform edge detection on the target two-dimensional code image to obtain a second edge contour map;
将所述第一边缘轮廓图和所述第二边缘轮廓图进行融合,得到目标边缘轮廓图;Fusing the first edge contour map and the second edge contour map to obtain a target edge contour map;
通过所述目标边缘轮廓图对所述目标二维码图像进行锐化,以增强所述目标二维码图像的边缘。The target two-dimensional code image is sharpened through the target edge contour map to enhance the edge of the target two-dimensional code image.
优选地,所述第三算法为Canny边缘检测算法,所述第四算法为Sobel边缘检测算法;Preferably, the third algorithm is a Canny edge detection algorithm, and the fourth algorithm is a Sobel edge detection algorithm;
将所述第一边缘轮廓图和所述第二边缘轮廓图进行融合,得到目标边缘轮廓图包括:The first edge contour map and the second edge contour map are fused to obtain the target edge contour map including:
将所述第一边缘轮廓图和所述第二边缘轮廓图按照预设的比例进行像素叠加,得到目标边缘轮廓。The first edge contour map and the second edge contour map are pixel superimposed according to a preset ratio to obtain a target edge contour.
优选地,所述对待校正二维码图像进行处理,得到所述待校正二维码图像的边界轮廓包括:Preferably, the processing of the to-be-corrected two-dimensional code image to obtain the boundary contour of the to-be-corrected two-dimensional code image includes:
对待校正二维码图像进行二值化处理;Binarize the QR code image to be corrected;
对经过二值化处理的待校正二维码图像进行形态学腐蚀,得到所述待校正二维码图像的边界轮廓。Morphological corrosion is performed on the two-dimensional code image to be corrected that has undergone the binarization process to obtain the boundary contour of the two-dimensional code image to be corrected.
按照本发明的另一方面,提供了一种校正装置,包括至少一个处理器;以及,与所述至少一个处理器通信连接的存储器;其中,所述存储器存储有可被所述至少一个处理器执行的指令,所述指令被程序设置为执行本发明所述的校正方法。According to another aspect of the present invention, there is provided a correction device, comprising at least one processor; and, a memory communicatively connected to the at least one processor; wherein the memory stores information that can be used by the at least one processor Instructions to be executed, the instructions are programmed to perform the calibration method of the present invention.
总体而言,通过本发明所构思的以上技术方案与现有技术相比,具有如下有益效果:本发明提供一种二维码图像的校正方法及校正装置,该校正方法包括对待校正二维码图像进行处理,得到待校正二维码图像的边界轮廓;采用第一算法对边界轮廓进行处理,得到第一组顶点位置,采用第二算法对边界轮廓进行处理,得到第二组顶点位置;将第一组顶点位置与第二组顶点位置中的同类顶点位置进行调和,得到目标顶点位置;基于目标顶点位置对待校正二维码图像进行校正,得到目标二维码图像。本发明通过两种不同的算法得到二维码图像的两组顶点位置,并将两组顶点位置中的同类顶点进行调和,得到目标顶点位置,从而确定二维码图像的边界点,通过两种算法可以提高顶点位置确定的准确性,最后再根据目标顶点位置进行校正,可以校正二维码图像的形变,以便于对二维码进行识别。In general, compared with the prior art, the above technical solutions conceived by the present invention have the following beneficial effects: the present invention provides a two-dimensional code image correction method and correction device, the correction method includes the two-dimensional code to be corrected. The image is processed to obtain the boundary contour of the two-dimensional code image to be corrected; the first algorithm is used to process the boundary contour to obtain the first group of vertex positions, and the second algorithm is used to process the boundary contour to obtain the second group of vertex positions; The first group of vertex positions and the similar vertex positions in the second group of vertex positions are reconciled to obtain the target vertex position; based on the target vertex position, the two-dimensional code image to be corrected is corrected to obtain the target two-dimensional code image. The invention obtains two sets of vertex positions of the two-dimensional code image through two different algorithms, and reconciles the same vertices in the two sets of vertex positions to obtain the target vertex position, thereby determining the boundary points of the two-dimensional code image. The algorithm can improve the accuracy of vertex position determination, and finally correct according to the target vertex position, which can correct the deformation of the two-dimensional code image, so as to facilitate the identification of the two-dimensional code.
进一步地,通过多算子边缘检测获取二维码图像的边缘轮廓图像,以对二维码图像进行锐化,增强二维码图像的边缘轮廓,可以消除二维码边缘模糊的影响,得到背景干净、条码清晰以及对比度强的二维码图像,可以有效提高后续识别的准确性。Further, the edge contour image of the two-dimensional code image is obtained through multi-operator edge detection, so as to sharpen the two-dimensional code image and enhance the edge contour of the two-dimensional code image, which can eliminate the influence of the blurred edge of the two-dimensional code and obtain the background. A QR code image with clean, clear barcode and strong contrast can effectively improve the accuracy of subsequent identification.
附图说明Description of drawings
为了更清楚地说明本发明实施例的技术方案,下面将对本发明实施例中所需要使用的附图作简单地介绍。显而易见地,下面所描述的附图仅仅是本发明的一些实施例,对于本领域普通技术人员来讲,在不付出创造性劳动的前提下,还可以根据这些附图获得其他的附图。In order to describe the technical solutions of the embodiments of the present invention more clearly, the following briefly introduces the accompanying drawings that need to be used in the embodiments of the present invention. Obviously, the drawings described below are only some embodiments of the present invention, and for those of ordinary skill in the art, other drawings can also be obtained from these drawings without creative effort.
图1是本发明实施例提供的一种二维码图像的校正方法的流程示意图;1 is a schematic flowchart of a method for correcting a two-dimensional code image according to an embodiment of the present invention;
图2是本发明实施例提供的一种待校正二维码图像的边界轮廓的示意图;2 is a schematic diagram of a boundary outline of a two-dimensional code image to be corrected according to an embodiment of the present invention;
图3是本发明实施例提供的一种边界线段簇转换为边界线的示意图;3 is a schematic diagram of converting a boundary line segment cluster into a boundary line provided by an embodiment of the present invention;
图4是本发明实施例提供的两个四边形之间的交区域和并区域的结构示意图;4 is a schematic structural diagram of an intersection area and a merge area between two quadrilaterals provided by an embodiment of the present invention;
图5是本发明实施例提供的图4中的两个四边形的并区域的结构示意图;FIG. 5 is a schematic structural diagram of the merged region of two quadrilaterals in FIG. 4 according to an embodiment of the present invention;
图6是本发明实施例提供的图4中的两个四边形的交区域的结构示意图;6 is a schematic structural diagram of an intersection area of two quadrilaterals in FIG. 4 according to an embodiment of the present invention;
图7是本发明实施例提供的一种第一组顶点位置和第二组顶点位置相互调和得到的目标顶点位置的结构示意图;7 is a schematic structural diagram of a target vertex position obtained by reconciling a first group of vertex positions and a second group of vertex positions according to an embodiment of the present invention;
图8是本发明实施例提供的校正后的二维码图像的示意图;8 is a schematic diagram of a corrected two-dimensional code image provided by an embodiment of the present invention;
图9是本发明实施例提供的图1中的步骤102的实现方式之一的流程示意图;FIG. 9 is a schematic flowchart of one of the implementation manners of
图10是本发明实施例提供的一种边界线段簇的分布示意图;10 is a schematic diagram of the distribution of a boundary line segment cluster provided by an embodiment of the present invention;
图11是本发明实施例提供的另一种二维码图像的校正方法的流程示意图;11 is a schematic flowchart of another method for calibrating a two-dimensional code image provided by an embodiment of the present invention;
图12是本发明实施例提供的第一边缘轮廓图的示意图;12 is a schematic diagram of a first edge contour diagram provided by an embodiment of the present invention;
图13是本发明实施例提供的第二边缘轮廓图的示意图;13 is a schematic diagram of a second edge contour diagram provided by an embodiment of the present invention;
图14是本发明实施例提供的一种校正装置的结构示意图。FIG. 14 is a schematic structural diagram of a calibration apparatus provided by an embodiment of the present invention.
具体实施方式Detailed ways
为了使本发明的目的、技术方案及优点更加清楚明白,以下结合附图及实施例,对本发明进行进一步详细说明。应当理解,此处所描述的具体实施例仅仅用以解释本发明,并不用于限定本发明。In order to make the objectives, technical solutions and advantages of the present invention clearer, the present invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention, but not to limit the present invention.
此外,下面所描述的本发明各个实施方式中所涉及到的技术特征只要彼此之间未构成冲突就可以相互组合。In addition, the technical features involved in the various embodiments of the present invention described below can be combined with each other as long as they do not conflict with each other.
实施例1:Example 1:
在实际应用场景下,由于打印设备精度原因或者拍照角度原因,经常会出现获取到的二维码图像发生形变,影响后续的二维码识别。为了解决前述问题,本实施例提供一种二维码图像的校正方法,可以对发生形变的二维码图像进行校正,将二维码图像转换为形状标准的图像,以便于后续的二维码识别。In practical application scenarios, due to the accuracy of the printing equipment or the angle of the camera, the obtained QR code image is often deformed, which affects subsequent QR code recognition. In order to solve the aforementioned problems, this embodiment provides a method for correcting a two-dimensional code image, which can correct a deformed two-dimensional code image, and convert the two-dimensional code image into a standard shape image, so as to facilitate the subsequent two-dimensional code image. identify.
参阅图1,本实施例的二维码图像的校正方法包括如下步骤:Referring to FIG. 1 , the method for calibrating a two-dimensional code image in this embodiment includes the following steps:
在步骤101中,对待校正二维码图像进行处理,得到所述待校正二维码图像的边界轮廓。In
其中,待校正二维码图像主要是具有形变或畸变的二维码图像,二维码的形变可能是由于打印设备引起的,导致二维码图像引起形变或畸变,二维码的形变也有可能是用户在使用智能设备拍照或扫描二维码时,拍摄角度倾斜引起的。Among them, the two-dimensional code image to be corrected is mainly a two-dimensional code image with deformation or distortion. The deformation of the two-dimensional code may be caused by the printing equipment, causing the two-dimensional code image to cause deformation or distortion, and the deformation of the two-dimensional code may also be It is caused by a tilted shooting angle when a user takes a photo or scans a QR code with a smart device.
其中,待校正二维码图像的码制有:PDF417、Data Matrix、QR Code、Code 49、Code16K或Code one等。Among them, the code system of the QR code image to be corrected includes: PDF417, Data Matrix, QR Code, Code 49, Code16K or Code one, etc.
其中,在获取待校正二维码图像的边界轮廓之前,先对待校正二维码图像进行灰度处理和二值化处理,然后再采用形态学腐蚀处理分割出边界轮廓。Before acquiring the boundary contour of the two-dimensional code image to be corrected, grayscale processing and binarization processing are performed on the two-dimensional code image to be corrected, and then the boundary contour is segmented by morphological corrosion processing.
如图2所示,位于左侧的图为待校正二维码图像,位于右侧的图为待校正二维码图像的边界轮廓。As shown in FIG. 2 , the picture on the left is the image of the two-dimensional code to be corrected, and the picture on the right is the boundary outline of the image of the two-dimensional code to be corrected.
在步骤102中,采用第一算法对所述边界轮廓进行处理,得到第一组顶点位置,采用第二算法对所述边界轮廓进行处理,得到第二组顶点位置。In
在本实施例中,如图3所示,采用两种不同的算法对边界轮廓进行处理,得到边界线段簇(图3位于左侧的图),在根据边界线段簇得到目标边界线(图3位于右侧的图),根据目标边界线之间的交点,分别得到第一组顶点位置和第二组顶点位置,其中,每一组顶点位置均包括多个顶点的位置坐标,顶点可以理解为二维码图像的边界点,通过顶点可以确定二维码图像的形状以及区域大小。In this embodiment, as shown in Figure 3, two different algorithms are used to process the boundary contour to obtain a boundary line segment cluster (the figure on the left in Figure 3), and the target boundary line is obtained according to the boundary line segment cluster (Figure 3 The figure on the right), according to the intersection between the target boundary lines, the first group of vertex positions and the second group of vertex positions are obtained respectively, wherein each group of vertex positions includes the position coordinates of multiple vertices, and the vertices can be understood as The boundary point of the two-dimensional code image, the shape and area size of the two-dimensional code image can be determined by the vertex.
在实际应用场景下,二维码图像一般呈四边形,在可选的实施例中,每一组顶点位置均包括四个顶点的位置坐标,其中,围成四边形的四条边界线之间的交点即为二维码图像的顶点(边界点)。In practical application scenarios, the two-dimensional code image is generally in the shape of a quadrilateral. In an optional embodiment, each group of vertex positions includes the position coordinates of the four vertices, wherein the intersection between the four boundary lines enclosing the quadrilateral is is the vertex (boundary point) of the QR code image.
在可选的方案中,所述第一算法为LSD(Line Segment Detector,简写为LSD)直线检测,第二种算法为霍夫变换。在其他方案中,第一算法和第二算法的具体类型也可以为其他算法,在此,不做具体限定。In an optional solution, the first algorithm is LSD (Line Segment Detector, LSD for short) straight line detection, and the second algorithm is Hough transform. In other solutions, the specific types of the first algorithm and the second algorithm may also be other algorithms, which are not specifically limited here.
在步骤103中,将所述第一组顶点位置与所述第二组顶点位置中的同类顶点位置进行调和,得到目标顶点位置。In
其中,同类顶点位置指的是在第一组顶点位置与所述第二组顶点位置中,距离最近的两个顶点。Wherein, the vertex positions of the same type refer to two vertices that are closest to each other in the first set of vertex positions and the second set of vertex positions.
其中,顶点位置可以依据预设坐标系下的坐标而确定。Wherein, the vertex position may be determined according to the coordinates in the preset coordinate system.
在可选的实施例中,在预设的坐标系下,根据同类顶点的横纵坐标进行调和平均,得到目标顶点坐标。In an optional embodiment, in a preset coordinate system, the coordinates of the target vertex are obtained by performing harmonic averaging according to the horizontal and vertical coordinates of vertices of the same type.
在步骤104中,基于所述目标顶点位置对所述待校正二维码图像进行校正,得到目标二维码图像。In step 104, the to-be-corrected two-dimensional code image is corrected based on the target vertex position to obtain a target two-dimensional code image.
在本实施例中,在所述待校正二维码图像上确定所述目标顶点位置,以确定所述待校正二维码图像的边界点;然后,对所述待校正二维码图像进行透视变换,校正所述待校正二维码图像的形变,得到目标二维码图像。In this embodiment, the position of the target vertex is determined on the two-dimensional code image to be corrected to determine the boundary point of the two-dimensional code image to be corrected; then, the perspective of the two-dimensional code image to be corrected is performed Transform, correct the deformation of the two-dimensional code image to be corrected, and obtain the target two-dimensional code image.
本发明通过两种不同的算法得到二维码图像的两组顶点位置,并将两组顶点位置中的同类顶点进行调和,得到目标顶点位置,从而确定二维码图像的边界点,通过两种算法可以提高顶点位置确定的准确性,最后再根据目标顶点位置进行校正,可以校正二维码图像的形变,以便于对二维码进行识别。The invention obtains two sets of vertex positions of the two-dimensional code image through two different algorithms, and reconciles the same vertices in the two sets of vertex positions to obtain the target vertex position, thereby determining the boundary points of the two-dimensional code image. The algorithm can improve the accuracy of vertex position determination, and finally correct according to the target vertex position, which can correct the deformation of the two-dimensional code image, so as to facilitate the identification of the two-dimensional code.
在实际应用场景下,虽然第一算法和第二算法获取顶点位置的具体方法不同,但是对于同一个待校正二维码图像,不论哪种算法针对同一个顶点,获得的顶点位置相差不会很大,否则有可能是顶点位置的识别与标定出错而引起的。为了避免由于算法本身的问题导致顶点位置识别出错,提高准确率,在优选的实施例中,需要对第一组顶点位置和第二组顶点位置进行相似度匹配,在满足相似度条件下,再执行后续步骤。In practical application scenarios, although the specific methods of obtaining the vertex positions of the first algorithm and the second algorithm are different, for the same two-dimensional code image to be corrected, no matter which algorithm targets the same vertex, the obtained vertex positions will not be very different. Otherwise, it may be caused by errors in the identification and calibration of the vertex positions. In order to avoid errors in the identification of the vertex positions due to the problem of the algorithm itself and improve the accuracy, in a preferred embodiment, it is necessary to perform similarity matching between the positions of the first group of vertices and the positions of the second group of vertices. Perform the next steps.
本实施例提供了另一种二维码图像的校正方法,在步骤102之后,还包括获取所述第一组顶点位置和所述第二组顶点位置之间的相似度,当所述第一组顶点位置与所述第二组顶点位置的相似度不小于预设的相似度阈值时,再执行步骤103。This embodiment provides another method for calibrating a two-dimensional code image. After
在可选的实施例中,可以采用交并比的方式获取所述第一组顶点位置和所述第二组顶点位置之间的相似度。In an optional embodiment, the degree of similarity between the first set of vertex positions and the second set of vertex positions may be obtained by means of an intersection ratio.
具体地,第一组顶点位置中包括四个顶点位置,所述第二组顶点位置中包括四个顶点位置,获取所述第一组顶点位置和所述第二组顶点位置之间的相似度包括:分别获取所述第一组顶点位置所形成的第一四边形和所述第二组顶点所形成的第二四边形;分别获取所述第一四边形和所述第二四边形所形成的交区域的面积,以及所述第一四边形和所述第二四边形所形成的并区域的面积;计算所述交区域的面积与所述并区域的面积的交并比,通过所述交并比标记所述第一组顶点位置和所述第二组顶点位置之间的相似度。Specifically, the first set of vertex positions includes four vertex positions, the second set of vertex positions includes four vertex positions, and the similarity between the first set of vertex positions and the second set of vertex positions is obtained The steps include: respectively acquiring a first quadrilateral formed by the first group of vertex positions and a second quadrilateral formed by the second group of vertices; acquiring the first quadrilateral and the second quadrilateral respectively the area of the intersection area formed by the polygons, and the area of the union area formed by the first quadrilateral and the second quadrilateral; calculate the intersection of the area of the intersection area and the area of the union area Union ratio, the similarity between the first set of vertex positions and the second set of vertex positions is marked by the intersection and union ratio.
其中,预设的相似度阈值为交并比可以为0.8,即,当所述第一四边形的面积S1和所述第二四边形的面积S2的交并比不小于0.8时,所述第一组顶点位置和所述第二组顶点位置满足相似度条件。当然,预设的相似度阈值还可以为其他数值,在此,不做具体限定。The preset similarity threshold value may be an intersection ratio of 0.8, that is, when the intersection ratio of the area S1 of the first quadrilateral and the area S2 of the second quadrilateral is not less than 0.8 , the first set of vertex positions and the second set of vertex positions satisfy the similarity condition. Of course, the preset similarity threshold may also be other values, which are not specifically limited here.
其中,交并比IOU按照如下公式进行计算:IOU=(S1∩S2)/(S1∪S2)。The intersection ratio IOU is calculated according to the following formula: IOU=(S 1 ∩ S 2 )/(S 1 ∪ S 2 ).
在实际应用场景下,交并比IOU的计算过程如下:(1)根据第一组顶点位置和第二组顶点位置中的顶点坐标,分别求取第一四边形和第二四边形的边界线,并确定第一四边形的边界线和第二四边形的边界线之间的交叉点;(2)分别确定第一四边形和第二四边形的内点和外点,其中,内点定义为一个四边形的在另一个四边形内的边界点(顶点),外点定义为一个四边形在另一个四边形外的边界点(顶点);(3)依次连接内点和交叉点构成交区域,依次连接外点和交叉点构成并区域;(4)将交区域和并区域根据边界点(顶点)划分为多个三角形,求解三角形面积和;(5)交区域的面积比上并区域的面积即得交并比。In the actual application scenario, the calculation process of the intersection-union ratio IOU is as follows: (1) According to the vertex coordinates in the first group of vertex positions and the second group of vertex positions, calculate the first quadrilateral and the second quadrilateral respectively. boundary line, and determine the intersection point between the boundary line of the first quadrilateral and the boundary line of the second quadrilateral; (2) determine the inner and outer points of the first quadrilateral and the second quadrilateral respectively , where the interior point is defined as the boundary point (vertex) of one quadrilateral inside another quadrilateral, and the exterior point is defined as the boundary point (vertex) of one quadrilateral outside the other quadrilateral; (3) Connect the interior point and the intersection point in turn The intersection area is formed, and the outer points and the intersection points are connected in turn to form the merge area; (4) The intersection area and the merge area are divided into multiple triangles according to the boundary points (vertices), and the area sum of the triangles is calculated; (5) The area of the intersection area is higher than The area of the merged area is to be combined and compared.
其中,可以采用海伦公式求取三角形面积和,海伦公式如下:Among them, the Heron formula can be used to obtain the sum of the areas of the triangles, and the Heron formula is as follows:
其中,a,b,c为三角形的边长, where a, b, and c are the side lengths of the triangle,
下面结合图4~图6,进行举例说明,在此,需要说明的是,为了清楚地说明本实施的实现方案,附图中所展示的两个四边形进行了形变以及位置差异的夸大处理,在实际应用场景下,采用两种不同的算法所得到的四边形之间的位置差异是很小的。4 to 6, an example will be given. Here, it should be noted that, in order to clearly illustrate the implementation of this implementation, the two quadrilaterals shown in the accompanying drawings are deformed and the position difference is exaggerated. In practical application scenarios, the position difference between the quadrilaterals obtained by the two different algorithms is very small.
假设进行相似度判别的两个四边形分别为ABCD和A′B′C′D′,二者相交于点E、F。其中,E、F称为交叉点,B、D′为内点,A′、B′、C′、C、D、A称为外点。Assume that the two quadrilaterals for similarity determination are ABCD and A'B'C'D' respectively, and they intersect at points E and F. Among them, E and F are called intersection points, B and D' are inner points, and A', B', C', C, D, and A are called outer points.
将交叉点和外点依次连接,得到多边形A′B′C′FCDAE(如图5所示),称为并区域。以某点(可以为E)为固定点,其余点依次取按顺时针(或逆时针)连续的两点分别与E构成三角形,通过海伦公式求解三角形面积进一步求解并区域面积。Connect the intersection point and the outer point in turn to obtain the polygon A'B'C'FCDAE (as shown in Figure 5), which is called the union area. Take a certain point (it can be E) as a fixed point, and take two consecutive points clockwise (or counterclockwise) for the other points to form a triangle with E respectively, and solve the area of the triangle by Heron's formula to further solve the area area.
将交叉点和内点依次连接,得到四边形EBFD′(如图6所示),称为交区域。以某点(可以为E)为固定点,其余点依次取按顺时针(或逆时针)连续的两点分别与E构成三角形,通过海伦公式求解三角形面积进一步求解交区域面积。The intersection and interior points are connected in turn to obtain a quadrilateral EBFD' (as shown in Figure 6), which is called the intersection area. Take a certain point (it can be E) as a fixed point, and take two consecutive points clockwise (or counterclockwise) for the other points to form a triangle with E respectively, and then solve the area of the intersection area by solving the triangle area by Heron's formula.
在程序执行过程中,需要按照严格的算法执行“依次连接”,才能正确获取交区域和并区域。下面具体说明,算法实现过程:In the process of program execution, it is necessary to execute "sequential connection" according to a strict algorithm to obtain the intersection area and merge area correctly. The following describes the algorithm implementation process in detail:
对于第一多边形和第二多边形,二者的边界线之间的交点为交叉点,第一多边形包括多个顶点,位于第二多边形外部的为第一多边形的外点,位于第二多边形内部的为第一多边形的内点;第二多边形包括多个顶点,位于第一多边形外部的为第二多边形的外点,位于第一多边形内部的为第二多边形的内点。For the first polygon and the second polygon, the intersection point between the boundary lines of the two is the intersection point, the first polygon includes a plurality of vertices, and the first polygon is located outside the second polygon The outer point of the second polygon is the inner point of the first polygon; the second polygon includes a plurality of vertices, and the outer point of the second polygon is located outside the first polygon, Inside the first polygon is the interior point of the second polygon.
交区域的判定过程:以第一多边形的任意点为起点,按照预设的方向记录第一多边形的内点和交叉点对应的第一点序,其中,预设的方向为顺时针或者逆时针;以第二多边形的任意点为起点,按照预设的方向记录第二多边形的内点和交叉点对应的第二点序,其中,预设的方向为顺时针或者逆时针。对第一点序和第二点序进行预设的综合处理,得到交区域上点的连接顺序,从而得到交区域。具体地,在第一点序中确定交叉点点序,在第二点序中确定交叉点序中的内点点序,将内点点序插入到第一点序中的交叉点序之间,得到交区域上点的连接顺序。The determination process of the intersection area: take any point of the first polygon as the starting point, and record the first point sequence corresponding to the interior point of the first polygon and the intersection point according to the preset direction, wherein the preset direction is the order Clockwise or counterclockwise; take any point of the second polygon as the starting point, and record the second point sequence corresponding to the interior points and intersection points of the second polygon according to the preset direction, wherein the preset direction is clockwise Or counterclockwise. Preset comprehensive processing is performed on the first point sequence and the second point sequence to obtain the connection sequence of points on the intersection area, thereby obtaining the intersection area. Specifically, the intersection point sequence is determined in the first point sequence, the interior point sequence in the intersection point sequence is determined in the second point sequence, and the interior point sequence is inserted between the intersection point sequences in the first point sequence to obtain the intersection point sequence. The connection order of points on the area.
并区域的判定过程:以第一多边形的任意点为起点,按照预设的方向记录第一多边形的外点和交叉点对应的第三点序,其中,预设的方向为顺时针或者逆时针;以第二多边形的任意点为起点,按照预设的方向记录第二多边形的外点和交叉点对应的第四点序,其中,预设的方向为顺时针或者逆时针。对第三点序和第四点序进行预设的综合处理,得到并区域上点的连接顺序,从而得到并区域。具体地,在第三点序中确定交叉点点序,在第四点序中确定交叉点序中的外点点序,将外点点序插入到第三点序中的交叉点序之间,得到并区域上点的连接顺序。The process of judging the merged area: take any point of the first polygon as the starting point, and record the third point sequence corresponding to the outer point and the intersection point of the first polygon according to the preset direction, wherein the preset direction is the order Clockwise or counterclockwise; take any point of the second polygon as the starting point, and record the fourth point sequence corresponding to the outer point and the intersection point of the second polygon according to the preset direction, wherein the preset direction is clockwise Or counterclockwise. Preset comprehensive processing is performed on the third point sequence and the fourth point sequence, and the connection sequence of the points on the merged area is obtained, thereby obtaining the merged area. Specifically, the intersection point sequence is determined in the third point sequence, the outer point sequence in the intersection point sequence is determined in the fourth point sequence, and the outer point sequence is inserted between the intersection point sequences in the third point sequence to obtain the The connection order of points on the area.
下面结合图4,具体说明在进行相似度判别时,如何获取交区域和并区域。在此,需要说明的是,在本实施例中,是以四边形进行举例说明的,在实际应用场景下,前述算法过程适用于任意多边形。In the following, in conjunction with FIG. 4 , how to obtain the intersection area and the merge area when the similarity is discriminated will be described in detail. Here, it should be noted that, in this embodiment, a quadrilateral is used as an example for illustration, and in an actual application scenario, the foregoing algorithm process is applicable to any polygon.
如图4所示,假设进行相似度判别的两个四边形分别为第一四边形ABCD和第二四边形A′B′C′D′,二者相交于点E、F。其中,E、F称为交叉点,B为第一四边形的内点,D′为第二四边形的内点,A′、B′、C′为第二四边形的外点,C、D、A为第一四边形的外点。As shown in FIG. 4 , it is assumed that the two quadrilaterals for similarity determination are the first quadrilateral ABCD and the second quadrilateral A′B′C′D′ respectively, and the two intersect at points E and F. Among them, E and F are called intersection points, B is the interior point of the first quadrilateral, D' is the interior point of the second quadrilateral, and A', B', and C' are the exterior points of the second quadrilateral , C, D, and A are the outer points of the first quadrilateral.
交区域判定:以第一四边形ABCD上任意点为起点,顺时针记录其上的内点和交叉点点序,可为EBF(第一点序);以第二四边形A′B′C′D′上任意点为起点,顺时针记录其上的内点和交叉点点序,可为FD′E(第二点序)。综合第一点序和第二点序,得到交区域上点的连接顺序为EBFD′。具体地,在第一点序中,EF为交叉点点序,在第二点序中,D′为交叉点点序之间的内点点序,按照交叉点点序循环的方式(EFEF……),将D′插入到第一点序的交叉点点序FE之间,得到EBFD′。在此,需要说明的是,交叉点点序EF和交叉点点序FE实质上为同一个交叉点点序,保证能够按照EFEF……方式循环即可。Intersection area determination: take any point on the first quadrilateral ABCD as the starting point, record the sequence of interior points and intersection points clockwise, which can be EBF (first point sequence); take the second quadrilateral A'B' as the starting point Any point on C'D' is the starting point, and the sequence of interior points and intersection points on it is recorded clockwise, which can be FD'E (second sequence of points). By synthesizing the first point sequence and the second point sequence, the connection sequence of the points on the intersection area is obtained as EBFD'. Specifically, in the first point sequence, EF is the intersection point sequence, and in the second point sequence, D' is the interior point sequence between the intersection point sequences. D' is inserted between the intersection point sequences FE of the first point sequence to obtain EBFD'. Here, it should be noted that the intersection point sequence EF and the intersection point sequence FE are substantially the same intersection point sequence, and it is sufficient to ensure that the cycle can be performed in the manner of EFEF.
并区域判定:以第一四边形ABCD上任意点为起点,顺时针记录其上的外点和交叉点点序,可为AEFCD(第三点序);Combined area determination: take any point on the first quadrilateral ABCD as the starting point, record the point sequence of the outer points and intersection points clockwise, which can be AEFCD (third point sequence);
以第二四边形A′B′C′D′上任意点为起点,顺时针记录其上的外点和交叉点点序,可为A′B′C′FE(第四点序)。综合第三点序和第四点序,得到并区域上点的连接顺序为AEA′B′C′FCD。具体地,在第三点序中,EF为交叉点点序,在第四点序中,A′B′C′为交叉点点序之间的外点点序,按照交叉点点序循环的方式(EFEF……),将A′B′C′插入到第一点序的交叉点点序EF之间,得到AEA′B′C′FCD。前述以交叉点为两个进行举例说明的,在具体应用场景中,交叉点的数目可能会多于两个,依次取连续两个交叉点(注意,是循环点序)按照上述进行处理即可,在此,不再赘述。Taking any point on the second quadrilateral A'B'C'D' as the starting point, record the point sequence of the outer points and intersection points clockwise, which can be A'B'C'FE (the fourth point sequence). Combining the third point sequence and the fourth point sequence, the connection sequence of the points on the merged region is obtained as AEA'B'C'FCD. Specifically, in the third point sequence, EF is the intersection point sequence, and in the fourth point sequence, A'B'C' is the outer point sequence between the intersection point sequences, according to the intersection point sequence cycle (EFEF... ...), insert A'B'C' into the intersection point sequence EF of the first point sequence to obtain AEA'B'C'FCD. The above-mentioned two intersection points are used as an example. In a specific application scenario, the number of intersection points may be more than two. Take two consecutive intersection points in turn (note that it is a cyclic point sequence) and process it according to the above. , and will not be repeated here.
本实施例的方法可以很好地通过程序实现,以便于计算机理解依次连接的意思,从而保证能够准确判定交区域和并区域,完成相似度判断。The method of this embodiment can be well implemented by a program, so that the computer can understand the meaning of sequential connection, so as to ensure that the intersection area and the merge area can be accurately determined, and the similarity determination can be completed.
在通过相似度判定后,则对同类的两个点的调和平均数作为最终的边界点,同类的两个点定义为位于邻近方位的点,亦即最近的两个点;如果不通过,校正失败。其中数a和数b的调和平均数c定义为:After passing the similarity judgment, the harmonic mean of the two points of the same kind is used as the final boundary point, and the two points of the same kind are defined as the points located in the adjacent azimuths, that is, the two nearest points; if they do not pass, the correction fail. where the harmonic mean c of numbers a and b is defined as:
如图7所示,为实际情况下通过两种算法得到的四边形的位置情况,A、B、C、D为其中一个四边形的边界点(顶点),A′、B′、C′、D′为另一个四边形的边界点(顶点)。其中,A与A′、B与B′、C与C′、D与D′称为同类点,根据同类点的横纵坐标取加权平均,得到新的四边形的四个边界点,即为图中a、b、c、d,其中,a、b、c、d即为目标顶点。As shown in Figure 7, it is the position of the quadrilateral obtained by the two algorithms in the actual situation, A, B, C, D are the boundary points (vertices) of one of the quadrilaterals, A', B', C', D' is the boundary point (vertex) of another quadrilateral. Among them, A and A', B and B', C and C', D and D' are called similar points. According to the weighted average of the horizontal and vertical coordinates of similar points, the four boundary points of the new quadrilateral are obtained, that is, the graph In a, b, c, d, among them, a, b, c, d are the target vertices.
如图8所示,为基于目标顶点位置对待校正二维码图像进行校正后的二维码图像,即,在步骤104中,得到的目标二维码图像。As shown in FIG. 8 , it is the two-dimensional code image after the two-dimensional code image to be corrected is corrected based on the target vertex position, that is, the target two-dimensional code image obtained in step 104 .
在步骤101中,对待校正二维码图像进行处理,得到所述待校正二维码图像的边界轮廓具体包括:对待校正二维码图像进行灰度处理后,对待校正二维码图像进行二值化处理。然后,对待校正二维码图像进行多次形态学腐蚀运算,得到待校正二维码图像的边界轮廓。In
在可选的方案中,依次对待校正二维码图像进行n次形态学腐蚀运算,再与第n+1次形态学腐蚀运算后的图像作差,得到待校正二维码图像的边界轮廓,其中,n可以为5或者其他数值。在本实施例中通过形态学腐蚀运算,增加图像的黑色区域面积并减少图像白色区域面积,可以有效提取待校正二维码图像的边界轮廓。In an optional solution, the image of the two-dimensional code to be corrected is subjected to n times of morphological erosion operations in turn, and then the difference is made with the image after the n+1th morphological corrosion operation to obtain the boundary contour of the image of the two-dimensional code to be corrected, Among them, n can be 5 or other values. In this embodiment, through the morphological erosion operation, the area of the black area of the image is increased and the area of the white area of the image is reduced, so that the boundary contour of the two-dimensional code image to be corrected can be effectively extracted.
其中,形态学腐蚀的具体方法为:首先设置方形的结构元素S(可为5×5),利用结构元素S对二维码图像X进行腐蚀,若结构元素移动x后包含于图像前景X0,则令其仍属于图像前景X0,否则属于图像背景X1,其中图像前景X0指白色区域部分,图像背景X1指黑色区域部分。运算式如下:Among them, the specific method of morphological corrosion is: first set a square structural element S (can be 5 × 5), use the structural element S to erode the two-dimensional code image X, if the structural element is moved by x and included in the image foreground X 0 , then it still belongs to the image foreground X 0 , otherwise it belongs to the image background X 1 , wherein the image foreground X 0 refers to the white area and the image background X 1 refers to the black area. The formula is as follows:
其中,x为结构元素移动的向量距离,x的定义域为前景图像X0上的所有点到原点的距离向量。Among them, x is the vector distance that the structuring element moves, and the definition domain of x is the distance vector from all points on the foreground image X 0 to the origin.
在可选的方案中,可以采用最大类间方差法(Otsu)进行二值化,并将二维码图像X划分为图像前景X0和图像背景X1。具体地,记二维码图像为M·N大小,二维码图像中灰度值小于阈值T的像素个数记作N0(前景像素,形成图像前景X0),像素灰度值大于阈值T的像素个数记作N1(背景像素,形成图像背景X1)。则有:N0+N1=M·N。In an optional solution, the maximum inter-class variance method (Otsu) can be used for binarization, and the two-dimensional code image X is divided into an image foreground X 0 and an image background X 1 . Specifically, the size of the two-dimensional code image is M·N, the number of pixels whose gray value is less than the threshold value T in the two-dimensional code image is recorded as N0 (foreground pixels, forming the image foreground X 0 ), and the gray value of the pixels is greater than the threshold value T The number of pixels is denoted as N 1 (background pixels, forming the image background X 1 ). Then there are: N 0 +N 1 =M·N.
另记前景像素所占图像比例为w0,平均灰度为u0;背景像素所占图像比例为w1,平均灰度为u1。图像的平均灰度为u,类间方差为g,则有:Also note that the proportion of the image occupied by the foreground pixels is w 0 , and the average gray level is u 0 ; the proportion of the image occupied by the background pixels is w 1 , and the average gray level is u 1 . The average gray level of the image is u, and the inter-class variance is g, then:
w0=N0/(M·N) w1=N1/(M·N) w0+w1=1w 0 =N 0 /(M·N) w 1 =N 1 /(M·N) w 0 +w 1 =1
u=w0·u0+w1·u1 g=w0·(u0-u)2+w1·(u1-u)2 u=w 0 ·u 0 +w 1 ·u 1 g=w 0 ·(u 0 -u) 2 +w 1 ·(u 1 -u) 2
得到等价公式:g=w0·w1·(u0-u1)2,采用遍历的方法得到使类间方差最大的阈值T,即为所求二值化阈值。The equivalent formula is obtained: g=w 0 ·w 1 ·(u 0 -u 1 ) 2 , and the threshold T that maximizes the variance between classes is obtained by traversing, which is the desired binarization threshold.
在本实施例中,背景和前景之间的类间方差越大,说明构成图像的2部分的差别越大,错分概率越小。In this embodiment, the larger the inter-class variance between the background and the foreground, the larger the difference between the two parts constituting the image, and the smaller the misclassification probability.
进一步,根据获取到的二值化阈值对待校正二维码图像进行二值化处理,并将待校正二维码图像X划分为图像前景X0和图像背景X1。Further, the two-dimensional code image to be corrected is binarized according to the obtained binarization threshold, and the two-dimensional code image to be corrected X is divided into an image foreground X 0 and an image background X 1 .
在可选的实施例中,所述第一算法为LSD线段检测算法,所述第一组顶点位置包括四个顶点位置,所述采用第一算法对所述边界轮廓进行处理,获得四条边界线,求取四条边界线的交点,得到四个顶点坐标,从而得到第一组顶点位置。In an optional embodiment, the first algorithm is an LSD line segment detection algorithm, the first group of vertex positions includes four vertex positions, and the first algorithm is used to process the boundary contour to obtain four boundary lines , find the intersection of the four boundary lines, and obtain the coordinates of the four vertices, thereby obtaining the first set of vertex positions.
下面具体说明,对边界轮廓进行LSD线段检测的过程,主要包括将边界轮廓中的短直线检测出来,然后将相连的短直线拼合,得到边界线。本实施例的LSD线段检测基于传统的检测方法进行了改进,具体包括如下步骤:The following is a detailed description of the process of performing LSD line segment detection on the boundary contour, which mainly includes detecting short straight lines in the boundary contour, and then splicing the connected short straight lines to obtain a boundary line. The LSD line segment detection in this embodiment is improved based on the traditional detection method, and specifically includes the following steps:
(1)对边界轮廓进行多次形态学细化,具体方法为:设置方形结构元素S(可为3×3),利用结构元素S对边界轮廓X进行多次细化操作,运算式为:其中,X*S为击中击不中变换,解释如下:设XC是边界轮廓X的补集,结构元素S是由两个互不相交的部分S1和S2组成,即S=S1∪S2且S1∩S2=Φ(Φ代表空集)。则击中击不中变换定义为X*S=(XΘS1)∩(XCΘS2),Θ为形态学腐蚀运算。(1) Perform multiple morphological refinements on the boundary contour. The specific method is as follows: set a square structural element S (can be 3×3), and use the structural element S to perform multiple refinement operations on the boundary contour X. The operation formula is: Among them, X*S is the hit-miss transformation, which is explained as follows: Let X C be the complement of the boundary contour X, and the structuring element S is composed of two disjoint parts S 1 and S 2 , that is, S=S 1 ∪S 2 and S 1 ∩S 2 =Φ (Φ represents the empty set). Then the hit-miss transformation is defined as X*S=(XΘS 1 )∩(X C ΘS 2 ), and Θ is the morphological erosion operation.
(2)断裂边界轮廓中的线段连接点。对边界轮廓进行边缘细化后,边缘像素的8个连接区域中的邻居数量应该在以下情况中,则定义:具有两个邻居的像素是线段的中间点;具有一个邻居的像素是线段的端点;具有两个以上邻居的像素是连接线段的连接点。将具有连接点的线段在连接点处进行断裂,得到独立的边界线段。(2) Line segment connection points in the fracture boundary contour. After edge thinning of the boundary contour, the number of neighbors in the 8 connected regions of edge pixels should be defined in the following cases: a pixel with two neighbors is the midpoint of a line segment; a pixel with one neighbor is an endpoint of a line segment ; Pixels with more than two neighbors are junctions connecting line segments. Break a line segment with a connection point at the connection point to obtain an independent boundary line segment.
(3)设定高斯核尺度s(可为0.8)对边界轮廓进行高斯下采样,计算每一个点的梯度值及梯度方向。根据梯度值对所有点进行伪排序,建立状态列表,首先预设所有点为UNUSED状态(表示未被处理),然后将梯度值小于特定值ρ的点的状态设置为USED状态(表示已处理),更新状态列表,其中,特定值ρ与角度容忍度有关,ρ=2/sin(t)。为了保证所有梯度值均经过下述步骤(4)中的判定得出是否是边界线段上的点,其中,小于ρ直接设定为USED是由于梯度值过小不可能为边界线段,不需要经过判定。(3) Set the Gaussian kernel scale s (which can be 0.8) to perform Gaussian downsampling on the boundary contour, and calculate the gradient value and gradient direction of each point. All points are pseudo-sorted according to the gradient value, and a state list is established. First, all the points are preset to the UNUSED state (indicating that they have not been processed), and then the state of the points whose gradient value is less than a specific value ρ is set to the USED state (indicating that they have been processed). , update the state list, where the specific value ρ is related to the angle tolerance, ρ=2/sin(t). In order to ensure that all gradient values are determined by the following step (4) to determine whether they are points on the boundary line segment, where the value less than ρ is directly set as USED because the gradient value is too small, it is impossible to be a boundary line segment, and no need to go through determination.
(4)取出状态列表中梯度最大的点作为种子点,状态设为USED。以种子点为起点搜索周围角度承受范围[-t,t]内的点,将满足角度承受范围的点的状态改为USED,并生成包含所有点的矩形R。判断矩形R的内点密度是否大于阈值D,其中,阈值D可为0.2或其他数据;若点密度不大于阈值D,则截断矩形R,生成多个矩形,直至矩形内的点密度大于阈值D。(4) Take the point with the largest gradient in the state list as the seed point, and set the state as USED. Starting from the seed point, search for points within the surrounding angle tolerance range [-t, t], change the status of the points that meet the angle tolerance range to USED, and generate a rectangle R containing all the points. Determine whether the inner point density of the rectangle R is greater than the threshold D, where the threshold D can be 0.2 or other data; if the point density is not greater than the threshold D, the rectangle R is truncated, and multiple rectangles are generated until the point density in the rectangle is greater than the threshold D. .
(5)计算矩形R的NFA(Number of False Alarms),若小于阈值ε,则将矩形R加入输出列表;否则改变矩形R,直至NFA小于阈值ε,其中,阈值ε可为1或者其他数据。NFA计算公式如下:(5) Calculate the NFA (Number of False Alarms) of the rectangle R. If it is less than the threshold ε, add the rectangle R to the output list; otherwise, change the rectangle R until the NFA is less than the threshold ε, where the threshold ε can be 1 or other data. The NFA calculation formula is as follows:
NFA=N·PhO[k(r,I)≥k(r,i)]NFA=N·PhO[k(r,I)≥k(r,i)]
其中,N为当前大小为m·n边界轮廓图像中线段数量,为(m·n)2.5。k(r,I)为预先建立的模型对比图(contrario model),I中矩形R里同性点(alignedpt)的数量。k(r,i)为观测图像(observe image),即矩形R,i中矩形R里aligned pt的数量。Among them, N is the number of line segments in the current size m·n boundary contour image, which is (m·n) 2.5 . k(r,I) is the pre-established contrast model, and the number of aligned points in the rectangle R in I. k(r,i) is the observed image (observe image), that is, the number of aligned pts in the rectangle R in the rectangle R, i.
(6)根据长度阈值将所有的矩形R分为两类,一类是长矩形,若长宽比大于阈值γ(其中,阈值γ可以为10),则将对应的矩形加入直接候选矩形;若长宽比小于阈值γ,则沿长边中点进行切断后加入矩形R集合中,进行再次判定。另一类是短矩形,可以将其作为候选矩形、曲线一部分或短线段,并将长度小于阈值d短线段删除,其中,阈值d可以为10或者其他数值。(6) Divide all rectangles R into two categories according to the length threshold, one is long rectangle, if the aspect ratio is greater than the threshold γ (where the threshold γ can be 10), the corresponding rectangle will be added to the direct candidate rectangle; if If the aspect ratio is smaller than the threshold γ, it will be cut along the midpoint of the long side and added to the rectangular R set for re-judgment. The other type is short rectangles, which can be used as candidate rectangles, part of curves or short line segments, and the short line segments whose length is less than the threshold d are deleted, where the threshold d can be 10 or other values.
(7)通过最小二乘法拟合短矩形中的点,若短矩形中的点与拟合直线的平均距离小于阈值μ(其中,阈值μ可为0.5),则视为候选矩形。否则,将短矩形删除。(7) The points in the short rectangle are fitted by the least squares method. If the average distance between the points in the short rectangle and the fitted straight line is less than the threshold μ (where the threshold μ can be 0.5), it is regarded as a candidate rectangle. Otherwise, delete the short rectangle.
(8)直线合并阶段,对任意两候选矩形I、J合并,进行最小二乘拟合,并计算估计方差,其中,估计方差的公式如下:(8) In the straight line merging stage, any two candidate rectangles I and J are merged, and the least squares fitting is performed, and the estimated variance is calculated. The formula for the estimated variance is as follows:
其中,xi为两候选矩形中的点,L为拟合的直线方程,N为两候选矩形的总点数。Among them, x i is the point in the two candidate rectangles, L is the fitted line equation, and N is the total number of points in the two candidate rectangles.
若属于同一条直线,则估计方差应小于方差阈值,否则,拒绝将两候选矩形中的点拟合为一条直线。If they belong to the same straight line, the estimated variance should be less than the variance threshold, otherwise, the points in the two candidate rectangles are rejected to be fitted to a straight line.
其中,拒绝区域(方差阈值)如下:Among them, the rejection area (variance threshold) is as follows:
其中,ni和nj分别是两个候选矩形I、J中的点个数,μ可以取值为-2,σ0 2为1。Among them, n i and n j are the number of points in the two candidate rectangles I and J respectively, μ can take a value of -2, and σ 0 2 is 1.
(9)对候选矩形中的点进行最小二乘法拟合,得到最终的直线簇。最小二乘法拟合目标为残差最小,残差计算公式为:(9) Perform least squares fitting on the points in the candidate rectangle to obtain the final line cluster. The least squares fitting target is the smallest residual error, and the residual error calculation formula is:
其中,a,b为待求直线的斜率和截距,xi与yi为点坐标。最终解得:Among them, a and b are the slope and intercept of the line to be determined, and x i and y i are the point coordinates. The final solution is:
按照前述步骤(1)~(9)可以得到待校正二维码图像对应的边界线段簇。According to the foregoing steps (1) to (9), the boundary line segment clusters corresponding to the two-dimensional code image to be corrected can be obtained.
在本实施例中,其中,步骤(1)、步骤(2)、步骤(6)、步骤(7)、步骤(8)、步骤(9)是针对LSD线段检测算法的改进,步骤(1)消除了过粗的边界轮廓在直线检测时被认定为多条直线,以致边界判定模糊的情况;步骤(2)将不规则的线段断裂,保证后续直线检测的结果更大程度上符合要求;步骤(6)有优选矩形的作用,删除其中的短矩形,断裂其中的折线和曲线;步骤(7)将矩形中不需要的曲线删除,保证直线检测结果的正确性;步骤(8)、(9)的直线合并操作,极大程度上降低了边界线的数量,使得求解顶点位置具有可实施性。In this embodiment, step (1), step (2), step (6), step (7), step (8), step (9) are improvements to the LSD line segment detection algorithm, step (1) Eliminates the situation that the excessively thick boundary contour is identified as multiple straight lines during straight line detection, so that the boundary determination is blurred; step (2) break the irregular line segments to ensure that the results of subsequent straight line detection meet the requirements to a greater extent; step (6) It has the function of optimizing the rectangle, delete the short rectangle in it, and break the polyline and curve in it; step (7) delete the unnecessary curve in the rectangle to ensure the correctness of the straight line detection result; steps (8), (9) ), which greatly reduces the number of boundary lines, making it feasible to solve vertex positions.
在实际应用场景下,理论上,边界线段簇内有且仅有四条边界线,但是,同时可能会存在多出四条的情况,在此情况下,需要对边界线段簇进行筛选,得到四条边界线,然后根据四条边界线,得到第一组顶点位置。In the actual application scenario, in theory, there are only four boundary lines in the boundary line segment cluster, but at the same time, there may be four more boundary lines. In this case, the boundary line segment cluster needs to be screened to obtain four boundary lines. , and then obtain the first set of vertex positions according to the four boundary lines.
在可选的实施例中,参阅图9,步骤102优化方案如下:In an optional embodiment, referring to FIG. 9 , the optimization scheme of
在步骤1021中,采用所述LSD线段检测算法对所述边界轮廓进行处理,得到多条边界线。In
其中,多条边界线形成前述的边界线段簇。Among them, a plurality of boundary lines form the aforementioned boundary line segment clusters.
在步骤1022中,判断所述边界线的数目是否大于四条,若所述边界线的数目大于四条,则将多条边界线中最长的边界线标记为目标边界线。In
为了清楚说明本步骤,参阅图10,边界线段簇中包含8条边界线,并按照边界线的长短情况对每条边界线进行降序排序,其中,最长的边界线的序号为1,最短的边界线的序号为8。In order to clearly illustrate this step, referring to FIG. 10 , the boundary line segment cluster includes 8 boundary lines, and each boundary line is sorted in descending order according to the length of the boundary line. The sequence number of the boundary line is 8.
在本实施例中,首先将边界线1标记为目标边界线。In this embodiment, the
在步骤1023中,依次遍历剩余边界线的长度,选取最长的一条作为待验证边界线,计算待验证边界线的中点与目标边界线的中点之间的距离是否小于预设的距离阈值。In
在步骤1024中,若不小于,则标记所述待验证边界线为目标边界线,若小于,则剔除待验证边界线,直至筛选出四条目标边界线。In step 1024, if it is not less than, mark the boundary line to be verified as the target boundary line, and if it is less than, remove the boundary line to be verified until four target boundary lines are screened.
其中,需要进行多次遍历筛选出其他目标边界线。Among them, it is necessary to perform multiple traversal to filter out other target boundary lines.
第一次遍历:遍历剩余边界线(边界线2~边界线8)的长度,选取边界线2作为待验证边界线,计算待验证边界线(边界线2)的中点与目标边界线(边界线1)的中点之间的距离是否小于预设的距离阈值。若不小于,则标记所述待验证边界线为目标边界线,若小于,则剔除待验证边界线。其中,预设的距离阈值与二维码大小相关,假设,二维码大小为m·n,预设的距离阈值可为0.5·(m·n)0.5。The first traversal: traverse the length of the remaining boundary lines (
其中,边界线2的中点与目标边界线(边界线1)的中点之间的距离不小于预设的距离阈值,则将边界线2标记为目标边界线。Wherein, if the distance between the midpoint of the
第二次遍历:遍历剩余边界线(边界线3~边界线8)的长度,选取边界线3作为待验证边界线,计算待验证边界线(边界线3)的中点与目标边界线(边界线1和边界线2)的中点之间的距离是否小于预设的距离阈值。若不小于,则标记所述待验证边界线为目标边界线,若小于,则剔除待验证边界线。The second traversal: traverse the length of the remaining boundary lines (
其中,边界线3的中点与目标边界线(边界线2)的中点之间的距离小于预设的距离阈值,则剔除边界线3,其中,待验证边界线的中点与所有的目标边界线的中点之间的距离不小于预设的距离阈值时,才将对应的待验证边界线标记为目标边界线,否则,剔除对应待验证边界线。Wherein, if the distance between the midpoint of the
第三次遍历:遍历剩余边界线(边界线4~边界线8)的长度,选取边界线4作为待验证边界线,计算待验证边界线(边界线4)的中点与目标边界线(边界线1和边界线2)的中点之间的距离是否小于预设的距离阈值。若不小于,则标记所述待验证边界线为目标边界线,若小于,则剔除待验证边界线。The third traversal: traverse the length of the remaining boundary lines (boundary line 4 to boundary line 8), select boundary line 4 as the boundary line to be verified, and calculate the midpoint of the boundary line to be verified (boundary line 4) and the target boundary line (boundary line 4). Whether the distance between the midpoints of
其中,边界线4的中点与目标边界线(边界线1和边界线2)的中点之间的距离均不小于预设的距离阈值,则将边界线4标记为目标边界线。Wherein, if the distance between the midpoint of the boundary line 4 and the midpoint of the target boundary line (
依次类推,直至筛选出四条目标边界线。And so on, until four target boundary lines are filtered out.
在步骤1025中,获取四条目标边界线的交点,得到四个顶点位置,从而得到第一组顶点位置。In
在本实施例中,根据边界线中点的距离进行筛选,可以有效剔除彼此之间相隔比较近的边界线,有效得出最接近二维码边界轮廓的边界线,提高了检测的准确性。In this embodiment, screening is performed according to the distance between the midpoints of the boundary lines, which can effectively eliminate the boundary lines that are relatively close to each other, and effectively obtain the boundary line closest to the boundary contour of the two-dimensional code, which improves the detection accuracy.
在具体应用场景下,所述第二算法为霍夫变换,所述第二组顶点位置包括四个顶点位置,所述采用第二算法对所述边界轮廓进行处理,得到第二组顶点位置包括:对所述边界轮廓进行霍夫变换,得到边界线段簇;对边界线段簇进行斜率和截距二分聚类,得到上边界线段簇、下边界线段簇、左边界线段簇和右边界线段簇;分别对上边界线段簇、下边界线段簇、左边界线段簇和右边界线段簇进行直线拟合,得到四条目标边界线;获取四条目标边界线的交点,得到四个顶点位置,从而得到第二组顶点位置。In a specific application scenario, the second algorithm is Hough transform, the second set of vertex positions includes four vertex positions, the second algorithm is used to process the boundary contour, and the obtained second set of vertex positions includes: : perform Hough transform on the boundary contour to obtain the boundary line segment cluster; perform slope and intercept binary clustering on the boundary line segment cluster to obtain the upper boundary line segment cluster, the lower boundary line segment cluster, the left boundary line segment cluster and the right boundary line segment cluster; Perform straight line fitting on the upper boundary line segment cluster, the lower boundary line segment cluster, the left boundary line segment cluster and the right boundary line segment cluster, respectively, to obtain four target boundary lines; Group vertex positions.
在本实施例中,可以采用RANSAC算法对上边界线段簇、下边界线段簇、左边界线段簇和右边界线段簇进行处理,得到四条边界线。In this embodiment, the RANSAC algorithm may be used to process the upper boundary line segment cluster, the lower boundary line segment cluster, the left boundary line segment cluster and the right boundary line segment cluster to obtain four boundary lines.
首先简要说明一下霍夫变换思路为:读取一幅待处理的二值图像;获取二值图像空间的源像素数据;通过量化极坐标系参数空间为有限个值间隔等分或累加格子,即ρ、theta,其中,ρ、theta为参数空间(极坐标系)上参量,分别代表长度和角度。First, briefly explain the idea of Hough transform: read a binary image to be processed; obtain the source pixel data of the binary image space; quantify the polar coordinate system parameter space into a finite number of value intervals to equally divide or accumulate the lattice, namely ρ and theta, where ρ and theta are parameters in the parameter space (polar coordinate system), representing length and angle respectively.
通过霍夫变换算法,将每个像素坐标点P(x,y)被转换到(ρ,theta)的曲线点上面,并累加到对应的格子数据点;寻找最大霍夫值,设置阈值(其中,如果二维码大小为m·n,阈值可在0.1·(m·n)0.5~0.2·(m·n)0.5之间取值),反变换到图像空间,以得到四条边界线。Through the Hough transform algorithm, each pixel coordinate point P(x, y) is converted to the curve point of (ρ, theta), and accumulated to the corresponding grid data point; find the maximum Hough value, and set the threshold (wherein , if the size of the two-dimensional code is m·n, the threshold value can be between 0.1·(m·n) 0.5 ~ 0.2·(m·n) 0.5 ), and inversely transform to the image space to obtain four boundary lines.
在本实施例中,对所述边界轮廓进行霍夫变换,得到边界线段簇,然后对边界线段簇进行斜率和截距二分聚类,得到上边界线段簇、下边界线段簇、左边界线段簇和右边界线段簇。具体地,先按照斜率将上边界线段簇和下边界线段簇与左边界线段簇和右边界线段簇分开,再按照截距将上边界线段簇与下边界直线簇区分开,以及将左边界线段簇与右边界线段簇区分开。In this embodiment, Hough transform is performed on the boundary contour to obtain a boundary line segment cluster, and then slope and intercept binary clustering is performed on the boundary line segment cluster to obtain an upper boundary line segment cluster, a lower boundary line segment cluster, and a left boundary line segment cluster. and the right boundary line segment cluster. Specifically, the upper boundary line segment cluster and the lower boundary line segment cluster are separated from the left boundary line segment cluster and the right boundary line segment cluster according to the slope, and then the upper boundary line segment cluster and the lower boundary line segment cluster are separated according to the intercept, and the left boundary line segment is separated. Clusters are distinguished from right boundary line segment clusters.
在进行聚类之前,先K-means异常点检测,具体步骤如下:Before clustering, K-means outlier detection is performed first. The specific steps are as follows:
(1)初始异常数据集M为全体点集,其中异常数据集M中包含全部的斜率和截距所形成的点集;(1) The initial abnormal data set M is the whole point set, wherein the abnormal data set M contains all the point sets formed by the slope and intercept;
(2)任意选择k个对象(其中k为2个)作为初始的簇中心,将每个对象赋给最类似的簇;(2) arbitrarily select k objects (where k is 2) as the initial cluster center, and assign each object to the most similar cluster;
(3)计算簇中对象的平均值,并计算簇的平均半径;(3) Calculate the average value of the objects in the cluster, and calculate the average radius of the cluster;
(4)求取候选异常对象集N,其中,候选异常对象集N指的是距中心对象距离大于平均半径的对象,以及只有一个对象的簇;(4) Obtain the candidate abnormal object set N, wherein the candidate abnormal object set N refers to the objects whose distance from the central object is greater than the average radius, and the clusters with only one object;
(5)求与前次异常数据集的交集M=M∩N,作为新的异常数据集;(5) Find the intersection M=M∩N with the previous abnormal data set as a new abnormal data set;
(6)重复以上步骤直到异常数据集收敛或达到最大迭代次数,得到样本。(6) Repeat the above steps until the abnormal data set converges or the maximum number of iterations is reached, and a sample is obtained.
然后,选取K-medoids聚类算法进行聚类,以再次避免异常值对边界线确定算法的影响。K-mediods聚类算法为:Then, K-medoids clustering algorithm is selected for clustering to avoid the influence of outliers on the boundary line determination algorithm again. The K-mediods clustering algorithm is:
(1)选取样本最大值和最小值作为初始mediods(中心点集);(1) Select the maximum and minimum values of the sample as the initial mediods (center point set);
(2)按照与初始mediods就近的原则,将剩余的n-2个点分配到当前最佳的mediods代表的类中;(2) According to the principle of being close to the initial mediods, assign the remaining n-2 points to the class represented by the current best mediods;
(3)如果某个类的数目为1,则从删除此类,并从另一个类中选取最大值和最小值作为mediods;(3) If the number of a certain class is 1, delete this class, and select the maximum and minimum values from another class as mediods;
(4)对类中除mediods点之外的所有其他点,按顺序计算当其为新的mediods时准则函数的值,遍历所有可能,选取准则函数最小时对应的点作为新的mediods;其中,准则函数为:当前mediods中所有其他点到该中心点的距离之和最小;(4) For all other points in the class except the mediods point, calculate the value of the criterion function in order when it is a new mediods, traverse all possibilities, and select the point corresponding to the minimum criterion function as the new mediods; among them, The criterion function is: the sum of the distances from all other points in the current mediods to the center point is the smallest;
(5)重复(2)~(4)过程,直到所有的mediods不再发生变化或已达到设定的最大迭代次数。(5) Repeat the process of (2) to (4) until all the mediods no longer change or the set maximum number of iterations has been reached.
在本实施例中,对原始K-mediods聚类算法,其中,在步骤(1)中,区别于原始K-mediods聚类算法中的任意取值,本实施例将最大值和最小值作为初始mediods,可以加快聚类效率;区别于原始K-mediods聚类算法,增加了步骤(3),可以防止异常点的影响。In this embodiment, for the original K-mediods clustering algorithm, in step (1), different from the arbitrary values in the original K-mediods clustering algorithm, this embodiment uses the maximum value and the minimum value as the initial value mediods can speed up the clustering efficiency; different from the original K-mediods clustering algorithm, step (3) is added to prevent the influence of outliers.
通过前述的K-means异常点检测和K-medoids聚类算法对斜率和截距进行处理后,得到上边界线段簇、下边界线段簇、左边界线段簇和右边界线段簇,然后,采用RANSAC算法分别对各个边界线段簇进行处理,得到四个边界线。After processing the slope and intercept through the aforementioned K-means outlier detection and K-medoids clustering algorithm, the upper boundary line segment cluster, the lower boundary line segment cluster, the left boundary line segment cluster and the right boundary line segment cluster are obtained. Then, using RANSAC The algorithm processes each boundary line segment cluster separately, and obtains four boundary lines.
具体地,采用RANSAC算法通过反复选择数据中的一组随机子集来达成目标,被选取的子集被假设为局内点,并用下述方法进行验证:Specifically, the RANSAC algorithm is used to achieve the goal by repeatedly selecting a set of random subsets in the data. The selected subsets are assumed to be intra-office points, and the following methods are used to verify:
(1)对原始数据集进行K-means++聚类,分为两类数据点;(1) K-means++ clustering is performed on the original data set, which is divided into two types of data points;
(2)在两类中分别选取一个点用来估计直线模型,该模型确定且唯一;(2) Select a point in each of the two categories to estimate the straight line model, which is definite and unique;
(3)用(2)得到的模型去测试其他所有的数据,如果某个点到该直线模型的距离小于阈值L,其中,阈值L可为10个(或者其他数目)像素点的距离,则认为它也是局内点;(3) Use the model obtained in (2) to test all other data, if the distance from a point to the line model is less than the threshold L, where the threshold L can be the distance of 10 (or other numbers) pixels, then Consider it also an in-office point;
(4)如果该模型局内点个数超过阈值N,其中,阈值N可以为80%,则认为该模型合理,然后用所有局内点最小二乘法拟合最终的直线;(4) If the number of intra-office points of the model exceeds the threshold N, where the threshold N can be 80%, the model is considered reasonable, and then the final straight line is fitted by the least squares method of all intra-office points;
(5)如果局内点个数小于阈值N但大于上个模型局内点个数,则保留该模型,否则舍弃;(5) If the number of intra-office points is less than the threshold N but greater than the number of intra-office points of the previous model, the model is retained, otherwise it is discarded;
(6)重复(2)~(5)直到达到最大迭代次数。(6) Repeat (2) to (5) until the maximum number of iterations is reached.
在本实施例中,对原始的RANSAC算法进行改进,区别于原始的RANSAC算法,对步骤(1)和步骤(4)进行改进,可以提高算法效率。In this embodiment, the original RANSAC algorithm is improved, which is different from the original RANSAC algorithm, and the improvement of step (1) and step (4) can improve the efficiency of the algorithm.
在可选的实施例中,K-means++聚类算法过程如下:In an optional embodiment, the K-means++ clustering algorithm process is as follows:
(1)从原始数据集中随机选取一个样本点作为初始聚类中心c1;(1) randomly select a sample point from the original data set as the initial cluster center c 1 ;
(2)对于原始数据集中的每一个样本点xi,计算它与当前已有聚类中心之间的最短距离,用D(x)表示;(2) For each sample point xi in the original data set, calculate the shortest distance between it and the existing cluster center, which is represented by D(x);
(3)选择一个新的样本作为新的聚类中心,选择的原则是:D(x)较大的点被选取作为聚类中心的概率较大;将D(x)反应到点被选择的概率上,方法为:首先把数据集中每个点与其距离最近的聚类中心点之间的距离相加,其和用sum(D(x))表示;在0-sum(D(x))之间取一个随机值Random,然后Random=Random-D(x),直到Random≤0,此时的点就是下一个聚类中心;(3) Select a new sample as the new cluster center. The principle of selection is: the point with larger D(x) is more likely to be selected as the cluster center; D(x) is reflected to the selected point In terms of probability, the method is: first add the distance between each point in the data set and the nearest cluster center point, and the sum is represented by sum(D(x)); in 0-sum(D(x)) Take a random value Random, then Random=Random-D(x), until Random≤0, the point at this time is the next cluster center;
(4)重复(2)~(3)直到选出k个聚类中心;(4) Repeat (2) to (3) until k cluster centers are selected;
(5)针对数据集中的每个样本xi,计算它到k个聚类中心的欧氏距离,并将其分到距离最小的聚类中心所对应的簇中;(5) For each sample x i in the data set, calculate its Euclidean distance to k cluster centers, and divide it into the cluster corresponding to the cluster center with the smallest distance;
(6)针对每个簇,更新聚类中心,更新函数用F(x)表示;用Ci表示簇中数据点集合,用|Ci|表示簇中数据点的个数,则更新函数F(x)为:(6) For each cluster, update the cluster center, and the update function is represented by F(x); by C i to represent the set of data points in the cluster, and |C i | to represent the number of data points in the cluster, then the update function F (x) is:
(7)重复(5)~(6)直到聚类中心不再变化,误差平方和准则函数收敛E(x);目标是最小化均方误差E,准则函数E(x)为:(7) Repeat (5) to (6) until the cluster center does not change, and the error square sum criterion function converges E(x); the goal is to minimize the mean square error E, and the criterion function E(x) is:
初始数据集表示为:X={x1,x2,...,xn},簇的数目为k。The initial dataset is expressed as: X={x 1 , x 2 ,...,x n }, and the number of clusters is k.
在实际应用场景下,可以按照如下方式进行取交点,从而得到第一组顶点位置和第二组顶点位置。在本实施例中,对按照如下公式对四条目标边界线进行处理,从而求取交点,例如,直线L1:y=ax+b,直线L2:y=cx+d,交点坐标为: In a practical application scenario, the intersection point can be taken as follows, so as to obtain the first set of vertex positions and the second set of vertex positions. In this embodiment, the four target boundary lines are processed according to the following formula to obtain the intersection point. For example, straight line L1: y=ax+b, straight line L2: y=cx+d, and the coordinates of the intersection point are:
在可选的方案中,在步骤104中,基于所述目标顶点位置对所述待校正二维码图像进行校正,得到目标二维码图像具体包括:在所述待校正二维码图像上确定所述目标顶点位置,以确定所述待校正二维码图像的边界点;对所述待校正二维码图像进行透视变换,校正所述待校正二维码图像的形变,得到目标二维码图像。In an optional solution, in step 104, correcting the two-dimensional code image to be corrected based on the target vertex position, and obtaining the target two-dimensional code image specifically includes: determining on the two-dimensional code image to be corrected The target vertex position is used to determine the boundary point of the two-dimensional code image to be corrected; perspective transformation is performed on the two-dimensional code image to be corrected, and the deformation of the two-dimensional code image to be corrected is corrected to obtain the target two-dimensional code image.
其中,透视变换是将图像从原始二维空间转换到三维空间,再从三维空间转换到另一个新二维空间的映射变换。其中,原始二维空间到三维空间映射的公式如下:Among them, perspective transformation is a mapping transformation that converts the image from the original two-dimensional space to the three-dimensional space, and then from the three-dimensional space to another new two-dimensional space. Among them, the formula of the original two-dimensional space to three-dimensional space mapping is as follows:
其中三维空间到新二维空间的映射公式为:The mapping formula from the three-dimensional space to the new two-dimensional space is:
x=x′/w′y=y′/w′x=x'/w'y=y'/w'
此处,原始二维空间对应原始图像,新二维空间对应新图像。其中,(u,v)是原始图像上的点,对应得到变换后的新图像点坐标(x,y)。重写之前的变换公式,得到:Here, the original two-dimensional space corresponds to the original image, and the new two-dimensional space corresponds to the new image. Among them, (u, v) are the points on the original image, corresponding to the coordinates (x, y) of the transformed new image points. Rewrite the previous transformation formula to get:
根据前述透视变换,校正所述待校正二维码图像的形变,得到目标二维码图像。According to the aforementioned perspective transformation, the deformation of the to-be-corrected two-dimensional code image is corrected to obtain the target two-dimensional code image.
区别于现有技术,本发明通过两种不同的算法得到二维码图像的两组顶点位置,并将两组顶点位置中的同类顶点进行调和,得到目标顶点位置,从而确定二维码图像的边界点,通过两种算法可以提高顶点位置确定的准确性,最后再根据目标顶点位置进行校正,可以校正二维码图像的形变,以便于对二维码进行识别。Different from the prior art, the present invention obtains two sets of vertex positions of the two-dimensional code image through two different algorithms, and reconciles the same vertices in the two sets of vertex positions to obtain the target vertex position, thereby determining the position of the two-dimensional code image. For boundary points, two algorithms can improve the accuracy of vertex position determination, and finally correct according to the target vertex position, which can correct the deformation of the two-dimensional code image, so as to facilitate the identification of the two-dimensional code.
实施例2:Example 2:
前述实施例1主要说明了对存在形变的二维码图像进行校正的过程,在实际应用场景下,二维码图像的边缘会存在模糊的情况,导致无法快速准确地识别二维码图像,为解决前述问题,本实施例基于实施例1进行了进一步地改进,通过多算子边缘检测获取二维码图像的边缘轮廓图像,以对二维码图像进行锐化,增强二维码图像的边缘轮廓,可以消除二维码边缘模糊的影响,得到背景干净、条码清晰以及对比度强的二维码图像,可以有效提高后续识别的准确性。The foregoing
参阅图11,本实施例提供另一种二维码图像的校正方法,该校正方法还包括如下步骤:Referring to FIG. 11 , this embodiment provides another method for calibrating a two-dimensional code image, and the method further includes the following steps:
在此,需要说明的是,当二维码图像存在形变和边缘模糊问题时,按照实施例1的步骤101~步骤104对图像进行校正后,得到目标二维码图像,再按照本实施例的方法对目标二维码图像进行边缘锐化,消除边缘模糊的问题。当二维码图像不存在形变,但二维码图像的边缘模糊,可以直接采用本实施例的方法对目标二维码图像进行边缘锐化,消除边缘模糊的问题。即,本实施例的校正方法适用于不同的应用场景。Here, it should be noted that when the two-dimensional code image has problems of deformation and edge blurring, after correcting the image according to
在步骤105中,采用第三算法对所述目标二维码图像进行边缘检测得到第一边缘轮廓图,采用第四算法对所述目标二维码图像进行边缘检测得到第二边缘轮廓图。In
其中,所述第三算法为Canny边缘检测算法,所述第四算法为Sobel边缘检测算法。第三算法和第四算法还可以为其他算法。Wherein, the third algorithm is the Canny edge detection algorithm, and the fourth algorithm is the Sobel edge detection algorithm. The third algorithm and the fourth algorithm may also be other algorithms.
在本实施例中,是采用两种算法进行多算子融合,在实际应用场景下,进行多算子融合时,算法的数量和种类可以多于2个或者更多个,在此不做具体限定,按照前述方式分别采用不同的算法进行边缘检测,然后将每个算法对应的边缘轮廓图按照下面的步骤进行融合,得到目标边缘轮廓图。In this embodiment, two algorithms are used to perform multi-operator fusion. In an actual application scenario, when multi-operator fusion is performed, the number and type of algorithms may be more than two or more, which are not detailed here. Restriction, use different algorithms for edge detection in the aforementioned manner, and then fuse the edge contour maps corresponding to each algorithm according to the following steps to obtain the target edge contour map.
在实际应用场景下,在进行边缘检测前,对目标二维码图像进行中值滤波和基于各向异性的细节保持算法进行平滑去噪后进行多算子边缘检测。In practical application scenarios, before edge detection, the target QR code image is subjected to median filtering and anisotropy-based detail preservation algorithm for smoothing and denoising, and then multi-operator edge detection is performed.
在步骤106中,将所述第一边缘轮廓图和所述第二边缘轮廓图进行融合,得到目标边缘轮廓图。In
在本实施例中,将所述第一边缘轮廓图和所述第二边缘轮廓图按照预设的比例进行像素叠加,得到目标边缘轮廓。In this embodiment, pixels of the first edge contour map and the second edge contour map are superimposed according to a preset ratio to obtain a target edge contour.
其中,预设的比例为1:1或者其他比例,在此,不做具体限定。The preset ratio is 1:1 or other ratios, which are not specifically limited here.
在步骤107中,通过所述目标边缘轮廓图对所述目标二维码图像进行锐化,以增强所述目标二维码图像的边缘。In
在可选的方案中,中值滤波的原理是:把数字图像中的一个点的值用该点周围的一个区域的各个点的中值代替。以3×3窗口为例:In an optional solution, the principle of median filtering is to replace the value of a point in the digital image with the median value of each point in an area around the point. Take a 3×3 window as an example:
g(x,y)=median(f(x-1,y-1),f(x,y-1),f(x+1,y-1),f(x-1,y),g(x,y)=median(f(x-1,y-1),f(x,y-1),f(x+1,y-1),f(x-1,y),
f(x,y),f(x+1,y),f(x-1,y+1),f(x,y+1),f(x+1,y+1))f(x,y),f(x+1,y),f(x-1,y+1),f(x,y+1),f(x+1,y+1))
其中,f(x,y)为点(x,y)像素值,g(x,y)为中值滤波后的像素值,median为取中值函数。Among them, f(x, y) is the pixel value of point (x, y), g(x, y) is the pixel value after median filtering, and median is the median function.
在可选的方案中,基于各向异性的细节保持算法具体为:在传统PM各向异性扩散模型中,可以通过使用四个最近邻居和拉普拉斯算子来离散的表示,其中用▽Ii t(x,y)代表四个方向的梯度,ci t(x,y)代表与之相关的扩散系数,It(x,y)是处理前像素点值,It+1(x,y)是处理后的像素点值:In an optional solution, the detail preservation algorithm based on anisotropy is specifically: In the traditional PM anisotropic diffusion model, it can be represented discretely by using the four nearest neighbors and the Laplacian operator, where ▽ I i t (x, y) represents the gradient in four directions, c i t (x, y) represents the diffusion coefficient associated with it, I t (x, y) is the pixel value before processing, I t+1 ( x, y) is the processed pixel value:
函数g(▽I)必须是一个单调递减函数,满足g(0)=1,g(∞)=0,可表示为(其中K为常数):The function g(▽I) must be a monotonically decreasing function, satisfying g(0)=1, g(∞)=0, which can be expressed as (where K is a constant):
g(▽I)=1/[1+(|▽I|/K)2]g(▽I)=1/[1+(|▽I|/K) 2 ]
在实际应用场景下,K太大,则扩散过程会过渡平滑并导致图像模糊;K太小,则扩散过程将在早期迭代过程中停止平滑并产生与原始图像类似的恢复图像。因此,考虑一种各向异性的扩散模型,并将梯度和方差作为两个局部特征。以3×3的区域为例,其灰度方差:In practical application scenarios, if K is too large, the diffusion process will transition smoothly and cause image blurring; if K is too small, the diffusion process will stop smoothing in the early iteration process and produce a restored image similar to the original image. Therefore, consider an anisotropic diffusion model with gradient and variance as two local features. Taking a 3×3 area as an example, its grayscale variance:
由于灰度比梯度变化范围大,需将方差归一化以使二者相互兼容:Since the gray level has a larger variation range than the gradient, the variance needs to be normalized to make the two compatible:
则扩散系数函数修正为:Then the diffusion coefficient function is corrected as:
在可选的方案中,对图像进行多算子融合边缘检测实质上为:将通过Canny边缘检测算法得到的第一边缘轮廓图和通过Sobel边缘检测算法得到的第二边缘轮廓图进行融合叠加,得到目标边缘轮廓图,其中,叠加比例可以为0.5叠加比例还可以取其他数值,依据实际情况进行设置即可。In an optional solution, the essence of performing multi-operator fusion edge detection on the image is: merging and superposing the first edge contour map obtained by the Canny edge detection algorithm and the second edge contour map obtained by the Sobel edge detection algorithm, Obtain the target edge contour map, in which the superposition ratio can be 0.5 and other values can be used, which can be set according to the actual situation.
下面具体说明Canny边缘检测算法的处理过程。The processing process of the Canny edge detection algorithm is described in detail below.
首先,简要说明Canny边缘检测算法的传统步骤:First, a brief description of the traditional steps of the Canny edge detection algorithm:
(1)高斯滤波平滑图像(1) Gaussian filtering to smooth the image
Canny边缘检测算法使用二维高斯函数导数来处理原始图像,其中(x,y)为图像点坐标。其中,二维高斯函数如下:The Canny edge detection algorithm uses the derivative of a two-dimensional Gaussian function to process the original image, where (x,y) are the image point coordinates. Among them, the two-dimensional Gaussian function is as follows:
梯度向量为:The gradient vector is:
(2)计算梯度的幅度和方向(2) Calculate the magnitude and direction of the gradient
沿x和y方向的偏导数分别是来自图像I(x,y)的Px(i,j)和Py(i,j)。将直角坐标转换为极坐标,将像素的Px(i,j)和Py(i,j)转换为梯度幅度M(i,j)和梯度方向θ(i,j)。M(i,j)代表任意点的边强度,θ(i,j)代表任意点的法向量。The partial derivatives along the x and y directions are Px (i,j) and Py (i,j) from the image I(x,y), respectively. Convert Cartesian coordinates to polar coordinates, and convert pixel P x (i, j) and P y (i, j) to gradient magnitude M(i, j) and gradient direction θ(i, j). M(i,j) represents the edge strength of any point, and θ(i,j) represents the normal vector of any point.
其中Px和Py定义如下:where Px and Py are defined as follows:
(3)对梯度幅度进行非极大值抑制(3) Non-maximum suppression of gradient amplitude
以3×3邻域点为研究对象,发现邻域点梯度方向在扇形0°、45°、90°、135°梯度方向上。如果该值大于相邻点,则该点可能是边缘点,可以再次保留以供判断。否则,该点不是边缘点,值设置为0。Taking the 3×3 neighborhood points as the research object, it is found that the gradient directions of the neighborhood points are in the direction of the 0°, 45°, 90°, and 135° gradients of the sector. If the value is greater than the adjacent point, the point may be an edge point and can be reserved again for judgment. Otherwise, the point is not an edge point and the value is set to 0.
(4)使用双阈值检测和连接边缘(4) Use dual thresholds to detect and connect edges
将点(i,j)的值与阈值进行比较,如果点梯度幅度大于高阈值Th时,可确定为边缘点。如果Tl≤M(i,j)≤Th,则等待进一步判断,如果点梯度幅度小于低阈值Tl,可确定不是边缘点。The value of point (i, j) is compared with the threshold, and if the gradient magnitude of the point is greater than the high threshold Th, it can be determined as an edge point. If Tl≤M(i,j)≤Th, wait for further judgment, and if the point gradient magnitude is less than the low threshold T1, it can be determined that it is not an edge point.
进一步判断中,首先使用高阈值得到一个边缘图像,但是该边缘图像由于采用的是高阈值,所以存在不连续的情况比较多。这个时候在高阈值边缘图像中所有的边缘线端点的8邻域处寻找可以连接的到轮廓上的边缘,当满足Tl≤M(i,j)时连接。In further judgment, a high threshold is used to obtain an edge image first, but since the edge image adopts a high threshold, there are many cases of discontinuity. At this time, the 8 neighborhoods of all edge line endpoints in the high-threshold edge image are searched for edges that can be connected to the contour, and the connection is made when Tl≤M(i,j) is satisfied.
由于传统Canny边缘检测算法容易产生检测伪边缘和细节丢失的问题,在优选的实施例中,对传统的Canny边缘检测算法进行改进,如下:Since the traditional Canny edge detection algorithm is prone to the problems of detecting false edges and lost details, in a preferred embodiment, the traditional Canny edge detection algorithm is improved as follows:
改进后的Canny边缘检测算法使用多模板卷积原始图像f(x,y),其中,原始图像指的是前述经过平滑去噪处理后的二维码图像。当模板影响到点时,根据梯度的大小进行计算,加权值不断变化,高斯函数G(x,y,σ)卷积f(x,y)得到平滑图像。The improved Canny edge detection algorithm uses multiple templates to convolve the original image f(x,y), where the original image refers to the aforementioned two-dimensional code image after smoothing and denoising. When the template affects the point, it is calculated according to the size of the gradient, the weighted value is constantly changing, and the Gaussian function G(x, y, σ) convolves f(x, y) to obtain a smooth image.
当像素值发生显著变化时,滤波器权重可以设置为0,避免像素变化的平滑化,提高了计算效率。改进的自适应滤波算法的特点是当权重系数满足无显著变化的区域时自动调整权重系数。When the pixel value changes significantly, the filter weight can be set to 0 to avoid the smoothing of the pixel change and improve the computational efficiency. The characteristic of the improved adaptive filtering algorithm is that the weight coefficient is automatically adjusted when the weight coefficient satisfies the region without significant change.
其中,k为迭代次数,I(k)(x,y)为图像像素值,I(k)(x,y)的导数为I′(k)(x,y),Φ(I′(k)(x,y))为单调递减函数,最大值为Φ(0)=1,当I′(k)(x,y)不断增加时,Φ(I′(k)(x,y))减小到0。I′(k)(x,y)可以检测出灰度变化是否有突变,w(k)(x,y)可以描述为:Among them, k is the number of iterations, I (k) (x, y) is the image pixel value, the derivative of I (k ) (x, y) is I′ (k) (x, y), Φ(I′ (k ) (x, y)) is a monotonically decreasing function, the maximum value is Φ(0)=1, when I′ (k) (x, y) keeps increasing, Φ(I′ (k) (x, y)) reduced to 0. I' (k) (x, y) can detect whether there is a sudden change in the grayscale change, and w (k) (x, y) can be described as:
其中,h为常数,I′(k)(x,y)定义为:where h is a constant, and I′ (k) (x,y) is defined as:
权重系数:Weight factor:
其中,in,
在实际应用场景下,发明人发现经过5次迭代,边缘锐化效果最佳。In practical application scenarios, the inventor found that after 5 iterations, the edge sharpening effect was the best.
在本实施例中,采用改进后的Canny边缘检测算法得到第一边缘轮廓图。In this embodiment, the improved Canny edge detection algorithm is used to obtain the first edge contour map.
下面具体说明Sobel边缘检测算法的处理过程。The processing process of the Sobel edge detection algorithm is described in detail below.
首先,传统的Sobel边缘检测算法如下:First, the traditional Sobel edge detection algorithm is as follows:
位置(i,j)处的图像f(x,y)的梯度定义为:The gradient of the image f(x,y) at position (i,j) is defined as:
梯度向量的大小为:The magnitude of the gradient vector is:
在经典Sobel算子中,横纵方向模板和图像f(x,y)进行卷积以近似梯度值,可以通过以下公式求解:In the classic Sobel operator, the horizontal and vertical direction template and image f(x,y) are convolved to approximate the gradient value, which can be solved by the following formula:
Gx=f(i+1,j-1)+2f(i+1,j)+f(i+1,j+1)-f(i-1,j-1)-2f(i-1,j)-f(i-1,j+1)G x =f(i+1,j-1)+2f(i+1,j)+f(i+1,j+1)-f(i-1,j-1)-2f(i-1 ,j)-f(i-1,j+1)
Gy=f(i-1,j+1)+2f(i,j+1)+f(i+1,j+1)-f(i-1,j-1)-2f(i,j-1)-f(i-1,j-1)G y =f(i-1,j+1)+2f(i,j+1)+f(i+1,j+1)-f(i-1,j-1)-2f(i,j -1)-f(i-1,j-1)
传统的Sobel边缘检测算法处理过程为:①水平和垂直双向模板分别沿着图像从左到右,从上到下,从一个像素移动到另一个像素,模板的中心点对应于图像中的对应像素;②将模板中的每个位置的权重转换为其对应的图像像素值,并依此计算出梯度值;③将模板中心点对应的图像像素值替换为梯度向量的值,表示为px;④设置合适的阈值T(可为150),如果px≥T,则确定为图像边缘的像素点。The processing process of the traditional Sobel edge detection algorithm is as follows: ① The horizontal and vertical bidirectional templates move from left to right, from top to bottom, and from one pixel to another along the image respectively, and the center point of the template corresponds to the corresponding pixel in the image ; ② Convert the weight of each position in the template to its corresponding image pixel value, and calculate the gradient value accordingly; ③ Replace the image pixel value corresponding to the center point of the template with the value of the gradient vector, expressed as px; ④ Set a suitable threshold T (can be 150), if px≥T, it is determined as the pixel point at the edge of the image.
由于传统的Sobel算子对其他方向边缘不敏感、对复杂纹理和倾斜图像轮廓提取不理想、抗噪能力低,在优选的实施例中,对传统Sobel算子作以下改进:Because the traditional Sobel operator is insensitive to edges in other directions, is not ideal for complex texture and oblique image contour extraction, and has low anti-noise ability, in a preferred embodiment, the traditional Sobel operator is improved as follows:
(1)使用形态学中的开放操作和闭合操作交替平滑图像;(1) Alternately smooth the image using the opening and closing operations in morphology;
(2)使用拓展的Sobel算子进行边缘检测;(2) Use the extended Sobel operator for edge detection;
(3)使用边缘二进制Otsu算法选择合适的阈值进行二值化。(3) Use the edge binary Otsu algorithm to select an appropriate threshold for binarization.
其中,形态学操作定义如下,其中F(x,y)为灰度图像,(x,y)是图像点坐标,S(m,n)是形态结构元素(大小可为3×3),(m,n)是结构元素点坐标:Among them, the morphological operation is defined as follows, where F(x,y) is the grayscale image, (x,y) is the image point coordinates, S(m,n) is the morphological structure element (the size can be 3×3), ( m,n) are the structuring element point coordinates:
膨胀: Expansion:
腐蚀:FΘS=min{F(x+m,y+n)}Corrosion: FΘS=min{F(x+m,y+n)}
开放: open:
闭合: closure:
其中,改进后的Sobel边缘检测算法相较传统Sobel边缘检测算法增加了六个方向,分别为45°、135°、180°、225°、270°、315°,可以得到较准确的边缘图像。Among them, compared with the traditional Sobel edge detection algorithm, the improved Sobel edge detection algorithm adds six directions, which are 45°, 135°, 180°, 225°, 270°, and 315°, which can obtain more accurate edge images.
在本实施例中,通过Sobel边缘检测算法得到第二边缘轮廓图。In this embodiment, the second edge contour map is obtained through the Sobel edge detection algorithm.
在本实施中,将所述第一边缘轮廓图和所述第二边缘轮廓图进行融合,得到目标边缘轮廓图;通过所述目标边缘轮廓图对所述目标二维码图像进行锐化,以增强所述目标二维码图像的边缘。In this implementation, the first edge contour map and the second edge contour map are fused to obtain a target edge contour map; the target two-dimensional code image is sharpened through the target edge contour map to Enhance the edge of the target QR code image.
结合图12和图13,其中,图12中位于右侧的图为经过Canny算子得到的第一边缘轮廓图。可以看出,在第一边缘轮廓图中,包含了很细致的轮廓,但是容易把噪声误判为边界。图13中位于右侧的图为经过Sobel算子得到的第二边缘轮廓图。可以看出,对噪声处理的效果较好,但是对边缘定位不是很准确。在本实施例中,将所述第一边缘轮廓图和所述第二边缘轮廓图进行融合,得到目标边缘轮廓图,既可以保证细致的轮廓,边缘定位准确,还具有较好的噪声处理能力,能够有效对二维码图像进行锐化。Referring to FIG. 12 and FIG. 13 , the image on the right in FIG. 12 is the first edge contour image obtained by the Canny operator. It can be seen that the first edge contour map contains very detailed contours, but it is easy to misjudge noise as a boundary. The figure on the right in FIG. 13 is the second edge contour figure obtained by the Sobel operator. It can be seen that the effect of noise processing is better, but the edge location is not very accurate. In this embodiment, the first edge contour map and the second edge contour map are fused to obtain the target edge contour map, which can not only ensure detailed contours, accurate edge positioning, but also have better noise processing capabilities , which can effectively sharpen the QR code image.
在实际应用场景下,对锐化后的目标二维码图像进一步进行自适应阈值二值化,然后再将处理后的二维码图像送入识别组件,进行二维码的识别。In practical application scenarios, the sharpened target QR code image is further subjected to adaptive threshold binarization, and then the processed QR code image is sent to the recognition component for QR code recognition.
在可选的方案中,自适应阈值二值化算法介绍如下:In an optional solution, the adaptive threshold binarization algorithm is introduced as follows:
算法的基本思想是遍历图像,计算一个移动的平均值,如果这个像素明显低于这个平均值,则设置为黑色,否则设置为白色。假设Pn为图像中位于点n处的像素,fs(n)是点n处最后s个像素的总和:The basic idea of the algorithm is to traverse the image, calculate a moving average, and set the pixel to black if it is significantly below this average, otherwise set to white. Suppose Pn is the pixel at point n in the image, and fs(n) is the sum of the last s pixels at point n:
最后,图像T(n)是1(黑色)或0(白色)则依赖于其是否比其前s个像素的平均值的百分之t的暗。其中,s取图像的1/8宽而t取值15能取得比较好的效果。Finally, the image T(n) is 1 (black) or 0 (white) depending on whether it is darker than t percent of the average of its first s pixels. Among them, s takes 1/8 of the width of the image and t takes 15 to achieve better results.
平均值的计算还可以采用关于点n对称的两侧,甚至可以用二维平滑值代替一维平滑值。假设Pi0,j0为图像中位于点(i0,j0)处的像素,fs(i0,j0)是以点(i0,j0)为中心,周围(s+1)·(s+1)个像素值的和:The calculation of the average can also take two sides symmetrical about the point n, or even replace the one-dimensional smoothed value with a two-dimensional smoothed value. Suppose P i0,j0 is the pixel located at point (i 0 ,j 0 ) in the image, f s (i 0 ,j 0 ) is the center of point (i 0 ,j 0 ), around (s+1)· Sum of (s+1) pixel values:
计算图像指定矩形区域内像素值的和可采用图像积分图,假设ii(x,y)表示该点左上角像素之和,表示如下:To calculate the sum of the pixel values in the specified rectangular area of the image, the image integral map can be used. Assuming that ii(x,y) represents the sum of the pixels in the upper left corner of the point, it is expressed as follows:
则以点(x0,y0)和点(x,y)表示的矩形区域内像素值的和表示为:Then the sum of pixel values in the rectangular area represented by point (x 0 , y 0 ) and point (x, y) is expressed as:
S=ii(x,y)+ii(x0,y0)-ii(x,y0)-ii(x0,y)S=ii(x,y)+ii(x 0 ,y 0 )-ii(x,y 0 )-ii(x 0 ,y)
为了使图像像素过渡相对平滑,遍历图像采用Z形遍历。为了使阈值计算考虑垂直方向上的信息,遍历时保留前一行的平均效果,然后把当前行的平均值和上一行的平均值再取平均作为新的平均值。In order to make the image pixel transition relatively smooth, traversing the image adopts a Z-shaped traversal. In order to make the threshold calculation take into account the information in the vertical direction, the average effect of the previous line is retained during traversal, and then the average of the current line and the average of the previous line is averaged as a new average.
区别于现有技术,通过多算子边缘检测获取二维码图像的边缘轮廓图像,以对二维码图像进行锐化,增强二维码图像的边缘轮廓,可以消除二维码边缘模糊的影响,得到背景干净、条码清晰以及对比度强的二维码图像,可以有效提高后续识别的准确性。Different from the prior art, the edge contour image of the two-dimensional code image is obtained through multi-operator edge detection, so as to sharpen the two-dimensional code image, enhance the edge contour of the two-dimensional code image, and eliminate the influence of the blurred edge of the two-dimensional code. , to obtain a QR code image with clean background, clear barcode and strong contrast, which can effectively improve the accuracy of subsequent identification.
实施例3:Example 3:
在实际应用场景下,可能存在由于算法本身的问题导致顶点位置识别出错,即上述实施例1中,第一组顶点位置和第二组顶点位置之间的相似度小于预设的阈值,为了避免校正出错或者校正时间过长,此时,可以采用其中一种算法得到的顶点位置作为目标顶点位置,以进行图像校正。In practical application scenarios, there may be errors in the identification of the vertex positions due to the problem of the algorithm itself. That is, in the above-mentioned
在实际应用场景下,虽然LSD线段检测算法的检测速度较快,但是,当待校正二维码图像较模糊时,容易出现识别率低的问题。结合实施例1,当第一组顶点位置和第二组顶点位置之间的相似度小于预设的相似度阈值时,获取待校正二维码图像的单位面积内的像素总数,其中,单位面积内的像素总数可以反映待校正二维码图像的清晰度,在所述待校正二维码图像的单位面积内的像素总数大于预设的数量阈值时,即,待校正二维码图像较清晰时,可以采用第一组顶点位置(通过LSD线段检测算法而获得)作为目标顶点位置,确定所述待校正二维码图像的边界点,从而避免校正出错或者校正时间过长。In practical application scenarios, although the detection speed of the LSD line segment detection algorithm is fast, when the image of the QR code to be corrected is blurred, the problem of low recognition rate is prone to occur. With reference to
在所述待校正二维码图像的单位面积内的像素总数不大于预设的数量阈值时,即待校正二维码图像较模糊时,可以采用第二组顶点位置作为目标顶点位置(通过霍夫变换而获得),确定所述待校正二维码图像的边界点,从而避免校正出错或者校正时间过长。When the total number of pixels in the unit area of the two-dimensional code image to be corrected is not greater than the preset number threshold, that is, when the two-dimensional code image to be corrected is relatively blurred, the second group of vertex positions can be used as the target vertex positions (through the Huo obtained from the QR code image), and determine the boundary points of the two-dimensional code image to be corrected, so as to avoid correction errors or excessive correction time.
在可选的方案中,可以通过单位面积图像所占用的内存空间确定单位面积内的像素总数,即,获取待校正二维码图像的单位面积内的像素总数具体包括:预先建立单位面积图像所占用的内存空间与单位面积内的像素总数之间的映射关系,获取待校正二维码图像的单位面积图像所占用的内存空间,基于前述映射关系,计算待校正二维码图像的单位面积内的像素总数。In an optional solution, the total number of pixels per unit area can be determined by the memory space occupied by the image per unit area, that is, acquiring the total number of pixels per unit area of the two-dimensional code image to be corrected specifically includes: pre-establishing the total number of pixels per unit area image. The mapping relationship between the occupied memory space and the total number of pixels per unit area, obtain the memory space occupied by the unit area image of the two-dimensional code image to be corrected, and calculate the per-unit area of the two-dimensional code image to be corrected based on the aforementioned mapping relationship the total number of pixels.
在本实施例中,为了避免由于算法导致的校正出错问题,提供了多种可选的执行支路,优选地,采用两种算法融合的方式确定目标顶点位置,以提高准确性;在两种算法确定的顶点位置之间存在较大差异时,根据待校正二维码图像的单位面积内的像素总数,选择性选择LSD线段检测算法或霍夫变换,确定的顶点位置,避免校正出错或通知用户再次采集图像,提高用户的用户体验。In this embodiment, in order to avoid the problem of correction errors caused by the algorithm, a variety of optional execution branches are provided. Preferably, the position of the target vertex is determined by the fusion of the two algorithms to improve the accuracy; When there is a large difference between the vertex positions determined by the algorithm, according to the total number of pixels in the unit area of the QR code image to be corrected, the LSD line segment detection algorithm or Hough transform is selectively selected to determine the vertex positions to avoid correction errors or notifications. The user captures the image again to improve the user experience of the user.
关于二维码图像的校正方法的其他过程详见实施例1和实施例2,在此,不再赘述。For other processes of the two-dimensional code image correction method, please refer to
实施例4:Example 4:
请参阅图14,图14是本发明实施例提供的一种校正装置的结构示意图。本实施例的校正装置包括一个或多个处理器41以及存储器42。其中,图14中以一个处理器41为例。Please refer to FIG. 14. FIG. 14 is a schematic structural diagram of a calibration apparatus provided by an embodiment of the present invention. The calibration apparatus of this embodiment includes one or
处理器41和存储器42可以通过总线或者其他方式连接,图14中以通过总线连接为例。The
存储器42作为一种基于二维码图像的校正方法的非易失性计算机可读存储介质,可用于存储非易失性软件程序、非易失性计算机可执行程序以及模块,如实施例1和/或实施例2中的二维码图像的校正方法以及对应的程序指令。处理器41通过运行存储在存储器42中的非易失性软件程序、指令以及模块,从而执行二维码图像的校正方法的各种功能应用以及数据处理,实现实施例1和/或实施例2的二维码图像的校正方法的功能。The
其中,存储器42可以包括高速随机存取存储器,还可以包括非易失性存储器,例如至少一个磁盘存储器件、闪存器件、或其他非易失性固态存储器件。在一些实施例中,存储器42可选包括相对于处理器41远程设置的存储器,这些远程存储器可以通过网络连接至处理器41。上述网络的实例包括但不限于互联网、企业内部网、局域网、移动通信网及其组合。The
关于二维码图像的校正方法请实施例1和实施例2的相关的文字描述在此,不再赘述。Regarding the correction method of the two-dimensional code image, please describe the relevant texts of
值得说明的是,上述装置和系统内的模块、单元之间的信息交互、执行过程等内容,由于与本发明的处理方法实施例基于同一构思,具体内容可参见本发明方法实施例中的叙述,此处不再赘述。It is worth noting that the information exchange, execution process and other contents between the modules and units in the above-mentioned device and the system are based on the same concept as the processing method embodiments of the present invention. For details, please refer to the descriptions in the method embodiments of the present invention. , and will not be repeated here.
本领域普通技术人员可以理解实施例的各种方法中的全部或部分步骤是可以通过程序来指令相关的硬件来完成,该程序可以存储于一计算机可读存储介质中,存储介质可以包括:只读存储器(Read Only Memory,简写为ROM)、随机存取存储器(Random AccessMemory,简写为RAM)、磁盘或光盘等。Those of ordinary skill in the art can understand that all or part of the steps in the various methods of the embodiments can be completed by instructing relevant hardware through a program, and the program can be stored in a computer-readable storage medium, and the storage medium can include: Read Only Memory (abbreviated as ROM), random access memory (Random Access Memory, abbreviated as RAM), magnetic disk or optical disk, etc.
本领域的技术人员容易理解,以上所述仅为本发明的较佳实施例而已,并不用以限制本发明,凡在本发明的精神和原则之内所作的任何修改、等同替换和改进等,均应包含在本发明的保护范围之内。Those skilled in the art can easily understand that the above are only preferred embodiments of the present invention, and are not intended to limit the present invention. Any modifications, equivalent replacements and improvements made within the spirit and principles of the present invention, etc., All should be included within the protection scope of the present invention.
Claims (8)
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201910602805.6A CN110309687B (en) | 2019-07-05 | 2019-07-05 | A two-dimensional code image correction method and correction device |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201910602805.6A CN110309687B (en) | 2019-07-05 | 2019-07-05 | A two-dimensional code image correction method and correction device |
Publications (2)
Publication Number | Publication Date |
---|---|
CN110309687A CN110309687A (en) | 2019-10-08 |
CN110309687B true CN110309687B (en) | 2020-06-05 |
Family
ID=68079002
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201910602805.6A Active CN110309687B (en) | 2019-07-05 | 2019-07-05 | A two-dimensional code image correction method and correction device |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN110309687B (en) |
Families Citing this family (17)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN110807783B (en) * | 2019-10-28 | 2023-07-18 | 衢州学院 | A high-efficiency field of view region segmentation method and device for achromatic long video |
CN111337011A (en) * | 2019-12-10 | 2020-06-26 | 亿嘉和科技股份有限公司 | Indoor positioning method based on laser and two-dimensional code fusion |
CN113240768B (en) * | 2019-12-18 | 2022-03-15 | 北京城市网邻信息技术有限公司 | House type graph processing method, system, device and computer readable storage medium |
CN111311497B (en) * | 2020-02-12 | 2023-05-05 | 广东工业大学 | A barcode image angle correction method and device |
CN111310508B (en) * | 2020-02-14 | 2021-08-10 | 北京化工大学 | Two-dimensional code identification method |
CN111563930B (en) * | 2020-04-29 | 2023-07-07 | 达闼机器人股份有限公司 | Positioning method, device, medium, electronic equipment and auxiliary positioning module |
CN112651259B (en) * | 2020-12-29 | 2024-08-06 | 芜湖哈特机器人产业技术研究院有限公司 | Two-dimensional code positioning method and mobile robot positioning method based on two-dimensional code |
CN112766012B (en) * | 2021-02-05 | 2021-12-17 | 腾讯科技(深圳)有限公司 | Two-dimensional code image recognition method and device, electronic equipment and storage medium |
CN113034415B (en) * | 2021-03-23 | 2021-09-14 | 哈尔滨市科佳通用机电股份有限公司 | Method for amplifying small parts of railway locomotive image |
CN113128246B (en) * | 2021-03-25 | 2024-07-19 | 维沃移动通信有限公司 | Information processing method and device and electronic equipment |
CN113158704B (en) * | 2021-04-07 | 2023-06-09 | 福州符号信息科技有限公司 | Method and system for rapidly positioning Dotcode code |
CN113111674A (en) * | 2021-04-12 | 2021-07-13 | 广东奥普特科技股份有限公司 | Aztec code positioning and decoding method, system, equipment and storage medium |
CN113822849B (en) * | 2021-06-11 | 2025-03-18 | 腾讯科技(深圳)有限公司 | Identification code detection method and device, electronic device and storage medium |
CN113536822B (en) * | 2021-07-28 | 2024-05-03 | 中移(杭州)信息技术有限公司 | Two-dimensional code correction method and device and computer readable storage medium |
CN116030120B (en) * | 2022-09-09 | 2023-11-24 | 北京市计算中心有限公司 | Method for identifying and correcting hexagons |
CN116385742B (en) * | 2023-03-20 | 2024-04-12 | 北京兆讯恒达技术有限公司 | Low-quality bar code image signal extraction method and device |
CN118537346B (en) * | 2024-05-10 | 2025-01-17 | 紫金矿业集团西南地质勘查有限公司 | An automatic processing method for core photos based on template matching and Hough transform |
Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN102999772A (en) * | 2012-11-14 | 2013-03-27 | 韩偲铭 | Novel array-type two-dimension code encoding and decoding methods |
CN104424457A (en) * | 2013-08-20 | 2015-03-18 | 复旦大学 | Method for identifying two-dimensional code under the condition of nonlinear distortion |
CN108564557A (en) * | 2018-05-31 | 2018-09-21 | 京东方科技集团股份有限公司 | Method for correcting image and device |
Family Cites Families (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN103745221B (en) * | 2014-01-08 | 2017-05-24 | 杭州晟元数据安全技术股份有限公司 | Two-dimensional code image correction method |
CN105069389B (en) * | 2015-07-27 | 2017-10-31 | 福建联迪商用设备有限公司 | Quick Response Code piecemeal coding/decoding method and system |
CN106156684B (en) * | 2016-06-30 | 2019-01-18 | 南京理工大学 | A kind of two-dimensional code identification method and device |
CN107944324A (en) * | 2017-11-16 | 2018-04-20 | 凌云光技术集团有限责任公司 | A kind of Quick Response Code distortion correction method and device |
-
2019
- 2019-07-05 CN CN201910602805.6A patent/CN110309687B/en active Active
Patent Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN102999772A (en) * | 2012-11-14 | 2013-03-27 | 韩偲铭 | Novel array-type two-dimension code encoding and decoding methods |
CN104424457A (en) * | 2013-08-20 | 2015-03-18 | 复旦大学 | Method for identifying two-dimensional code under the condition of nonlinear distortion |
CN108564557A (en) * | 2018-05-31 | 2018-09-21 | 京东方科技集团股份有限公司 | Method for correcting image and device |
Also Published As
Publication number | Publication date |
---|---|
CN110309687A (en) | 2019-10-08 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN110309687B (en) | A two-dimensional code image correction method and correction device | |
CN110866924B (en) | Line structured light center line extraction method and storage medium | |
CN111612781B (en) | A screen defect detection method, device and head-mounted display device | |
CN107545207A (en) | DM two-dimensional code identification methods and device based on image procossing | |
CN102930268A (en) | Accurate positioning method for data matrix code under pollution and multi-view situation | |
US9679354B2 (en) | Duplicate check image resolution | |
CN104463067B (en) | Method for extracting macro blocks of Grid Matrix two-dimensional bar code | |
CN105160652A (en) | Handset casing testing apparatus and method based on computer vision | |
CN109949227A (en) | Image stitching method, system and electronic device | |
CN108896278A (en) | A kind of optical filter silk-screen defect inspection method, device and terminal device | |
CN106709500B (en) | Image feature matching method | |
CN107016394B (en) | Cross fiber feature point matching method | |
CN111695373A (en) | Zebra crossing positioning method, system, medium and device | |
CN107578011A (en) | The decision method and device of key frame of video | |
CN115578571A (en) | QFN element rapid and accurate identification and positioning method and device and storage medium | |
CN111310508A (en) | A two-dimensional code identification method | |
CN116109637B (en) | System and method for detecting appearance defects of turbocharger impeller based on vision | |
CN115587966A (en) | A method and system for detecting whether parts are missing or not under the condition of uneven illumination | |
CN118379317B (en) | Glass fiber cloth edge identification method, equipment and storage medium | |
CN110263778A (en) | A kind of meter register method and device based on image recognition | |
CN118505634A (en) | Defect detection method and system for printed contact lenses based on cloud-machine vision | |
CN118196043A (en) | A vehicle parts detection method based on machine vision and 3D point cloud fusion | |
CN118351097A (en) | Method and device for detecting glue path quality, electronic equipment and storage medium | |
CN109508571B (en) | Strip-space positioning method and device, electronic equipment and storage medium | |
CN104915951A (en) | Stippled DPM two-dimensional code area positioning method |
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 | ||
GR01 | Patent grant | ||
GR01 | Patent grant |