Casting Defect Detection via Advanced Image Registration

My research focuses on improving the accuracy and efficiency of casting defect detection using X-ray nondestructive testing combined with advanced image registration techniques. In industrial practice, casting defects such as gas pores, inclusions, cracks, and shrinkage cavities can significantly compromise the safety and reliability of critical components, including automotive wheels, engine blocks, and chassis parts. The challenge is that X-ray images of castings often exhibit rigid and elastic deformations due to positioning variations, making direct digital subtraction between a test image and a template image unreliable. Therefore, precise image registration is an indispensable prerequisite for effective defect extraction. In this article, I present my work on developing robust registration-based methods that address both rigid and non-rigid deformations, ultimately enabling accurate casting defect detection.

Introduction and Problem Statement

X-ray inspection is widely used for industrial casting defect detection because it can reveal internal flaws without damaging the component. However, the interpretation of radiographic images is still largely performed by human inspectors in many factories. This manual process is tedious, subjective, and prone to errors, especially when large batches of castings need to be inspected in real time. To automate the process, researchers have explored various image processing and machine vision techniques. Digital subtraction radiography is a promising approach: if a flawless template image of a standard casting is available, subtracting the aligned test image from the template should highlight only the defect regions. The key obstacle is that, due to mechanical positioning tolerances, the test image is not perfectly aligned with the template. The misalignment can be decomposed into a global rigid component (translation and rotation) and a local elastic component caused by minor variations in casting geometry, material shrinkage, or X-ray source instability. Thus, my work aims to develop a registration framework that handles both rigid and elastic deformations, followed by threshold-based segmentation to isolate true casting defects from residual registration artifacts.

Fundamentals of Image Registration

Image registration is the process of transforming different images into a common coordinate system. Given a reference image \(f_1\) and a moving image \(f_2\), the goal is to find a spatial transformation \(T\) such that \(f_1(x,y) \approx f_2(T(x,y))\). The transformation can be rigid (translation, rotation), affine (including scaling and shearing), projective, or nonlinear (elastic). In casting defect detection, the deformation between the test image and the template is usually a combination of rigid and elastic components. Table 1 summarizes the transformation types relevant to my research.

Transformation type Translation Rotation Scaling Shear Local warp
Rigid Yes Yes No No No
Affine Yes Yes Yes Yes No
Projective Yes Yes Yes Yes Yes (global perspective)
Elastic (Nonlinear) Yes Yes Yes Yes Yes (local arbitrary)

The standard registration pipeline includes feature space, search space, search strategy, interpolation, and similarity measure. For my work, I primarily use intensity-based methods because X-ray casting images have rich gray-level information and distinct local contrast around defects. The similarity measures I adopted for objective evaluation are:

Mean Squared Error (MSE):
\[
MSE = \frac{1}{\Omega} \sum_{(x,y) \in \Omega} \left( f_1(x,y) – f_2(x,y) \right)^2
\]
where \(\Omega\) is the total number of pixels.

Peak Signal-to-Noise Ratio (PSNR):
\[
PSNR = 10 \log_{10} \left( \frac{255^2 \cdot \Omega}{\sum_{x,y} (f_1(x,y) – f_2(x,y))^2} \right)
\]

Cross-Correlation (CC):
\[
CC = \frac{\sum_{x,y} (f_1(x,y) – \bar{f}_1)(f_2(x,y) – \bar{f}_2)}{\sqrt{\sum_{x,y} (f_1(x,y) – \bar{f}_1)^2 \sum_{x,y} (f_2(x,y) – \bar{f}_2)^2}}
\]

Residual Variance (RV):
\[
RV = \frac{1}{\Omega} \sum_{x,y} \left| f_1(x,y) – f_2(x,y) \right|
\]

Structural Similarity (SSIM):
\[
SSIM = \frac{(2\mu_f \mu_r + C_1)(2\sigma_{fr} + C_2)}{(\mu_f^2 + \mu_r^2 + C_1)(\sigma_f^2 + \sigma_r^2 + C_2)}
\]

These metrics provide quantitative evidence for the quality of registration and, consequently, the accuracy of casting defect detection.

Rigid Registration for Initial Alignment

In my first approach, I began with a two-stage rigid registration to correct global translation and rotation. The translational shift is estimated using the phase correlation method. Let \(f_1(x,y)\) be the template image and \(f_2(x,y)\) be the test image with a shift \((x_0, y_0)\):
\[
f_2(x,y) = f_1(x – x_0, y – y_0).
\]
In the Fourier domain:
\[
F_2(\xi,\eta) = F_1(\xi,\eta) e^{-j2\pi(\xi x_0 + \eta y_0)}.
\]
The normalized cross-power spectrum is:
\[
\frac{F_1(\xi,\eta) F_2^*(\xi,\eta)}{|F_1(\xi,\eta) F_2^*(\xi,\eta)|} = e^{j2\pi(\xi x_0 + \eta y_0)}.
\]
Taking the inverse Fourier transform yields a Dirac delta function centered at \((x_0, y_0)\), which gives the translation vector. This method is fast, robust, and achieves sub-pixel accuracy when combined with peak interpolation.

