# CLensRimVision: A Novel Computer Vision Algorithm for Detecting Rim Defects in Contact Lenses
## 4 Methodology
### Defect Detection
#### 4.4.2 Correction of Criteria  
This substep is proposed to slightly adjust the average from the previous substep to be more accurate as the thickness and area of the contact lens rims. The calculated average value from the previous step may include the value of the defects, which may lead to an unusually high value of the average. To detect this, we proposed the correction criteria method to reevaluate the average value by excluding the outliers in consideration. The first step for correction criteria is to check each window's black contour value with the set criteria that uses the average and SD values from the previous step. The equation for correction criteria is as shown in Equation (6). \[\text{Average}-\text{F}\times\text{SD}\leq\text{qualified value}\leq\text{ Average}+\text{F}\times\text{SD} \tag{6}\]  
The average and SD values are specified from each set of windows for each iteration. The factor of SD to average is the required input parameter. If the window's black contour value is within the range of lower bound and upper bound, then that window's value is gathered and used for calculating a new average value. Figure 9 demonstrates how valid windows are selected for the calculation of a new average. The pseudocode used for demonstrating the concept in this step is as shown in Algorithm 2. ```
Input (1) Window_local_range = either Thickness_based_list or Area_based_list (2) Factor_of_SD as parameter \"Factor of SD to average\" (3) AVG as Window_local_AVG (4) SD as Window_local_SD Output (1) Window_new_local_AVG Begin 1.//find new average in correction of criterion, the for loop(line3) is the same in last loop in Algorithm 1 2.For (window = 0, window= Criteria_based_list.length, window=window+1) then//line 12 in Algorithm 1 3.//window sliding return AVG, SD, Window_local_range
4 Window_new_local_range=[] 5.For (window=0, window= Window_local_range.length, window=window+1) then
6 If (AVG - (SDFactor_of_SD) < Criteria_based_list[window] < AVG + (SDFactor_of_SD))  Window_new_local_range = Window_new_local_range.append(Criteria_based_list [window])
7 Window_new_AVG = Average [Window_new_local_range]
8 Return Window_new_AVG

Figure 9: The execution process in a window sliding calculation. #### 4.4.3 Calculation of Acceptable Range for Defect Detection

The last step in defect detection on the circle edge of the contact lens is to perform the defect detection. In this step, the algorithm is prompted with equations that determine whether the window has a burr defect. The equations are separately designed for area-based approach and thickness-based approach, in which the lower bound is multiplied by the shrink value, and the upper bound is multiplied by extended value. These two values have different domains for area-based and thickness-based approaches. For a thickness-based approach, it is calculated with the instant value of pixel as shown in Equation (7). For area-based approach, the extended and shrink values are calculated with the percentage of the average of the black contour area as shown in Equation (8). \[\mathrm{C_{t}-S_{t}\leq acceptable\ thickness\leq C_{t}+E_{t}} \tag{7}\]

\[\mathrm{C_{a}-S_{a}\leq acceptable\ area\leq C_{a}+E_{a}} \tag{8}\]

From Equations (7) and (8), \(C\) represents the new average value after being calculated from the correction criteria algorithm. S represents the shrink value and E represents the extended value. For the area-based approach, the subscription symbol a is used to represent this method, while the symbol t represents the thickness-based approach. With this equation, if any window's value has the value outside the equation range, it is considered a defect. Algorithm 3 shows the pseudocode for the calculation of the acceptable range based on 4 inputs, including the output of Algorithm 2, the shrink criterion, the extended criterion and the window value. In this stage of the process, the algorithm undertakes a couple of significant actions.