Machine Learning-Based Casting Defect Recognition

In my research, I focused on the problem of automatically recognizing casting defects from X-ray images of aerospace light alloy castings. Casting defects such as porosity, shrinkage, cracks, and inclusions are common in industrial production, and their accurate identification is critical for ensuring product quality and safety. Traditional manual inspection is subjective, slow, and expensive. Therefore, I developed a machine learning-based method that combines image processing, multi-feature extraction, feature selection, and ensemble classification to detect and classify casting defects effectively. The entire framework was validated using real production data from an aerospace casting line. In this article, I present the details of my methodology, the mathematical formulations, and the experimental results. I also compare my approach with other commonly used methods to demonstrate its effectiveness and robustness.

1. Background and Significance

Casting is one of the primary methods for producing industrial components. In my study, I examined the production of aerospace light alloy castings, which are widely used in aircraft and spacecraft. The casting process involves multiple stages, including sand inspection, molding, melting, pouring, sand cleaning, grinding, rough machining, X-ray inspection, defect repair, heat treatment, and final inspection. Each stage can introduce casting defects due to variations in sand permeability, gas evolution, compactness, environmental conditions, and other uncontrolled factors. If these casting defects are not detected, they can develop into fatigue cracks or stress corrosion cracks, leading to catastrophic failure. Thus, accurate and efficient recognition of casting defects is essential.

Non-destructive testing (NDT) has evolved over decades. It began with non-destructive inspection (NDI), then non-destructive testing (NDT), and now non-destructive evaluation (NDE). X-ray detection is particularly suitable for casting defects because it is not limited by material shape or structure and has strong penetration. However, manual interpretation of X-ray images is still common in many foundries. This manual approach depends heavily on the inspector’s experience, making it subjective and inefficient. With the rapid development of computer vision and machine learning, automated casting defect recognition has become a promising research direction. My work addresses this need by proposing a complete pipeline for casting defect recognition based on machine learning.

2. Types and Characteristics of Casting Defects

I analyzed several common casting defects in aerospace light alloy castings. These include gas porosity, shrinkage, cracks, inclusions, segregation, misruns, sand burn-on, shrinkage cavities, sand holes, metal penetration, and cold shuts. Each casting defect has distinct formation causes and visual characteristics. For example, gas porosity appears as smooth round or oval dark spots, shrinkage appears as sponge-like cloudy regions, cracks are elongated and sharp at the ends, and inclusions are irregular bright spots. Understanding these characteristics is essential for designing effective image preprocessing and feature extraction methods. Table 1 summarizes the causes and features of several casting defects.

Casting Defect Cause Characteristics
Gas porosity Gas trapped in molten metal during pouring Smooth round or oval, very dark, small size
Shrinkage Incomplete feeding during solidification Sponge-like, cloudy appearance
Crack Uneven solidification and external resistance Thin and long, sharp ends
Inclusion Foreign matter mixed into molten metal Irregular shape, bright, small
Segregation Uneven chemical composition Similar to crack but higher gray value
Misrun Low pouring temperature, slow speed Incomplete filling
Sand burn-on Excessive clay adhesion Thin sand shell inside
Shrinkage cavity Improper chemical composition, high pouring temperature Rough internal surface, irregular shape
Sand hole Unreasonable gating system, low sand compactness Hole with sand particles
Metal penetration High pouring temperature or thin coating Rough surface with sand particles
Cold shut Low pouring temperature, poor fluidity Unfused gap or depression

3. Overall Framework for Casting Defect Recognition

My overall framework consists of three main stages: image preprocessing and segmentation, feature extraction and selection, and classifier construction. First, I applied linear grayscale transformation and bilateral filtering to reduce noise and enhance contrast. Then, I used edge detection and adaptive threshold segmentation to isolate casting defects from the background. Next, I extracted multiple features, including geometric shape features, Hu moment features, local binary pattern (LBP) features, and histogram of oriented gradient (HOG) features. Because HOG features have high dimensionality, I used principal component analysis (PCA) to reduce their dimension. I then fused all features into a full information feature set and applied support vector machine recursive feature elimination (SVM-RFE) to select the most sensitive features. Finally, I built an Adaboost-SVM classifier to recognize different casting defects. The framework is designed to handle small sample sizes and high-dimensional features, which are typical in casting defect recognition problems.

