On The Lash Bar Count Algorithm Based on Image Recognition

This paper solved the problem that lash bars were difficult to be quickly and accurately counted,and proposed a kind of lash bar count algorithm based on image recognition through observation and study of the morphology of lash bars, namely the algorithm that combines cross type algorithm and flexible T type algorithm. Firstly,the color image is processed by graying and threshold method binarization. Secondly,the cross type discriminance is used in the middle section of the binary image. Thirdly,the flexible T type discriminance is used to detect on the boundary. Experimental analysis indicates that the detection algorithm saves time, which can be identified and gives the number of bars in a time less than 3s. The accuracy of bar count is as high as 98%. After joining the artificial modification, it can reach 100%.


Introduction
Bar as a common material products can be seen everywhere in all areas of production and life.With the development of Chinese society, the demands of all kinds of bar are more and more big, meanwhile, china is also a large bar production country.However, one of the big problems that bar production companies face is bar count.Therefore, how to efficiently and accurately solve the problem of bar count appears very important.
The artificial mean was commonly used in traditional bar count, which not only wasted manpower and time, but also was vulnerable to the impact of subjective judgment and large work intensity to cause misjudgment and omission.With the development of science and technology, currently, bar count mainly includes mechanical and electrical method, image method, RF Tag method.Mechanical and electrical method applys photoelectric sensor to collect bar information.And the serious overlapping situation easily leads to misjudgment and omission, lash bar does not use this method.RF Tag method is to post RF tag on the corresponding bar, through reading the label information to get the number of bars.Although it is not easy for misjudgment and omission, the cost is higher than the others.so this method is not generally used.Image method is to use the camera to shoot the images of the end surfaces of bars, which is transmitted to the processing end for processing and analysis to obtain the number of bars.Relatively speaking, the image method that has the least cost and the highest accuracy is the most popular method in the research field of bar count.
This paper proposes a lash bar count algorithm based on image recognition.The algorithm is by using cross type discriminance in the middle of the image and the flexible T type discriminance on the boundary for testing.
The algorithm improves the shortcomings of traditional algorithm that has the large amount of computation and imperfect processing on the border area.

Algorithm design
According to the obtained images and the final effects, the algorithm framework is divided into four parts, namely image acquisition, image preprocessing, image processing and display results.Figure 1 shows Algorithm frame.

Image acquisition
Image acquisition is based on the specific situation of hardware design to choose different focal length and resolution of the camera.In this paper, the collected images are saved in RGB mode.RGB mode is one of the most important mode in digital images, and R , G , B respectively show a pixel of red, green and blue sub-RGB in color images.R , G , B modes are additive models, and the three color synthesis becomes white when the R , G , B value is maximum.RGB mode is a 24-bit color depth.It consists of three channels and each channel has a depth of 8 bits.Three channels synthesizing together can generate 16.77 million kinds of colors, which are also called a "true color".

Image preprocessing
Image preprocessing includes the process of image graying and image binaryzation.

Image graying
There are many kinds of ways of image graying, the specific process is to convert color image RGB information into information that contain only brightness.Common conversion formula is as follows: In formula (1), the R, G, B, respectively show a pixel of red, green, blue component in the color image.After the formula (1) conversion, color images can be converted to the image containing only brightness information.

Image binarization
Because there is a difference between bar and background in image, this article can use the method of image binarization to distinguish bar and background.
Threshold value method binarization is a method that separates the bar and the background.Through the figure 2 ,we can observe that the histogram of graying image presents the phenomenon of two peaks and one valley phenomenon.So global threshold can be gotten by using the method of automatic threshold selection.Then the image is segmented with the global threshold to obtain the desired binary image.

