What’s Wrong inside These Devices? Defect Detection in X-ray Images

Neurond AI
Nerd For Tech
Published in
5 min readMar 18, 2022

Defect detection has become an essential demand for the production line to check whether the products meet standards. Although we consider that problem through image processing, we always have different approaches to detect what we need for every product in different industrial areas.

In this blog post, we will introduce some technologies for defect detection with X-ray images — a usual and popular case used in many factories.

Mask RCNN Neural Network

In recent years, mask RCNN has proven its efficiency, performance, and accuracy in solving object detection and object segmentation problems.

Mask RCNN

When considering the defects of objects from X-ray images, we’ve divided them into two cases:

  • Cases that can use Mask RCNN: The defects of the objects need to be visible, big “enough,” have almost the same particular pattern, and don’t need to be pre-processed. We can label these cases and train them with Mask RCNN if we have enough datasets for the objects.
  • Cases that cannot use Mask RCNN: In some cases, the defects could be very difficult to determine, whether they’re too small, invisible, have unclear patterns, or need image preprocessing.

For example, there is a defect in a casting wheel under X-ray. If the defect is visible and has a particular pattern, we can train it with Mask RCNN to detect that defect.

Image of defect that seems to be unique and good for masking.

Edge Detection

Edge detection includes a variety of mathematical methods that aim at identifying edges and curves, at which the image’s brightness changes sharply. In our project, we focus on two algorithms:

  • Sobel Edge Detection: this is a very common edge extraction filter, which is an approximation to a derivative of an image. We can use the Sobel filter for both directions x and y. In the image processing field, using Sobel edge detection means we do a convolution with 2 matrixes for x direction and y direction.

Here is an example of using Sobel edge detection for the horizontal and vertical direction:

Sobel filter takes advantage when we look for black-to-white and white-to-black regions
  • Canny Edge Detection: This is an upgrade from Sobel Edge Detection which shows better results by enhancing the computation’s complexity. This filter consists of 5 steps in the whole process.

Here is an example of using Canny Edge detection with the same image above. We can easily see that the edges of any part are present better and thinner than using Sobel Edge.

Threshold

In the OpenCV Library, there are two kinds of thresholding: simple and adaptive.

Simple Thresholding: If the pixel value is smaller than the threshold, it is set to 0, otherwise it is set to a maximum value. There are at least 5 algorithms for threshold. For X-ray images, we will use BINARY and TOZERO thresholds to detect the faults in products.

  • BINARY: this filter will check if the pixel is over or under a threshold value. If this value is bigger, the pixel will be changed to white. Otherwise, it is black.
  • TOZERO: this filter has the same comparison mechanism as BINARY, but the rules for the pixel over the threshold value are different. Any pixel under this value will be changed to black, the others will remain unchanged.

Here is an example of applying those thresholds, using the same parameters to control the quality of the output image (remember to convert the image to gray before using threshold).

In this example, both filters help us check the basic detail of the object. However, we found that TOZERO threshold will be more useful than BINARY if we want to detect any defects in the gray pixel.

Adaptive Thresholding is applied to deal with different lighting conditions, which is the tough case for normal thresholding. With adaptive thresholding, the algorithm determines the threshold for a pixel based on a small area around it. So, we get different thresholds for different areas of the same image, from which we will get a better result.

There are two kinds of adaptive thresholding: THRESH MEAN (mean value of pixel in an area) and THRESH GAUSSIAN (gaussian weighted sum). Let’s check out an example where we apply the adaptive thresholding for the sample image above using the same parameters.

Conclusion

As mentioned earlier, every product has multiple approaches to deal with defects checking problems in the image field. What we have done is an approach for this kind of X-ray sample. To adapt to the project’s requirements, we should combine different techniques and adjust the parameters till we get a better detection result. We hope this article has provided you with useful techniques to handle fault detection on devices from X-ray images.

References

Mask RCNN Lib: ayoolaolafenwa/PixelLib: Visit PixelLib’s official documentation https://pixellib.readthedocs.io/en/latest/ (github.com)

Image enhancing: https://towardsdatascience.com/measuring-enhancing-image-quality-attributes-234b0f250e10

Line detection: https://www.geeksforgeeks.org/line-detection-python-opencv-houghline-method/

Image thresholding: OpenCV: Image Thresholding

Edge detection: Edge Detection Using OpenCV | LearnOpenCV #

SKIMAGE filter: Thresholding — skimage v0.19.2 docs (scikit-image.org)

Sample images: http://dmery.ing.puc.cl/index.php/material/gdxray/

Read our original article at: https://www.neurond.com/blog/defect-detection-in-xray-images

NeurondAI is a transformation business. Contact us at:

Website: https://www.neurond.com/

--

--