4. Image Preprocessing and Segmentation

Image preprocessing is crucial because X-ray images of castings often suffer from irregular noise, cluttered backgrounds, and uneven brightness. I first performed linear grayscale transformation to stretch the gray level range and improve contrast. The linear transformation is defined as:

$$ f(i,j) = a’ + \frac{b’ – a’}{b – a}(g(i,j) – a) $$

where \(g(i,j)\) is the original image, \(f(i,j)\) is the transformed image, \([a,b]\) is the original gray range, and \([a’,b’]\) is the target gray range. For more complex needs, piecewise linear transformation can be used:

$$ f(i,j) = \begin{cases}
(c/a) g(i,j), & 0 \leq g(i,j) < a \\
\frac{d-c}{b-a}[g(i,j)-a] + c, & a \leq g(i,j) < b \\
\frac{M_f – d}{M_g – b}[g(i,j)-b] + d, & b \leq g(i,j) \leq M_g
\end{cases} $$

I also considered nonlinear transformation, such as logarithmic transformation:

$$ f(i,j) = a + \frac{\ln[g(i,j)+1]}{b \ln c} $$

After comparing these methods, I selected linear grayscale transformation because it is simple and effective for my casting defect images.

For image filtering, I compared mean filtering, median filtering, Gaussian filtering, and bilateral filtering. Mean filtering replaces each pixel with the average of its neighborhood:

$$ f'(x,y) = \frac{1}{n \times n} \sum_{(x,y) \in S} f(x,y) $$

Gaussian filtering uses a 2D Gaussian function as the kernel:

$$ G(x,y,\sigma) = \frac{1}{2\pi \sigma^2} e^{-\frac{x^2+y^2}{2\sigma^2}} $$

Bilateral filtering is a nonlinear method that combines spatial distance and intensity similarity:

$$ M’_x = \frac{1}{W_x} \sum_{y \in S} G_{\sigma_s}(||x-y||) G_{\sigma_r}(|M_x – M_y|) M_y $$

where \(G_{\sigma_s}\) is the spatial weight and \(G_{\sigma_r}\) is the range weight. I found that bilateral filtering preserved edges better while reducing noise, so I chose it for my casting defect images.

For image segmentation, I used both edge detection and threshold segmentation. Edge detection is based on discontinuities in gray level. The gradient magnitude and direction are:

$$ M(x,y) = \sqrt{G_x^2 + G_y^2} $$

$$ \theta = \arctan\left(\frac{G_y}{G_x}\right) $$

I selected the Canny operator because it is robust to noise and can detect fine edges. For threshold segmentation, I used adaptive thresholding to handle uneven illumination:

$$ T(h,w) = (1 – ratio) \times \text{mean}(f(h,w)) $$

$$ J(h,w) = \begin{cases}
255, & J(h,w) < T(h,w) \\
0, & J(h,w) \geq T(h,w)
\end{cases} $$

Table 2 shows the parameter settings used in my preprocessing and segmentation steps.

Algorithm Parameter Value
Bilateral filter Template size (H, W) (5, 5)
Spatial standard deviation \(\sigma_1\) 20
Similarity standard deviation \(\sigma_2\) 0.2
Canny operator Low threshold 50
High threshold 250
Adaptive threshold Smooth operator size (15, 15)
ratio 0.15

5. Feature Extraction for Casting Defects

Feature extraction is the key to accurate casting defect recognition. Because casting defects vary in size, orientation, and position, I extracted multiple types of features to capture their characteristics. These include geometric shape features, Hu moment features, LBP features, and HOG features.

5.1 Geometric Shape Features

I extracted several geometric features: area, perimeter-to-area ratio, aspect ratio, rectangularity, sharpness, and average brightness. The area \(S\) is the number of pixels in the defect region. The perimeter-to-area ratio is \(P/S\). The aspect ratio is \(L/W\), where \(L\) and \(W\) are the length and width of the minimum bounding rectangle. Rectangularity is:

$$ R = \frac{S_0}{S} $$

where \(S_0\) is the defect area and \(S\) is the area of the minimum bounding rectangle. Sharpness is defined as:

$$ P = \frac{S_1 + S_2}{S} $$

where \(S_1\) and \(S_2\) are the areas of the two end parts of the defect region. Table 3 shows some extracted geometric features for three casting defect types.

