Machine Vision-Based Surface Roughness Detection for Sand Casting

In the rapidly evolving field of manufacturing, the sand casting industry has become a critical component of high-end equipment manufacturing, playing an increasingly vital role in the modernization of industry and the development of equipment manufacturing. Surface roughness is a key indicator for assessing the quality of component surfaces, closely related to wear resistance, fatigue strength, and vibration noise, significantly influencing part performance and service life. For sand casting components, surface roughness typically ranges from Ra3.2 to Ra50. Traditional measurement methods, such as comparison with standard specimen blocks, are prone to subjective influences, while contact-based methods like stylus profilometers, with a measurement range usually between Ra0.025 and Ra12.5, face significant spatial limitations in practical applications, making them unsuitable for measuring the surface roughness of sand castings or molds. Therefore, there is an urgent need for a rapid, accurate, and convenient method to measure the roughness of castings in actual production environments.

To address this challenge, we have developed a non-contact surface roughness detection system for sand casting based on machine vision technology, integrating image processing and convolutional neural networks. This system aims to reduce the impact of subjective factors on measurement results, enhance the objectivity of surface roughness assessment, and improve detection efficiency, providing a reference for the application of non-contact measurement methods in the future. The technical approach involves capturing surface images of standard casting roughness specimens, preprocessing these images to create a dataset, constructing and training a convolutional neural network incorporating an attention mechanism, and then using the trained network as the core to build a detection system. The feasibility of this method is validated by comparing its results with those obtained from the standard specimen comparison method on actual castings.

The dataset for this study was derived from standard casting surface roughness specimens, specifically iron-sand type and aluminum-zinc-magnesium metal type specimens, covering five roughness grades from Ra3.2 to Ra50. A total of 3,478 images were collected from these specimens. Image acquisition was performed using a DYVCAM-W20246 V22 macro camera module based on a CMOS image sensor, with an output image size of 640 pixels by 480 pixels. Single-sided lateral illumination was employed to stabilize lighting conditions during image capture. To ensure consistent imaging, an auxiliary housing was designed and 3D printed using black resin, assembled with the sensor module, and connected via a USB interface for image acquisition. Python scripts were used to call OpenCV interfaces for capturing surface images of the specimens.

Image preprocessing was crucial to enhance the quality of the dataset and improve the performance of the neural network. The collected images were first cropped to 480 pixels by 480 pixels to eliminate blurry edges caused by the macro lens. Subsequently, the images were converted to grayscale to focus on texture differences rather than color variations, as surface roughness in sand casting is primarily reflected in light and dark textures. The grayscale conversion was performed using the formula:

$$ f(i, j) = 0.30 \times R(i, j) + 0.59 \times G(i, j) + 0.11 \times B(i, j) $$

where \( f \) represents the grayscale value, and \( R \), \( G \), and \( B \) are the red, green, and blue channel values at pixel position \( (i, j) \), respectively. To address uneven brightness caused by single-sided lighting, Contrast Limited Adaptive Histogram Equalization (CLAHE) was applied instead of standard Histogram Equalization (HE). CLAHE enhances contrast while suppressing local noise, with a clip limit of 200 and a grid size of 8×8. The discrete expression for histogram equalization is given by:

$$ s_k = T(r_k) = (L – 1) \sum_{j=0}^{k} P_r(r_j) = \frac{L – 1}{MN} \sum_{j=0}^{k} n_j \quad (k = 0,1,2,\ldots,L-1) $$

where \( MN \) is the total number of pixels, \( n_j \) is the number of pixels with grayscale value \( j \), \( P_r(r_j) \) is the probability of grayscale \( j \) in the original image, \( L \) is the number of grayscale levels (256 for 8-bit images), and \( T \) represents the histogram transformation. After grayscale conversion and CLAHE, a median filter with a kernel size of 7 was applied for noise reduction, effectively removing random noise and impulse interference while preserving edge information.

Data augmentation was employed to expand the dataset and improve the generalization ability of the neural network. The original 3,478 images were horizontally flipped, doubling the dataset size to 6,956 images. The dataset was divided into training, validation, and test sets, with no overlap between them. The training set consisted of 4,916 images, the validation set had 1,040 images, and the test set contained 1,000 images. The composition of the dataset is summarized in the following table:

Dataset Category Specimen Type Ra3.2 Ra6.3 Ra12.5 Ra25 Ra50 Total
Training Set Iron-Sand Type 484 492 484 492 504 2,456
Al-Zn-Mg Metal Type 484 516 484 484 492 2,460
Validation Set Iron-Sand Type 104 104 104 104 104 520
Al-Zn-Mg Metal Type 104 104 104 104 104 520
Test Set Iron-Sand Type 100 100 100 100 100 500
Al-Zn-Mg Metal Type 100 100 100 100 100 500