For rotational alignment, I used the orientation of the inertia ellipse of the casting region. The moment of inertia around an axis through the centroid can be computed from the image intensity distribution. For a two-dimensional image, the inertia matrix is:
\[
A = \begin{pmatrix}
\sum m_i y_i^2 & -\sum m_i x_i y_i \\
-\sum m_i x_i y_i & \sum m_i x_i^2
\end{pmatrix}
\]
where \(m_i\) is the intensity at pixel \(i\). The eigenvectors of this matrix define the principal axes of the inertia ellipse. The angle between the principal axis and the horizontal axis is:
\[
\theta = \frac{1}{2} \tan^{-1} \left( \frac{2H}{A-B} \right)
\]
where \(A = \sum m_i y_i^2\), \(B = \sum m_i x_i^2\), and \(H = \sum m_i x_i y_i\). By calculating the angle for both the template and the test image, I can obtain the rotation angle and correct it accordingly.

After applying these rigid corrections, the test image is globally aligned with the template. However, residual local distortions remain, which necessitate an elastic registration step.

Elastic Registration with Demons Algorithms

The Demons algorithm, originally inspired by Maxwell’s demon thought experiment, treats image registration as a diffusion process. The deformation field is driven by forces computed from the intensity difference and the gradient of the reference image. The original Demons force for each pixel is:
\[
\mathbf{u}(x,y) = \frac{(f_1(x,y) – f_2(x,y)) \nabla f_1(x,y)}{\|\nabla f_1(x,y)\|^2 + (f_1(x,y) – f_2(x,y))^2}
\]
where \(f_1\) is the reference (template) and \(f_2\) is the moving image. This formulation only uses the gradient of the template, which limits its ability to handle large deformations.

The Active Demons algorithm improves the original method by incorporating the gradient of the moving image as well, yielding a more symmetric force:
\[
\mathbf{u}(x,y) = \frac{(f_1 – f_2) \nabla f_1}{\|\nabla f_1\|^2 + \alpha^2 (f_1 – f_2)^2} + \frac{(f_1 – f_2) \nabla f_2}{\|\nabla f_2\|^2 + \alpha^2 (f_1 – f_2)^2}
\]
where \(\alpha\) is a normalization factor that controls the deformation step size. The parameter \(\alpha\) needs to be tuned manually for optimal performance, which is a significant drawback in an automated inspection pipeline.

To further improve the registration accuracy, I explored a variant that incorporates the direction field of the image. The direction field, unlike the gradient field, represents the local orientation of image structures. For an image \(I\), the gradient components are \(dI_x\) and \(dI_y\). The direction field angle is computed as:
\[
O_I = \frac{1}{2} \tan^{-1} \left( \frac{2 dI_x dI_y}{dI_x^2 – dI_y^2} \right).
\]
By combining both the gradient field and the direction field, the Demons force can better utilize the local geometry, leading to more accurate registration, especially for images with complex textures and edges.

Proposed Rigid-Elastic Registration Framework

My first major contribution is a casting defect detection method based on a combination of rigid registration and Active Demons elastic registration. The workflow is illustrated in the following steps:

  1. Rigid registration: Use phase correlation for translation and inertia ellipse for rotation to align the test casting image to the template.
  2. Elastic registration: Apply Active Demons on the rigidly aligned image to correct local nonlinear deformations.
  3. Digital subtraction: Subtract the registered test image from the template to obtain a residual image.
  4. Threshold segmentation: Apply a gray-level threshold to separate true casting defects from residual registration noise.
  5. Defect marking: Label the connected components in the binary mask and overlay them on the original test image.

The digital subtraction step is mathematically expressed as:
\[
D(x,y) = |f_{template}(x,y) – f_{registered}(x,y)|.
\]
In an ideal case, \(D(x,y)\) would be zero everywhere except at defect locations. In practice, interpolation artifacts and small registration errors produce a nonzero background. Therefore, a threshold \(T_h\) is applied:
\[
B(x,y) = \begin{cases}
1, & \text{if } D(x,y) > T_h \\
0, & \text{otherwise}
\end{cases}
\]
The threshold is chosen based on the known contrast of casting defects in X-ray images; in my experiments, I set \(T_h = 15\) gray levels.