Defect Type Area (pixel) Perimeter/Area Aspect Ratio Rectangularity Sharpness Average Brightness
Crack 289 6.122 16.234 0.432 0.869 122
Crack 371 2.992 19.173 0.544 0.884 142
Gas porosity 24 0.632 1.543 0.879 0.122 14
Gas porosity 28 0.611 1.452 0.845 0.091 19
Shrinkage 1782 3.122 2.535 0.761 0.432 167
Shrinkage 1981 2.872 4.335 0.739 0.454 159

5.2 Hu Moment Features

Hu moments are invariant to translation, scaling, and rotation, making them suitable for describing casting defect shapes. The seven Hu invariant moments are computed from central moments:

$$ M_{pq} = \sum_x \sum_y (x-\bar{x})^p (y-\bar{y})^q f(x,y) $$

Then the seven moments are:

$$ I_1 = \mu_{20} + \mu_{02} $$
$$ I_2 = (\mu_{20} – \mu_{02})^2 + 4\mu_{11}^2 $$
$$ I_3 = (\mu_{30} – 3\mu_{12})^2 + (3\mu_{21} – \mu_{03})^2 $$
$$ I_4 = (\mu_{30} + \mu_{12})^2 + (\mu_{21} + \mu_{03})^2 $$
$$ I_5 = (\mu_{30} – 3\mu_{12})(\mu_{30} + \mu_{12})[(\mu_{30} + \mu_{12})^2 – 3(\mu_{21} + \mu_{03})^2] + (3\mu_{21} – \mu_{03})(\mu_{21} + \mu_{03})[3(\mu_{30} + \mu_{12})^2 – (\mu_{21} + \mu_{03})^2] $$
$$ I_6 = (\mu_{20} – \mu_{02})[(\mu_{30} + \mu_{12})^2 – (\mu_{21} + \mu_{03})^2] + 4\mu_{11}(\mu_{30} + \mu_{12})(\mu_{21} + \mu_{03}) $$
$$ I_7 = (3\mu_{21} – \mu_{03})(\mu_{30} + \mu_{12})[(\mu_{30} + \mu_{12})^2 – 3(\mu_{21} + \mu_{03})^2] + (3\mu_{12} – \mu_{30})(\mu_{21} + \mu_{03})[3(\mu_{30} + \mu_{12})^2 – (\mu_{21} + \mu_{03})^2] $$

Table 4 shows some extracted Hu moment features for different casting defects.

Defect Type \(I_1\) \(I_2\) \(I_3\) \(I_4\) \(I_5\) \(I_6\) \(I_7\)
Crack 7.129 6.122 16.234 8.232 14.469 9.122 18.212
Crack 6.231 8.102 15.207 9.123 10.901 8.199 19.742
Gas porosity 7.192 8.391 14.089 10.921 16.012 11.093 16.821
Gas porosity 6.924 8.091 14.952 8.082 16.124 17.239 19.031
Shrinkage 7.073 11.381 14.782 12.032 14.054 17.205 18.982
Shrinkage 7.921 12.094 13.092 11.342 13.982 17.209 18.264

5.3 Local Binary Pattern (LBP) Features

LBP is a powerful texture descriptor. For a center pixel \(i_c\) and its neighbors \(i_p\), the LBP value is:

$$ LBP(x_c, y_c) = \sum_{p=0}^{P-1} 2^p S(i_p – i_c) $$

where \(S(x) = 1\) if \(x \geq 0\), otherwise \(0\). I used the uniform pattern extension, which reduces the number of binary patterns from \(2^P\) to \(P(P-1)+2\). This makes the feature vector more compact while preserving texture information. The LBP features are extracted from the segmented casting defect regions.

5.4 Histogram of Oriented Gradient (HOG) Features

HOG captures local shape and edge information. For each pixel, the gradient magnitude and orientation are computed:

$$ G_x(x,y) = H(x+1,y) – H(x-1,y) $$
$$ G_y(x,y) = H(x,y+1) – H(x,y-1) $$
$$ G(x,y) = \sqrt{G_x^2 + G_y^2} $$
$$ \theta(x,y) = \arctan\left(\frac{G_y}{G_x}\right) $$

I divided the image into 8×8 cells and used 9 orientation bins. The HOG features from all blocks are concatenated. Because the resulting dimension is very high, I applied PCA for dimensionality reduction. PCA finds the principal components by solving the eigenvalue problem:

