## 4 Image Processing
### Image Preprocessing  
First, cut the YOLOv7 object detection picture to obtain the image of the region of interest (ROI) of the _Camellia oleifera_ fruit. To avoid the interference of subsequent fruit stalks on the recognition area, we use the copyMakeBorder function command to copy the source image to the middle of the target image and form a border around the image; then, we expand the edge of the obtained _Camellia oleifera_ fruit image by 10 pixels.  
In image processing, it is normally necessary to reduce the impact of noise in the image through filtering. Common methods include mean filtering, Gaussian filtering, median filtering, and bilateral filtering. Bilateral filtering is a nonlinear filtering, which can achieve the noise removal and edge preservation effects [30]. Therefore, bilateral filtering of the pixel-expanded _Camellia oleifera_ fruit image can reduce noise while maintaining the edge.  
The _Camellia oleifera_ fruits collected in this study are partially bright or dark due to uneven illumination, reflection, and other reasons, which is not conducive to the image segmentation of _Camellia oleifera_ fruits. The histogram equalization algorithm is used to equalize the light and dark degree of the _Camellia oleifera_ fruit image. We transform the  
Figure 4: Flow chart.  
RGB image after bilateral filtering into the color space to obtain the HSV image. The V component in the three channel components of the converted HSV image is separated, and the V component is separately equalized via a histogram. The V component in the separated HSV three channel components is replaced by the image equalized by the V component histogram, and then the enhanced HSV image is generated by merging the components. Then, we convert the HSV image to an RGB image. The specific preprocessed image is shown in Figure 5.  
The RGB image is a three-channel image, which is composed of red, green, and blue data of the same line and column; the HSV image also contains three channels of data of the same size: \(H\) channel, \(S\) channel and \(V\) channel [31]. The principle of RGB to HSV is shown in the following Equations (1)-(10) [32]. First, convert \(R\), \(G\), and \(B\) values to 0-1.  
\[R =R/255 \tag{1}\] \[G =G/255\] (2) \[B =B/255\] (3) \[V =\max(R,G,B)\] (4) \[S =\left\{\begin{array}{cc}\frac{V-\min(R,G,B)}{V}&,\ if\ V\
eq 0\\ 0&,\ else\end{array}\right. \tag{5}\]  
\[H =\left\{\begin{array}{cc}60*(G-B)/(V-\min(R,G,B))&,if\ V=R\\ 120+60*(B-R)/(V-\min(R,G,B))&,if\ V=G\\ 240+60*(R-G)/(V-\min(R,G,B))&,if\ V=B\end{array}\right. \tag{6}\]  
If the calculated \(H\) is less than 0, add 360 to this value to obtain the final value:  
\[H =H+360 \tag{7}\]  
Because OpenCV needs to visualize HSV images, the values need to be converted to 0-255, via:  
\[H =H/2 \tag{8}\] \[S =S*255 \tag{9}\]  
Figure 5: Image preprocessing. (**a**) ROI, (**b**) Pixel expansion, (**c**) Bilateral filtering, (**d**) RGB to HSV, (**e**) V component diagram, (**f**) Histogram equalization of V component graph, (**g**) Merge, (**h**) Consolidated enhanced HSV to RGB.  
\[V=V*255 \tag{10}\]  
The principle of HSV to RGB is shown in the following Equations (11)-(19). First, convert the \(H\), \(S\), and \(V\) values of the visualization image to the range of 0-360, 0-1, and 0-1, respectively.  
\[H=H*2 \tag{11}\]  
\[S=S/255 \tag{12}\]  
\[V=V/255 \tag{13}\]  
Then, \(R\), \(G\), and \(B\) are calculated as follows, where the floor function represents a downward rounding operation:  
\[h_{i}=floor(\frac{H}{60}) \tag{14}\]  
\[f=\frac{H}{60}-h_{i} \tag{15}\]  
\[p=V*(1-S) \tag{16}\]  
\[q=V*(1-f*S) \tag{17}\]  
\[t=V*(1-(1-f)*S) \tag{18}\]  
\[(R,G,B)=\left\{\begin{array}{l}(V,t,p)\,\text{,if }h_{i}=0\\ (q,V,p)\ \,\text{,if }h_{i}=1\\ (p,V,t)\ \,\text{,if }h_{i}=2\\ (p,q,V)\ \,\text{,if }h_{i}=3\\ (t,p,V)\ \,\text{,if }h_{i}=4\\ (V,p,q)\ \,\text{,if }h_{i}=5\end{array}\right. \tag{19}\]