Bar count algorithm
Through observing and analyzing the form of lash bar, the morphological characteristics of lash bar can be summarized as three points: 1.The morphological characteristics of cross-section of each bar is similar, and similar to the circle that its diameter is R.
2. In lash bar, each bar around has about 2~6 roots bars inside lash bar.Each bar around has about 2~3 roots bars on the boundary of lash bar.
3. The center distance of each bar and the center distance of its surrounding bars are about the morphological diameter R. According to the three morphological characteristics of lash bar, the algorithm that combines cross type algorithm and flexible T type algorithm is proposed.According to the current bar position detected, the corresponding cross or flexible T type algorithm is adopted.Specific operations are as follows: When the center bar is judged to be in the interior of the image, the center bar is defined as the position of the initial bar, and the search of the cross type is carried out by the position of the initial bar.Cross type algorithm model is as follows: Structuring the matrix of the cross type algorithm is as follows: In Figure 3, the coordinates of the center position of the bar 1~4 are  x y .Flexible T type algorithm matrix can be constructed Formula (4) represents T type algorithm of bar corresponding to figure 4-1.The coordinate of center position of the bar 1~3 is Formula ( 5) indicates the label of the bar1~3.
( ) Gradf i represents the center coordinates of the bar i .Gradf Formula ( 6) corresponding to T type algorithm of bar in figure 4-2.The coordinate of center position of the bar 1~3 is as follows Formula ( 8) represents T type algorithm of bar corresponding to figure 4-3.The coordinate of center position of the bar 1~3 is Formula ( 9) is indicated the label of the bar 1~3.
( ) Grad i represents the center coordinates of the bar i .

Gradf
Formula ( 10) represents T type algorithm of bar corresponding to figure 4-4.The coordinate of center position of the bar 1~3 is Formula ( 11) is indicated the label of the bar 1~3.( ) Grad i represents the center coordinates of the bar i .

Gradf i
represents the first elements of the i-th row of the matrix 5 Gradf .

5( , 2) Gradf i
represents the second element of the i-th row of the matrix 5 Gradf .

Algorithm realization and analysis
The first step of the algorithm in this paper is to preprocess pictures by using grayscaling and threshold method binarization,then the binarization image is processed with cross and flexible T type algorithm to get the total number of bars.The specific flow chart of the algorithm is as follows：

Realization of image graycaling
After the conversion of formula ( 1), the color image can be converted to the image that only contains brightness information, and realize the process of image grayscaling.Compared with figure 6, the figure 7 can be observed that the image after the grayscaling does not contain color pixels, contain only brightness information.

Realization of image binarization
Through figure 2 can be observed, the histogram of the grayscaling image shows the phenomenon of two peak and one valley.Therefore, the global threshold can be obtained by the method of automatic threshold selection, and then the image can be segmented by the global threshold to get required binary image.

Figure 8．Binary Image
Through the figure 8 can be clearly observed: the image after binarization containes gray value of pixels only two 0 and two 255.In this paper, 0 represents the gray value of the background pixel, and 255 represents the gray value of bar pixel.By judging the gray value of any pixel points,you can judge out that the pixel is a background or a bar.

The realization of bar count
In this paper,we adopt the algorithm of bars that combines cross type and flexible T type.There Algorithm Step ( 6) can be seen.When a bar is searched, bar count plus one, calculating and judging out the center position of the bar at the same time.When the center position of the bar as shown in figure 14 is located in the image, cross type algorithm is adopted to search the center position of the bar.Search results are shown in figure 15:  ITA 2016 count plus one, and the bar is put as a new centre bar, and the appropriate algorithm to continue their search is selected in the vicinity of the new center bar.

Experimental result and performance analysis of bar counting
Through MATLAB simulation, the experimental result of the algorithm in this paper is as follows: In order to verify the effect of the algorithm in this paper,the paper under the 64 bit windows system uses MATLAB for statistics, getting counting results of various types of binding types.The experimental results are shown in table 1 According to the data in table 1, this article take the bar counting algorithm that combines cross type and flexible T type, and the accuracy of the bar count is as high as 98%.For those bars whose diameter are in 15 ~30 mm can get better detection.And the accuracy can achieve 100% under adding the condition of artificial correction.

Conclusion
In this paper, according to number distribution of lash bar around , the cross type algorithm is proposed.At the same time, a flexible T type algorithm is proposed aiming at the boundary,by combining the cross type algorithm and the flexible T type algorithm as the whole bar detection algorithm.The algorithm does not need to consider the boundary problem, while adding artificial correction function on the result of the processing to achieve better 100% counting aiming at 5~30 mm bars.
Through the above test results and performance analysis, summarizing the advantages of the algorithm in this paper are as follows: (1) In this paper, the flexible T type algorithm is used to detect the bars around, and the detection effect is better.
(2) Because the number of surrounding bars in the vicinity of bars are basically 3~6 roots, the whole search volume of the algorithm is much less, saving the search time, while the whole calculation is also less.
(3) According to the processed image, you can visually see the detection results, Therefore, the bar count rate can reach 100% with the addition of artificial correction.

