## 2 Materials and Methods
### Proposed Method
#### 2.3.1. SSR Image Enhancement Based on Adaptive Bilateral Filtering
Secondly, during imaging, the spherical fruiting body of the apple causes the light reflectance to change too quickly at shorter distances. The Retinex theory does not cope well with this situation and some compensation is needed. Based on this, the bilateral filter function was improved. The smoothness of the region was reflected by the mean value of the gradient in the filter neighborhood, and the grayscale domain weighting coefficients were adaptively adjusted according to the smoothness, then adaptively adjusted the spatial domain weighting coefficient according to the distance of the filter center from the image center to cope with the above problems. The specific calculation process is as follows:  
Figure 6: Halation phenomenon. (**a**) Apple image; (**b**) MSR reflection component image. Let any point on the image be \(P(x,y)\) and the center of the filter be \(Q(x_{c},y_{c})\); then the bilateral filter function is calculated as follows:  
\[f_{filtered}(x,y)=\frac{1}{w_{p}}\sum_{Q\subset\omega}I_{Q}f(\|P-Q\|,\delta_{s})g \big{(}\big{|}I_{P}-I_{Q}\big{|},\delta_{r}\big{)} \tag{4}\]  
\[f(\|P-Q\|,\delta_{s})=e^{-\frac{(x-x_{c})^{2}+(y-y_{c})^{2}}{2\delta_{s}^{2}}} \tag{5}\]  
\[I(x,y)=R(x,y)\times L(x,y) \tag{1}\]0  
where \(f_{filtered}(x,y)\) represents the output value of pixel \(P\); \(I_{Q}\) denotes the original value of pixel point \(Q\); \(I_{P}\) denotes the original value of pixel point \(P\); \(\omega\) is the neighborhood region of the pixel; \(f(\|P-Q\|,\delta_{s})\) denotes the spatial distance weight function, which is usually weighted using a Gaussian function; \(g\big{(}\big{|}I_{P}-I_{Q}\big{|},\delta_{r}\big{)}\) denotes the pixel variance weight function, which is also weighted using a Gaussian function; \(\delta_{s}\) and \(\delta_{r}\) denote the weighting coefficients in the spatial and gray domains, respectively; and \(w_{p}\) is the normalization coefficient, which is used to make sure that the output pixel value is in the range of 0 to 255. Let the image size be \(mn\) and the filter neighborhood size be \((2a+1)(2a+1)\). The gradient \(P_{grad}(x,y)\) of the experimental image is calculated using the Sobel operator with the following expression:  
\[I(x,y)=R(x,y)\times L(x,y) \tag{1}\]1  
\[Sobel_{H}= \begin{bmatrix}-1&0&1\\ -2&0&2\\ -1&0&1\end{bmatrix}  \tag{8}\]  
\[Sobel_{V}= \begin{bmatrix}-1&-2&-1\\ 0&0&0\\ 1&2&1\end{bmatrix}  \tag{9}\]  
Then the full graph gradient mean \(mean_{all}\) and the filter neighborhood gradient mean \(mean_{bf}\) are calculated as follows:  
\[mean_{all}=\frac{1}{mn}\sum_{x=0}^{m-1}\sum_{y=0}^{n-1}P_{grad}(x,y) \tag{10}\]  
\[mean_{bf}=\frac{1}{(2a+1)(2a+1)}\sum_{x=x_{c}-a}^{x_{c}+a}\sum_{y=y_{c}-a}^{ y_{c}+a}P_{grad}(x,y) \tag{11}\]  
Let the gray-scale standard deviation of the filter neighborhood be \(std_{bf}\); then the gray-scale domain weighting coefficients are adjusted as  
\[\delta_{r}=std_{bf}\times\frac{\alpha\times mean_{all}}{mean_{bf}} \tag{12}\]  
Here, \(\alpha\) is the weighting constant for obtaining the weighting coefficients in the gray domain. The smaller the value, the smaller the filter intensity adjustment range. On the contrary, the larger \(\alpha\) is, the larger the filter intensity adjustment range and the larger the difference between the filter intensity in the smoothing and fringe regions. In the smoothing area, \(mean_{bf}<mean_{all}\), \(\delta_{r}\) becomes larger, and the degree of bilateral filtering becomes larger, which can better restore the spatial illumination situation; in the edge area, \(mean_{bf}>mean_{all}\), \(\delta_{r}\) becomes smaller, the weight of the pixel points with the larger gray difference in the filtering neighborhood becomes smaller, which has less influence on the filtering result, and the edge can be better preserved. For the filter compensation at the edge of the image, the intensity of the bilateral filter should be reduced as the distance of the filter from the center of the image increases so as to prevent over-enhancement of the Retinex reflective layer at the edge of the image.