# Calibration and Image Reconstruction in a Spot Scanning Detection System for Surface Defects
## 3 System Calibration and Image Reconstruction Methods
### System Calibration Method
#### 3.2.2 Establishment of Constraint Function  
For an arbitrary system parameter vector \(\psi\), the mapping relationship \(\mathbf{g}_{\psi}\) can be obtained according to the practical scanning trace model established in Section 3.1. By using \(\mathbf{g}_{\psi}\), one can compute the image coordinates of each feature sequence number set and then observe the appearance of the corresponding defect in the reconstructed image. To avoid image distortions, our method will place two types of constraints on these coordinates. First, straight scratches should not be bent in the reconstructed image, meaning that all sequence numbers in each \(L_{m}\) are expected to map to image coordinates which are collinear. Hence a straightness constraint is required. As is known, the distance of a point \(\mathbf{u}=\left(u,v\right)^{T}\) to a line determined by two endpoints \(\mathbf{u}_{end1}\) and \(\mathbf{u}_{end2}\) is \(\left|\left(\mathbf{u}-\mathbf{u}_{end1}\right)^{T}\mathbf{n}(\mathbf{u}_{ end1},\mathbf{u}_{end2})\right|\), where  
\[\mathbf{n}(\mathbf{u}_{end1},\mathbf{u}_{end2})=\left[\begin{array}{cc}0&-1 \\ 1&0\\ \end{array}\right]\frac{\left(\mathbf{u}_{end1}-\mathbf{u}_{end2}\right)}{ \left\|\mathbf{u}_{end1}-\mathbf{u}_{end2}\right\| \tag{10}\]  
Figure 5: Flow chart of the system calibration method.  
is the unit normal vector of the line. \(|x|\) and \(||\mathbf{x}||\) denote the operations that return the absolute value of a scalar and the L2-norm of a vector, respectively. For the \(m\)th selected scratch, assuming that its two endpoints correspond to the first and last sequence numbers in \(L_{m}\), the straightness constraint for \(L_{m}\) can be defined by the maximum straightness error \(c_{m}^{l}\) as  
\[\begin{split} c_{m}^{l}(\psi)=\max\bigl{\{}d_{m}(q)\big{|}q=1,2, \cdots,Q_{m}\bigr{\}}\text{ with }d_{m}(q)=\biggl{|}\biggl{(}\mathbf{u}_{m,q}^{l}-\mathbf{u}_{m,1}^{l} \biggr{)}^{T}\mathbf{n}\biggl{(}\mathbf{u}_{m,1}^{l},\mathbf{u}_{m,Q_{m}}^{l} \biggr{)}\biggr{|},\\ \mathbf{u}_{m,q}^{l}=\mathbf{g}_{\psi}\biggl{(}\mathbf{u}_{m,q}^{ l}\biggr{)}\end{split} \tag{11}\]  
Second, scaling is not permitted in the reconstructed image, otherwise the relationship between the pixel size and the actual size will be changed, making the extracted geometric features of defects inaccurate. As described in Section 2.2, \(k_{0}\), the actual size of each pixel in the reconstructed image, is a fixed preset value. To maintain this characteristic, a scale constraint, for constraining the two sequence numbers in each \(K_{m}\) to map to two image coordinates between which the distance is equivalent to the actual value \(D_{m}\), is constructed by the distance error \(c_{m}^{k}\), whose expression is given by  
\[c_{m}^{k}(\psi)=\Bigl{|}\Bigl{(}D_{m}/k_{0}-||\mathbf{g}_{\psi}\bigl{(}\mathbf{ u}_{m,1}^{k}\bigr{)}-\mathbf{g}_{\psi}\bigl{(}\mathbf{u}_{m,2}^{k}\bigr{)}|| \Bigr{)}\Bigr{|} \tag{12}\]  
Finally, the total constraint function is defined as a weighted sum of the squares of straightness constraints and scale constraints:  
\[C(\psi)=\sum_{m=1}^{M^{l}}\Bigl{[}c_{m}^{l}(\psi)\Bigr{]}^{2}+\sum_{m=1}^{M^{k }}\Bigl{[}\lambda c_{m}^{k}(\psi)\Bigr{]}^{2} \tag{13}\]  
where \(\lambda\) is the weight factor used to adjust the relative importance between the two types of constraints.