The neural network architecture was based on ResNet50, a deep residual network that mitigates gradient vanishing issues in deep networks through skip connections. To enhance feature learning, we incorporated a Squeeze-and-Excitation (SE) module, a lightweight attention mechanism, into the residual blocks. The SE module consists of a squeeze operation using global average pooling to compress spatial dimensions to 1×1, followed by an excitation operation with two 1×1 convolutional layers and a ReLU layer to learn feature correlations. A Sigmoid function then generates an attention weight vector, which is multiplied by the original feature map to emphasize important features. This Conv2d-SE residual module increases the network’s learning capacity with minimal computational overhead. The modified residual module can be represented as follows: let \( H \), \( W \), and \( C \) be the height, width, and number of input channels of the feature map, respectively, and \( r \) be the compression ratio. The output feature map \( Y \) is computed as:

$$ Y = X \cdot \sigma(W_2 \delta(W_1 z)) $$

where \( X \) is the input feature map, \( z \) is the squeezed vector from global average pooling, \( W_1 \) and \( W_2 \) are weights of the 1×1 convolutions, \( \delta \) is the ReLU activation, and \( \sigma \) is the Sigmoid function.

The network was implemented using Python and PyTorch, with training conducted on a system equipped with an Intel i5-12600K CPU, 32 GB RAM, and an NVIDIA GeForce RTX 3060 GPU. The Adam optimizer and cross-entropy loss function were used for training, with an initial learning rate of 0.1, batch size of 64, and a maximum of 2000 epochs. Early stopping was applied if the validation accuracy did not improve for 200 consecutive epochs. The training process converged after approximately 90 epochs, taking 6 hours and 31 minutes to complete. The model selected for final use achieved validation accuracies of 99.04% for iron-sand type specimens and 99.23% for aluminum-zinc-magnesium metal type specimens. Testing on the separate test sets yielded accuracies of 99.00% and 99.60%, respectively, demonstrating the model’s strong ability to distinguish between the five roughness grades in sand casting.

To enable real-time surface roughness measurement, we developed a detection software using Python. The software captures live video from the image acquisition device, performs the same preprocessing steps as during training, and loads the trained neural network model for single-step inference, outputting the detection result. The interface allows users to easily obtain roughness measurements for sand casting components.

The system was tested on actual sand castings, including a stainless steel pump casing and an aluminum alloy volute casing. The pump casing had surface roughness primarily between Ra3.2 and Ra12.5, while the volute casing ranged from Ra12.5 to Ra50. Sixteen sampling points were randomly selected from key areas of each casting, and measurements were taken using both the standard specimen comparison method and our machine vision-based method. The results are summarized in the table below:

Sample No. Stainless Steel Pump Casing Correct? Sample No. Aluminum Alloy Volute Casing Correct?
Standard Specimen SE-ResNet-50 Standard Specimen SE-ResNet-50
1 Ra6.3 Ra6.3 Yes 17 Ra50 Ra50 Yes
2 Ra12.5 Ra12.5 Yes 18 Ra50 Ra50 Yes
3 Ra6.3 Ra6.3 Yes 19 Ra50 Ra50 Yes
4 Ra6.3 Ra6.3 Yes 20 Ra25 Ra25 Yes
5 Ra3.2 Ra3.2 Yes 21 Ra12.5 Ra12.5 Yes
6 Ra3.2 Ra3.2 Yes 22 Ra12.5 Ra12.5 Yes
7 Ra3.2 Ra6.3 No 23 Ra25 Ra50 No
8 Ra12.5 Ra12.5 Yes 24 Ra50 Ra50 Yes
9 Ra12.5 Ra12.5 Yes 25 Ra50 Ra50 Yes
10 Ra12.5 Ra12.5 Yes 26 Ra50 Ra50 Yes
11 Ra6.3 Ra12.5 No 27 Ra50 Ra50 Yes
12 Ra6.3 Ra6.3 Yes 28 Ra25 Ra50 No
13 Ra6.3 Ra6.3 Yes 29 Ra25 Ra25 Yes
14 Ra6.3 Ra6.3 Yes 30 Ra12.5 Ra12.5 Yes
15 Ra12.5 Ra12.5 Yes 31 Ra25 Ra25 Yes
16 Ra12.5 Ra12.5 Yes 32 Ra25 Ra25 Yes

Out of 32 measurements, 28 were correct, resulting in an overall accuracy of 87.5%. The average inference time per image was 0.086 seconds, indicating that the method is both accurate and fast, meeting the requirements for rapid and convenient roughness measurement in sand casting production.

In conclusion, this study successfully demonstrates a machine vision-based approach for non-contact surface roughness detection in sand casting. Key contributions include the development of an image preprocessing pipeline utilizing CLAHE to address uneven illumination, the integration of an attention mechanism into a residual convolutional neural network to enhance feature learning, and the creation of a practical detection system. The method achieved high accuracy on standard specimens and maintained good performance on actual sand castings, with fast inference times suitable for industrial applications. This work provides a reliable solution for improving surface quality assessment in sand casting processes, reducing subjectivity, and increasing efficiency. Future research could explore the adaptation of this system to other casting materials or the integration of additional sensors for comprehensive quality control in sand casting operations.

Scroll to Top