Casting stands as one of the most fundamental manufacturing processes, with cast components finding extensive applications across diverse industries. However, the occurrence of casting defects is almost inevitable during production, and these defects severely compromise product performance and reliability. Therefore, this research investigates a rapid and accurate method for the judgment and classification of casting defects by integrating digital image processing technology with artificial neural networks. Specifically, we first explore the use of image signal processing techniques to digitally enhance and analyze images of casting defects captured by CCD cameras. This involves extracting multiple characteristic parameters from these images and, through systematic sample processing, obtaining a substantial dataset of high-quality casting defect images suitable for analysis. Subsequently, we employ the principles of the Backpropagation (BP) neural network to classify and recognize the extracted casting defect images. After rigorous network training, we develop a BP neural network model that demonstrates effective classification performance. The overarching research methodology is illustrated in the following conceptual framework.

In the casting production process, defects arise due to various factors including equipment limitations, process parameters, raw material quality, and environmental conditions. Among these, pinholes, cracks, and inclusions are three particularly severe types of casting defects that critically affect mechanical properties. The primary causes for these casting defects are outlined below.
Pinholes: This type of casting defect forms during the cooling of the casting, where the solubility of gases in the molten metal decreases sharply with temperature. If the gases cannot fully escape before solidification, they become trapped, forming small cavities. The presence of pinholes significantly reduces the strength of the casting.
Cracks: The formation of crack defects is multifaceted. A common cause is uneven cooling rates within different sections of the casting, often due to constraints from the mold walls or cores. This creates excessive internal stress between regions cooling at different rates, leading to fracture. Casting cracks drastically diminish the component’s strength and ductility and often act as initiation sites for corrosion.
Inclusions: Inclusion defects originate from two main sources. Firstly, they can form from chemical reactions within the molten metal or at the metal-mold interface during pouring and solidification. Secondly, they can be non-metallic impurities dislodged from furnaces, ladles, or other equipment. The presence of inclusions markedly reduces the fatigue resistance of the casting.
This study utilizes digital image technology to process images of these three primary casting defects, aiming to generate high-quality samples for training the BP neural network. The ability to automatically identify these specific casting defects is crucial for quality control.
Digital Image Processing Techniques for Casting Defect Analysis
To analyze and recognize casting defects, a large set of high-quality digital images is required. Images were acquired using a CCD camera under controlled darkroom conditions with cold light illumination. An HP-480 color video capture card was used for digitization. The raw images obtained contain significant noise and cannot be used directly; thus, a series of algorithmic processing steps is essential. The key steps in the digital image processing pipeline are detailed below.
Gaussian Filtering
Given that noise in CCD imaging often follows a normal distribution, Gaussian filtering is highly effective for initial image smoothing. The Gaussian filter is a low-pass filter commonly used for image denoising. The two-dimensional Gaussian mask is defined as:
$$G(x,y)= \frac{1}{2\pi\sigma} \exp\left(-\frac{x^2+y^2}{\sigma^2}\right)$$
where \(x\) and \(y\) represent the distance from the pixel center, and \(\sigma\) is the standard deviation controlling the spread of the distribution. The shape of the Gaussian kernel can be adjusted by changing \(\sigma\). Applying this filter significantly enhances image quality by reducing high-frequency noise while preserving edge information, which is vital for subsequent casting defect analysis.
Fuzzy Enhancement
To improve image contrast and better separate potential casting defect regions (often lighter) from the background (often darker), a fuzzy enhancement algorithm is applied. This process accentuates edge details. We define a fuzzy operator \(k_{mn}^t\) as:
$$h_{mn}^t = \begin{cases}
2^{(t-1)}(h_{mn})^r & 0 \leq h_{mn} \leq 0.5 \\
1 – 2^{(t-1)}(1 – h_{mn})^r & 0.5 \leq h_{mn} \leq 1
\end{cases}$$
where \(t\) is the number of iterative fuzzy transformations, \(r\) is a parameter (empirically set to 4 for optimal enhancement), and \(h_{mn}\) is the membership degree of the pixel’s grayscale value in the fuzzy set. This operator intensifies pixels with membership above 0.5 and diminishes those below, effectively enhancing contrast after several iterations (\(t\)).
Image Segmentation and Edge Feature Extraction
Threshold segmentation is a fundamental technique for partitioning an image into distinct regions. We employ an optimal threshold method based on the image’s grayscale histogram to binarize the image, separating potential casting defect areas from the background. This creates a binary image where defects are highlighted.
For edge detection within these segmented casting defect regions, the Laplacian operator proves effective due to its high localization accuracy and good edge continuity. The Laplacian of a 2D function \(f(x,y)\) is a second-order derivative defined as:
$$\nabla^2 f = \frac{\partial^2 f}{\partial x^2} + \frac{\partial^2 f}{\partial y^2}$$
In digital implementation, it is often approximated using convolution kernels. Applying this operator after segmentation yields clear, continuous boundaries of the casting defects, which are crucial for accurate feature measurement.
The cumulative effect of Gaussian filtering, fuzzy enhancement, optimal threshold segmentation, and Laplacian edge detection results in significantly refined images where casting defect boundaries are well-defined for further quantitative analysis. The entire preprocessing pipeline is designed to maximize the signal-to-noise ratio for the features of the casting defect.
Feature Extraction for Casting Defect Characterization
Machine recognition of casting defects requires a quantitative description of their morphological features. Based on the distinct visual characteristics of pinholes, cracks, and inclusions, we select four key geometric parameters to describe each casting defect region: Aspect Ratio, Perimeter, Elongation, and Circularity. These parameters are calculated from the binary image after edge detection.
Let \(S\) denote the area (in pixels or calibrated mm²) of the defect region, \(L\) its perimeter, and \(a\) and \(b\) the width and length of the minimum bounding rectangle enclosing the defect.
- Aspect Ratio (\(AR\)): Defined as the ratio of the major axis length to the minor axis length of an ellipse approximating the defect. For simplicity, it can be derived from the bounding rectangle: \(AR \approx b/a\).
- Perimeter (\(P\)): The total length of the defect boundary, measured in mm.
- Elongation (\(E\)): This parameter helps distinguish approximately circular defects from elongated ones. It is defined as:
$$E = \frac{a \times b}{S}$$
A value closer to 1 indicates a more compact (near-square) shape, while larger values suggest elongation. - Circularity (\(C\)): This metric describes how closely the shape resembles a perfect circle. It is defined as:
$$C = \frac{4\pi S}{L^2}$$
For a perfect circle, \(C = 1\). Values less than 1 indicate increasingly non-circular shapes. This parameter is excellent for identifying pinholes which tend to be circular.
The typical range of these parameters for different casting defect types is summarized in the table below. This forms the basis for the feature vector input to the neural network.
| Casting Defect Type | Aspect Ratio (AR) | Perimeter (P) mm | Elongation (E) | Circularity (C) |
|---|---|---|---|---|
| Pinhole | 1.1 – 1.2 | 1.0 – 1.5 | 0.8 – 0.9 | 0.85 – 1.0 |
| Crack | 2.8 – 3.6 | 3.5 – 5.5 | 0.1 – 0.2 | 0.05 – 0.15 |
| Inclusion | 1.6 – 2.6 | 2.5 – 3.5 | 0.4 – 0.6 | 0.45 – 0.60 |
BP Neural Network for Classification of Casting Defects
Fundamental Principles
The Backpropagation (BP) neural network is a multi-layer feedforward network trained using error backpropagation. It is capable of approximating any continuous nonlinear function with arbitrary accuracy, making it suitable for pattern classification tasks like casting defect identification. Our network is designed to map the 4-dimensional feature vector (AR, P, E, C) to a 3-dimensional output vector representing the three casting defect classes.
The basic mathematical model for a neuron involves a weighted sum of inputs and an activation function. For a neuron \(j\) in a hidden or output layer, its output \(y_j\) is calculated as:
$$y_j = f\left(\sum_{i=1}^{n} w_{ji} x_i + b_j\right)$$
where \(x_i\) are inputs, \(w_{ji}\) are connection weights, \(b_j\) is the bias, and \(f(\cdot)\) is the activation function (commonly a sigmoid function like the logistic function).
$$f(z) = \frac{1}{1 + e^{-z}}$$
Network Architecture and Training
We constructed a three-layer BP network consisting of an input layer, one hidden layer, and an output layer. The input layer has 4 neurons corresponding to the four casting defect features. The output layer has 3 neurons, each representing one casting defect type (Pinhole, Crack, Inclusion). The target output is encoded as follows: Pinhole [1,0,0]; Crack [0,1,0]; Inclusion [0,0,1].
The number of neurons in the hidden layer (\(N_h\)) is critical. It is determined empirically using a trial-and-error method guided by the empirical formula \(N_h = \sqrt{N_i + N_o} + k\), where \(N_i=4\), \(N_o=3\), and \(k\) is a constant between 1 and 10. We evaluated the Mean Squared Error (MSE) for different \(N_h\) values.
| Hidden Neurons (\(N_h\)) | Final Training MSE (x10⁻³) | Remarks |
|---|---|---|
| 3 | 85 | Underfitting |
| 5 | 52 | Good candidate |
| 7 | 48 | Optimal in our test |
| 9 | 51 | Slight overfitting tendency |
| 12 | 55 | Increased computational cost |
Based on the minimal MSE, we selected \(N_h = 7\) for the final network. The ‘logsig’ (logistic sigmoid) activation function was used for both hidden and output layers. The network was trained using the Levenberg-Marquardt backpropagation algorithm, which offers faster convergence for medium-sized networks.
The training process aims to minimize the global error \(E\) over all training samples \(p\):
$$E = \frac{1}{2} \sum_{p=1}^{P} \sum_{k=1}^{3} (t_k^p – y_k^p)^2$$
where \(t_k^p\) is the target output and \(y_k^p\) is the actual network output for the \(k\)-th output neuron and the \(p\)-th sample. Weight updates are performed using the gradient descent rule with momentum to accelerate convergence and avoid local minima.
Sample Preparation and Normalization
From a database of several hundred casting defect images, 12 representative samples were selected for initial network development. Their feature parameters were calculated and normalized to the range [0, 1] to ensure stable and efficient network training. Normalization for a parameter \(x\) is done as:
$$x_{\text{norm}} = \frac{x – x_{\min}}{x_{\max} – x_{\min}}$$
A subset of these samples (8 out of 12) was used for training, and the remainder for testing. The sample data is shown below.
| Sample ID | Actual Casting Defect | Aspect Ratio | Perimeter | Elongation | Circularity | Target Output Vector |
|---|---|---|---|---|---|---|
| 1 | Pinhole | 0.05 | 0.03 | 0.85 | 0.90 | [1, 0, 0] |
| 2 | Crack | 0.85 | 0.78 | 0.05 | 0.04 | [0, 1, 0] |
| 3 | Inclusion | 0.45 | 0.55 | 0.51 | 0.53 | [0, 0, 1] |
| 4 | Pinhole | 0.08 | 0.02 | 0.82 | 0.93 | [1, 0, 0] |
| 5 | Crack | 0.80 | 0.95 | 0.08 | 0.08 | [0, 1, 0] |
| 6 | Inclusion | 0.58 | 0.48 | 0.44 | 0.58 | [0, 0, 1] |
| 7 | Crack | 1.00 | 1.00 | 0.17 | 0.10 | [0, 1, 0] |
| 8 | Inclusion | 0.78 | 0.45 | 0.41 | 0.48 | [0, 0, 1] |
| 9 | Pinhole | 0.06 | 0.06 | 0.83 | 0.89 | [1, 0, 0] |
| 10 | Crack | 0.92 | 0.68 | 0.04 | 0.57* | [0, 1, 0] |
| 11 | Pinhole | 0.07 | 0.10 | 0.84 | 0.97 | [1, 0, 0] |
| 12 | Inclusion | 0.50 | 0.48 | 0.45 | 0.56 | [0, 0, 1] |
*Note: Sample 10’s circularity is higher than typical for a crack, representing a challenging case for classification.
Experimental Results and Performance Analysis
The network was trained with a goal MSE of \(1 \times 10^{-5}\). The training converged after approximately 23 epochs, demonstrating the efficiency of the chosen architecture and algorithm. The performance of the trained BP network was then evaluated using all 12 samples, including those not seen during training. The network’s output for each sample is a 3-element vector. The element with the highest value is taken as the predicted class.
| Sample ID | Actual Casting Defect | Network Output Vector | Predicted Casting Defect | Classification Result |
|---|---|---|---|---|
| 1 | Pinhole | [0.98, 0.01, 0.02] | Pinhole | Correct |
| 2 | Crack | [0.03, 0.94, 0.04] | Crack | Correct |
| 3 | Inclusion | [0.01, 0.12, 0.91] | Inclusion | Correct |
| 4 | Pinhole | [0.85, 0.10, 0.08] | Pinhole | Correct |
| 5 | Crack | [0.05, 0.89, 0.07] | Crack | Correct |
| 6 | Inclusion | [0.04, 0.05, 0.93] | Inclusion | Correct |
| 7 | Crack | [0.02, 0.96, 0.03] | Crack | Correct |
| 8 | Inclusion | [0.15, 0.20, 0.78] | Inclusion | Correct* |
| 9 | Pinhole | [0.96, 0.02, 0.03] | Pinhole | Correct |
| 10 | Crack | [0.25, 0.70, 0.12] | Crack | Correct* |
| 11 | Pinhole | [0.97, 0.01, 0.02] | Pinhole | Correct |
| 12 | Inclusion | [0.02, 0.08, 0.95] | Inclusion | Correct |
* Samples 8 and 10 show lower confidence scores but are still correctly classified. The overall accuracy on this test set is 100% (12/12).
To rigorously assess the generalization capability of the model, we expanded the experiment using larger datasets. Multiple training and validation sets of varying sizes were created from a pool of over 200 unique casting defect images. The results are summarized below.
| Training Set Size | Validation Set Size | Number of Misclassified Casting Defects | Classification Accuracy (%) |
|---|---|---|---|
| 10 | 20 | 3 | 85.0 |
| 20 | 50 | 4 | 92.0 |
| 50 | 100 | 6 | 94.0 |
| 100 | 200 | 8 | 96.0 |
The results clearly indicate that the classification accuracy improves as the volume of training data increases. Even with a validation set of 200 casting defect images, the model maintains a high accuracy of 96%, demonstrating robust generalization and reliability for the task of casting defect identification. The model performs exceptionally well in distinguishing the three targeted casting defect types based on their extracted morphological features.
Mathematical Analysis of Decision Boundaries
The BP neural network effectively learns complex, non-linear decision boundaries in the 4-dimensional feature space. We can approximate the network’s function as a mapping \(F: \mathbb{R}^4 \to \mathbb{R}^3\). For a given input feature vector \(\mathbf{x} = [AR, P, E, C]^T\), the output \(\mathbf{y} = [y_1, y_2, y_3]^T\) can be expressed as a composition of functions:
$$\mathbf{y} = f^{(3)}\left( \mathbf{W}^{(3)} f^{(2)}\left( \mathbf{W}^{(2)} f^{(1)}\left( \mathbf{W}^{(1)} \mathbf{x} + \mathbf{b}^{(1)} \right) + \mathbf{b}^{(2)} \right) + \mathbf{b}^{(3)} \right)$$
where \(\mathbf{W}^{(l)}\) and \(\mathbf{b}^{(l)}\) are the weight matrix and bias vector for layer \(l\), and \(f^{(l)}\) is the activation function at layer \(l\). The network learns the parameters that minimize the classification error, effectively creating hyper-surfaces that separate the clusters of data points corresponding to different casting defect types.
The sensitivity of the network to each feature can be analyzed by examining the partial derivatives of the output with respect to the input features. For instance, the importance of Circularity \(C\) in classifying a pinhole casting defect can be assessed by \(\partial y_1 / \partial C\). In our trained network, these derivatives confirmed that Circularity and Aspect Ratio are the most influential features for distinguishing pinholes from cracks and inclusions, which aligns with the physical morphology of these casting defects.
Discussion and Potential Improvements
The proposed system combining digital image processing and BP neural networks offers a fast, automated, and accurate method for casting defect classification. The preprocessing pipeline successfully enhances casting defect features while suppressing noise, and the four chosen geometric parameters provide a compact yet descriptive representation of each casting defect’s morphology.
However, several avenues exist for enhancing the system. Firstly, incorporating more advanced features, such as texture descriptors (e.g., Haralick features from Gray-Level Co-occurrence Matrices) or moments (Hu moments), could improve discrimination, especially for casting defects with similar shapes but different surface textures. The mathematical form for a Hu moment invariant is:
$$\phi_i = \log_{10} |\eta_i|$$
where \(\eta_i\) are scale and rotation invariant moments derived from central moments.
Secondly, while the BP network performed well, other machine learning models like Support Vector Machines (SVMs) or Convolutional Neural Networks (CNNs) could be explored. CNNs, in particular, can automatically learn features directly from raw pixel data, potentially bypassing the manual feature extraction step. The convolution operation in a CNN layer is defined as:
$$(I * K)(i,j) = \sum_m \sum_n I(i-m, j-n) K(m,n)$$
where \(I\) is the input image and \(K\) is the filter kernel.
Thirdly, the system’s robustness could be tested against a wider variety of casting defects, such as shrinkage porosity, cold shuts, or misruns, and under varying imaging conditions (e.g., different lighting angles or intensities).
Finally, for real-time application in foundries, optimizing the computational efficiency of both the image processing algorithms and the neural network inference is crucial. Techniques like network pruning or quantization could be employed to deploy the model on embedded systems for on-line casting defect inspection.
Conclusion
Casting is a critical manufacturing process, and the quality of castings directly impacts the safety and performance of final products. This research successfully demonstrates the application of digital image processing combined with a BP neural network for the automated classification of casting defects. We developed a comprehensive image processing pipeline involving Gaussian filtering, fuzzy enhancement, threshold segmentation, and Laplacian edge detection to reliably extract casting defect regions. From these regions, four key geometric features—Aspect Ratio, Perimeter, Elongation, and Circularity—were calculated to form a discriminative feature vector.
A three-layer BP neural network was designed, trained, and validated. The network achieved high classification accuracy on test samples and demonstrated excellent generalization capability when scaled to larger datasets. The system provides a foundation for the development of real-time, online casting defect diagnostic systems in industrial settings. By enabling rapid and objective assessment of casting quality, this approach can contribute significantly to reducing scrap rates, improving process control, and enhancing overall product reliability in the foundry industry. The continuous evolution of imaging technology and machine learning algorithms promises even more powerful tools for casting defect analysis in the future.
