# FPGA-Based Linear Detection Algorithm of an Underground Inspection Robot
## 3 System Implementation
### Edge Detection Algorithm Module Design  
The edge detection IP core consists of four parts, namely, the gradient information calculation module, the non-maximum value judgment module, the threshold segmentation, and the edge link module [19]. Since the goal of the algorithm is to detect line segments, which are included in the edge part of the image, where the gray level changes sharply, enhancing the edge of the image is an indispensable step for subsequent line segment detection. In order to detect the correct single-pixel edge, in addition to obtaining the accurate gradient value, the gradient information calculation module also needs to solve the maximum value of the gradient, the gradient quadrant, and the edge direction angle (level line angle (LLA)). The calculation architecture of the overall algorithm module is shown in Figure 6. Additionally, the calculation method is shown in Figure 7.  
Figure 4: Calculation circuit module of Gaussian filtering. Figure 5: Comparison of Gaussian filtering results. (**a**) Original image; (**b**) software processing result; (**c**) FPGA processing result. Since the offset of the conveyor belt will not be very large, we only pay attention to \(\pi/4\)-\(3\pi/4\) when grouping the edge direction angles. As shown in Figure 8, a pyramid configuration is used to group LLAs. First, 3-bit data are used to discretely divide the interval (\(\pi/4\)-\(3\pi/4\)) into 8 groups according to the unit of \(\pi/8\). Secondly, use the 2-bit grouping value to merge the 8 groups of 3-bit LLA. This grouping method sets the tolerance angle of line segment growth to 22.5 degrees. In addition, a 1 bit is used to mark the angle of the effective interval (\(\pi/4\)-\(3\pi/4\)). When the flag bit is 0, it means a valid grouping. When the flag bit is 1, it means an invalid group that exceeds the valid interval. The flag bit participates in the calculation of subsequent edge suppression. At the same time, in order to avoid the problem of missed detection of the line segment at the edge of the group, the original grouping method is offset by \(\pi/16\) for regrouping to ensure that the angle on the frame of one grouping method is at the center of the other grouping method. The output of this grouping method is a 3-bit grouping value, and each potential edge point has two grouping results, which will be calculated at the same time in the subsequent area growth module. Although this method limits the possibility of growth to a certain extent, in the implementation process, multiplication and comparison are used to replace the arc-tangent function solving process, which saves logic resources. Because the two-digit grouping result is conducive to the judgment operation of subsequent modules, the method reduces the difficulty of algorithm placement and routing. A larger gradient value in a gradient image does not mean that the pixel is an edge point but only shows that the pixel has a higher potential. The non-maximum value judgment determines the single edge property of edge detection, which is particularly important for further line segment detection. This module performs 3 \(\times\) 3 windowing on  
Figure 8: LLA grouping method. Figure 6: Circuit diagram of gradient information calculation module. Figure 7: Gradient calculation method. the result of gradient calculation, uses interpolation to calculate the potential maximum value, and then determines whether the center pixel is the local gradient maximum value and realizes the removal of false edges. In addition to eliminating false edges, the edge detection module must also ensure the continuity of the edges. In addition to the result of judging the non-maximum value, whether a pixel can be defined as an edge is still affected by the gradient in the field. In this module, dual thresholds are used to link potential edges and suppress false edges for the results of non-maximum suppression. When the threshold is manually specified, there will always be edge loss caused by too-large thresholds or false edges caused by too-small thresholds.