Steel bar target counting system, method and storage medium based on distance transformation and watershed operator in complex scenes
By using distance transformation and watershed operators in the steel bar counting system, the accuracy and efficiency of steel bar counting in complex scenarios are solved, and efficient and accurate steel bar counting is achieved.
Patent Information
- Application Number
- CN202411270484.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-11
- Publication Date
- 2025-03-18
- Estimated Expiration
- 2044-09-11
AI Technical Summary
In complex scenarios, it is difficult for traditional steel bar counting methods to accurately count multiple bundles of densely stacked steel bars, and traditional image processing algorithms have low calculation accuracy, long running time, high computing power requirements, and high cost.
The steel bar target counting system based on distance transformation and watershed operator is adopted to realize the counting of steel bars in complex scenarios through steps such as image preprocessing, morphological processing, distance calculation and watershed algorithm.
In complex scenarios, efficient and accurate steel bar counting is achieved, which improves calculation accuracy, reduces running time and cost, and is suitable for multi-package steel bar counting without rules and dense stacking.
Smart Images

Figure CN118823051B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of image processing, and in particular relates to a steel bar target counting system, method and storage medium based on distance transformation and watershed operator in complex scenes. Background Art
[0002] Rebar is an important structural material in the construction industry and is widely used in infrastructure construction such as residential buildings, bridges and tunnels, airports and stations. With the development of urban construction, the use of rebars is increasing. In many construction sites, a large number of rebars are stacked in large outdoor venues, waiting to be delivered for use. How to accurately count complex stacked rebars during the management process is crucial for managers and users. In the construction industry, traditional rebar counting methods rely on manual detection. Whether it is detected by weighing or by artificial vision, it is time-consuming and labor-intensive and lacks precision. The weighing method is easily affected by the size and type of the rebar. The artificial vision method will cause a serious waste of human resources, not only consuming a lot of time, but also easy to cause eye fatigue and inaccurate counting. General image processing algorithms are only applicable to the counting of a bundle of rebars that are neatly arranged and evenly distributed. When multiple bundles of rebars are irregularly densely stacked and the lighting is uneven in complex scenes, the traditional image processing algorithm has low calculation accuracy. In addition, when the traditional counting method uses NCC algorithm and deep neural network to count rebars, the running time is long, the computing power requirements are high, and the cost is high. Therefore, how to efficiently and accurately complete the counting of rebars in complex scenes is an urgent problem to be solved. Summary of the invention
[0003] In view of the deficiencies in the prior art, the present invention provides a steel bar target counting system, method and storage medium based on distance transformation and watershed operator in complex scenes, which realizes efficient and accurate steel bar counting in complex scenes.
[0004] The present invention achieves the above technical objectives through the following technical means.
[0005] A steel bar target counting method based on distance transformation and watershed operator in complex scenes includes the following processes:
[0006] Step 1: Use image acquisition equipment to collect source image A to be counted at the construction site, input it into the preprocessing module for foreground and background distinction processing, and obtain image B;
[0007] Step 2: The binary processing module performs image binarization processing on image B to obtain image D;
[0008] Step 3: The morphological processing module performs morphological processing on image D to obtain image F;
[0009] Step 4: The distance calculation module performs distance transformation, normalization, and binarization on the image F to obtain the image I, and performs coloring on the connected areas of the image I;
[0010] Step 5: The counting module performs preliminary counting of the steel bars based on the image I to obtain the preliminary identified number of steel bars;
[0011] Step 6: The marking module uses the watershed algorithm to segment the source image, mark the target contour, and obtain image P;
[0012] Step 7: Based on the image P, the number of unmarked peripheral steel bars is manually counted and input into the counting module, and the counting module calculates the total number of steel bars based on the number of unmarked peripheral steel bars and the number of initially identified steel bars.
[0013] Furthermore, in step 1, the specific process of distinguishing the foreground and the background is as follows:
[0014] Create a mask and initialize it as the background; set a rectangular area that includes the foreground; use the GrabCut algorithm to segment the image and iterate. During the iteration process, continuously optimize the distribution of the foreground and background. Finally, by multiplying the source image A with the binary mask, we can obtain the foreground and background distinction map of the source image A, that is, image B.
[0015] Furthermore, in step 2, the specific process of image binarization is as follows:
[0016] According to the image B obtained in step 1, its grayscale distribution diagram is drawn and recorded as image C. Then, by analyzing the grayscale distribution characteristics of image B, multiple steel bar pixel grayscale values R of image B are obtained. Then, according to the grayscale frequency of image C and the obtained steel bar pixel grayscale value R, a threshold is set. Finally, image B is converted into a binary image D according to the threshold.
[0017] Furthermore, in step 3, the specific process of morphological processing is as follows:
[0018] First, a kernel matrix for dilation processing is created, and the holes of image D are filled by dilation to obtain image E. Then, a kernel matrix for opening operation is created, and isolated points of image E are removed by opening operation to obtain image F.
[0019] Furthermore, in step 4, the specific process of distance transformation and normalization is as follows:
[0020] Let Ω be the pixel matrix of image F, D(x,y)=min x′y′ ((xx′) 2 +(yy′) 2 );
[0021] Wherein, (x,y)∈Ω, (x′,y′)∈Ω and the grayscale value of the pixel at (x′,y′) is 0; (x,y) and (x′,y′) represent the pixel point for distance transformation located in the xth row and yth column of the pixel matrix Ω of the image F, and the zero pixel point located in the x′th row and y′th column of the pixel matrix Ω of the image F, respectively; x′ and x represent the row coordinates of the zero pixel point in the matrix Ω and the row coordinates of the pixel point for distance transformation located in the matrix Ω, respectively; y′ and y represent the column coordinates of the zero pixel point in the matrix Ω and the column coordinates of the pixel point for distance transformation located in the matrix Ω, respectively; D(x,y) represents the minimum square of the Euclidean distance between all zero pixels in the pixel matrix Ω and the independent variable (x,y).
[0022] The shortest distance D(x,y) from a non-zero pixel to a zero pixel in image F is calculated by the above formula, and the value of D(x,y) is used as the grayscale value at the grid (x,y) to generate image G. The farther the pixel of the foreground object in image F is from the background, the larger the grayscale value of the corresponding pixel in image G and the higher the brightness value;
[0023] Let α = 0, β = 1.0,
[0024] Among them, src(x,y) is the value of the pixel at the coordinate (x,y) of image G; min is the minimum grayscale value of all pixels in image G; max is the maximum grayscale value of all pixels in image G; α and β represent the lower bound and upper bound of the scaling target interval respectively; value(x,y) represents the mapping value of the pixel at the coordinate (x,y) of image G in the target interval (α, β);
[0025] The normalized grayscale value of each pixel in image G is calculated by the above formula to obtain image H.
[0026] Furthermore, the specific process of step 5 is as follows:
[0027] The counting module calculates the number μ of connected regions in image I, obtains the area sequence τ of connected regions in image I, converts it into an ascending sequence ρ, and extracts the last 20% of the elements of ρ to construct an ascending sequence K;
[0028] Recount the ascending sequence K: Where len(K) is the length of sequence K, K[i] is the (i+1)th element of sequence K, int(a) means rounding a, K[0] is the area value of the first element of sequence K, and num is the area of sequence K and the number of targets contained based on K[0].
[0029] The number X of initially identified steel bars is obtained, X=μ-len(K)+num, where μ represents the number of connected regions of image I.
[0030] Furthermore, the specific process of step 6 is as follows:
[0031] Subtract the grayscale value of each pixel of image F from that of image I to obtain the unknown area image L;
[0032] Mark the connected areas of image I and obtain the matrix M;
[0033] Perform mathematical processing on M: M=M+1, M[L==255]=0, so that the unique identification number of all marked areas is increased by 1, and the identification number of unknown areas is changed to 0;
[0034] Using the watershed algorithm, the source image A is processed according to the marker M to obtain the marker N;
[0035] N is mathematically processed: A[N==-1]=[0,255,0], so that the steel bar targets preliminarily counted in step 5 are marked with colored contours to obtain image P.
[0036] A system for realizing the steel bar target counting method based on distance transformation and watershed operator in the above complex scene, comprising a preprocessing module, a binary processing module, a morphological processing module, a distance calculation module, a counting module, and a marking module;
[0037] The preprocessing module distinguishes the foreground and background of the original steel bar image to be counted based on the GrabCut algorithm; the binary processing module is used to draw and analyze the grayscale distribution map, obtain the pixel grayscale value, and binarize the image; the morphological processing module is used to expand and open the image, that is, fill the holes and remove isolated points; the distance calculation module is used to calculate the distance from the non-zero pixel point to the nearest zero pixel point in the image, the normalized grayscale value of each pixel in the image, and can also color the connected area of the image; the counting module is used to count the ascending sequence of the area of the connected area, and calculate the preliminary identification number of steel bars based on the counting results, and is also used to calculate the final total number of steel bars based on the manual counting results; the marking module is used to segment the source image according to the watershed algorithm and mark the target contour for subsequent manual counting.
[0038] The present invention has the following beneficial effects:
[0039] Since the present invention uses iterative distinction between foreground and background, and reasonably sets the binarization threshold by referring to the grayscale distribution map and manually detecting the grayscale value of the steel bar coordinate pixel, the technical solution can better distinguish targets from noise in complex scenarios such as multiple bundles of steel bars are irregularly densely stacked and uneven lighting; since the present invention uses distance transformation and normalization processing, the technical solution can better separate steel bar targets that are partially overlapped or mutually blocked; since the present invention uses the ascending sequence recount correction of the maximum 20% area of the connected area, the technical solution greatly improves the target counting accuracy of the connected area containing multiple steel bar targets; since the watershed algorithm is used to segment the source image, mark the target contour, and manually correct the number of a few unmarked steel bars, the technical solution counts the steel bar targets without omissions. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] Figure 1 This is a flow chart of the steel bar target counting method based on distance transformation and watershed operator in complex scenes.
[0041] Figure 2 It is the source image A of the steel bars to be counted acquired by the image acquisition device.
[0042] Figure 3 is the grayscale distribution of image B.
[0043] Figure 4 It is the image D after the image B is binarized.
[0044] Figure 5 Image E is obtained by dilating image D and filling the holes.
[0045] Figure 6 Image F is obtained by performing an opening operation on image E to remove isolated points.
[0046] Figure 7 It is the image G obtained after performing distance transformation on the image F.
[0047] Figure 8 It is the image I obtained after normalizing and binarizing the image G.
[0048] Fig. 9 Schematic diagram of coloring the connected regions of image I.
[0049] Fig.10 It is the unknown area image L obtained by comparing the image F with the target image I. DETAILED DESCRIPTION
[0050] The present invention is further described below in conjunction with the accompanying drawings and specific embodiments, but the protection scope of the present invention is not limited thereto.
[0051] Embodiment 1:
[0052] The steel bar target counting method based on distance transformation and watershed operator in complex scenes of the present invention is as follows Figure 1 As shown, the specific process includes the following:
[0053] Step 1: Image preprocessing;
[0054] At the construction site, image acquisition equipment is used to collect the steel bar count data. Figure 2 The source image A shown is input to the preprocessing module for subsequent analysis and processing. The preprocessing module performs foreground and background distinction processing on the source image A to obtain image B. The specific method of distinguishing the foreground and background is as follows:
[0055] Create a mask and initialize it as the background; specify a rectangular area that contains the foreground;
[0056] The GrabCut algorithm is used to segment the image and iterate. During the iteration process, the algorithm continuously optimizes the distribution of the foreground and background. Finally, by multiplying the source image A with the binary mask, the foreground and background distinction map of the source image, that is, image B, is obtained.
[0057] Step 2: The binary processing module performs image binarization processing on image B;
[0058] According to the image B obtained in step 1, draw its grayscale distribution diagram, such as Figure 3 As shown ( Figure 3 In the figure, the horizontal axis Graylevel represents the gray value, and the vertical axis Frequency represents the probability), which is recorded as image C. Then, by analyzing the gray distribution characteristics of image B, the gray values R of several steel bar pixels in image B are obtained. Then, according to the gray frequency of image C and the gray values R of several steel bar pixels obtained, a threshold is set. Finally, according to the threshold, image B is converted into a binary (0 / 255) image D, as shown in Figure 4 shown.
[0059] Step 3: The morphological processing module performs morphological processing on the image D;
[0060] Create a kernel matrix for dilation processing, and fill the holes of image D by dilation to obtain image E, as shown in Figure 5 As shown;
[0061] Create a kernel matrix for the opening operation, remove isolated points from image E through the opening operation, and obtain image F, as shown in Figure 6 shown.
[0062] In this embodiment, preferably, the kernel matrix of the expansion process is a 2×2 matrix. All elements are of type uint8, used as kernel parameters for dilation processing Image D is expanded once to fill the holes and obtain image E. Preferably, the kernel matrix of the opening operation is a 5×5 matrix. All elements are of uint8 type, which are used as the kernel parameter ω of the opening operation. The image E is opened to remove isolated small points and the image F is obtained.
[0063] Step 4: The distance calculation module performs distance transformation and normalization processing;
[0064] Let Ω be the pixel matrix of image F, D(x,y)=min x′y′ ((xx′) 2 +(yy′) 2 );
[0065] Wherein, (x,y)∈Ω, (x′,y′)∈Ω and the grayscale value of the pixel at (x′,y′) is 0; (x,y) and (x′,y′) represent the pixel point for distance transformation located in the xth row and yth column of the pixel matrix Ω of the image F, and the zero pixel point located in the x′th row and y′th column of the pixel matrix Ω of the image F, respectively; x′ and x represent the row coordinates of the zero pixel point in the matrix Ω and the row coordinates of the pixel point for distance transformation located in the matrix Ω, respectively; y′ and y represent the column coordinates of the zero pixel point in the matrix Ω and the column coordinates of the pixel point for distance transformation located in the matrix Ω, respectively; D(x,y) represents the minimum square of the Euclidean distance between all zero pixels in the pixel matrix Ω and the independent variable (x,y).
[0066] The distance from the non-zero pixel in image F to the nearest zero pixel (here the distance is the square of the Euclidean distance), that is, the shortest distance to the zero pixel D(x,y); the value of D(x,y) is used as the grayscale value at the grid (x,y) to generate image G, as shown in Figure 7 As shown in the figure, the farther the pixel of the foreground object in image F is from the background, the larger the gray value of the corresponding pixel in image G is, and the higher the brightness value is;
[0067] Let α = 0, β = 1.0,
[0068] Among them, src(x,y) is the value of the pixel at the coordinate (x,y) of image G; min is the minimum grayscale value of all pixels in image G; max is the maximum grayscale value of all pixels in image G; α and β represent the lower bound and upper bound of the scaling target interval respectively; value(x,y) represents the mapping value of the pixel at the coordinate (x,y) of image G in the target interval (α, β);
[0069] The normalized grayscale value of each pixel in the image G is calculated by the above formula to obtain the image H;
[0070] Then set the threshold γ, and γ∈(0,1), and use the binary processing module to convert the image H into a binary (0 / 255) image I, as shown in Figure 8 As shown;
[0071] The connected areas of image I are colored, such as Fig. 9 shown.
[0072] Step 5: Quantity statistics;
[0073] The counting module calculates the number μ of connected regions of image I; obtains the area sequence τ of connected regions of image I, converts it into an ascending sequence ρ, and extracts the last 20% of the elements of ρ to construct an ascending sequence K;
[0074] Recount the ascending sequence K (a subsequence of ρ): Where len(K) is the length of sequence K, K[i] is the (i+1)th element of sequence K, int(a) means rounding a, K[0] is the area value of the first element of sequence K, and num is the area of sequence K and the number of targets contained based on K[0].
[0075] Obtain the initial number of steel bars identified, X = μ-len(K)+num, where μ represents the number of connected regions in image I;
[0076] Step 6: The marking module uses the watershed algorithm to segment the source image, mark the target contour, and obtain image P;
[0077] Subtract the grayscale value of each pixel of image F from that of image I to obtain Fig.10 The unknown region image L shown, L = FI;
[0078] Mark the connected areas of image I and obtain the matrix M;
[0079] Mathematically process M: M=M+1, M[L==255]=0, so that the unique identification number of all marked areas is increased by 1 (the identification number of the background is 1 at this time), and the identification number of the unknown area is changed to 0;
[0080] Using the watershed algorithm, the source image A is processed according to the marker M to obtain the marker N;
[0081] N is mathematically processed: A[N==-1]=[0,255,0], so that the steel bar targets preliminarily counted in step 5 are marked with green (other colors can also be used in actual applications) outlines to obtain image P.
[0082] Step 7: Manual counting correction;
[0083] Since the foreground and background distinction in step 1 causes the peripheral steel bars to be filtered out, the number of unmarked peripheral steel bars Y is manually counted based on image P and input into the counting module. Then, the counting module calculates the total number of steel bars Z based on the number of unmarked peripheral steel bars Y and the number of steel bars initially identified X, where Z=X+Y.
[0084] Embodiment 2:
[0085] The steel bar target counting system based on distance transformation and watershed operator in complex scenes of the present invention comprises a preprocessing module, a binary processing module, a morphological processing module, a distance calculation module, a counting module and a marking module.
[0086] The preprocessing module performs foreground and background distinction preprocessing on the original steel bar image to be counted based on the GrabCut algorithm;
[0087] The binary processing module is used to draw and analyze the grayscale distribution map, obtain the pixel grayscale value, and perform binary processing on the image;
[0088] The morphological processing module is used to perform expansion and opening operations on the image, that is, to fill holes and remove isolated points on the image;
[0089] The distance calculation module is used to calculate the distance from the non-zero pixel in the image to the nearest zero pixel, the normalized gray value of each pixel in the image, and can also color the connected areas of the image;
[0090] The counting module is used to count the connected area areas in ascending order, and calculate the initial number of steel bars according to the counting results. It is also used to calculate the final total number of steel bars according to the manual counting results.
[0091] The marking module is used to segment the source image according to the watershed algorithm and mark the target contours for subsequent manual counting.
[0092] Embodiment 3:
[0093] This embodiment provides a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the steps of the steel bar target counting method based on distance transformation and watershed operator in the above-mentioned complex scene are implemented.
[0094] The embodiments are preferred implementations of the present invention, but the present invention is not limited to the above-mentioned implementations. Any obvious improvements, substitutions or modifications that can be made by those skilled in the art without departing from the essential content of the present invention belong to the protection scope of the present invention.
Claims
1. A steel bar target counting method based on distance transformation and watershed operator in complex scenes, characterized in that: The process includes the following: Step 1: Use image acquisition equipment to collect source image A to be counted at the construction site, input it into the preprocessing module for foreground and background distinction processing, and obtain image B; Step 2: The binary processing module performs image binarization processing on image B to obtain image D; Step 3: The morphological processing module performs morphological processing on image D to obtain image F; Step 4: The distance calculation module performs distance transformation, normalization, and binarization on the image F to obtain the image I, and performs coloring on the connected areas of the image I; Step 5: The counting module performs preliminary counting of the steel bars based on the image I to obtain the preliminary identified number of steel bars; Step 6: The marking module uses the watershed algorithm to segment the source image, mark the target contour, and obtain image P; Step 7: manually counting the number of unmarked peripheral steel bars based on the image P and inputting it into the counting module, and the counting module calculates the total number of steel bars based on the number of unmarked peripheral steel bars and the number of steel bars initially identified; In step 4, the specific process of distance transformation and normalization is as follows: Let Ω be the pixel matrix of image F, D(x,y)=min x′y′ ((xx′) 2 +(yy′) 2 ); Wherein, (x,y)∈Ω, (x′,y′)∈Ω and the grayscale value of the pixel at (x′,y′) is 0; (x,y) and (x′,y′) represent the pixel point for distance transformation located in the xth row and yth column of the pixel matrix Ω of the image F, and the zero pixel point located in the x′th row and y′th column of the pixel matrix Ω of the image F, respectively; x′ and x represent the row coordinates of the zero pixel point in the matrix Ω and the row coordinates of the pixel point for distance transformation located in the matrix Ω, respectively; y′ and y represent the column coordinates of the zero pixel point in the matrix Ω and the column coordinates of the pixel point for distance transformation located in the matrix Ω, respectively; D(x,y) represents the minimum square of the Euclidean distance between all zero pixels in the pixel matrix Ω and the independent variable (x,y). The shortest distance D(x,y) from a non-zero pixel to a zero pixel in image F is calculated by the above formula, and the value of D(x,y) is used as the grayscale value at the grid (x,y) to generate image G. The farther the pixel of the foreground object in image F is from the background, the larger the grayscale value of the corresponding pixel in image G and the higher the brightness value; Then let α=0,β=1.0, Among them, src(x,y) is the value of the pixel at the coordinate (x,y) of image G; min is the minimum grayscale value of all pixels in image G; max is the maximum grayscale value of all pixels in image G; α and β represent the lower bound and upper bound of the scaling target interval respectively; value(x,y) represents the mapping value of the pixel at the coordinate (x,y) of image G in the target interval (α, β); The normalized grayscale value of each pixel in the image G is calculated by the above formula to obtain the image H; The specific process of step 5 is as follows: The counting module calculates the number μ of connected regions of image I, obtains the area sequence τ of connected regions of image I, converts it into an ascending sequence ρ, and intercepts the last 20% of the elements of ρ to construct an ascending sequence K; Recount the ascending sequence K: Where len(K) is the length of sequence K, K[i] is the (i+1)th element of sequence K, int(a) means rounding a, K[0] is the area value of the first element of sequence K, and num is the area of sequence K and the number of targets contained based on K[0]. Obtain the initial number of steel bars identified, X = μ-len(K)+num, where μ represents the number of connected regions in image I; The specific process of step 6 is as follows: Subtract the grayscale value of each pixel of image F from that of image I to obtain the unknown area image L; Mark the connected areas of image I and obtain the matrix M; Perform mathematical processing on M: M=M+1, M[L==255]=0, so that the unique identification number of all marked areas is increased by 1, and the identification number of unknown areas is changed to 0; Using the watershed algorithm, the source image A is processed according to M to obtain the label N; N is mathematically processed: A[N==-1]=[0,255,0], so that the steel bar targets preliminarily counted in step 5 are marked with colored contours to obtain image P.
2. According to claim 1, the steel bar target counting method based on distance transformation and watershed operator in complex scenes is characterized in that: In step 2, the specific process of image binarization is as follows: According to the image B obtained in step 1, its grayscale distribution diagram is drawn and recorded as image C. Then, by analyzing the grayscale distribution characteristics of image B, multiple steel bar pixel grayscale values R of image B are obtained. Then, according to the grayscale frequency of image C and the obtained steel bar pixel grayscale value R, a threshold is set. Finally, image B is converted into a binary image D according to the threshold.
3. A system for implementing the steel bar target counting method based on distance transformation and watershed operator in complex scenes as described in claim 1, characterized in that: It includes preprocessing module, binary processing module, morphological processing module, distance calculation module, counting module and marking module; The preprocessing module distinguishes the foreground and background of the original steel bar image to be counted based on the GrabCut algorithm; the binary processing module is used to draw and analyze the grayscale distribution map, obtain the pixel grayscale value, and binarize the image; the morphological processing module is used to expand and open the image, that is, fill the holes and remove isolated points; the distance calculation module is used to calculate the distance from the non-zero pixel point to the nearest zero pixel point in the image, the normalized grayscale value of each pixel in the image, and can also color the connected area of the image; the counting module is used to count the ascending sequence of the connected area area, and calculate the preliminary identification number of steel bars based on the counting results, and is also used to calculate the final total number of steel bars based on the manual counting results; The marking module is used to segment the source image according to the watershed algorithm and mark the target contours for subsequent manual counting.
4. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, which, when executed by a processor, implements the steps of the method for counting steel bar targets based on distance transformation and watershed operator in complex scenes as described in claim 1.
Citation Information
Patent Citations
Steel bar counting method based on structured light
CN111833292A