# CLensRimVision: A Novel Computer Vision Algorithm for Detecting Rim Defects in Contact Lenses
## 4 Methodology
### Defect Detection
#### 4.4.1 Contour Calculation Using Window Sliding  
Window sliding solves the problem of the edge line of the contact lens does not have an uneven thickness along the whole circle, as shown in Figure 7. In this scenario, if we create an algorithm to obtain the data from the whole circle and calculate the statistical result, it would be biased, and unfortunately, half of the thicker circle may be detected as a defect. Figure 6: The workflow of defect detection. Figure 7: Edge of a contact lens with uneven thickness. To avoid that, we perform the window-sliding technique to set a range of windows and obtain the data from the local area where it has a similar ground truth thickness. First, the algorithm starts with performing window sliding, at which point it receives the window size parameter as input along with the straightened preprocessed image. The size will be set to the window vertical height of all windows in the input image. The demonstration of window sliding in our algorithm is as shown in Figure 8.  
Second, we select the method to count black contours. As you can see in Figure 8, the black contours along the image are uneven with some rough edges. The red box represents a window section for the window sliding method. To determine whether these surfaces are defects, we proposed two approaches to perform the calculation, which are area-based approach and thickness-based approach. For the area-based approach, the algorithm counts the number of black contours in pixels inside each window using OpenCV library. For a thickness-based approach, the algorithm counts the maximum width of black contour along the \(x\)-axis. This can be achieved by using OpenCV library. The reason for proposing two different approaches is to compare their performance and check either of these approaches would be suitable for different objectives of defect detection. For example, let us assume that the average thickness in the example of \(10\times 10\) window slides is four with SD of one and the average number of black pixels (area) is 35 with SD of 10. If we consider only thickness of the black contour, there could be a case in which the thickness seems normal but somehow the defect is a slight swelling. In this case, area-based approach would detect and count the number of black pixels and return an unusually high value of black pixels inside that window. Another possibility is that, if a thin spike occurs in an image, the area-based approach may perform calculations and  
Figure 8: The sliding window process and an explanation of the parameters. find that this window's black pixel value is normal; however, if we used thickness-based approach, it would immediately know that there is a spike in this window. After calculating the value of black contours in each window, either using area-based approach or thickness-based approach, the last step is to calculate the average and SD values from a selected range of window slides. In this part, the parameter \"number of windows above and below\" is used to decide the range of each calculation. The reason we calculated the average is to use it as a determined value for the later step. To provide a clear explanation of each step, we present the procedure of this step in pseudocode in Algorithm 1. ```
Input (1) Preprocessed image (2) Size_window = parameter \"Size of separated image\" (3) Num_above = parameter \"Number of images above\" (4) Num_below = parameter \"Number of images below\" Output (1) Criteria_based_list = the list contain in the local region of interest range either \"thickness_based_list\" or \"area_based_list\". (2) Criteria_avg = the average of Criteria list (\"Criteria_list_value\") (3) Criteria_sd = the standard deviation of Criteria list (\"Criteria_list_value\") Begin 1. Image_total_length = Preprocessed_image_length 2. Number_of_window = Image_total_length/size_window 3. Criteria_based_list = [] 4. If (area_based is True) then 5. For (window = 0, window=Number_of_window, window=window+size_window) then 6.