# 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.1 Gradient Computation Based on the Sobel Operator  
Here, we use the Sobel operator [21; 22] as a gradient mask. There is significant motivation for the employment of the Sobel operator rather than other operators used in [4; 23] because it combines differentiation and Gaussian smoothing. More weight is allocated to pixel intensities around edges. Figure 3 shows the 3 \(\times\) 3 sub-window of an image and Sobel operator kernels in the horizontal (\(x\)) and vertical (\(y\)) directions. The gradients in the \(x\)- and \(y\)-direction denoted by \(f_{x}(x,y)\) and \(f_{y}(x,y)\) are computed using Equations (1) and (2).  
\[f_{x}(x,y) =(p_{2}-p_{0})+2(p_{5}-p_{3})+(p_{8}-p_{6}) \tag{1}\] \[f_{y}(x,y) =(p_{6}-p_{0})+2(p_{7}-p_{1})+(p_{8}-p_{2}) \tag{2}\]  
Figure 1: Block diagram of the Canny edge detection algorithm.  
Figure 2: Architecture of improved Canny edge detection.  
Figure 4: Architecture of Line Buffer.  
Figure 5: Architecture of \(f_{x}(x,y)\).  
Figure 3: Sub-window of an image and Sobel operator kernel.  
\[\theta(x,y)=\arctan\Bigl{(}\frac{f_{y}(x,y)}{f_{x}(x,y)}\Bigr{)} \tag{4}\]  
The hardware cost of implementing Equations (3) and (4) is high. For Equation (3), we use the addition of the absolute values of \(f_{x}(x,y)\) and \(f_{y}(x,y)\), which are presented in [21; 22], to find the gradient magnitude _mag_ (\(x,y\)). Equation (3) can be rewritten as  
\[mag(x,y)=\left|f_{x}(x,y)\right|+\left|f_{y}(x,y)\right| \tag{5}\]  
For Equation (4), the coordinate rotation digital computer (CORDIC) module suggested in [24; 25] is used to calculate the value of \(\theta\)(x,y). The downside of the CORDIC module is that many iterations are required to achieve acceptable accuracy, which leads to the utilization of more hardware. In this part, the shift-based orientation method [9] is adopted. The orientations are spaced uniformly and are divided into eight zones, as shown in Figure 6. The approximated values of \(tan\theta(x,y)\) based on shift operations are listed in Table 1. The tabulated value shows that orientations \(tan0^{\circ}\) to \(tan90^{\circ}\) lie in the first quadrant, while other orientations until \(tan180^{\circ}\) lie in the second quadrant. The tan values of the second and first quadrants are the same but differ in polarity. The quadrant is found using quadrant_flag. The gradient magnitude calculation and quadrant_flag computation are shown in Figure 7.  
To find a zone for \(\theta\)(x,y), we modify \(\theta\)(x,y) as \(tan\theta(x,y)\) and a particular zone is defined between two angle values. For simplification, the Equation (4) can be rewritten as  
\[tan\ \theta(x,y)=\frac{f_{y}(x,y)}{f_{x}(x,y)} \tag{6}\]  
The value of \(tan\theta(x,y)\) is defined by the Equation (7),  
\[tan\theta_{j}(x,y)\leq tan\ \theta(x,y)<tan\theta_{j+1}(x,y) \tag{7}\]  
\begin{table}
\begin{tabular}{c c c c} \hline
**Tangent** & **Approximate Value** & **Tangent** & **Approximate Value** \\ \hline \(\tan 0^{\circ}\) & 0 & \(\tan 112.5^{\circ}\) & \(-\tan 67.5^{\circ}\) \\ \(\tan 22.5^{\circ}\) & \(2^{-2}+2^{-3}+2^{-5}+2^{-7}\) & \(\tan 135^{\circ}\) & \(-\tan 45^{\circ}\) \\ \(\tan 45^{\circ}\) & 1 & \(\tan 157.5^{\circ}\) & \(-\tan 22.5^{\circ}\) \\ \(\tan 67.5^{\circ}\)Rewriting the Equation (7) gives Equation (8),  
\[tan\ \theta_{j}(x,y)\leq\frac{f_{y}(x,y)}{f_{x}(x,y)}<tan\ \theta_{j+1}(x,y) \tag{8}\]  
The condition derived from Equation (8) is given in Equation (9),  
\[f_{x}(x,y)*tan\ \theta_{j}(x,y)\leq f_{y}(x,y)<f_{x}(x,y)*tan\ \theta_{j+1}(x,y) \tag{9}\]  
The zone value is determined by satisfying the condition given in Equation (9). Since the condition \(f_{x}(x,y)*tan\theta(x,y)\) contains a multiplication operation, it is equivalent to shifting \(f_{x}(x,y)\) by a particular value, as mentioned in Table 1. Figure 8 illustrates the architecture for the computation of \(f_{x}(x,y)*tan22.5^{\circ}\) and finding the direction.