# Visual Ranging Based on Object Detection Bounding Box Optimization
## 3 Methods
### Object Detection Bounding Box Optimization  
#### 3.2.1 Methods and Principles  
In the field of visual ranging based on object detection, when we carry out object detection, the non-fit phenomenon between the object detection bounding box and the object may be caused by factors such as uneven illumination and fuzzy object boundaries. The non-fit phenomenon, which leads to pixel gaps (as shown in Figure 1), can eventually result in ranging errors.  
In order to solve this issue, an object detection bounding box optimization method is proposed. Overall, the method includes five steps: object detection, image preprocessing, non-interactive GrabCut foreground segmentation, segmentation postprocessing, and scanning the edge contour to obtain optimized bounding box information. The specific details of each step are as follows:  
1. Object detection. The object in the image is detected using the detection model, and the bounding box data for the detected object is obtained.
2. Image preprocessing. First, the original RGB image is converted to an HSV image, and then divided into three single-channel images: Hue (H), Saturation (S), and Value (V), respectively. Then, the S image and the V image are processed using contrast limited adaptive histogram equalization (CLAHE). The processed single-channel images are merged into an HSV image and converted to an RGB image.
3. Non-interactive GrabCut foreground segmentation. The bounding box information from object detection in step (1) and the preprocessed image from step (2) are used as the input parameters for the GrabCut algorithm to achieve automatic non-interactive foreground segmentation, eliminating the need to manually set the foreground area in traditional interactive GrabCut segmentation.
4. Segmentation postprocessing. The segmentation postprocessing includes gray transformation, adaptive binarization and morphological optimization. The adaptive binarization threshold is generated automatically based on the background pixel value set after foreground segmentation.
5. Scanning the edge contour to obtain the optimized bounding box. The object contour is scanned by row and column pixels using the object detection bounding box from step (1) as the boundary, and the optimized object detection bounding box is obtained based on the column and row index.  
In the above five steps, step (1) is based on the YOLOv5 object detection in Section 3.1, and the pixel information of the detection boundary box is obtained. Step (2) converts the RGB image to an HSV image and then performs CLAHE processing. This step adjusts the image contrast and enhances the image boundary, which is more conducive to the non-interactive GrabCut foreground segmentation in step (3). In step (3), the non-interactive  
Figure 2: Data augmentation.  
GrabCut foreground segmentation is realized by using the pixel information of the object detection bounding box in step (1) and the preprocessed image in step (2), which is an automatic object segmentation method. In step (4), the postprocessing of the non-interactive GrabCut foreground segmentation results can remove the excess pixels generated by segmentation, so as to better obtain the optimized bounding box in step (5). In step (5), after pixel scanning of the segmentation postprocessing results, we obtain the optimized detection bounding box, which is applied in the subsequent visual ranging method to achieve more accurate distance measurement.