$$ Q M = \lambda M $$

where \(Q\) is the covariance matrix of the normalized data. I retained enough principal components to preserve 99.99% of the information. The dimension of the HOG feature was thus greatly reduced.

6. Feature Selection with SVM-RFE

After extracting geometric, Hu moment, LBP, and HOG features, I fused them into a full information feature set. However, this set still contains redundant and irrelevant information. I used SVM-RFE to select the most sensitive features. SVM-RFE recursively eliminates the feature with the smallest weight. The ranking score for feature \(j\) is:

$$ g_j = w_j^2 $$

where \(w_j\) is the weight from the SVM model. The feature with the smallest \(g_j\) is removed, and the model is retrained. This process continues until the desired number of features is reached. The structure of SVM-RFE is shown in the algorithm flow. I also used mutual information to evaluate feature relevance:

$$ I(X,Y) = \sum_{X,Y} P(X,Y) \log \frac{P(X,Y)}{P(X)P(Y)} = H(Y) – H(Y|X) $$

where \(H(Y)\) is the entropy of the class labels. By selecting the top 60% of features, I achieved the best balance between accuracy and computational efficiency. Table 5 shows the comparison of different feature selection thresholds.

Threshold Crack Accuracy Crack Variance Crack False Negative Gas Porosity Accuracy Gas Porosity Variance Gas Porosity False Negative Shrinkage Accuracy Shrinkage Variance Shrinkage False Negative
10% 0.894 0.0332 0.027 0.891 0.0324 0.050 0.905 0.0321 0.038
20% 0.902 0.0312 0.027 0.908 0.0319 0.050 0.900 0.0303 0.038
30% 0.924 0.0300 0.025 0.905 0.0320 0.049 0.923 0.0310 0.037
40% 0.921 0.0299 0.025 0.913 0.0321 0.048 0.920 0.0311 0.036
50% 0.932 0.0291 0.024 0.918 0.0350 0.048 0.931 0.0303 0.036
60% 0.961 0.0302 0.022 0.921 0.0352 0.046 0.943 0.0311 0.034
70% 0.942 0.0281 0.025 0.915 0.0310 0.047 0.934 0.0312 0.035
80% 0.931 0.0298 0.026 0.906 0.0315 0.048 0.921 0.0311 0.036
90% 0.926 0.0327 0.026 0.891 0.0323 0.049 0.929 0.0336 0.037
100% 0.894 0.0321 0.027 0.892 0.0325 0.049 0.918 0.0335 0.038

7. Classifier Construction: Adaboost-SVM

For classification, I chose support vector machine (SVM) as the base classifier because it performs well on small samples and high-dimensional features. The SVM optimization problem is:

$$ \min_{w,b,\xi} \frac{1}{2} ||w||^2 + C \sum_{i=1}^N \xi_i $$

$$ \text{s.t. } y_i(w^T x_i + b) \geq 1 – \xi_i, \quad \xi_i \geq 0 $$

where \(C\) is the penalty parameter and \(\xi_i\) are slack variables. I used a Gaussian kernel:

$$ K(x_i, x_j) = \exp(-\gamma ||x_i – x_j||^2) $$

I selected \(\gamma = 1.01\) using grid search. However, a single SVM may suffer from low generalization. To improve performance, I combined SVM with Adaboost. Adaboost is an ensemble algorithm that combines multiple weak classifiers into a strong classifier. The weight of each classifier is:

$$ \alpha_t = \frac{1}{2} \ln\left(\frac{1 – e_t}{e_t}\right) $$

where \(e_t\) is the training error. The sample weights are updated as:

$$ w_{t+1,j} = \frac{w_{t,j}}{B_t} \exp[-\alpha_t y_j h_t(x_j)] $$

where \(B_t\) is a normalization factor. The final strong classifier is:

$$ H(x) = \text{sign}\left(\sum_{t=1}^T \alpha_t h_t(x)\right) $$

I built the Adaboost-SVM model with 1000 iterations. The construction flow includes data normalization, weight initialization, SVM training, weight updating, and final classifier combination. This ensemble approach improves both accuracy and robustness compared to a single SVM.

8. Case Study and Experimental Validation