Table 2 shows the quantitative comparison of registration methods using the first experimental dataset. The methods compared are: rigid registration only, Active Demons only (without rigid pre-alignment), rigid + B-spline elastic registration, and my proposed rigid + Active Demons elastic registration.

Method MSE PSNR CC RV SSIM
Rigid only 50.50 19.59 0.9409 5.16 0.9357
Active Demons only 38.33 20.21 0.9962 1.96 0.9744
Rigid + B-spline 9.46 21.89 0.9972 1.78 0.9910
My proposed (Rigid + Active Demons) 8.59 23.53 0.9987 1.31 0.9942

The results demonstrate that the combined rigid-elastic approach outperforms the other methods in all metrics. The MSE is significantly reduced, while PSNR, CC, and SSIM are improved, indicating that the registered test image is much closer to the template. Visual inspection of defect marking also confirmed that the proposed method correctly identifies nearly all true casting defects while producing fewer false positives compared to the alternatives.

Improved Demons with Direction Field and Mutual Information

Despite the success of the Active Demons approach, I identified two limitations: (1) it only uses gradient information, ignoring the directional structure of the image, and (2) the normalization factor \(\alpha\) requires manual adjustment. To address these issues, I developed an improved Demons registration method that integrates the direction field and uses mutual information as an adaptive control parameter.

The direction field provides complementary information to the gradient field. For a given image, the gradient vector points in the direction of maximum intensity change, while the direction field indicates the orientation of edges and ridges. By incorporating the direction field into the Demons force calculation, I obtained a more comprehensive deformation vector. The improved force formula for the template \(R\) and moving image \(F\) is:

\[
\mathbf{u}_F = \frac{\arctan(\theta_F) \cdot \nabla F \cdot \mathbf{eO}_F}{NI(m,f) \cdot (R-F)}
\]
\[
\mathbf{u}_R = \frac{\arctan(\theta_R) \cdot \nabla R \cdot \mathbf{eO}_R}{NI(m,f) \cdot (R-F)}
\]
where \(\mathbf{eO}_F\) and \(\mathbf{eO}_R\) are unit vectors along the direction field of the moving and reference images, respectively, and \(NI(m,f)\) is the normalized mutual information between the two images. The overall deformation field is:
\[
\mathbf{u} = \frac{1}{2} \left( \sin(2\theta_F) \cdot \mathbf{eO}_F + \sin(2\theta_R) \cdot \mathbf{eO}_R \right) \cdot \frac{k}{NI(m,f)}
\]
where \(k\) is a constant and \(NI(m,f)\) is computed as:
\[
NI(m,f) = \frac{H(m) + H(f)}{H(m,f)}
\]
with \(H(m)\) and \(H(f)\) being the marginal entropies and \(H(m,f)\) the joint entropy of the images. The mutual information value is high when the images are well aligned and low when they are misaligned. Therefore, using \(NI(m,f)\) in the denominator automatically scales the deformation step size: larger steps when the images are far apart, and smaller steps when they are close. This adaptive behavior eliminates the need for manual tuning of \(\alpha\), while also improving convergence speed and final accuracy.

Experimental Setup and Comparative Analysis

I conducted extensive experiments on three different industrial casting image datasets. Each dataset consisted of a template image (created by averaging multiple aligned images of defect-free castings) and a test image containing real defects. The image size was 3072 × 3072 pixels. All experiments were performed on a computer with an Intel Core i7-9700K CPU and 32 GB RAM, using MATLAB and Visual Studio environments.

For each dataset, I compared three elastic registration methods after the same initial rigid registration: (1) B-spline elastic registration, (2) Active Demons elastic registration, and (3) my proposed improved Demons method with direction field and mutual information. The defect detection results were evaluated both subjectively (visual inspection of marked defects) and objectively (using the five similarity metrics).

Table 3 presents the quantitative results for all three datasets.

Dataset Metric B-spline Active Demons Proposed Method
Set 1 MSE 9.46 8.59 6.87
PSNR 21.89 23.53 26.05
CC 0.9972 0.9987 0.9992
RV 1.78 1.31 1.17
SSIM 0.9910 0.9942 0.9983
Set 2 MSE 9.37 9.02 6.50
PSNR 21.77 23.11 25.94
CC 0.9928 0.9978 0.9994
RV 2.05 1.93 1.76
SSIM 0.9902 0.9926 0.9985
Set 3 MSE 14.40 10.45 8.38
PSNR 21.06 22.46 25.74
CC 0.9864 0.9982 0.9989
RV 2.46 2.08 1.64
SSIM 0.9894 0.9937 0.9979

In every dataset, the proposed method achieved the lowest MSE and RV, and the highest PSNR, CC, and SSIM. This consistent improvement confirms that the direction field information combined with mutual information-based adaptive stepping significantly enhances the registration accuracy. The better registration directly translates into more precise casting defect detection, as the residual image after subtraction contains fewer false artifacts and the true defects become more prominent.

