# Sampling Based on Kalman Filter for Shape from Focus in the Presence of Noise
## 4 Focus Curve Modeling  
In order to filter out jitter noise, the focus curve obtained by one of the focus measure operators is modeled by Gaussian approximation with mean \(z_{f}\) and standard deviation \(\sigma_{f}\)[11]. This focus curve modeling is shown in Figure 4.  
Figure 3: Noise modeling through Gaussian function.  
Related equation about this method is given as:  
\[F(z)=F_{p}\times e^{(-\frac{1}{2}(\frac{z-z_{f}}{\sigma_{f}})^{2})} \tag{7}\]  
where, \(z\) is the position of each image frame, \(F(z)\) is the focus value at \(z\), \(z_{f}\) is the best-focused position of the object point, \(\sigma_{f}\) is standard deviation of the approximated focus curve (by Gaussian function), and \(F_{p}\) is amplitude of the focus curve. Using the natural logarithm to (7), (8) is obtained:  
\[ln(F(Z))=ln\Big{(}F_{p}\Big{)}-\frac{1}{2}(\frac{z-z_{f}}{\sigma_{f}})^{2} \tag{8}\]  
Using (8) and initial best-focused position obtained through one of the focus measure operators \(z_{i}\) and the positions below and above initial best-focused position \(z_{i-1}\) and \(z_{i+1}\) and their corresponding focus values \(F_{i}\), \(F_{i-1}\) and \(F_{i+1}\), (9) and (10) are obtained:  
\[ln(F_{i})-ln(F_{i-1})=-\frac{1}{2}\frac{\big{(}\big{(}z_{i}-z_{f}\big{)}^{2}- \big{(}z_{i-1}-z_{f}\big{)}^{2}\big{)}}{\sigma_{f}^{2}} \tag{9}\]  
\[ln(F_{i})-ln(F_{i+1})=-\frac{1}{2}\frac{\big{(}\big{(}z_{i}-z_{f}\big{)}^{2}- \big{(}z_{i+1}-z_{f}\big{)}^{2}\big{)}}{\sigma_{f}^{2}} \tag{10}\]  
Using (10), (11) is acquired as follows:  
\[\frac{1}{\sigma_{f}^{2}}=\frac{ln(F_{i})-ln(F_{i+1})}{-\frac{1}{2}\big{(} \big{(}z_{i}-z_{f}\big{)}^{2}-\big{(}z_{i+1}-z_{f}\big{)}^{2}\big{)}} \tag{11}\]  
Applying (11) to (9), (12) is obtained:  
\[ln(F_{i})-ln(F_{i-1})=\frac{\big{(}\big{(}z_{i}-z_{f}\big{)}^{2}-\big{(}z_{i- 1}-z_{f}\big{)}^{2}\times(ln(F_{i})-ln(F_{i+1}))\big{)}}{\big{(}z_{i}-z_{f} \big{)}^{2}-\big{(}z_{i+1}-z_{f}\big{)}^{2}} \tag{12}\]  
Figure 4: Gaussian fitting of the focus curve.  
Assuming \(\Delta z=z_{i+1}-z_{i}=z_{i}-z_{i-1}=1\) and utilizing (12), (13) is acquired as:  
\[z_{f}=\frac{(ln(F_{i})-ln(F_{i+1}))\Big{(}{z_{i}}^{2}-{z_{i-1}}^{2}\Big{)}-(ln(F _{i})-ln(F_{i-1}))\Big{(}{z_{i}}^{2}-{z_{i+1}}^{2}\Big{)}}{2((ln(F_{i})-ln(F_{i-1 }))+(ln(F_{i})-ln(F_{i+1})))} \tag{13}\]  
Using (11) and (13), (14) is obtained as:  
\[{\sigma_{f}}^{2}=-\frac{\Big{(}{z_{i}}^{2}-{z_{i-1}}^{2}\Big{)}+\Big{(}{z_{i}} ^{2}-{z_{i+1}}^{2}\Big{)}}{2((ln(F_{i})-ln(F_{i-1}))+(ln(F_{i})-ln(F_{i+1})))} \tag{14}\]  
Utilizing (7), (13), and (14), \(F_{p}\) is acquired by following:  
\[F_{p}=\frac{F_{i}}{e^{(-\frac{1}{2}{\frac{{z_{i}}^{2}-{z_{f}}}{{\sigma_{f}}} ^{2}})}} \tag{15}\]  
Substituting (13), (14), and (15) into (7), final focus curve obtained by Gaussian approximation is acquired. Since jitter noise is considered in this paper, Equation (7) is modified as follows:  
\[F_{n}(z)=F_{p}\times e^{(-\frac{1}{2}(\frac{{(z+\zeta)-z_{f}}}{{\sigma_{f}}} )}^{2}) \tag{16}\]  
where, \(\zeta\) is previously modeled (jitter) noise, approximated by Gaussian or speckle function. Using the proposed filter (in the next section), this noise is filtered to obtain a noise-free focus curve.