## 3 Method
### Local Maximum Gradient  
[PERSON] et al. [25] proposed a deep CNN with an MA mechanism for the classification of COVID-19 from chest X-ray images. They used a segmentation model to predict lung region masks, which were used as a spatial attention map to adjust the features of the classification model. This attention mechanism can suppress the feature value of the background region and improve the classification accuracy. Inspired by this method, we decided to use gradient information as a spatial attention map to suppress the influence of blurred areas. Different from [25], the gradient image was extracted by local maximum gradient method, described below, instead of the segmentation result of a deep CNN.  
As low-level image information, gradient is often used in IQA methods, which can effectively reflect whether the region is blurred or sharp. Inspired by local total variation research [26], we proposed a local maximum gradient method to measure image quality. The specific algorithm is as follows.  
Firstly, we define a \(2\times 2\) image patch as \(\xi\) and calculate the average gradient value \(g(\xi)\) of the upper left and lower right pixels in \(\xi\), as shown in Equations (1)-(5):  
\[g_{1}=|f(x+1,y)-f(x,y)| \tag{1}\]  
\[g_{2}=|f(x,y+1)-f(x,y)| \tag{2}\]  
\[g_{3}=|f(x+1,y+1)-f(x,y+1)| \tag{3}\]  
\[g_{4}=|f(x+1,y+1)-f(x+1,y)| \tag{4}\]  
\[g(\xi)=(g_{1}+g_{2}+g_{3}+g_{4})/4 \tag{5}\]  
where \(f(x,y)\) is the gray value; \(x\) and \(y\) represent pixel coordinates in the horizontal and vertical directions, respectively; \(g_{1}\) and \(g_{2}\) are the horizontal and vertical gradient of the upper left pixel in \(\xi\); \(g_{3}\) and \(g_{4}\) are the horizontal and vertical gradient of the lower right pixel in \(\xi\).  
Then, we define the \(h\times w\) image patch as Block \(\varphi\). As shown in Figure 2c, the Block \(\varphi\) is divided into overlapping \(\xi\) with the stride size 1 in the horizontal and vertical directions. \(g(\xi)\) is computed for each \(\xi\) in Block \(\varphi\). Let \(g(\varphi)\) denote the maximum value of all \(g(\xi)\) in Block \(\varphi\), and it can be given by Equation (6). We consider \(g(\varphi)\) as the local maximum gradient of Block \(\varphi\).  
\[g(\varphi)=\max_{\xi\in\varphi}g(\xi) \tag{6}\]  
Finally, the image is divided into overlapping \(\varphi\) with stride of \(s_{h}\) and \(s_{v}\) in the horizontal and vertical directions. By calculating \(g(\varphi)\) of each Block \(\varphi\), the feature map of the local maximum gradient is obtained. Shown in Figure 2a is a fecal microscopic image with fungal spores, and (b) is a local enlargement image of a fungal spore in (a). The local maximum gradient image of (a) and (b) is shown in (d) and (e), respectively. (f) is the gradient map of (b) calculated by the Tenengrad method [7]. The internal region of a sharp fungal spore is a flat area with high brightness, and it has a high gradient value in the local maximum gradient image. However, its gradient value is low in the Tenengrad gradient image and is close to the background response. Comparing (e) and (f), the Tenengrad method focuses on sharp edges but the local maximum gradient method focuses on sharp objects. Using Figure 2e as the gradient attention mechanism can force the CNN to focus on clear objects.  
In the Supplementary Materials, we demonstrate the prediction accuracy of the local maximum gradient method used as a gradient-based IQA method on finding the clearest human fecal microscopic image in the autofocus process.