# Cycle Consistent Generative Motion Artifact Correction in Coronary Computed Tomography Angiography
## 3 Methodology
### Dynamic Time Warping for Synthetic Patch Alignment and Cross-Phase Style Transfer  
The patches extracted from the coronary artery from different phases of a 4D CT scan are not only distinct in terms of severity of motion artifact but also in terms of the anatomical structures. The motion of the heart causes variability in the surrounding local features as well. Our goal is to obtain corresponding patches with similar neighboring distinctive features without motion disturbance, to train our CycleGAN model. Because this is clinically unattainable, we synthesize a same-phase-no-artifact patch, \(\hat{\mathbf{P}}_{j}^{\phi}\), using style transfer to the source patch \(\mathbf{P}_{j}^{\phi}\) with a different-phase-no-artifact patch as the target patch \(\mathbf{P}_{j}^{\phi*}\). We refer to this process as _cross-phase style transfer_, where \(\phi\) denotes the cardiac phase and \(\phi*\) denotes the phase within the heartbeat when the motion is the slowest, resulting in the least severe motion artifacts. To optimally align the patches, we use the Dynamic Time Warping (DTW) algorithm. Let \(\hat{\mathbf{P}}^{\phi}=\{\hat{\mathbf{P}}_{1}^{\phi},\hat{\mathbf{P}}_{2}^{ \phi},\dots,\hat{\mathbf{P}}_{N}^{\phi}\}\) be the target patch sequence, and \(\mathbf{P}^{\phi}=\{\mathbf{P}_{1}^{\phi},\mathbf{P}_{2}^{\phi},\dots,\mathbf{ P}_{M}^{\phi}\}\) be the source patch sequence, where \(N\) and \(M\) are the lengths of the target and source sequences, respectively. We calculate the distance or similarity measure between each pair of patches \(\hat{\mathbf{P}}_{i}^{\phi}\) and \(\mathbf{P}_{j}^{\phi}\):  
\[\text{cost}(i,j)=\text{distance}(\hat{\mathbf{P}}_{i}^{\phi},\mathbf{P}_{j}^{ \phi})\]  
In the context of source and target patch matching with similar anatomical structures (background), we employ the Structural Similarity Index (SSIM) as the similarity measure. We chose this metric as it takes into account luminance, contrast, and structural information, which is crucial in our case. Mathematically, it is given by  
\[\text{SSIM}(\hat{\mathbf{P}}^{\phi},\mathbf{P}^{\phi})=\frac{(2\mu_{\mathbf{P} }\mu_{\mathbf{P}}+c_{1})(2\sigma_{\mathbf{P}\mathbf{P}}+c_{2})}{(\mu_{ \mathbf{P}}^{2}+\mu_{\mathbf{P}}^{2}+c_{1})(\sigma_{\mathbf{P}}^{2}+\sigma_{ \mathbf{P}}^{2}+c_{2})}\]  
The mean values, \(\mu_{\hat{\mathbf{P}}}\) and \(\mu_{\mathbf{P}}\), capture the average intensity of pixel values in \(\hat{\mathbf{P}}^{\phi}\) and \(\mathbf{P}^{\phi}\). The covariance, \(\sigma_{\mathbf{P}\mathbf{P}}\), measures the joint variability between the patches, while \(\sigma_{\mathbf{P}}^{2}\) and \(\sigma_{\mathbf{P}}^{2}\) represent their individual variances. The constants \(c_{1}\) and \(c_{2}\) prevent instability in the SSIM computation. The resulting SSIM scores are stored in a cost matrix of size \(N\times M\), which represents the accumulated cost of the optimal alignment up to position \((i,j)\). To find the minimum cost path from the starting point to each point \((i,j)\), an accumulated cost matrix of size \(N\times M\), where \(\text{AccCost}(i,j)\), is calculated. This accumulates costs using the recurrence relation:  
\[\text{AccCost}(i,j)=\text{cost}(i,j)+\min(\text{AccCost}(i-1,j),\text{AccCost }(i,j-1),\text{AccCost}(i-1,j-1))\]  
Then, this is traced back from \((N,M)\) to \((1,1)\) to find the optimal alignment path. This path represents the matching between the target and source patches. The technique of transforming a source image's style--but not its content--into a target image's style is known as style transfer. Texture, color, and contrast--both local and global--are components of the style. Conversely, content usually refers to the shapes, hues, and textures needed to identify the scene, including any particular people or objects. We  
Figure 3: Using dynamic programming to find corresponding patches based on structural similarities between target and source patches. consider local anatomical features to be content in our framework, but motion artifacts as part of the style.