A method and device for detecting the circular shape of industrial products
Through the acquisition of images, edge detection, area of interest screening and Hoff transformation detection in the circular detection of industrial products, the problems of large calculation volume and low detection efficiency of Hoff transformation algorithm are solved, and the rapid and efficient industrial product circles are achieved. Shape detection.
Patent Information
- Application Number
- CN202111025672.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-09-02
- Publication Date
- 2025-02-18
- Estimated Expiration
- 2041-09-02
AI Technical Summary
In the prior art, the Hough transform's circular detection algorithm has a large calculation volume and low detection efficiency, which cannot meet the demand for rapid detection of industrial products in industrial production.
By collecting the image to be tested, performing edge detection, determining the region of interest, filtering the target pixel points, and performing Hough transform circular detection based on the edge pixel points and the target pixel points, reducing the calculation amount and improving detection efficiency.
It has achieved the reduction of the calculation amount of Hoff's transformation algorithm, improved detection efficiency, and met the demand for rapid detection of industrial products in industrial production.
Smart Images

Figure CN113870189B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of intelligent detection technology, and in particular to a method and device for detecting the roundness of industrial products. Background Art
[0002] Some industrial products often have strict requirements on the roundness of the product appearance. The appearance is required to be round. Due to the uneven force during the processing stage in the production process, the edge of the product cannot achieve a perfect circle, and the radius of the product may also change slightly. Before the product leaves the factory, it is necessary to check whether the radius of the product is within the allowable range and whether the boundary is round.
[0003] At present, the industry usually uses Hough transform circle detection technology to realize circle detection of industrial products, but the algorithm has a large amount of calculation and low detection efficiency, which cannot meet the requirements of rapid detection of industrial products in the industrial production process.
[0004] Therefore, how to improve detection efficiency is a technical problem that needs to be urgently solved in this field. Summary of the invention
[0005] The technical problem to be solved by the present invention is that the circle detection algorithm of the general Hough transform has a large amount of calculation and a low detection efficiency. A circle detection method for industrial products is provided, which can help reduce the amount of calculation and improve the detection efficiency.
[0006] In order to solve the above technical problems, the first aspect of the present invention discloses a method for detecting the roundness of an industrial product, the method comprising:
[0007] Collecting the industrial product to be tested in the shooting area to obtain the image to be tested;
[0008] Performing edge detection on the image to be tested by using an edge detection algorithm to obtain edge pixel points of the image to be tested;
[0009] Determine the region of interest in the image to be tested according to the edge pixel points and a preset rectangular frame size;
[0010] Traversing the pixel points in the region of interest, and screening out a plurality of target pixel points falling into a predetermined target area from all the pixel points in the region of interest;
[0011] Based on the edge pixel points, all the target pixel points and the Hough transform algorithm, circle detection is performed on the region of interest to obtain the circle included in the region of interest and the center point and radius corresponding to the circle.
[0012] As an optional implementation, in the first aspect of the present invention, before screening out a plurality of target pixel points falling into a predetermined target area from all pixel points in the region of interest, the method further includes:
[0013] Determine a standard rectangular area according to the preset rectangular frame size;
[0014] Starting from when the standard circle is at the initial position, the standard circle is slid according to a preset step length until the standard circle is tangent to any two adjacent sides of the standard rectangular area during the sliding process, wherein the preset step length is one pixel;
[0015] The area through which the standard circle slides is determined as the target area.
[0016] As an optional implementation, in the first aspect of the present invention, the initial position is a position where the standard circle is tangent to any two adjacent sides of the standard rectangular area;
[0017] The method starts from when the standard circle is in the initial position and slides the standard circle according to a preset step length until the standard circle is tangent to any two adjacent sides of the standard rectangular area during the sliding process, including:
[0018] Determine a moving starting position of the standard circle, wherein the moving starting position initially determined is the initial position;
[0019] Starting from the standard circle being at the moving starting position, the standard circle is slid along a first preset direction and by a preset step length each time until the standard circle is tangent to a first target edge of the standard rectangular area, and then the sliding is stopped, wherein the first preset direction is parallel to any one edge of all edges of the standard rectangular area, and the first target edge is an edge of all edges of the standard rectangular area to which the first preset direction points;
[0020] Reset the standard circle to the moving starting position, slide the standard circle once along a second preset direction and with a preset step length, and obtain a first position of the standard circle, wherein the second preset direction is perpendicular to the first preset direction;
[0021] Determine whether the standard circle is tangent to a second target edge of the standard rectangular area when in the first position, wherein the second target edge is an edge of all edges of the standard rectangular area to which the second preset direction points;
[0022] If it is determined that the standard circle is not tangent to the second target side of the standard rectangular area when it is at the first position, the moving starting position is updated according to the first position, and the steps of starting with the standard circle being at the moving starting position, sliding the standard circle according to the rule of sliding along a first preset direction and sliding the preset step length each time until the standard circle is tangent to the first target side of the standard rectangular area and then stopping the sliding are performed, and the step of resetting the standard circle to the moving starting position, sliding the standard circle once according to the rule of sliding along a second preset direction and sliding the preset step length to obtain the first position of the standard circle are performed;
[0023] If it is determined that the standard circle is tangent to the second target edge of the standard rectangular area when it is in the first position, the standard circle is slid starting from the first position along a first preset direction and with a preset step length each time until the sliding ends when the standard circle is tangent to the first target edge of the standard rectangular area.
[0024] As an optional implementation manner, in the first aspect of the present invention, the step of screening out a plurality of target pixel points that fall into a predetermined target area from all pixel points in the region of interest includes:
[0025] In the standard rectangular area, the pixel values of all the pixels included in the target area are marked as 1, and the pixel values of a plurality of remaining pixels except the pixels included in the target area are marked as 0, so as to obtain a mask image;
[0026] Determining coordinates of all first pixel points in the region of interest and coordinates of all second pixel points in the mask image, wherein the coordinates of a first first pixel point in the region of interest are the same as the coordinates of a first second pixel point in the mask image;
[0027] For each first pixel point in the region of interest, according to the coordinates of the first pixel point, obtaining a second pixel point with the same coordinates as the first pixel point in the mask image, and performing an AND operation on a pixel value of the first pixel point and a pixel value of the second pixel point;
[0028] If the result of the operation of the pixel value of the first pixel and the pixel value of the second pixel is 0, it is determined that the first pixel does not fall into the target area;
[0029] If the result of the operation of the pixel value of the first pixel and the pixel value of the second pixel is not 0, it is determined that the first pixel falls into the target area, and the first pixel is determined as the target pixel.
[0030] As an optional implementation, in the first aspect of the present invention, all pixels within the region of interest are stored continuously in rows in a memory;
[0031] The traversing the pixel points in the region of interest includes:
[0032] Get the preset pixel pointer p;
[0033] Point the pixel pointer p to the memory start address of the first pixel point in the region of interest, and obtain the first pixel point in the region of interest;
[0034] Execute p++ to make the pixel pointer p point to the memory start address of the next pixel point, and obtain the next pixel point, wherein the next pixel point is a subsequent pixel point adjacent to the storage position of the first pixel point in the region of interest in the memory;
[0035] Repeat p++ to obtain all pixel points in the region of interest.
[0036] As an optional implementation, in the first aspect of the present invention, after determining the center point and radius corresponding to the detected circle, the method further includes:
[0037] Performing least square fitting on edge pixels of the image to be tested to obtain a fitting result;
[0038] The fitting result is compared with the center point and radius corresponding to the circle to obtain the degree of roundness of the industrial product to be tested.
[0039] As an optional implementation manner, in the first aspect of the present invention, performing least squares fitting on edge pixels of the image to be measured and obtaining a fitting result includes:
[0040] Obtaining a set of edge pixel coordinates of the image to be measured;
[0041] According to the edge pixel coordinate set of the image to be measured, an overdetermined equation group of the circle to be fitted is established;
[0042] The overdetermined equations are solved using the least squares estimation method to obtain a fitting result of the circle to be fitted.
[0043] The second aspect of the present invention discloses a device for detecting a circular shape of an industrial product, the device comprising:
[0044] A collection module, used to collect the industrial products to be tested in the shooting area to obtain images to be tested;
[0045] An edge detection module is used to perform edge detection on the image to be tested by using an edge detection algorithm to obtain edge pixel points of the image to be tested;
[0046] A determination module, used to determine the region of interest in the image to be tested according to edge pixels of the image to be tested and a preset rectangular frame size;
[0047] A traversal module, used for traversing the pixel points in the region of interest, and screening out a plurality of target pixel points falling into a predetermined target area from all the pixel points in the region of interest;
[0048] The detection module is used to perform circle detection on the image to be tested based on the edge pixels of the image to be tested, all the target pixels and the Hough transform algorithm to obtain the circle contained in the image to be tested and the center point and radius corresponding to the circle.
[0049] As an optional implementation, in the second aspect of the present invention, the device further includes: a target area generation module, the target area generation module is used to:
[0050] Before screening out a plurality of target pixel points falling into a predetermined target area from all pixel points in the region of interest, determining a standard rectangular area according to the preset rectangular frame size;
[0051] Starting from when the standard circle is at the initial position, the standard circle is slid according to a preset step length until the standard circle is tangent to any two adjacent sides of the standard rectangular area during the sliding process, wherein the preset step length is one pixel;
[0052] The area through which the standard circle slides is determined as the target area.
[0053] As an optional implementation, in the second aspect of the present invention, the initial position is a position where the standard circle is tangent to any two adjacent sides of the standard rectangular area;
[0054] The target area generation module is used to start from when the standard circle is in the initial position, slide the standard circle according to a preset step length until the standard circle is tangent to any two adjacent sides of the standard rectangular area during the sliding process. The specific implementation method includes:
[0055] Determine a moving starting position of the standard circle, wherein the moving starting position initially determined is the initial position;
[0056] Starting from the standard circle being at the moving starting position, the standard circle is slid along a first preset direction and by a preset step length each time until the standard circle is tangent to a first target edge of the standard rectangular area, and then the sliding is stopped, wherein the first preset direction is parallel to any one edge of all edges of the standard rectangular area, and the first target edge is an edge of all edges of the standard rectangular area to which the first preset direction points;
[0057] Reset the standard circle to the moving starting position, slide the standard circle once along a second preset direction and with a preset step length, and obtain a first position of the standard circle, wherein the second preset direction is perpendicular to the first preset direction;
[0058] Determine whether the standard circle is tangent to a second target edge of the standard rectangular area when in the first position, wherein the second target edge is an edge of all edges of the standard rectangular area to which the second preset direction points;
[0059] If it is determined that the standard circle is not tangent to the second target side of the standard rectangular area when it is at the first position, the moving starting position is updated according to the first position, and the steps of starting with the standard circle being at the moving starting position, sliding the standard circle according to the rule of sliding along a first preset direction and sliding the preset step length each time until the standard circle is tangent to the first target side of the standard rectangular area and then stopping the sliding are performed, and the step of resetting the standard circle to the moving starting position, sliding the standard circle once according to the rule of sliding along a second preset direction and sliding the preset step length to obtain the first position of the standard circle are performed;
[0060] If it is determined that the standard circle is tangent to the second target edge of the standard rectangular area when it is in the first position, the standard circle is slid starting from the first position along a first preset direction and with a preset step length each time until the sliding ends when the standard circle is tangent to the first target edge of the standard rectangular area.
[0061] As an optional implementation, in the second aspect of the present invention, the traversal module is used to filter out a plurality of target pixel points that fall into a predetermined target area from all pixel points in the region of interest, and the specific implementation method includes:
[0062] In the standard rectangular area, the pixel values of all the pixels included in the target area are marked as 1, and the pixel values of a plurality of remaining pixels except the pixels included in the target area are marked as 0, so as to obtain a mask image;
[0063] Determining coordinates of all first pixel points in the region of interest and coordinates of all second pixel points in the mask image, wherein the coordinates of a first first pixel point in the region of interest are the same as the coordinates of a first second pixel point in the mask image;
[0064] For each first pixel point in the region of interest, according to the coordinates of the first pixel point, obtaining a second pixel point with the same coordinates as the first pixel point in the mask image, and performing an AND operation on a pixel value of the first pixel point and a pixel value of the second pixel point;
[0065] If the result of the operation of the pixel value of the first pixel and the pixel value of the second pixel is 0, it is determined that the first pixel does not fall into the target area;
[0066] If the result of the operation of the pixel value of the first pixel and the pixel value of the second pixel is not 0, it is determined that the first pixel falls into the target area, and the first pixel is determined as the target pixel.
[0067] As an optional implementation, in the second aspect of the present invention, all pixels within the region of interest are stored continuously in rows in a memory;
[0068] The traversal module is used to traverse the pixel points in the region of interest, and the specific implementation method includes:
[0069] Get the preset pixel pointer p;
[0070] Point the pixel pointer p to the memory start address of the first pixel point in the region of interest, and obtain the first pixel point in the region of interest;
[0071] Execute p++ to make the pixel pointer p point to the memory start address of the next pixel point, and obtain the next pixel point, wherein the next pixel point is a subsequent pixel point adjacent to the storage position of the first pixel point in the region of interest in the memory;
[0072] Repeat p++ to obtain all pixel points in the region of interest.
[0073] As an optional implementation, in the second aspect of the present invention, the device further includes: a fitting module, the fitting module being configured to:
[0074] After determining the center point and radius corresponding to the detected circle, performing least square fitting on the edge pixel points of the image to be tested to obtain a fitting result;
[0075] The fitting result is compared with the center point and radius corresponding to the circle to obtain the degree of roundness of the industrial product to be tested.
[0076] As an optional implementation, in the second aspect of the present invention, the fitting module is used to perform least squares fitting on the edge pixels of the image to be measured to obtain a fitting result, and the specific implementation method includes:
[0077] Obtaining a set of edge pixel coordinates of the image to be measured;
[0078] According to the edge pixel coordinate set of the image to be measured, an overdetermined equation group of the circle to be fitted is established;
[0079] The overdetermined equations are solved using the least squares estimation method to obtain a fitting result of the circle to be fitted.
[0080] The third aspect of the present invention discloses another device for detecting a circular shape of an industrial product, characterized in that the device comprises:
[0081] A memory storing executable program code;
[0082] a processor coupled to the memory;
[0083] The processor calls the executable program code stored in the memory to execute the method of the industrial product roundness detection device disclosed in the first aspect of the present invention.
[0084] The fourth aspect of the present invention discloses a computer-readable storage medium, characterized in that the computer-readable storage medium stores computer program instructions, and when the computer program instructions are called, they are used to execute the industrial product circularity detection method disclosed in the first aspect of the present invention.
[0085] Compared with the prior art, the embodiments of the present invention have the following beneficial effects:
[0086] In an embodiment of the present invention, an image to be tested is obtained by collecting an industrial product to be tested located in a shooting area; edge detection is performed on the image to be tested using an edge detection algorithm to obtain edge pixel points of the image to be tested; a region of interest in the image to be tested is determined based on the edge pixel points and a preset rectangular frame size; pixel points in the region of interest are traversed, and a plurality of target pixel points falling into a predetermined target area are screened out from all pixel points in the region of interest; circle detection is performed on the region of interest based on the edge pixel points, all the target pixel points and a Hough transform algorithm to obtain a circle contained in the region of interest and a center point and radius corresponding to the circle; the amount of calculation of the Hough transform algorithm can be reduced, detection efficiency can be improved, and the need for rapid detection of industrial products in industrial production can be met. BRIEF DESCRIPTION OF THE DRAWINGS
[0087] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0088] Figure 1 It is a schematic flow chart of a method for detecting a circular shape of an industrial product disclosed in an embodiment of the present invention;
[0089] Figure 2 It is a schematic diagram of an image to be tested in a method for detecting a circular shape of an industrial product disclosed in an embodiment of the present invention;
[0090] FIG3( a ) is a schematic diagram of a target area in a circular detection method for industrial products disclosed in an embodiment of the present invention;
[0091] FIG3( b ) is a schematic diagram of a target area in another method for detecting a circular shape of an industrial product disclosed in an embodiment of the present invention;
[0092] Figure 4 is a diagram of circle detection based on Hough transform disclosed in an embodiment of the present invention;
[0093] Figure 5 It is a flow chart of another method for detecting a circular shape of an industrial product disclosed in an embodiment of the present invention;
[0094] Figure 6 It is a structural schematic diagram of a circular detection device for industrial products disclosed in an embodiment of the present invention;
[0095] Figure 7 It is a schematic structural diagram of another industrial product circular detection device disclosed in an embodiment of the present invention. DETAILED DESCRIPTION
[0096] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0097] The terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish different objects rather than to describe a specific order. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, device, product or end including a series of steps or units is not limited to the listed steps or units, but may optionally include steps or units that are not listed, or may optionally include other steps or units inherent to these processes, methods, products or ends.
[0098] Reference to "embodiments" herein means that a particular feature, structure, or characteristic described in conjunction with the embodiments may be included in at least one embodiment of the present invention. The appearance of the phrase in various places in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It is explicitly and implicitly understood by those skilled in the art that the embodiments described herein may be combined with other embodiments.
[0099] The present invention discloses a method and device for detecting the circular shape of industrial products, which can reduce the calculation amount of the Hough transform algorithm, improve the detection efficiency, and meet the needs of rapid detection of industrial products in industrial production. The following are detailed descriptions.
[0100] Embodiment 1
[0101] See also Figure 1 , Figure 1 1 is a flow chart of a method for detecting a circular shape of an industrial product disclosed in an embodiment of the present invention. Figure 1 The method for detecting a circular shape of an industrial product described above can be applied to devices such as smart phones, personal computers, and servers, and the embodiments of the present invention are not limited thereto. Figure 1 As shown, the industrial product circularity detection method may include the following operations:
[0102] S1. Collect the industrial products to be tested in the shooting area to obtain the images to be tested.
[0103] In the embodiment of the present invention, an electronic device with a camera is used to collect images, such as Figure 2 As shown, Figure 2 The dotted rectangular area is a preset shooting area, and the circle is an example of an industrial product to be tested. In an optional embodiment, the robot periodically sends the circular product to the front of the camera for collection to obtain the image to be tested.
[0104] S2. Perform edge detection on the image to be tested by using an edge detection algorithm to obtain edge pixel points of the image to be tested.
[0105] In the embodiment of the present invention, an edge detection algorithm is used to perform edge detection, separate the boundary of the industrial product to be tested in the image to be tested, and obtain all edge pixels in the image to be tested. Optionally, the edge detection algorithm can use a Sobel edge detection algorithm, a Laplacian edge detection algorithm, a Canny edge detection algorithm, and other edge detection algorithms commonly used in the art, and the present invention does not limit this.
[0106] S3. Determine the region of interest in the image to be tested according to the edge pixels and the preset rectangular frame size.
[0107] In the embodiment of the present invention, the position of the industrial product to be tested in the image to be tested obtained each time has a certain deviation, but will not exceed the rectangle of a fixed size, such as Figure 2 As shown in the solid rectangular frame. In the embodiment of the present invention, the preset rectangular frame size is determined to be W in width and H in height. According to the edge pixel points obtained in step S2 and the preset rectangular frame with W in width and H in height, the region of interest where the edge pixel points are located is determined in the image to be tested. At the same time, the method for determining the region of interest can use various operators and functions commonly used in machine vision software in the field to obtain the region of interest ROI, and the present invention is not limited to this.
[0108] S4, traversing the pixel points in the region of interest, and screening out a plurality of target pixel points that fall into a predetermined target area from all the pixel points in the region of interest.
[0109] In the prior art, pixels are usually randomly located by specifying (x, y) coordinates. The traditional calculation involved in accessing a pixel I[y][x] is: pixel memory starting address I+y*number of bytes occupied by a row of pixels+x*number of bytes occupied by a pixel. In other words, two additions and two multiplications are performed each time, resulting in a large amount of computation when accessing a pixel.
[0110] In the embodiment of the present invention, optionally, all pixels in the region of interest are stored continuously in the memory by row. That is, all pixels in the region of interest are stored continuously in the memory, and the data of all pixels in the region of interest can be regarded as a one-dimensional array.
[0111] In the embodiment of the present invention, further optionally, the operation of traversing the pixel points in the region of interest in step S4 is implemented in the following manner:
[0112] Get the preset pixel pointer p.
[0113] Point the pixel pointer p to the memory starting address of the first pixel in the region of interest, and obtain the first pixel in the region of interest.
[0114] Execute p++ to make the pixel pointer p point to the memory start address of the next pixel point, and obtain the next pixel point, where the next pixel point is the subsequent pixel point adjacent to the storage position of the first pixel point in the region of interest in the memory.
[0115] Repeat p++ to obtain all pixels in the region of interest.
[0116] That is to say, in the embodiment of the present invention, when traversing pixels, the pixel is consciously adjusted to be accessed in a row-first-column-later manner. Let a pixel pointer p initially point to the pixel memory start address I. After that, it only needs to execute p++ to point to the next adjacent pixel. Accessing the next pixel only requires one addition. The amount of calculation is one-fourth of the traditional method. Considering that multiplication instructions are more time-consuming than addition instructions, the improvement of this step improves the performance by more than 4 times.
[0117] In the embodiment of the present invention, optionally, before screening out a plurality of target pixel points falling into a predetermined target area from all pixel points in the region of interest, the method further includes:
[0118] The standard rectangular area is determined according to a preset rectangular frame size. For example, the preset rectangular frame size is W in width and H in height.
[0119] Starting from the initial position of the standard circle, the standard circle is slid according to a preset step length until the standard circle is tangent to any two adjacent sides of the standard rectangular area during the sliding process, wherein the preset step length is one pixel.
[0120] The area passed by the standard circle sliding is determined as the target area.
[0121] In the embodiment of the present invention, further optionally, the initial position is a position where any two adjacent sides of the standard circle and the standard rectangular area are tangent.
[0122] Starting from the initial position of the standard circle, the standard circle is slid according to the preset step length until the standard circle is tangent to any two adjacent sides of the standard rectangular area during the sliding process, which specifically includes the following contents:
[0123] The moving starting position of the standard circle is determined, wherein the initially determined moving starting position is the initial position.
[0124] Starting with the standard circle at the starting position of movement, the standard circle is slid along a first preset direction and a preset step length each time until the standard circle is tangent to a first target edge of the standard rectangular area, wherein the first preset direction is parallel to any one of all edges of the standard rectangular area, and the first target edge is the edge of all edges of the standard rectangular area to which the first preset direction points.
[0125] The standard circle is reset to the moving starting position, and the standard circle is slid once along the second preset direction and the preset sliding step length to obtain the first position of the standard circle, wherein the second preset direction is perpendicular to the first preset direction.
[0126] It is determined whether the standard circle is tangent to a second target edge of the standard rectangular area when in the first position, wherein the second target edge is an edge of all edges of the standard rectangular area to which the second preset direction points.
[0127] If it is determined that the standard circle is not tangent to the second target edge of the standard rectangular area when it is in the first position, the moving starting position is updated according to the first position, and the steps of starting with the standard circle in the moving starting position, sliding the standard circle according to the rule of sliding along the first preset direction and the preset step length each time until the standard circle is tangent to the first target edge of the standard rectangular area are stopped, and the step of resetting the standard circle to the moving starting position, sliding the standard circle once according to the rule of sliding along the second preset direction and the preset step length to obtain the first position of the standard circle are executed.
[0128] If it is determined that the standard circle is tangent to the second target edge of the standard rectangular area when it is in the first position, the standard circle is slid starting from the first position along the first preset direction with a preset step length each time until the sliding ends when the standard circle is tangent to the first target edge of the standard rectangular area.
[0129] For example, let the circle representing the edge of the product be located at the upper left corner of a W*H rectangular frame and tangent to both sides of the rectangular frame, as shown in Figure 3(a). Let the circle slide from left to right one pixel at a time in the W*H rectangular frame. When it touches the right edge of the rectangle, let the circle return to the leftmost side, then drop one pixel, and then move from left to right. Continue this way, and the last circle is located at the lower right corner of the rectangular frame and tangent to both sides of the rectangular frame, as shown in Figure 3(b). In this process, the area swept by the points on the edge of the circle is the target area. Obviously, points in non-target areas do not need to be detected in the Hough circle transform calculation.
[0130] In the embodiment of the present invention, optionally, a plurality of target pixel points falling into a predetermined target area are screened out from all pixel points in the region of interest, specifically including the following contents:
[0131] In the standard rectangular area, the pixel values of all pixels included in the target area are marked as 1, and the pixel values of a plurality of remaining pixels other than the pixels included in the target area are marked as 0, thereby obtaining a mask image. In this optional embodiment, a program can be written in Python to move a circle in a rectangle and record the points where the circle passes, thereby obtaining the target area.
[0132] Coordinates of all first pixel points in the region of interest and coordinates of all second pixel points in the mask image are determined, wherein the coordinates of a first first pixel point in the region of interest are the same as the coordinates of a first second pixel point in the mask image.
[0133] For each first pixel point in the region of interest, a second pixel point with the same coordinates as the first pixel point is obtained in the mask image according to the coordinates of the first pixel point, and an AND operation is performed on the pixel value of the first pixel point and the pixel value of the second pixel point.
[0134] If the result of the operation of the AND operation of the pixel value of the first pixel and the pixel value of the second pixel is 0, it is determined that the first pixel does not fall into the target area.
[0135] If the result of the operation of the pixel value of the first pixel and the pixel value of the second pixel is not 0, it is determined that the first pixel falls into the target area, and the first pixel is determined as the target pixel.
[0136] It can be seen that in this optional embodiment, when traversing a rectangle, it is only necessary to see whether the point corresponding to the mask image is 1 to determine whether traversal is required, and efficiency is further improved.
[0137] S5. Based on edge pixels, all target pixels and Hough transform algorithm, circle detection is performed on the region of interest to obtain the circle contained in the region of interest and the center point and radius corresponding to the circle.
[0138] Hough transform is a feature detection algorithm that is widely used in image analysis, computer vision, and digital image processing. Hough transform is used to identify features in objects, such as circles. The algorithm works as follows: given an object and the type of shape to be identified, the algorithm performs a vote in the parameter space to determine the shape of the object, which is determined by the local maximum in the accumulator space.
[0139] By defining the shape equation to be detected, shape detection can be achieved. For example, in explicit form, the Hough transform is defined by considering the equation of a circle as follows (1):
[0140] (x-x0) 2 +(y-y0) 2 =r 2 (1)
[0141] This equation defines a trajectory of points (x,y) with the origin (x0,y0) as the center and radius r as the circle. This equation can be visualized in two other ways: as the trajectory of points (x,y) in the image, or as the trajectory of points (x0,y0) with (x,y) as the center and radius r as the circle. Figure 4 The illustration of circle detection based on Hough transform illustrates both definitions.
[0142] Figure 4 In (a) the image contains circles, each edge point defines a set of circles in the accumulator space, which are defined by all possible radii, and the coordinates of their edge points are the centers of the circles. Figure 4 The accumulator space in (b) gives three circles defined by three edge points with a given radius, and each edge point defines a circle with a radius of the other value. This means that the accumulator space is three-dimensional (with three parameters of interest) and the edge points are mapped to a cone of votes in the accumulator space. Figure 4 (c) The three-dimensional accumulation space is such an accumulator. After collecting all edge point evidence, the maximum value of the accumulator space corresponds to the parameters of the circle of the original image. The voting is formed by generating a cone according to the above formula (1).
[0143] The above formula (1) can be defined in parameter form as:
[0144] x=x0+rcos(θ)y=y0+rsin(θ) (2)
[0145] The advantage of this representation is that it allows solving parameter problems. Therefore, the Hough transform mapping is defined as:
[0146] x0=x-rcos(θ)y0=y-rsin(θ) (3)
[0147] These equations define a point in the accumulator space, depending on the radius r.
[0148] The Hough circle transform can be used to find a circle with a radius within a specified range and obtain its center point and radius. However, the traditional Hough transform process traverses all points in the entire rectangular area once. For a preset rectangular box with a width of W and a height of H, the traditional Hough transform needs to perform detection operations on all pixels in the area, which requires a lot of calculations and has a very low detection efficiency.
[0149] In the embodiment of the present invention, by determining the target area and the target pixel point, the detection points involved in the Hough transform are reduced, thereby increasing the speed by more than double. The proof is as follows:
[0150] For example, the rectangular frame of W*H is set to an area of 430 pixels*400 pixels, and the diameter of the product to be inspected is 370.
[0151] For the target area shown in Figure 3(a) and Figure 3(b), take the midpoints of each side of the rectangular frame, connect the midpoints of two sets of opposite sides, and get the rectangular cross lines, which divide the rectangle into four areas of equal size. Take two diameter lines of the circle, which are parallel to the rectangular cross lines, and divide the circle into four areas of equal size.
[0152] The areas of the two white areas are recorded as S1 and S2, as shown in Figure 3(b). It can be seen that as long as S1+S2 is divided by W*H / 4, the proportion of the gray area in the rectangle can be obtained.
[0153] The coordinates of point A are (Ax, Ay) and the coordinates of point B are (Bx, By).
[0154]
[0155] Calculate S2 by definite integral:
[0156]
[0157] Calculate the ratio of gray area to rectangular area:
[0158]
[0159] The calculation results are:
[0160]
[0161] Finally, we substitute W = 430, H = 400, r = 370 / 2 into the above formula and get 0.667. That is to say, in the 430*400 rectangle, the non-gray area generated by the circle with a diameter of 370 accounts for more than 66% of the rectangle, so more than 66% of the points do not need to be traversed.
[0162] It can be seen that the embodiments of the present invention can reduce the number of detection points involved in the Hough transform by determining the target area and the target pixel points, thereby reducing the amount of Hough transform calculations and improving the calculation speed.
[0163] In the embodiment of the present invention, optionally, when updating the accumulator, the value of the accumulator of the Hough circle transform is updated once every preset degree, wherein the preset degree is greater than 1, for example, the preset degree is 10 degrees. This method can increase the speed of calculation by 10 times compared with the traditional method of updating once every degree.
[0164] In the embodiment of the present invention, optionally, in the Hough circle detection process, each traversed point is used as the center of the circle to calculate the pixel points involved on the edge of the circle of the specified radius. In this optional embodiment, determining the pixel points involved on the circumference includes the following steps:
[0165] Determine a circle with a specified radius with a target pixel as the center.
[0166] Taking the target pixel point as the origin of the coordinate system, determine the four quadrants of the circle.
[0167] Calculate the coordinates of the point in the first quadrant on the circumference of the circle relative to the target pixel point.
[0168] Using the symmetry of a circle, based on the calculated coordinates of a point in the first quadrant on the circumference of the circle relative to the target pixel, the coordinates of points in the remaining three quadrants on the circumference of the circle relative to the target pixel are obtained.
[0169] For each point on the circumference of the circle, determine the coordinates of the point relative to the target pixel; based on the coordinates of the point relative to the target pixel and the coordinates of the target pixel in the region of interest, obtain the coordinates of the point in the region of interest, and then determine the pixel point corresponding to the point in the region of interest. This step can determine all the pixel points involved on the circumference of the circle.
[0170] It can be seen that this optional embodiment utilizes the symmetry of the circle and can quickly obtain the coordinates of all points on the circumference by only calculating the coordinates of points in one quadrant, thereby reducing the amount of calculation during the calculation process and improving processing efficiency.
[0171] In the embodiment of the present invention, optionally, the target pixel point (x i ,y j ) is the center of the circle and r is the radius. When the pixel points involved on the circumference are (x, y), the pixel points involved on the circumference can be expressed by the polar coordinate formula:
[0172] x=x i +rcos(α)y=y j +rsin(α) (4)
[0173] However, in the actual operation process, calculating trigonometric functions is relatively time-consuming. In this optional embodiment, the properties of trigonometric functions can be used to reduce the amount of calculation:
[0174] Determine the XY coordinate system of the circle and determine the four quadrants of the circle;
[0175] Take any point on the circumference of the circle in the first quadrant, record it as point p1, determine the rotation angle α1 of point p1 relative to the X-axis, and calculate sin(α1), cos(α1);
[0176] Determine a point symmetric to point p1 about the Y axis, denoted as point p2, point p2 is a point on the circumference of the circle in the second quadrant, determine the rotation angle of point p2 as α2, and determine sin(α2) and cos(α2), where α2 = (π-α1), sin(α2) = sin(α1), cos(α2) = -cos(α1);
[0177] Determine a point symmetric to point p2 about the X-axis, denoted as point p3, point p3 is a point on the circumference of the circle in the third quadrant, determine the rotation angle of point p3 as α3, and determine sin(α3) and cos(α3), where α3 = (π + α1), sin(α3) = -sin(α1), cos(α2) = -cos(α1);
[0178] Determine a point symmetrical to point p1 about the X-axis, recorded as point p4. Point p4 is a point on the circumference of the circle in the fourth quadrant. Determine the rotation angle of point p4 as α4, and determine sin(α4) and cos(α4), where α4=(2π-α1), sin(α2)=-sin(α1), and cos(α2)=cos(α1).
[0179] It can be seen that this optional embodiment utilizes the properties of trigonometric functions and the symmetry of the circle and can quickly obtain the coordinates of all points on the circle by only calculating the trigonometric functions of points in one quadrant, thereby reducing the amount of calculation during the calculation process and improving processing efficiency.
[0180] It can be seen that through the implementation Figure 1 The method for detecting circles of industrial products described herein collects industrial products to be detected in a shooting area to obtain an image to be detected; performs edge detection on the image to be detected by an edge detection algorithm to obtain edge pixel points of the image to be detected; determines an area of interest in the image to be detected according to the edge pixel points and a preset rectangular frame size; traverses the pixel points in the area of interest, and screens out a plurality of target pixel points that fall into a predetermined target area from all the pixel points in the area of interest; performs circle detection on the area of interest based on the edge pixel points, all the target pixel points and the Hough transform algorithm to obtain a circle contained in the area of interest and a center point and radius corresponding to the circle; the method can reduce the amount of calculation of the Hough transform algorithm, improve the detection efficiency, and meet the needs of rapid detection of industrial products in industrial production.
[0181] Embodiment 2
[0182] See also Figure 5 , Figure 5 1 is a flow chart of another method for detecting the circular shape of industrial products disclosed in an embodiment of the present invention. Figure 5The method for detecting a circular shape of an industrial product described above can be applied to devices such as smart phones, personal computers, and servers, and the embodiments of the present invention are not limited thereto. Figure 5 As shown, the industrial product circularity detection method may include the following operations:
[0183] S51 , collecting the industrial product to be tested in the shooting area to obtain an image to be tested.
[0184] S52 , performing edge detection on the image to be tested by using an edge detection algorithm to obtain edge pixel points of the image to be tested.
[0185] S53: Determine the region of interest in the image to be tested according to the edge pixels and the preset rectangular frame size.
[0186] S54, traversing the pixel points in the region of interest, and screening out a plurality of target pixel points that fall into a predetermined target area from all the pixel points in the region of interest.
[0187] S55, based on edge pixels, all target pixels and Hough transform algorithm, circle detection is performed on the region of interest to obtain the circle included in the region of interest and the center point and radius corresponding to the circle.
[0188] In the embodiment of the present invention, for other descriptions of steps S51-S55, please refer to other specific descriptions of steps S1-S5 in the first embodiment, and the embodiment of the present invention will not be repeated here.
[0189] In the embodiment of the present invention, optionally, after step S55, the method for detecting the circular shape of industrial products further comprises the following steps:
[0190] S56, performing least square fitting on edge pixels of the image to be measured to obtain a fitting result.
[0191] S57. Compare the fitting result with the center point and radius corresponding to the circle to obtain the degree of roundness of the industrial product to be tested.
[0192] In the embodiment of the present invention, optionally, step S56, performing least square fitting on edge pixels of the image to be measured and obtaining a fitting result, includes:
[0193] The first step is to obtain the edge pixel coordinate set of the image to be tested.
[0194] Assume that the edge pixel coordinates of the image to be tested are {(x1,y1), (x2,y2)...(x n ,y n )}.
[0195] The second step is to establish an overdetermined set of equations for the circle to be fitted based on the edge pixel coordinates of the image to be measured.
[0196] To fit a circle: (x-c1) 2 +(y-c2) 2 =r 2
[0197] Its center is (c1, c2), the radius is r, let c3 = r 2 -c1 2 -c2 2 .
[0198] Then we get: 2xc1+2yc2+c3=x 2 +y 2
[0199] Substituting the boundary point array into the equation, we get an overdetermined system of equations:
[0200]
[0201] The third step is to use the least squares estimation method to solve the overdetermined equations and obtain the fitting result of the circle to be fitted.
[0202] Let the left side of the above overdetermined equations be the product of a matrix A with n rows and 3 columns and a vector Z with 3 rows and 1 column, and the right side be an n-element column vector b, and we get A*Z=b.
[0203] Using Python's Numpy package, we can find:
[0204] Import Numpy.linalg as la
[0205] res=la.lstsq(A,b) is the solution using the least squares method.
[0206] res[0] is a 3-element array containing the value of the unknown vector z.
[0207] res[1][0] is the fitting residual, which represents the quality of the fit.
[0208] It can be seen that through the implementation Figure 5 The described method for detecting the circularity of an industrial product can determine the degree of circularity of the industrial product to be detected by comparing the circle fitted by edge pixel points with the circle detected by Hough transform.
[0209] Embodiment 3
[0210] See also Figure 6 , Figure 6 This is a schematic diagram of a process flow of a circular detection device for industrial products disclosed in an embodiment of the present invention. Figure 6 The industrial product roundness detection device described above can be adapted to devices such as smart phones, personal computers, and servers, and the embodiments of the present invention are not limited thereto. Figure 6 As shown, the industrial product circular detection device may include the following operations:
[0211] The acquisition module 601 is used to acquire the industrial product to be tested in the shooting area to obtain the image to be tested.
[0212] In the embodiment of the present invention, an electronic device with a camera is used to collect images, such as Figure 2 As shown, Figure 2 The dotted rectangular area is a preset shooting area, and the circle is an example of an industrial product to be tested. In an optional embodiment, the robot periodically sends the circular product to the front of the camera for collection to obtain the image to be tested.
[0213] The edge detection module 602 is used to perform edge detection on the image to be tested by using an edge detection algorithm to obtain edge pixel points of the image to be tested.
[0214] In the embodiment of the present invention, an edge detection algorithm is used to perform edge detection, separate the boundary of the industrial product to be tested in the image to be tested, and obtain all edge pixels in the image to be tested. Optionally, the edge detection algorithm can use a Sobel edge detection algorithm, a Laplacian edge detection algorithm, a Canny edge detection algorithm, and other edge detection algorithms commonly used in the art, and the present invention does not limit this.
[0215] The determination module 603 is used to determine the region of interest in the image to be tested according to the edge pixels and the preset rectangular frame size.
[0216] In the embodiment of the present invention, the position of the industrial product to be tested in the image to be tested obtained each time has a certain deviation, but will not exceed the rectangle of a fixed size, such as Figure 2 As shown in the solid rectangular frame. In the embodiment of the present invention, the preset rectangular frame size is determined to be W in width and H in height. According to the edge pixel points obtained in step S2 and the preset rectangular frame with W in width and H in height, the region of interest where the edge pixel points are located is determined in the image to be tested. At the same time, the method for determining the region of interest can use various operators and functions commonly used in machine vision software in the field to obtain the region of interest ROI, and the present invention is not limited to this.
[0217] The traversal module 604 is used to traverse the pixel points in the region of interest and select a plurality of target pixel points that fall into a predetermined target area from all the pixel points in the region of interest.
[0218] In the prior art, pixels are usually randomly located by specifying (x, y) coordinates. The traditional calculation involved in accessing a pixel I[y][x] is: pixel memory starting address I+y*number of bytes occupied by a row of pixels+x*number of bytes occupied by a pixel. In other words, two additions and two multiplications are performed each time, resulting in a large amount of computation when accessing a pixel.
[0219] In the embodiment of the present invention, optionally, all pixels in the region of interest are stored continuously in the memory by row. That is, all pixels in the region of interest are stored continuously in the memory, and the data of all pixels in the region of interest can be regarded as a one-dimensional array.
[0220] In the embodiment of the present invention, further optionally, the specific implementation method of traversing the pixel points in the region of interest by the traversal module 604 includes:
[0221] Get the preset pixel pointer p.
[0222] Point the pixel pointer p to the memory starting address of the first pixel in the region of interest, and obtain the first pixel in the region of interest.
[0223] Execute p++ to make the pixel pointer p point to the memory start address of the next pixel point, and obtain the next pixel point, where the next pixel point is the subsequent pixel point adjacent to the storage position of the first pixel point in the region of interest in the memory.
[0224] Repeat p++ to obtain all pixels in the region of interest.
[0225] That is to say, in the embodiment of the present invention, when traversing pixels, the pixel is consciously adjusted to be accessed in a row-first-column-later manner. Let a pixel pointer p initially point to the pixel memory start address I. After that, it only needs to execute p++ to point to the next adjacent pixel. Accessing the next pixel only requires one addition. The amount of calculation is one-fourth of the traditional method. Considering that multiplication instructions are more time-consuming than addition instructions, the improvement of this step improves the performance by more than 4 times.
[0226] In the embodiment of the present invention, optionally, before the traversal module 604 screens out a plurality of target pixel points that fall into a predetermined target area from all pixel points in the region of interest, the device further includes: a target area generating module 600 .
[0227] The target area generation module 600 is used to determine the standard rectangular area according to the preset rectangular frame size. For example, the preset rectangular frame size is W in width and H in height. Starting from the time when the standard circle is in the initial position, the standard circle is slid according to the preset step length until the standard circle is tangent to any two adjacent sides of the standard rectangular area during the sliding process, wherein the preset step length is one pixel. The area through which the standard circle slides is determined as the target area.
[0228] In the embodiment of the present invention, further optionally, the initial position is a position where any two adjacent sides of the standard circle and the standard rectangular area are tangent.
[0229] The target area generation module 600 starts with the standard circle being at an initial position, and slides the standard circle according to a preset step length until the standard circle is tangent to any two adjacent sides of the standard rectangular area during the sliding process, specifically including the following contents:
[0230] The moving starting position of the standard circle is determined, wherein the initially determined moving starting position is the initial position.
[0231] Starting with the standard circle at the starting position of movement, the standard circle is slid along a first preset direction and a preset step length each time until the standard circle is tangent to a first target edge of the standard rectangular area, wherein the first preset direction is parallel to any one of all edges of the standard rectangular area, and the first target edge is the edge of all edges of the standard rectangular area to which the first preset direction points.
[0232] The standard circle is reset to the moving starting position, and the standard circle is slid once along the second preset direction and the preset sliding step length to obtain the first position of the standard circle, wherein the second preset direction is perpendicular to the first preset direction.
[0233] It is determined whether the standard circle is tangent to a second target edge of the standard rectangular area when in the first position, wherein the second target edge is an edge of all edges of the standard rectangular area to which the second preset direction points.
[0234] If it is determined that the standard circle is not tangent to the second target edge of the standard rectangular area when it is in the first position, the moving starting position is updated according to the first position, and the steps of starting with the standard circle in the moving starting position, sliding the standard circle according to the rule of sliding along the first preset direction and the preset step length each time until the standard circle is tangent to the first target edge of the standard rectangular area are stopped, and the step of resetting the standard circle to the moving starting position, sliding the standard circle once according to the rule of sliding along the second preset direction and the preset step length to obtain the first position of the standard circle are executed.
[0235] If it is determined that the standard circle is tangent to the second target edge of the standard rectangular area when it is in the first position, the standard circle is slid starting from the first position along the first preset direction with a preset step length each time until the sliding ends when the standard circle is tangent to the first target edge of the standard rectangular area.
[0236] For example, let the circle representing the edge of the product be located at the upper left corner of a W*H rectangular frame and tangent to both sides of the rectangular frame, as shown in Figure 3(a). Let the circle slide from left to right one pixel at a time in the W*H rectangular frame. When it touches the right edge of the rectangle, let the circle return to the leftmost side, then drop one pixel, and then move from left to right. Continue this way, and the last circle is located at the lower right corner of the rectangular frame and tangent to both sides of the rectangular frame, as shown in Figure 3(b). In this process, the area swept by the points on the edge of the circle is the target area. Obviously, points in non-target areas do not need to be detected in the Hough circle transform calculation.
[0237] In the embodiment of the present invention, optionally, the traversal module 604 selects a plurality of target pixel points that fall into a predetermined target area from all pixel points in the region of interest, specifically including the following contents:
[0238] In the standard rectangular area, the pixel values of all pixels included in the target area are marked as 1, and the pixel values of a plurality of remaining pixels other than the pixels included in the target area are marked as 0, thereby obtaining a mask image. In this optional embodiment, a program can be written in Python to move a circle in a rectangle and record the points where the circle passes, thereby obtaining the target area.
[0239] Coordinates of all first pixel points in the region of interest and coordinates of all second pixel points in the mask image are determined, wherein the coordinates of a first first pixel point in the region of interest are the same as the coordinates of a first second pixel point in the mask image.
[0240] For each first pixel point in the region of interest, a second pixel point with the same coordinates as the first pixel point is obtained in the mask image according to the coordinates of the first pixel point, and an AND operation is performed on the pixel value of the first pixel point and the pixel value of the second pixel point.
[0241] If the result of the operation of the AND operation of the pixel value of the first pixel and the pixel value of the second pixel is 0, it is determined that the first pixel does not fall into the target area.
[0242] If the result of the operation of the pixel value of the first pixel and the pixel value of the second pixel is not 0, it is determined that the first pixel falls into the target area, and the first pixel is determined as the target pixel.
[0243] It can be seen that in this optional embodiment, when traversing a rectangle, it is only necessary to see whether the point corresponding to the mask image is 1 to determine whether traversal is required, and efficiency is further improved.
[0244] The detection module 605 is used to perform circle detection on the region of interest based on edge pixels, all target pixels and the Hough transform algorithm to obtain the circle included in the region of interest and the center point and radius corresponding to the circle.
[0245] Hough transform is a feature detection algorithm that is widely used in image analysis, computer vision, and digital image processing. Hough transform is used to identify features in objects, such as circles. The algorithm works as follows: given an object and the type of shape to be identified, the algorithm performs a vote in the parameter space to determine the shape of the object, which is determined by the local maximum in the accumulator space.
[0246] By defining the shape equation to be detected, shape detection can be achieved. For example, in explicit form, the Hough transform is defined by considering the equation of a circle as follows:
[0247] (x-x0) 2 +(y-y0) 2 =r 2 (1)
[0248] This equation defines a trajectory of points (x,y) with the origin (x0,y0) as the center and radius r as the circle. This equation can be visualized in two other ways: as the trajectory of points (x,y) in the image, or as the trajectory of points (x0,y0) with (x,y) as the center and radius r as the circle. Figure 4 The illustration of circle detection based on Hough transform illustrates both definitions.
[0249] Figure 4 In (a) the image contains circles, each edge point defines a set of circles in the accumulator space, which are defined by all possible radii, and the coordinates of their edge points are the centers of the circles. Figure 4 The accumulator space in (b) gives three circles defined by three edge points with a given radius, and each edge point defines a circle with a radius of the other value. This means that the accumulator space is three-dimensional (with three parameters of interest) and the edge points are mapped to a cone of votes in the accumulator space. Figure 4 (c) The three-dimensional accumulation space is such an accumulator. After collecting all edge point evidence, the maximum value of the accumulator space corresponds to the parameters of the circle of the original image. The voting is formed by generating a cone according to the above formula (1).
[0250] The above formula (1) can be defined in parameter form as:
[0251] x=x0+rcos(θ)y=y0+rsin(θ) (2)
[0252] The advantage of this representation is that it allows solving parameter problems. Therefore, the Hough transform mapping is defined as:
[0253] x0=x-rcos(θ)y0=y-rsin(θ) (3)
[0254] These equations define a point in the accumulator space, depending on the radius r.
[0255] The Hough circle transform can be used to find a circle with a radius within a specified range and obtain its center point and radius. However, the traditional Hough transform process traverses all points in the entire rectangular area once. For a preset rectangular box with a width of W and a height of H, the traditional Hough transform needs to perform detection operations on all pixels in the area, which requires a lot of calculations and has a very low detection efficiency.
[0256] In the embodiment of the present invention, by determining the target area and the target pixel point, the detection points involved in the Hough transform are reduced, thereby increasing the speed by more than double. The proof is as follows:
[0257] For example, the rectangular frame of W*H is set to an area of 430 pixels*400 pixels, and the diameter of the product to be inspected is 370.
[0258] For the target area shown in Figure 3(a) and Figure 3(b), take the midpoints of each side of the rectangular frame, connect the midpoints of two sets of opposite sides, and get the rectangular cross lines, which divide the rectangle into four areas of equal size. Take two diameter lines of the circle, which are parallel to the rectangular cross lines, and divide the circle into four areas of equal size.
[0259] The areas of the two white areas are recorded as S1 and S2, as shown in Figure 3(b). It can be seen that as long as S1+S2 is divided by W*H / 4, the proportion of the gray area in the rectangle can be obtained.
[0260] The coordinates of point A are (Ax, Ay) and the coordinates of point B are (Bx, By).
[0261]
[0262] Calculate S2 by definite integral:
[0263]
[0264] Calculate the ratio of gray area to rectangular area:
[0265]
[0266] The calculation results are:
[0267]
[0268] Finally, we substitute W = 430, H = 400, r = 370 / 2 into the above formula and get 0.667. That is to say, in the 430*400 rectangle, the non-gray area generated by the circle with a diameter of 370 accounts for more than 66% of the rectangle, so more than 66% of the points do not need to be traversed.
[0269] It can be seen that the embodiments of the present invention can reduce the detection points involved in the Hough transform by determining the target area and the target pixel points, reduce the amount of Hough transform calculations, and improve the calculation speed.
[0270] In the embodiment of the present invention, optionally, when updating the accumulator, the value of the accumulator of the Hough circle transform is updated once every preset degree, wherein the preset degree is greater than 1, for example, the preset degree is 10 degrees. This method can increase the speed of calculation by 10 times compared with the traditional method of updating once every degree.
[0271] In the embodiment of the present invention, optionally, in the Hough circle detection process, each traversed point is used as the center of the circle to calculate the pixel points involved on the edge of the circle of the specified radius. In this optional embodiment, determining the pixel points involved on the circumference includes the following steps:
[0272] Determine a circle with a specified radius with a target pixel as the center.
[0273] Taking the target pixel point as the origin of the coordinate system, determine the four quadrants of the circle.
[0274] Calculate the coordinates of the point in the first quadrant on the circumference of the circle relative to the target pixel point.
[0275] Using the symmetry of a circle, based on the calculated coordinates of a point in the first quadrant on the circumference of the circle relative to the target pixel, the coordinates of points in the remaining three quadrants on the circumference of the circle relative to the target pixel are obtained.
[0276] For each point on the circumference of the circle, determine the coordinates of the point relative to the target pixel; based on the coordinates of the point relative to the target pixel and the coordinates of the target pixel in the region of interest, obtain the coordinates of the point in the region of interest, and then determine the pixel point corresponding to the point in the region of interest. This step can determine all the pixel points involved on the circumference of the circle.
[0277] It can be seen that this optional embodiment utilizes the symmetry of the circle and can quickly obtain the coordinates of all points on the circumference by only calculating the coordinates of points in one quadrant, thereby reducing the amount of calculation during the calculation process and improving processing efficiency.
[0278] In the embodiment of the present invention, optionally, the target pixel point (x i ,y j ) is the center of the circle and r is the radius. When the pixel points involved on the circumference are (x, y), the pixel points involved on the circumference can be expressed by the polar coordinate formula:
[0279] x=x i +rcos(α)y=y j +rsin(α) (4)
[0280] However, in the actual operation process, calculating trigonometric functions is relatively time-consuming. In this optional embodiment, the properties of trigonometric functions can be used to reduce the amount of calculation:
[0281] Determine the XY coordinate system of the circle and determine the four quadrants of the circle;
[0282] Take any point on the circumference of the circle in the first quadrant, record it as point p1, determine the rotation angle α1 of point p1 relative to the X-axis, and calculate sin(α1), cos(α1);
[0283] Determine a point symmetric to point p1 about the Y axis, denoted as point p2, point p2 is a point on the circumference of the circle in the second quadrant, determine the rotation angle of point p2 as α2, and determine sin(α2) and cos(α2), where α2 = (π-α1), sin(α2) = sin(α1), cos(α2) = -cos(α1);
[0284] Determine a point symmetric to point p2 about the X-axis, denoted as point p3, point p3 is a point on the circumference of the circle in the third quadrant, determine the rotation angle of point p3 as α3, and determine sin(α3) and cos(α3), where α3 = (π + α1), sin(α3) = -sin(α1), cos(α2) = -cos(α1);
[0285] Determine a point symmetrical to point p1 about the X-axis, recorded as point p4. Point p4 is a point on the circumference of the circle in the fourth quadrant. Determine the rotation angle of point p4 as α4, and determine sin(α4) and cos(α4), where α4=(2π-α1), sin(α2)=-sin(α1), and cos(α2)=cos(α1).
[0286] It can be seen that this optional embodiment utilizes the properties of trigonometric functions and the symmetry of the circle and can quickly obtain the coordinates of all points on the circle by only calculating the trigonometric functions of points in one quadrant, thereby reducing the amount of calculation during the calculation process and improving processing efficiency.
[0287] In the embodiment of the present invention, optionally, the circularity detection device for industrial products in the embodiment of the present invention further includes: a fitting module 606, and the fitting module 606 is used to:
[0288] The least squares fitting is performed on the edge pixels of the image to be tested to obtain the fitting result; and the fitting result is compared with the center point and radius corresponding to the circle to obtain the degree of roundness of the industrial product to be tested.
[0289] In the embodiment of the present invention, further optionally, least squares fitting is performed on edge pixels of the image to be measured, and a fitting result is obtained. The specific implementation method includes:
[0290] The first step is to obtain the edge pixel coordinate set of the image to be tested.
[0291] Assume that the edge pixel coordinates of the image to be tested are {(x1,y1), (x2,y2)...(x n ,y n )}.
[0292] The second step is to establish an overdetermined set of equations for the circle to be fitted based on the edge pixel coordinates of the image to be measured.
[0293] To fit a circle: (x-c1) 2 +(y-c2) 2 =r 2
[0294] Its center is (c1, c2), the radius is r, let c3 = r 2 -c1 2 -c2 2 .
[0295] Then we get: 2xc1+2yc2+c3=x 2 +y 2
[0296] Substituting the boundary point array into the equation, we get an overdetermined system of equations:
[0297]
[0298] The third step is to use the least squares estimation method to solve the overdetermined equations and obtain the fitting result of the circle to be fitted.
[0299] Let the left side of the above overdetermined equations be the product of a matrix A with n rows and 3 columns and a vector Z with 3 rows and 1 column, and the right side be an n-element column vector b, and we get A*Z=b.
[0300] Using Python's Numpy package, we can find:
[0301] Import Numpy.linalg as la
[0302] res=la.lstsq(A,b) is the solution using the least squares method.
[0303] res[0] is a 3-element array containing the value of the unknown vector z.
[0304] res[1][0] is the fitting residual, which represents the quality of the fit.
[0305] It can be seen that in this optional embodiment, the circle fitted by the edge pixel points is compared with the circle detected by the Hough transform, so as to determine the degree of roundness of the industrial product to be tested.
[0306] It can be seen that through Figure 6 The described industrial product circle detection device collects the industrial product to be tested located in the shooting area to obtain the image to be tested; performs edge detection on the image to be tested by using an edge detection algorithm to obtain edge pixel points of the image to be tested; determines the region of interest in the image to be tested according to the edge pixel points and the preset rectangular frame size; traverses the pixel points in the region of interest, and screens out a plurality of target pixel points that fall into a predetermined target area from all the pixel points in the region of interest; performs circle detection on the region of interest based on the edge pixel points, all the target pixel points and the Hough transform algorithm to obtain the circle contained in the region of interest and the center point and radius corresponding to the circle; the calculation amount of the Hough transform algorithm can be reduced, the detection efficiency can be improved, and the demand for rapid detection of industrial products in industrial production can be met.
[0307] Embodiment 4
[0308] See also Figure 7 , Figure 7 FIG. 1 is a schematic diagram of another industrial product circular detection device disclosed in an embodiment of the present invention. Figure 7 As shown, the industrial product circular detection device may include:
[0309] A memory 701 storing executable program codes;
[0310] a processor 702 coupled to the memory 701;
[0311] The processor 702 calls the executable program code stored in the memory 701 to execute the steps of the method for detecting the circular shape of industrial products described in the first embodiment of the present invention or the second embodiment of the present invention.
[0312] Embodiment 5
[0313] An embodiment of the present invention discloses a computer storable medium, which stores computer instructions. When the computer instructions are called, they are used to execute the steps of the industrial product roundness detection method described in the first embodiment of the present invention or the second embodiment of the present invention.
[0314] Embodiment 6
[0315] An embodiment of the present invention discloses a computer program product, which includes a non-transitory computer-readable storage medium storing a computer program, and the computer program is operable to enable a computer to execute the steps of the method for intelligently controlling the circulation of recyclable containers described in Embodiment 1 or Embodiment 2.
[0316] The device embodiments described above are only illustrative, wherein the modules described as separate components may or may not be physically separated, and the components displayed as modules may or may not be physical modules, i.e., they may be located in one place, or they may be distributed on multiple network modules. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Those of ordinary skill in the art may understand and implement it without creative work.
[0317] Through the specific description of the above embodiments, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on such an understanding, the above technical solution can be essentially or partly contributed to the prior art in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, and the storage medium includes a read-only memory (ROM), a random access memory (RAM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), a one-time programmable read-only memory (OTPROM), an electronically erasable rewritable read-only memory (EEPROM), a compact disc (CD-ROM) or other optical disc storage, magnetic disk storage, magnetic tape storage, or any other computer-readable medium that can be used to carry or store data.
[0318] Finally, it should be noted that the method and device for detecting a circular shape of an industrial product disclosed in the embodiment of the present invention disclose only a preferred embodiment of the present invention, which is only used to illustrate the technical solution of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, a person skilled in the art should understand that the technical solutions described in the aforementioned embodiments can still be modified, or some of the technical features therein can be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for detecting the circular shape of an industrial product, characterized in that: The method comprises: Collecting the industrial product to be tested in the shooting area to obtain the image to be tested; Performing edge detection on the image to be tested by using an edge detection algorithm to obtain edge pixel points of the image to be tested; Determine the region of interest in the image to be tested according to the edge pixel points and a preset rectangular frame size; Traversing the pixel points in the region of interest, and screening out a plurality of target pixel points falling into a predetermined target area from all the pixel points in the region of interest; Based on the edge pixel points, all the target pixel points and the Hough transform algorithm, a circle detection is performed on the region of interest to obtain the circle included in the region of interest and the center point and radius corresponding to the circle; Before screening out a plurality of target pixel points falling into a predetermined target area from all pixel points in the region of interest, the method further comprises: Determine a standard rectangular area according to the preset rectangular frame size; Starting from when the standard circle is at the initial position, the standard circle is slid according to a preset step length until the standard circle is tangent to any two adjacent sides of the standard rectangular area during the sliding process, wherein the preset step length is one pixel; Determine the area through which the standard circle slides as the target area; The initial position is the position where any two adjacent sides of the standard circle and the standard rectangular area are tangent; The method starts from when the standard circle is in the initial position and slides the standard circle according to a preset step length until the standard circle is tangent to any two adjacent sides of the standard rectangular area during the sliding process, including: Determine a moving starting position of the standard circle, wherein the moving starting position initially determined is the initial position; Starting from the standard circle being at the moving starting position, the standard circle is slid along a first preset direction and by a preset step length each time until the standard circle is tangent to a first target edge of the standard rectangular area, and then the sliding is stopped, wherein the first preset direction is parallel to any one edge of all edges of the standard rectangular area, and the first target edge is an edge of all edges of the standard rectangular area to which the first preset direction points; Reset the standard circle to the moving starting position, slide the standard circle once along a second preset direction and with a preset step length, and obtain a first position of the standard circle, wherein the second preset direction is perpendicular to the first preset direction; Determine whether the standard circle is tangent to a second target edge of the standard rectangular area when in the first position, wherein the second target edge is an edge of all edges of the standard rectangular area to which the second preset direction points; If it is determined that the standard circle is not tangent to the second target side of the standard rectangular area when it is at the first position, the moving starting position is updated according to the first position, and the steps of starting with the standard circle being at the moving starting position, sliding the standard circle according to the rule of sliding along a first preset direction and sliding the preset step length each time until the standard circle is tangent to the first target side of the standard rectangular area and then stopping the sliding are performed, and the step of resetting the standard circle to the moving starting position, sliding the standard circle once according to the rule of sliding along a second preset direction and sliding the preset step length to obtain the first position of the standard circle are performed; If it is determined that the standard circle is tangent to the second target side of the standard rectangular area when it is at the first position, starting from the standard circle being at the first position, the standard circle is slid along a first preset direction and each time by a preset step length until the sliding ends when the standard circle is tangent to the first target side of the standard rectangular area; The step of screening out a plurality of target pixel points that fall into a predetermined target area from all pixel points in the region of interest includes: In the standard rectangular area, the pixel values of all the pixels included in the target area are marked as 1, and the pixel values of a plurality of remaining pixels except the pixels included in the target area are marked as 0, so as to obtain a mask image; Determining coordinates of all first pixel points in the region of interest and coordinates of all second pixel points in the mask image, wherein the coordinates of a first first pixel point in the region of interest are the same as the coordinates of a first second pixel point in the mask image; For each first pixel point in the region of interest, according to the coordinates of the first pixel point, obtaining a second pixel point with the same coordinates as the first pixel point in the mask image, and performing an AND operation on a pixel value of the first pixel point and a pixel value of the second pixel point; If the result of the operation of the pixel value of the first pixel and the pixel value of the second pixel is 0, it is determined that the first pixel does not fall into the target area; If the result of the operation of the pixel value of the first pixel and the pixel value of the second pixel is not 0, it is determined that the first pixel falls into the target area, and the first pixel is determined as the target pixel.
2. The method for detecting the circular shape of industrial products according to claim 1, characterized in that: All pixels in the region of interest are stored continuously in rows in the memory; The traversing the pixel points in the region of interest includes: Get the preset pixel pointer p; Point the pixel pointer p to the memory start address of the first pixel point in the region of interest, and obtain the first pixel point in the region of interest; Execute p++ to make the pixel pointer p point to the memory start address of the next pixel point, and obtain the next pixel point, wherein the next pixel point is a subsequent pixel point adjacent to the storage position of the first pixel point in the region of interest in the memory; Repeat p++ to obtain all pixel points in the region of interest.
3. The method for detecting the circular shape of industrial products according to claim 1, characterized in that: After determining the center point and radius corresponding to the detected circle, the method further includes: Performing least square fitting on edge pixels of the image to be tested to obtain a fitting result; The fitting result is compared with the center point and radius corresponding to the circle to obtain the degree of roundness of the industrial product to be tested.
4. The method for detecting the circular shape of industrial products according to claim 3, characterized in that: The least squares fitting is performed on the edge pixels of the image to be tested, and a fitting result is obtained, including: Obtaining a set of edge pixel coordinates of the image to be measured; According to the edge pixel point coordinate set of the image to be measured, an overdetermined equation group of the circle to be fitted is established; The overdetermined equations are solved using the least squares estimation method to obtain a fitting result of the circle to be fitted.
5. A circular detection device for industrial products, characterized in that: The device comprises: A collection module, used to collect the industrial products to be tested in the shooting area to obtain images to be tested; An edge detection module is used to perform edge detection on the image to be tested by using an edge detection algorithm to obtain edge pixel points of the image to be tested; A determination module, used to determine a region of interest in the image to be tested according to edge pixels of the image to be tested and a preset rectangular frame size; A traversal module, used for traversing the pixel points in the region of interest, and screening out a plurality of target pixel points falling into a predetermined target area from all the pixel points in the region of interest; A detection module, used to perform circle detection on the image to be tested based on edge pixels of the image to be tested, all target pixels and a Hough transform algorithm, to obtain the circle contained in the image to be tested and the center point and radius corresponding to the circle; The device further includes: a target area generating module, wherein the target area generating module is configured to: Before screening out a plurality of target pixel points falling into a predetermined target area from all pixel points in the region of interest, determining a standard rectangular area according to the preset rectangular frame size; Starting from when the standard circle is at the initial position, the standard circle is slid according to a preset step length until the standard circle is tangent to any two adjacent sides of the standard rectangular area during the sliding process, wherein the preset step length is one pixel; Determine the area through which the standard circle slides as the target area; The initial position is the position where any two adjacent sides of the standard circle and the standard rectangular area are tangent; The target area generation module is used to start from when the standard circle is in the initial position, slide the standard circle according to a preset step length until the standard circle is tangent to any two adjacent sides of the standard rectangular area during the sliding process. The specific implementation method includes: Determine a moving starting position of the standard circle, wherein the moving starting position initially determined is the initial position; Starting from the standard circle being at the moving starting position, the standard circle is slid along a first preset direction and by a preset step length each time until the standard circle is tangent to a first target edge of the standard rectangular area, and then the sliding is stopped, wherein the first preset direction is parallel to any one edge of all edges of the standard rectangular area, and the first target edge is an edge of all edges of the standard rectangular area to which the first preset direction points; Reset the standard circle to the moving starting position, slide the standard circle once along a second preset direction and with a preset step length, and obtain a first position of the standard circle, wherein the second preset direction is perpendicular to the first preset direction; Determine whether the standard circle is tangent to a second target edge of the standard rectangular area when in the first position, wherein the second target edge is an edge of all edges of the standard rectangular area to which the second preset direction points; If it is determined that the standard circle is not tangent to the second target side of the standard rectangular area when it is at the first position, the moving starting position is updated according to the first position, and the steps of starting with the standard circle being at the moving starting position, sliding the standard circle according to the rule of sliding along a first preset direction and sliding the preset step length each time until the standard circle is tangent to the first target side of the standard rectangular area and then stopping the sliding are performed, and the step of resetting the standard circle to the moving starting position, sliding the standard circle once according to the rule of sliding along a second preset direction and sliding the preset step length to obtain the first position of the standard circle are performed; If it is determined that the standard circle is tangent to the second target side of the standard rectangular area when it is at the first position, starting from the standard circle being at the first position, the standard circle is slid along a first preset direction and each time by a preset step length until the sliding ends when the standard circle is tangent to the first target side of the standard rectangular area; The traversal module is used to filter out a plurality of target pixel points that fall into a predetermined target area from all pixel points in the region of interest, and the specific implementation method includes: In the standard rectangular area, the pixel values of all the pixels included in the target area are marked as 1, and the pixel values of a plurality of remaining pixels except the pixels included in the target area are marked as 0, so as to obtain a mask image; Determining coordinates of all first pixel points in the region of interest and coordinates of all second pixel points in the mask image, wherein the coordinates of a first first pixel point in the region of interest are the same as the coordinates of a first second pixel point in the mask image; For each first pixel point in the region of interest, according to the coordinates of the first pixel point, obtaining a second pixel point with the same coordinates as the first pixel point in the mask image, and performing an AND operation on a pixel value of the first pixel point and a pixel value of the second pixel point; If the result of the operation of the pixel value of the first pixel and the pixel value of the second pixel is 0, it is determined that the first pixel does not fall into the target area; If the result of the operation of the pixel value of the first pixel and the pixel value of the second pixel is not 0, it is determined that the first pixel falls into the target area, and the first pixel is determined as the target pixel.
6. A circular detection device for industrial products, characterized in that: The device comprises: A memory storing executable program code; a processor coupled to the memory; The processor calls the executable program code stored in the memory to execute the industrial product circularity detection method according to any one of claims 1 to 4.
7. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer program instructions, and when the computer program instructions are called, they are used to execute the industrial product roundness detection method according to any one of claims 1 to 4.
Citation Information
Patent Citations
Round small target accurate detection method based on image
CN103150730A
Circle detection method, circle detection device, program and storage medium
JP2018088206A