I validated my method using real X-ray images from an aerospace casting production line. The dataset contained crack defects, gas porosity defects, shrinkage defects, and defect-free samples. Because the original dataset was small, I used geometric transformations (flipping, rotation, mirroring) and noise addition to augment the data. Table 6 shows the sample distribution after augmentation.

Sample Class Before Augmentation After Augmentation
Crack defect 14 112
Shrinkage defect 14 112
Gas porosity defect 14 112
Defect-free 23 184
Total 38 560

I used 10-fold cross-validation to evaluate the model. The performance metrics include accuracy, variance of accuracy, and false negative rate. The false negative rate is important because missing a casting defect can lead to safety risks. Table 7 shows the mean results of 10-fold cross-validation for my method.

Defect Class Mean Accuracy Variance of Accuracy Mean False Negative Rate
Crack 0.961 0.0302 0.022
Gas porosity 0.921 0.0352 0.046
Shrinkage 0.943 0.0311 0.034
Overall 0.941 0.0321 0.034

I also compared my method with different feature extraction methods and different classifiers. Table 8 shows the comparison of feature extraction methods. My method, which combines multiple features and SVM-RFE selection, achieves the best overall accuracy and lowest false negative rate.

Feature Method Crack Accuracy Crack Variance Crack False Negative Gas Porosity Accuracy Gas Porosity Variance Gas Porosity False Negative Shrinkage Accuracy Shrinkage Variance Shrinkage False Negative
Geometric 0.938 0.0322 0.030 0.902 0.0381 0.052 0.921 0.0371 0.039
HOG 0.943 0.0321 0.039 0.912 0.0371 0.051 0.913 0.0361 0.041
LBP 0.921 0.0309 0.041 0.891 0.0359 0.075 0.901 0.0322 0.048
Hu moments 0.912 0.0311 0.039 0.899 0.0361 0.069 0.897 0.0331 0.049
My method 0.961 0.0302 0.022 0.921 0.0352 0.046 0.943 0.0311 0.034

Table 9 compares my Adaboost-SVM classifier with BP neural network and random forest. My method achieves the highest accuracy, lowest variance, and lowest false negative rate for all casting defect types. For the overall performance, my method reaches 94.1% accuracy, which is 5 percentage points higher than random forest and about 2 percentage points higher than BP neural network. The variance is also significantly lower, indicating better generalization.

Classifier Crack Accuracy Crack Variance Crack False Negative Gas Porosity Accuracy Gas Porosity Variance Gas Porosity False Negative Shrinkage Accuracy Shrinkage Variance Shrinkage False Negative
BP neural network 0.937 0.0501 0.025 0.913 0.0571 0.060 0.924 0.0508 0.066
Random forest 0.891 0.0791 0.035 0.881 0.0871 0.052 0.902 0.0861 0.050
My method 0.961 0.0302 0.022 0.921 0.0352 0.046 0.943 0.0311 0.034
Classifier Overall Accuracy Overall Variance Overall False Negative Rate
BP neural network 0.924 0.0526 0.050
Random forest 0.891 0.0841 0.045
My method 0.941 0.0340 0.034

9. Conclusions and Future Work

In my research, I developed a complete machine learning-based method for casting defect recognition. I analyzed the formation causes and characteristics of common casting defects, applied image preprocessing and segmentation, extracted multiple features, selected the most sensitive features using SVM-RFE, and built an Adaboost-SVM classifier. The experimental results show that my method achieves high accuracy and low false negative rates for crack, gas porosity, and shrinkage defects. The ensemble classifier outperforms single classifiers and other feature extraction methods. The main contributions of my work are: (1) a multi-feature extraction approach that is rich and representative for casting defects; (2) the use of SVM-RFE to select key features, reducing dimensionality and improving classification accuracy; and (3) the construction of an Adaboost-SVM model that integrates multiple SVMs for better performance and robustness.

For future work, I plan to collect more high-quality casting defect images to further improve the model. I also intend to consider the time efficiency of defect recognition, especially for real-time production lines. Additionally, I would like to build a complete casting defect detection and recognition system that integrates X-ray imaging, defect marking, defect repair, and re-inspection. This would provide more effective support for foundry workers and help improve overall production quality.

In summary, my machine learning-based casting defect recognition method provides a reliable and efficient solution for industrial casting inspection. The combination of advanced image processing, feature engineering, and ensemble learning can significantly enhance the detection of casting defects and contribute to safer and more reliable aerospace components.

Scroll to Top