Subjective Visual Analysis of Defect Marking

To visually evaluate the practical effectiveness of my algorithm, I examined the defect-marked images produced by each method. The manual defect markings were used as the ground truth. The results are described below for each dataset.

For Dataset 1, the B-spline method missed several small defects and produced some false alarms at the edges. The Active Demons method reduced false alarms but still missed a few defect clusters. In contrast, my proposed method correctly identified almost all defects visible in the manual marking, with very few false positives. The improvement was particularly noticeable in regions where defects were small and had low contrast, which are typically the most challenging cases.

For Dataset 2, a similar pattern emerged. The B-spline method exhibited significant under-detection, missing more than half of the defects in some dense areas. The Active Demons method improved detection but still left a few defects undetected. My method achieved near-complete detection, and the marked defect shapes closely matched the manual annotations. This indicates that the direction field helps preserve the local geometry of the defects, preventing the deformation field from smoothing them away during registration.

For Dataset 3, which contained defects near the edges of the casting, the B-spline method created some false positives due to poor edge alignment. The Active Demons method was better but still imperfect. My method aligned the edges precisely, and the subtraction image clearly revealed the defects without edge artifacts. This demonstrates the robustness of the proposed method in handling images with strong boundary gradients, which is a common challenge in industrial X-ray inspection.

Discussion on Parameter Adaptation

One of the key contributions of my improved Demons method is the elimination of manual parameter tuning. In the conventional Active Demons algorithm, the user must choose an appropriate value for \(\alpha\) to balance the influence of the gradient term and the intensity difference term. An improper choice can lead to either slow convergence or oscillatory behavior. By replacing \(\alpha\) with the normalized mutual information \(NI(m,f)\), I achieve an automatic and dynamic adjustment. During the early iterations, the images are poorly aligned, so \(NI\) is low. This produces larger deformation steps, allowing the algorithm to quickly capture the global displacement. As the iterations proceed and the images become more similar, \(NI\) increases, reducing the step size and enabling fine-tuning of local deformations. This adaptive strategy not only improves accuracy but also accelerates convergence, reducing computation time in practice.

I also observed that the direction field term helps avoid incorrect matches in regions with repetitive patterns or low gradient magnitude. The gradient field alone may be ambiguous in flat areas, but the direction field provides orientation information that helps the algorithm find the correct correspondence. This is especially relevant for casting images, which often contain uniform regions interrupted by sharp edges and local texture variations.

Computational Efficiency

Although my method adds the computation of the direction field and mutual information, the overall runtime remains acceptable for industrial applications. The mutual information is calculated from histograms of intensities, which is computationally inexpensive. The direction field can be computed directly from the gradient components with a small additional cost. Table 4 shows the average computation time for the three elastic registration methods on a 3072×3072 image.

Method Average time (seconds)
B-spline elastic registration 45.2
Active Demons 38.7
Proposed method 42.3

The proposed method is slightly slower than the Active Demons due to the additional direction field and mutual information computations, but it is still faster than the B-spline method and provides considerably better accuracy. Given the importance of accuracy in casting defect detection, the small computational overhead is acceptable.

Conclusion and Future Prospects

In this research, I have demonstrated that accurate casting defect detection in X-ray images can be achieved through a carefully designed image registration framework. My initial approach combining rigid registration (phase correlation and inertia ellipse) with Active Demons elastic registration already outperformed several existing methods. By further introducing the direction field and mutual information-based adaptive control, I developed an improved Demons algorithm that achieves even higher registration accuracy while removing the need for manual parameter adjustment. The experimental results on multiple industrial casting datasets confirm that the proposed method yields the lowest MSE and RV, and the highest PSNR, CC, and SSIM values, leading to more precise casting defect localization and fewer false positives.

Looking ahead, there are several avenues for further improvement. First, deep learning-based registration methods could be explored to handle highly complex deformations and to reduce computation time through GPU acceleration. Second, a more robust thresholding strategy based on local statistics or machine learning could further reduce residual artifacts. Third, the current framework assumes that the template image is defect-free; in practice, generating a perfect template may not always be possible, so future work could investigate simultaneous defect detection and template updating. Finally, extending the method to three-dimensional X-ray computed tomography (CT) images would be valuable for volumetric casting inspection, although the increased data size poses new challenges for registration efficiency and memory usage.

In conclusion, my work contributes a practical and effective solution for automated casting defect detection based on advanced image registration techniques. The combination of rigid and elastic registration, enhanced by direction field information and adaptive mutual information control, provides a solid foundation for reliable quality control in casting manufacturing.

Scroll to Top