# FPGA Implementation of a Real-Time Edge Detection System Based on an Improved Canny Algorithm
## 2 Proposed Design Strategy
### Improved Canny Edge Detection Algorithm
#### 2.2.4 Adaptive Threshold Computation  
Adaptive threshold calculation is the most important part of the proposed canny edge detector system. After NMS, we used [PERSON]'s algorithm to adaptively calculate the threshold we needed. Since the NMS data needs to be compared with the calculated threshold, the threshold value can only be obtained after the statistics of the entire image. Therefore, the NMS data needs to be cached in advance, which is completed by the SDRAM controller. For the threshold calculation, the relevant parameters of [PERSON]'s algorithm were obtained through histogram statistics, and then the threshold was calculated. After the threshold calculation was completed, SDRAM signal reading was enabled at the same time. Then the NMS data was compared with the threshold value for binarization, and finally, the hysteresis was carried out. Figure 10 shows the flow diagram of the system after NMS, including data caching, histogram statistics, threshold calculation, binarization, and hysteresis. 1. [PERSON]'s Algorithm  
[PERSON]'s algorithm [18] is a popular thresholding algorithm used to find an optimal threshold that separates an image into two classes: the background and the object. These classes are represented by C0 and C1, respectively. This method performs a variance analysis processing to find the optimal threshold \(k\) by a maximization, as shown in Equation (10). \[k=\frac{arg\ \max\left(\sigma_{k}^{2}\right)}{0\leq k\leq L-1} \tag{10}\]  
where \(\sigma_{k}^{2}\) is the k-th between-class variance of the image, defined as:  
\[\sigma_{k}^{2}=\frac{\left(u_{L-1}\times u_{k}-u_{k}\right)^{2}}{u_{k}(1-u_{k })} \tag{11}\]  
where \(w_{k}\) and \(u_{k}\) are the probability of class occurrence given a k threshold and the mean intensity value of the pixels up to the k threshold of the image, respectively, and \(u_{L-1}\) is the average intensity of the entire image, called the global mean, with a value equal to \(u_{k}\) when k = L \(-\) 1. Normally, L is equal to 256. Figure 9: Architecture of NMS. The variables \(w_{k}\) and \(u_{k}\) can be expressed as  
\[w_{k}=\sum\limits_{i=0}^{k}p_{i} \tag{12}\]  
and  
\[u_{k}=\sum\limits_{i=0}^{k}i\times p_{i} \tag{13}\]  
(2) Logarithm Approximation  
The division operation is costly to the hardware in terms of processing speed and is the architecture's bottleneck due to the highest critical time. To avoid the division operation in Equation (11), the logarithm function is presented in [26; 27], and Equation (11) can be accordingly rewritten as:  
\[2 log_{2}(\sigma_{k})=2 log_{2}(u_{L-1}\times w_{k}-u_{k})-log_{2}(w_{k})-log_{2 }(1-w_{k}) \tag{14}\]  
(3) Architecture of adaptive threshold computation  
(a) Architecture of histogram  
Figure 10: Flow diagram of the system after NMS. The histogram is the key part of [PERSON]'s algorithm. Histogram statistics are performed on a series of values generated by the NMS. Figure 11 shows the architecture of the histogram. Here, we used a RAM, and the address bit width is 8 bits. The data bit width can be flexibly selected according to the number of pixels in the image. Thirty-two bits are used here. The architecture is divided into two parts. The first part is before the completion of statistics, and the other part is after the completion of statistics. The steps for computing the histogram statistics are as follows:  
1. Read out the current statistics, add 1 and write them back to RAM. 2. Repeat the above steps until the current image has been counted. If imhist_end is 1, as shown in Figure 11, the statistics are complete. 3. Next, start reading out the data in order. Step (1) is realized by the statistical circuit module, and step (3) is realized by the circuit after completion of the statistics module, as shown in Figure 11.  
1. Architecture of [PERSON]'s algorithm calculation  
After the statistics were completed, parameters \(u_{L-1}\), \(u_{k}\) and \(w_{k}\) in [PERSON]'s algorithm were calculated. Before the calculation, a normalized cumulative histogram is needed.