Figure 2 .
Figure 2.Histogram Graying Images(Abscissa Represents Gray Values And Ordinate Represents Proportion)

Figure 3 .
Figure 3.Cross Type Algorithm Model As shown in Figure 3, the center bar is defined as the position of the initial bar, and the search for the cross type is carried out by the position of the initial bar.If the morphological diameter is R, we will start to search in the up and down, left and right four locations of the center bar.Center coordinate of the center bar is set ) , ( 0 0 y x .

( 3 )
Formula (3) represents the label of the bar 14 .( ) Grad i represents the center coordinates of the bar i .1( ,1) Gradf i represents the first elements of the i-th row of the matrix 1 Gradf .1( , 2) Gradf i represents the second element of the i-th row of the matrix 1 Gradf .As shown in Figure 4, when judge out that the bar is located in the boundary of four image, the central bar is defined as the position of the initial bar as well, and according to the flexible T type algorithm to search near the bar.Flexible T type model is as follows:

Figure 4 - 1 . 1 Figure 4 - 2 . 2 Figure 4 - 3 .Figure 4 .
Figure 4-1.Model 1 the i-th row of the matri 2 Gradf .As shown in Figure4-2, it is detected that the current bar is located in the right boundary of the image.The center bar is defined as the position of the initial bar, In the T type algorithm model 2 shown in Figure4-2 is applied to searching bar.If morphological diameter of the bar is R,the center coordinates of the center bar is )

Formula ( 7 )
indicates the label of the bar 1~3the i-th row of the matrix 3 Gradf .As shown in figure 4-3, it is detected that the current bar is located in the low boundary of the image.The center bar is defined as the position of the initial bar, In the T type algorithm model 3 shown in Figure 4-3 is applied to searching bar.If morphological diameter of the bar is R, the center coordinates of the center bar is 0 0 (x ,y ) .Flexible T type algorithm matrix can be the i-th row of the matrix 4 Gradf .As shown in figure 4-4, it is detected that the current bar is located in the upper boundary of the image.The center bar is defined as the position of the initial bar, In the T type algorithm model 4 shown in figure 4-4 is applied to bar search.If morphological diameter of the bar is R, the center coordinates of the center bar is

Figure 9 - 1 Figure 9 - 2 Figure 9 .
Figure 9-1 Figure 9-2 Figure 9. Simulation Results of Cross Type Algorithm(Figure 9-1 Center Bar of Cross Type Algorithm, Figure 9-2 Effect Diagram of Cross Type Algorithm) According to effect picture can be observed: the bars are found by figure 9-1 as the center bar.We can see that the center bar is located in the interior of the image.Therefore, we select cross type algorithm to search bar nearby, and the effect picture shown in figure 9-2 can be gotten.When bars have been searched in the up and down, left and right, the position of the bar is just covered, and bar count plus one, and the bar is put as a new centre bar, and the appropriate algorithm to continue their search is selected in the vicinity of the new center bar.When the center position of the bar as shown in Figure 10-1 is located in the image, flexible T type model 3 algorithm is adopted to search the central position of bars.Search results are shown in figure 10-2:

Figure 10 - 1 Figure 10 - 2 Figure 10 .
Figure 10-1 Figure 10-2 Figure 10.Simulation Results of Flexible T Type Algorithm (Figure 10-1．The Center Bar of Flexible T Type Algorithm, Figure 10-2.Effect Diagram of Flexible T Type Algorithm) According to the effect picture can be observed: the bars are found by figure 10-1 as the center bar.We can see that the center bar is located in the boundary of the image.The location of the bar belongs to the types of figure 4-3.Therefore,we adopt flexible T type model 3 algorithm as shown in figure 4-3 to search bars nearby.The effect picture shown in figure 10-2 can be gotten.When bars have been searched in the up and down, left and right, the position of the bar is just covered, and bar

Figure 11 .
Figure 11.Simulation Result of the Algorithm in This Paper According to the contrast of figure 2 and figure 11, we can directly observe that the number of bars in the original image are 23,and the results obtained from the MATLAB simulation experiment are 23.By processed image you can visually see which bars are not processed.Therefore, adding the artificial correction, the accuracy of bar counting can reach 100%.In order to verify the effect of the algorithm in this paper,the paper under the 64 bit windows system uses

Table 1 .
: Analysis of the Test Results and Performance of Lash Bar.