✅ Author Introduction: A MATLAB simulation developer passionate about research, improving both mindset and technology. For MATLAB project collaboration, please contact me privately.
🏆 Code Acquisition Method:
QQ:912100926
⛳️ Motto: Those who travel a hundred miles are halfway at ninety.
## ⛄ 1. Introduction to Morphological Defect Detection
**1 Image Analysis and Preprocessing**
Captured images can produce random disturbances and contain a certain amount of noise. To eliminate irrelevant information in the images, preprocessing is required.
**1.1 Grayscale Conversion**
To reduce computational load, the three-channel RGB image captured needs to be converted into a single-channel grayscale image. The grayscale conversion method uses a weighted average approach, where the psychological grayscale formula selects different weights based on the human eye’s sensitivity to the RGB colors:
In formula (1), R, G, and B represent the grayscale values of the RGB channels, and the grayscale result is shown in Figure 1 (a).
**1.2 Smoothing Processing**
To avoid misclassifying the background as defects, the image needs to undergo smoothing processing. Although this may blur the boundaries of defects, it helps reduce background interference. Note that the denoising method used is mean filtering, with the mean filtering formula as follows:
In formula (2), m and n are the length and width of the selected filter kernel, Sxy is the set of pixel positions corresponding to the filter kernel centered at (x,y), and the smoothing result is shown in Figure 1 (b). The disadvantage of mean filtering is that it can cause the loss of some details, such as edge information. Therefore, after finding seed points, region growing is performed on the original image to find defect boundaries.
**2 Algorithm Principles**
2.1 Threshold Segmentation**
Threshold segmentation is the simplest and most basic method in image segmentation, with stable performance, low computational load, and fast processing speed. It mainly includes global threshold segmentation, local threshold segmentation, and adaptive threshold segmentation methods. The threshold algorithm is based on threshold T, where pixel grayscale values greater than T are called foreground, and those less than T are called background. The transformation function expression is:
Figure 1 Mean Filtering Processing
In formula (3), T is the threshold, g (x,y) is the grayscale value of the original image pixel point (x,y), and f (x,y) is the grayscale value of the segmented image pixel point (x,y). The threshold segmentation result is shown in Figure 2.
Figure 2 Threshold Segmentation Result
**2.2 Morphological Opening Operation for Denoising**
Mathematical morphology, abbreviated as morphology, processes images through neighborhood operations, where the structural element interacts with the corresponding pixel positions in the image through logical operations. The influencing factors of this operation mainly include the size, shape of the structural element, and the rules of logical operations. Morphological operations mainly include dilation, erosion, gradient operations, top-hat operations, black-hat operations, opening, and closing operations, but the foundation lies in erosion and dilation, which can be used to perform different forms of operations.
Erosion can eliminate contour boundary points, shrinking the boundary inward, mainly used for refining binary image target contours and removing noise.
In formula (4), A is the original image, and B is the structural element. First, a reference point is defined for the structural element B. When the origin of structural element B moves to (x,y) in image A, if the pixels corresponding to the 1s in structural element B also equal 1 in image A, the grayscale value of image A at (x,y) is set to 1; otherwise, it is set to 0. The erosion illustration is shown in Figure 3.
Figure 3 Erosion Illustration
Dilation, on the other hand, expands the boundary outward, mainly used to fill gaps after image segmentation, connecting nearby but unconnected contours. Its formula is:
In formula (5), A is the original image, and B is the structural element. First, a reference point is defined for the structural element B. When the origin of structural element B moves to (x,y) in image A, if at least one of the pixels corresponding to the 1s in structural element B equals 1 in image A, the grayscale value of image A at (x,y) is set to 1; otherwise, it is set to 0.
First, perform erosion, then dilation on the eroded image, mainly used for denoising and counting. Its formula is:
In formula (6), A is the original image, and B and C are the structural elements. The effect of the opening operation is shown in Figure 4, and Figure 5 shows the result of the opening operation.
Figure 4 Opening Operation Effect
**2.3 Region Growing Method**
The idea of region growing is to group pixels in the neighborhood (4-neighborhood, 8-neighborhood, etc.) that are similar into one region. First, a seed point is needed to start the growth, and then pixels in the neighborhood of the seed point that meet the similarity criteria are merged into the seed’s region. The pixels of this region are then treated as new seed points, and the growth continues until no more points meet the criteria, at which point the growth ends, and all seed point pixels are considered as the grown region. The quality of segmentation depends on the initial seed points and the similarity criteria.
Figure 5 Morphological Opening Operation Result
**2.3.1 Seed Point Selection and Detection**
After threshold segmentation and morphological processing, the centers of the contours in the binary image are taken as candidate seed points. If the selected seed point is located in the absolute area of the defect, then the depth values of the pixels in one direction from the seed point will exhibit a high-low-high pattern. The detection template is designed as shown in Figure 6, calculating the depth changes of the seed point in the 0°, 45°, 90°, and 135° directions to determine if the changes exhibit a high-low-high pattern.
Figure 6 Detection Template
The average grayscale values of r pixels on both sides of the seed point are:
The grayscale changes in each direction are:
Depth pattern change determination:
In formula (10), I (u) is the grayscale value of the u-th pixel in the detection template, w=1,2,3,4 represents the 0°, 45°, 90°, and 135° directions, mwm is the minimum grayscale value on both sides in the w direction, and T1 is the morphological change threshold. If the seed point does not meet the depth pattern change determination, it is removed from the candidate seed points.
**2.3.2 Growth Process**
The specific process of region growing is as follows:
(1) Place the seed point coordinates into the seed point set seeds.
(2) Extract a seed point from the seed point set and perform similarity criterion judgment on the pixels in the eight neighborhoods of the seed point; points that meet the similarity criteria are treated as seed points and added to the seed point set.
(3) Store the extracted seed point in the seed set S.
(4) If there are no elements in the seed point set, jump to step (4); if there are still elements in the seed point set, jump to step (2).
(5) Generate an image I with the same dimensions as the input image, with all pixel values set to 0.
(6) Set the pixel values in image I corresponding to the coordinates of the seed set S to 255, resulting in the segmented image I’.
The similarity criteria for growth are:
Figure 7 Region Growing Result
**3 Experimental Process**
Image segmentation is the process of dividing an image into meaningful foreground and background according to pre-set rules. Region growing is an algorithm with good segmentation results, but it requires suitable seed points to start. A single segmentation algorithm may encounter limitations, so combining morphological and threshold segmentation methods helps find suitable seed points, assisting the region growing algorithm in completing the segmentation task and achieving the desired segmentation results. The segmentation method flowchart is shown in Figure 8.
Figure 8 Segmentation Flowchart
First, the input image undergoes grayscale processing to become a single-channel grayscale image, then filtering is applied to remove noise, making the image smoother. A suitable threshold is selected for threshold segmentation, and opening operation is used to remove smaller foregrounds after segmentation. The center points of each foreground region are taken as starting seed points for region growing, resulting in the final desired foreground.
## ⛄ 2. Partial Source Code
clear,clc;
I=imread(‘soybean.png’);
figure,imshow(I);title(‘Original Image’) ;
Ig=rgb2gray(I); % Grayscale Processing
figure,imshow(Ig);title(‘Grayscale Image’) ;
Im=medfilt2(Ig,[3,3]); % Median Filtering
figure,imshow(Im);title(‘Filtered Image’) ;
thresh = graythresh(Im);
It=im2bw(Im,thresh); % Binarization
figure,imshow(It);title(‘Binary Image’) ;
s
figure,imshow(A);title(‘Eroded Image’) ;
se1=strel(‘diamond’,5); % Create Structural Element
A1=imdilate(A,se1);
figure,imshow(A1);title(‘Dilated Image’) ;
## ⛄ 3. Running Results
## ⛄ 4. MATLAB Version and References
**1 MATLAB Version**
2014a
**2 References**
[1] Kang Shiying, Yao Bin. Rapid Counting Method for Grain Particles Based on Computer Vision [J]. Automation Technology and Applications. 2019, 38(08)
**3 Note**
This section is extracted from the internet for reference only. If there is any infringement, please contact for removal.
**🍅 Simulation Consulting**
1 Various Intelligent Optimization Algorithm Improvements and Applications**
Production scheduling, economic scheduling, assembly line scheduling, charging optimization, workshop scheduling, departure optimization, reservoir scheduling, three-dimensional packing, logistics site selection, cargo position optimization, bus scheduling optimization, charging pile layout optimization, workshop layout optimization, container ship loading optimization, pump combination optimization, medical resource allocation optimization, facility layout optimization, visual domain base station and drone site selection optimization.
**2 Machine Learning and Deep Learning**
Convolutional Neural Networks (CNN), LSTM, Support Vector Machines (SVM), Least Squares Support Vector Machines (LSSVM), Extreme Learning Machines (ELM), Kernel Extreme Learning Machines (KELM), BP, RBF, Wide Learning, DBN, RF, RBF, DELM, XGBOOST, TCN for wind power prediction, photovoltaic prediction, battery life prediction, radiation source identification, traffic flow prediction, load forecasting, stock price prediction, PM2.5 concentration prediction, battery health state prediction, water optical parameter inversion, NLOS signal identification, subway stop precision prediction, transformer fault diagnosis.
**3 Image Processing**
Image recognition, image segmentation, image detection, image hiding, image registration, image stitching, image fusion, image enhancement, image compression sensing.
**4 Path Planning**
Traveling Salesman Problem (TSP), Vehicle Routing Problem (VRP, MVRP, CVRP, VRPTW, etc.), UAV three-dimensional path planning, UAV collaboration, UAV formation, robot path planning, grid map path planning, multimodal transport problems, vehicle collaborative UAV path planning, antenna linear array distribution optimization, workshop layout optimization.
**5 UAV Applications**
UAV path planning, UAV control, UAV formation, UAV collaboration, UAV task allocation.
**6 Wireless Sensor Positioning and Layout**
Sensor deployment optimization, communication protocol optimization, routing optimization, target localization optimization, Dv-Hop localization optimization, Leach protocol optimization, WSN coverage optimization, multicast optimization, RSSI localization optimization.
**7 Signal Processing**
Signal recognition, signal encryption, signal denoising, signal enhancement, radar signal processing, signal watermark embedding and extraction, EMG signals, EEG signals, signal timing optimization.
**8 Power Systems**
Microgrid optimization, reactive power optimization, distribution network reconstruction, energy storage configuration.
**9 Cellular Automata**
Traffic flow, crowd evacuation, virus spread, crystal growth.
**10 Radar**
Kalman filtering tracking, track association, track fusion.