# An Efficient Automatic Midsagittal Plane Extraction in Brain MRI
## 2 Materials and Methods
### Estimation of Yaw Angle (\(\theta_{r}\))
#### 2.2.2 Principal Component Analysis  
After noise removal and ROI extraction steps, the method uses 2-D coordinates (column and row) of the non-zero pixels of the object's region as data points and an array \(X\) is formed as:  
\[X=\left[ \begin{array}{cc}c_{1}&r_{1}\\ c_{2}&r_{2}\\ \vdots&\vdots\\ c_{n}&r_{n}\end{array} \right] \tag{9}\]  
where \(r\), \(c\), and \(n\) represent row, column, and the total number of data points (1-valued pixels) in the ROI, respectively. The size of \(X\) is \(n\times 2\), and rows of \(X\) are the location coordinate values of each non-zero pixel. Mean of \(X\) is a row vector \(m_{x}\) (mean of the elements in each column of \(X\)), and can be computed as:  
\[m_{x}=\frac{1}{n}\sum_{i=1}^{n}X_{i} \tag{10}\]  
Similarly, a covariance matrix \(P_{x}\) can be calculated as:  
\[P_{x}=\frac{1}{n-1}\sum_{i=1}^{n}\left(X_{i}-m_{x}\right)\left(X_{i}-m_{x} \right)^{T} \tag{11}\]  
The mean subtraction is vital for performing PCA to ensure that the first principal component represents the direction of maximum variance. The covariance matrix \(P_{x}\) is a real and symmetric matrix with a size of 2 \(\times\) 2. Thus, finding a pair of orthonormal eigenvectors is always possible [46]. Suppose the elements of covariance matrix as:  
\[P_{x}=\left[ \begin{array}{cc}f&g\\ g&h\end{array} \right] \tag{12}\]  
where \(f=\text{var}(x_{ii})\), \(h=\text{var}(x_{jj})\), and \(g=\text{cov}(x_{ij})\). The principal components can be determined by solving an eigenvalue problem as:  
\[(P_{x}-\lambda I)e=0 \tag{13}\]  
where \(\lambda\), \(I\), and \(e\) are the eigenvalue, identity matrix, and eigenvector, respectively. If Equation (13) is to have a solution other than vector zero, then \((P_{x}-\lambda I)\) must be a nonsingular matrix. Therefore, it leads to a characteristics equation as:  
\[\det(P_{x}-\lambda I)e=0 \tag{14}\]  
After expansion of Equation (14), it becomes a second-degree equation as:  
\[\lambda^{2}-\lambda(f+h)+\left(fh-g^{2}\right)=0 \tag{15}\]  
The Equation (15) can be solved using a quadratic formula as:  
\[\lambda_{1},\lambda_{2}=\frac{tr(P_{x})\pm\sqrt{tr(P_{x})^{2}-4|P_{x}|}}{2} \tag{16}\]  
where \(tr(P_{x})=f+h\) and \(|P_{x}|=fh-g^{2}\). The corresponding eigenvectors of the eigenvalues \((\lambda_{1},\lambda_{2})\) can be calculated as:  
\[e_{j}=\frac{1}{\sqrt{g^{2}+(\lambda_{j}-f)}}\left[ \begin{array}{c}g\\ \lambda_{j}-f\end{array} \right] \tag{17}\]  
where \(e_{j}(j=1,2)\), and \(\lambda_{j}\) denote the eigenvectors and eigenvalues of \(P_{x}\), respectively. The eigenvalues represent the length of the semi-axes of the ellipse and the eigenvectors specify the directions of these axes, as illustrated in Figure 4.  
The rotation angle can be computed using the eigenvector corresponding to the largest eigenvalue as:  
\[\theta_{1}=\tan^{-1}\left(\frac{v_{y}}{v_{x}}\right),\quad\theta_{1}\in\left( -90^{\lx@math@degree},90^{\lx@math@degree}\right) \tag{18}\]  
where \(v_{x},v_{y}\) are the horizontal and vertical components of the eigenvector associated with the largest eigenvalue, respectively. This procedure provides a rough estimate of brain bilateral symmetric axis orientation \(\theta_{1}\). Next, the slice is realigned with the vertical axis of the image using the angle \(\theta_{1}\). Generally, it was observed that PCA aligned the brain bilateral symmetric axis with the vertical axis of the image with an error of less than \(1^{\circ}\), as displayed in Figure 5.  
The first row of Figure 5 indicates that bilateral symmetry axis of the brain is successfully aligned with the vertical axis (white vertical line) of the image by PCA, but sometimes as the second row in Figure 5 depicts, the bilateral symmetric axis of the brain is not completely aligned with the vertical axis of the image.