A machine vision-based method and apparatus for detecting fabric defects
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- ZHEJIANG UNIV
- Filing Date
- 2026-01-15
- Publication Date
- 2026-05-26
AI Technical Summary
Traditional fabric defect detection methods struggle to balance texture detail preservation and noise suppression, cannot adapt to uneven lighting and fabric material differences, and lack a channel and spatial attention coordination mechanism, leading to false alarms or missed detections.
A differential filter is used to calculate the scale gradient magnitude of pixels. A hybrid filter kernel is generated through spatial adaptive parameter mapping. A dual-stream feature extraction model is constructed and features are fused. An adaptive threshold is generated by combining a convolutional network for binarization.
It improves the accuracy and robustness of fabric defect detection, reduces the false detection rate, enhances the separation of defects in complex backgrounds, and adapts to the difference in defect contrast in different areas.
Smart Images

Figure CN121544597B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the technical field of fabric defect detection methods, specifically to a machine vision-based fabric defect detection method and apparatus. Background Technology
[0002] Traditional fabric defect detection methods typically rely on image processing algorithms with fixed thresholds or single feature analysis models. Conventional filters, such as Gaussian filtering and median filtering, use fixed kernel parameters, making it difficult to balance the contradiction between preserving texture details and suppressing noise. This results in high-texture areas losing details and low-contrast areas having residual noise.
[0003] Existing technologies, such as traditional binarization, employ a globally fixed threshold, which cannot adapt to dynamic scenarios involving uneven lighting and variations in fabric material. This leads to false alarms or missed detections due to threshold deviations. Furthermore, many technologies rely on single-stream feature extraction, neglecting the correlation between microscopic defects and macroscopic textures, and lack channel and spatial attention coordination mechanisms, resulting in the missed detection of minor defects. These existing technologies do not utilize gradient magnitude mapping to generate dynamic weights, making the filtering process region-sensitive, reducing smoothing intensity in high-gradient areas of the fabric to preserve edges, and enhancing smoothing effects in low-gradient noise areas. They also do not employ a two-stream structure to separately capture microscopic morphology and macroscopic texture, nor do they enhance feature complementarity through attention mechanisms. Finally, they do not dynamically adjust the threshold based on the heatmap mean and variance to adapt to differences in defect contrast across different regions. Therefore, a machine vision-based fabric defect detection method is urgently needed.
[0004] The information disclosed in the background section is only intended to enhance the understanding of the background of this disclosure, and therefore may include information that does not constitute prior art known to those skilled in the art. Summary of the Invention
[0005] The purpose of this invention is to provide a machine vision-based method and apparatus for detecting fabric defects, in order to solve the problems mentioned in the background art.
[0006] To achieve the above objectives, the present invention provides the following technical solution:
[0007] A machine vision-based method for detecting fabric defects, comprising the following steps:
[0008] S1: Acquire grayscale images of the fabric surface, construct a differential filter, calculate the scale gradient magnitude of the image pixels based on the differential filter, use spatial adaptive parameter mapping to map the scale gradient magnitude of the pixels to parameter values, and generate a hybrid filter kernel based on the parameter values to filter the pixels of the image.
[0009] S2: Based on the filtered image, a dual-stream feature extraction model is constructed to extract microscopic feature maps and texture feature maps. Based on the microscopic feature maps and texture feature maps, channel attention weight coefficients are constructed by channel splicing, and spatial attention weight coefficients of each pixel are generated at the same time. The channel attention weight coefficients and the spatial attention weight coefficients of the pixels are fused at multiple scales to generate a fused feature map.
[0010] S3: Based on the fused feature map, a convolutional network is used to construct a heatmap of the feature map. Based on the mean and variance of the pixels in the heatmap, an adaptive threshold is constructed. Binarization is used to compare the heatmap pixels of each pixel with the adaptive threshold to generate a binarized image of the defect. The detection result of the fabric is output based on the binarized image of the defect.
[0011] Furthermore, the scale gradient magnitude of image pixels is calculated based on the differential filter. The specific steps are as follows:
[0012] A differential filter is defined as a filter set containing three different differential filter structures, namely... of filter, of filter, The Gaussian derivative filter is used; each pixel of the grayscale image is convolved with the horizontal and vertical kernels of the difference filter, and the results are summed to obtain the gradient components in the horizontal and vertical directions of the pixel. Based on the gradient components, the gradient magnitude is calculated using the Pythagorean theorem. The gradient magnitudes generated by the three filters are compared, and the maximum value among the gradient magnitudes is selected as the final scale gradient magnitude of the pixel.
[0013] Furthermore, spatial adaptive parameter mapping is used to map the scale gradient magnitude of pixels to parameter values. The specific steps are as follows:
[0014] Based on the scale gradient magnitude of each pixel, a spatial adaptive parameter mapping is established to map the scale gradient magnitude of the pixel to parameter values, specifically:
[0015] Multiply the magnitude of the pixel's scale gradient by an adjustable adaptive weight coefficient, then take its negative value as the input to the exponential function, calculate the value of the exponential function and add a constant 1, divide the sum by a constant 2, and subtract the constant 1 from the quotient. The result is the parameter value that the pixel will ultimately map to.
[0016] Furthermore, a hybrid filter kernel is generated based on the parameter values to filter the pixels of the image. The specific steps are as follows:
[0017] Based on the parameter values obtained by mapping the gradient magnitude of each pixel in the image, a spatially adaptive hybrid filter kernel is generated. The hybrid filter kernel consists of two parts: one part is the parameter value minus 1 and then multiplied by a 5×5 Gaussian kernel; the other part is the parameter value multiplied by a 5×5 bilateral kernel. The results of these two parts are weighted to obtain the spatially adaptive hybrid filter kernel for each pixel.
[0018] For each pixel in the image, its surrounding pixels are... Each pixel value in the neighborhood is multiplied by the corresponding weight element in a hybrid filter kernel generated based on the pixel. All these weighted pixel values are summed to obtain the new pixel value of the filtered image at the pixel.
[0019] Furthermore, a dual-stream feature extraction model is constructed to extract microscopic feature maps and texture feature maps. The specific steps are as follows:
[0020] Based on the filtered grayscale image, a two-stream parallel structure is used to extract microscopic feature maps and texture feature maps. One branch is used to extract the microscopic feature maps. The processing flow of this branch is as follows: the filtered grayscale image is processed using a batch normalization process, and then passed through a... The activation function introduces non-linear results, and finally a convolutional kernel with 64 output channels and a size of [missing information] is used. Feature extraction is performed on the convolutional layers to obtain micro-feature maps;
[0021] Another branch is used to extract the texture feature map of the image. The processing flow of this branch is to first perform batch normalization on the filtered grayscale image and... The activation function introduces a non-linear result, which is then processed by a convolutional kernel with 32 output channels and a kernel size of [missing information]. The convolutional layer performs initial feature capture, and the captured result is then passed through a pooling window. The max pooling operation is used for downsampling, and the final output is a texture feature map.
[0022] Further, the fused feature map is generated, and the specific steps are as follows:
[0023] The extracted micro-feature maps and texture feature maps are concatenated along the channel dimension to form a concatenated feature map. Global average pooling is performed on the feature map to obtain a set of description vectors. The description vectors are input into a multilayer perceptron. The output of the multilayer perceptron is then mapped through an sigmoid function to finally generate a channel attention weight coefficient between 0 and 1.
[0024] The extracted microscopic feature maps and texture feature maps are added element-wise to form a composite feature map, which is then input into... The convolution kernel performs calculations, mapping multi-channel features to a single-channel spatial response map. It performs an exponential operation on each pixel value of the spatial response map, globally sums the values of all pixels after exponential amplification, and divides the exponentialized result of each pixel by the result of the global sum to generate the spatial attention weight coefficient at the pixel.
[0025] The generation of intermediate feature maps mainly consists of two parts: the first part is to multiply the spatial attention weight coefficient corresponding to each pixel with the micro feature map element by element; the second part is to subtract the spatial attention weight coefficient from 1 and then multiply the result with the texture feature map element by element. The two parts are weighted and combined to form an intermediate feature map. The obtained channel attention weight coefficient is then multiplied with the intermediate feature map to generate the fused feature map.
[0026] Furthermore, a heatmap of the image is constructed using a convolutional network. The specific steps are as follows:
[0027] Based on the fused feature map, a convolutional kernel with a size of [size missing] is used. A convolutional layer with 32 output channels performs feature transformation, followed by... The activation function introduces non-linearity, and finally a convolution kernel with a size of [size missing] is used. The convolutional layer with 1 output channel is subjected to channel compression and aggregation to generate a heatmap for highlighting key areas.
[0028] Furthermore, the safety factor of any grid center point is compared with a set safety threshold. If it exceeds the safety threshold, the grid location is considered a risk area. The specific steps are as follows:
[0029] when A value greater than or equal to 0.8 indicates the grid center point. In a safe state; when A value greater than or equal to 0.5 but less than 0.8 indicates the center point of the grid. In a state of alert; when When it is less than 0.5, it indicates the center point of the grid. They are in a high-risk state.
[0030] Further, a binarized image of the defect is generated, specifically through the following steps:
[0031] Based on the generated heatmap, the average value and standard deviation of all pixel values in the heatmap are calculated. Then, combined with the empirical coefficient, the average value is added to the product of the empirical coefficient and the standard deviation to form an adaptive judgment threshold.
[0032] Traverse each pixel in the heatmap and compare its pixel value with the calculated adaptive threshold. If the value of the point is greater than or equal to the threshold, mark it as 1 at the corresponding binarization mask position; otherwise, mark it as 0 to generate a flawed binarized image.
[0033] Furthermore, the detection results of the fabric are output based on the binarized image of the defect. The specific steps are as follows:
[0034] Based on the binarized image, count the number of pixels marked as 1 and calculate their proportion of all pixels. If the proportion exceeds a preset proportion threshold, the area represented by the binarized image is determined to be a defective area.
[0035] The present invention also provides a machine vision-based fabric defect detection device, the detection device being used to perform the above-described detection method, comprising:
[0036] The data acquisition module is used to determine the coordinate system and mark the location coordinates of potential fire sources. With a set grid step size, it uniformly divides the steel structure of the utility tunnel into several three-dimensional grids, determines the node coordinates of all three-dimensional grids, and collects the ambient temperature of the utility tunnel.
[0037] The data acquisition module is used to acquire grayscale images of the fabric surface, construct a differential filter, calculate the scale gradient magnitude of the image pixels based on the differential filter, use spatial adaptive parameter mapping to map the scale gradient magnitude of the pixels to parameter values, and generate a hybrid filter kernel based on the parameter values to filter the pixels of the image.
[0038] The feature fusion module is used to construct a dual-stream feature extraction model based on the filtered image to extract microscopic feature maps and texture feature maps. Based on the microscopic feature maps and texture feature maps, channel attention weight coefficients are constructed by channel splicing, and spatial attention weight coefficients of each pixel are generated at the same time. The channel attention weight coefficients and the spatial attention weight coefficients of the pixels are fused at multiple scales to generate a fused feature map.
[0039] The defect detection module is used to construct a heatmap of the feature map based on the fused feature map using a convolutional network. Based on the mean and variance of the pixels in the heatmap, an adaptive threshold is constructed. Binarization processing is used to compare the heatmap pixels of each pixel with the adaptive threshold to generate a binary image of the defect. The detection result of the fabric is output based on the binary image of the defect.
[0040] Compared with the prior art, the beneficial effects of the present invention are:
[0041] By dynamically adjusting the filter kernel weights based on pixel-level gradient magnitude, detail preservation is enhanced in high-texture areas, and noise suppression is improved in smooth areas, providing a high signal-to-noise ratio image base for subsequent feature extraction and avoiding feature misjudgment caused by detail loss or noise residue due to excessive smoothing. The dual-stream structure captures microscopic morphology and macroscopic texture respectively, and enhances feature complementarity through an attention mechanism. Channel attention filters salient channel features, and spatial attention locates the spatial distribution of defects. The fusion of the two effectively improves the discriminative performance of features. The threshold is dynamically adjusted based on the heatmap mean and variance, which can adapt to the difference in defect contrast in different regions, eliminate the interference of illumination fluctuations and material differences, reduce the false detection rate, and improve the separation of defects in complex backgrounds. Attached Figure Description
[0042] Figure 1 This is a schematic diagram of the overall method flow of the present invention;
[0043] Figure 2 This is the original grayscale image of the fabric and the gradient amplitude images generated by each filter in this invention;
[0044] Figure 3 This is the maximum gradient magnitude image selected in this invention;
[0045] Figure 4 This is a distribution diagram of parameter values for mapping the scale gradient magnitude of pixels in this invention;
[0046] Figure 5 This is the fabric image after hybrid filtering in this invention;
[0047] Figure 6 This invention includes a microscopic feature map, a texture feature map, and a fused feature map.
[0048] Figure 7 A heat map of the fabric with defects during the invention process;
[0049] Figure 8 A mask image for defects in the invention;
[0050] Figure 9 This is a graph showing the relationship between the gradient magnitude of a pixel and the corresponding mapping parameter values in this invention.
[0051] Figure 10 This is a structural block diagram of the detection device in this invention. Detailed Implementation
[0052] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to specific embodiments.
[0053] It should be noted that, unless otherwise defined, the technical or scientific terms used in this invention should have the ordinary meaning understood by one of ordinary skill in the art to which this invention pertains. The terms "first," "second," and similar terms used in this invention do not indicate any order, quantity, or importance, but are merely used to distinguish different components. Terms such as "comprising" or "including" mean that the element or object preceding the word encompasses the elements or objects listed following the word and their equivalents, without excluding other elements or objects. Terms such as "connected" or "linked" are not limited to physical or mechanical connections, but can include electrical connections, whether direct or indirect. Terms such as "upper," "lower," "left," and "right" are used only to indicate relative positional relationships; when the absolute position of the described object changes, the relative positional relationship may also change accordingly.
[0054] Example:
[0055] Please see Figures 1-9 The present invention provides a technical solution:
[0056] A machine vision-based method for detecting fabric defects, comprising the following steps:
[0057] S1: Acquire grayscale images of the fabric surface, construct a differential filter, calculate the scale gradient magnitude of the image pixels based on the differential filter, use spatial adaptive parameter mapping to map the scale gradient magnitude of the pixels to parameter values, and generate a hybrid filter kernel based on the parameter values to filter the pixels of the image.
[0058] The specific steps for calculating the scale gradient magnitude of image pixels based on the differential filter are as follows:
[0059] A differential filter is defined as a filter set containing three different differential filter structures, namely... of filter, of filter, The Gaussian derivative filter is used; each pixel of the grayscale image is convolved with the horizontal and vertical kernels of the difference filter, and the results are summed to obtain the gradient components in the horizontal and vertical directions of the pixel. Based on the gradient components, the gradient magnitude is calculated using the Pythagorean theorem. The gradient magnitudes generated by the three filters are compared, and the maximum value among the gradient magnitudes is selected as the final scale gradient magnitude of the pixel.
[0060] In the above process, of The filter has high spatial resolution and can sensitively capture local abrupt changes in microscopic defects such as fiber breaks and fine scratches on the fabric surface. of The filter significantly improves the accuracy of gradient direction calculation and excels at detecting low-contrast defects such as slight color differences in textile textures. The Gaussian derivative filter uses a large kernel superimposed with Gaussian smoothing to suppress high-frequency noise, while highlighting macroscopic texture anomalies such as wrinkles through the first derivative. By calculating the gradient magnitudes of the three filters in parallel, the Sobel filter provides the maximum response due to its high sensitivity when there are small cracks in the fabric. If there are large areas of color spots, the Gaussian derivative filter is more reliable in suppressing noise interference. For gradient defects such as bleed defects, the Scharr filter's advantage in mesoscale gradient calculation accuracy is more prominent.
[0061] As long as any filter detects a significant gradient at a certain location, a defect exists. Even if other filters do not respond due to scale mismatch or noise interference, the system can still mark that point as a candidate defect area. There is no need to preset the defect size. The algorithm automatically selects the most sensitive filter based on the actual gradient intensity at each location, avoiding the subjective bias of manually setting the detection scale.
[0062] In the above embodiments, please refer to Figure 2 This represents the original grayscale image of the fabric and the images after processing. of filter, of The gradient magnitudes generated by the filter and the Gaussian derivative filter can be seen from this. The filter has high spatial resolution and can sensitively capture local abrupt changes in microscopic defects such as fiber breaks and fine scratches on the fabric surface. The filter significantly improves the accuracy of gradient direction calculation and is good at detecting low-contrast defects such as slight color difference in textile textures. The Gaussian derivative filter is a large-size kernel superimposed with Gaussian smoothing, which prioritizes the suppression of high-frequency noise, while highlighting macroscopic texture anomalies such as wrinkles through the first derivative.
[0063] Please see Figure 3 , indicating comparison filter, The gradient magnitudes generated by the three filters—the filter, the Gaussian derivative filter, and the filter with the largest gradient magnitude are selected as the final gradient magnitude of the pixel.
[0064] The scale gradient magnitude of a pixel is mapped to a parameter value using spatial adaptive parameter mapping. The specific steps are as follows:
[0065] Based on the scale gradient magnitude of each pixel, a spatial adaptive parameter mapping is established to map the scale gradient magnitude of the pixel to parameter values, specifically:
[0066] Multiply the magnitude of the pixel's scale gradient by an adjustable adaptive weight coefficient, then take its negative value as the input to the exponential function, calculate the value of the exponential function and add a constant 1, divide the sum by a constant 2, and subtract the constant 1 from the quotient. The result is the parameter value that the pixel will ultimately map to.
[0067] The formula upon which the above process is based is:
[0068]
[0069] in, Indicates the adaptive weight parameters; Indicates at pixel point At this point, gradient magnitude The parameter values of the mapping; Indicates the horizontal index of the pixel. Indicates the vertical index of the pixel; Represents pixels Gradient magnitude at;
[0070] In the above formula, a strong response parameter close to 1 is automatically generated in high-gradient regions such as image edges and blemish contours, significantly enhancing the feature signal. In low-gradient regions such as smooth textures and uniform backgrounds, it is mapped to a low response value close to -1, effectively suppressing noise interference. The nonlinear transformation of the function produces smooth, gradual parameters in the region of moderate gradient magnitude, avoiding the abrupt change problem at the threshold of traditional linear mapping and improving the detection accuracy of gradual defects.
[0071] Indicates gradient magnitude The mapping parameter values take values within the following range: The original gradient magnitude is obtained through the S-shaped function. Nonlinear mapping to a normalized range, utilizing the saturation characteristics of the S-curve, suppresses low-gradient noise while amplifying response differences in high-gradient regions, combined with parameters. By dynamically adjusting the steepness of the mapping curve, scene adaptation with gradient sensitivity can be achieved. Increasing the gradient makes the mapping curve approach the step function, thus enhancing the binarization separation effect in high gradient regions. Reducing the size of the map curve results in a smoother curve, preserving more gradient details; Directly reflects the edge intensity of local areas in an image. The larger the amplitude, the larger the exponential term. Approaching 0, A value close to 1 indicates that the higher the edge strength at that location, the greater the probability of the corresponding defect existing.
[0072] Please refer to the above embodiments. Figure 4This represents the establishment of a spatial adaptive parameter mapping based on the scale gradient magnitude of each pixel, which maps the scale gradient magnitude of the pixel to a parameter value distribution map.
[0073] In the above embodiments, 10 sets of data on the gradient magnitude of a pixel and the corresponding mapping parameter values are given to reflect how the mapping parameter values change with the gradient magnitude, as shown in Table 1:
[0074] Table 1: Relationship between gradient magnitude of a pixel and the corresponding mapping parameter value
[0075] As can be seen from Table 1 above, In this case, The larger the amplitude, the larger the exponential term. Approaching 0, Approaching 1 until saturation, it reflects the characteristic of amplifying the response differences in the high gradient region.
[0076] The process of generating a hybrid filter kernel based on parameter values to filter the pixels of the image involves the following steps:
[0077] Based on the parameter values obtained by mapping the gradient magnitude of each pixel in the image, a spatially adaptive hybrid filter kernel is generated. The hybrid filter kernel consists of two parts: one part is the parameter value minus 1 and then multiplied by a 5×5 Gaussian kernel; the other part is the parameter value multiplied by a 5×5 bilateral kernel. The results of these two parts are weighted to obtain the spatially adaptive hybrid filter kernel for each pixel.
[0078] For each pixel in the image, its surrounding pixels are... Each pixel value in the neighborhood is multiplied by the corresponding weight element in a hybrid filter kernel generated based on the pixel. All these weighted pixel values are summed to obtain the new pixel value of the filtered image at the pixel.
[0079] The formula used in the above process is:
[0080]
[0081] in, express Gaussian kernel; express bilateral nuclear; Indicates a pixel The generated spatially adaptive hybrid filter kernel;
[0082] In the above formula, the spatial weighting characteristics of Gaussian filtering are given priority in the low gradient region, and subtle noise is quickly eliminated by fixing the standard deviation. In the high gradient region, the distance and chromatic difference constraints of bilateral filtering are used to suppress impulse noise and avoid edge blurring caused by classical Gaussian filtering.
[0083] Indicates a pixel The generated spatially adaptive hybrid filter kernel, based on The weight ratio of Gaussian smoothing (i.e., noise suppression) to bilateral filtering (i.e., edge preservation) is dynamically adjusted to overcome the limitations of a single filter in fabric defect detection; in high gradient regions... Approaching 1, it emphasizes spatial-luminosity joint filtering with two-sided kernels to avoid blurring defect boundaries, especially in low-gradient regions. Approaching 0, it focuses on the uniform smoothness of the Gaussian kernel, effectively suppressing background texture noise; The larger the value, the stronger the bilateral core. The weights increase linearly, enhancing the edge-preserving properties. The smaller the value, the stronger the Gaussian kernel. weight Increase, dominate noise suppression.
[0084] For each pixel in the image, its surrounding pixels are... Each pixel value in the neighborhood is multiplied by the corresponding weight element in a hybrid filter kernel generated based on the position of the center pixel. All these weighted pixel values are summed to obtain the new pixel value of the filtered image at the center pixel.
[0085] In the above process, a unique feature is dynamically generated for each pixel. A hybrid kernel can be formed based on the gradient features of a pixel. This reflects the adaptive adjustment of the contribution weight of each pixel in the neighborhood, thereby achieving high performance. In other words, in areas with edges or defects, the weights of both sides are strengthened to smooth out noise while preserving sharp edges, and at low... In flat regions, the focus is on Gaussian kernel weights to quickly suppress random noise through uniform weighting, avoiding computational redundancy introduced by edge filtering; The sum of the product of each pixel value in the neighborhood and its corresponding mixing kernel weight is a conditional average of the gray-level distribution in the local area. For the Gaussian weight term, ... It applies a spatial distance attenuation smoothing effect to all pixels in the neighborhood, effectively eliminating high-frequency noise, and is effective for bilateral weighting terms. Furthermore, a brightness similarity constraint is introduced to reduce the weight of pixels with large grayscale differences from the center pixel, thus avoiding edge blurring. The physical meaning of the weighted summation result is: the estimated value of the center pixel after filtering, which suppresses the interference of local anomalies such as isolated noise by linearly combining neighborhood information, while preserving structural texture.
[0086] Please refer to the above embodiments. Figure 5 This indicates that a hybrid filter kernel, generated based on parameter values, is used to filter the pixels of the image. It can be seen that in high gradient regions... Approaching 1, it emphasizes spatial-luminosity joint filtering with two-sided kernels to avoid blurring defect boundaries, especially in low-gradient regions. Approaching 0, it focuses on the uniform smoothness of the Gaussian kernel, effectively suppressing background texture noise.
[0087] S2: Based on the filtered image, a dual-stream feature extraction model is constructed to extract microscopic feature maps and texture feature maps. Based on the microscopic feature maps and texture feature maps, channel attention weight coefficients are constructed by channel splicing, and spatial attention weight coefficients of each pixel are generated at the same time. The channel attention weight coefficients and the spatial attention weight coefficients of the pixels are fused at multiple scales to generate a fused feature map.
[0088] The specific steps for constructing the dual-stream feature extraction model to extract microscopic feature maps and texture feature maps are as follows:
[0089] Based on the filtered grayscale image, a two-stream parallel structure is used to extract microscopic feature maps and texture feature maps. One branch is used to extract the microscopic feature maps. The processing flow of this branch is as follows: the filtered grayscale image is processed using a batch normalization process, and then passed through a... The activation function introduces non-linear results, and finally a convolutional kernel with 64 output channels and a size of [missing information] is used. Feature extraction is performed on the convolutional layers to obtain micro-feature maps;
[0090] Another branch is used to extract the texture feature map of the image. The processing flow of this branch is to first perform batch normalization on the filtered grayscale image and... The activation function introduces a non-linear result, which is then processed by a convolutional kernel with 32 output channels and a kernel size of [missing information]. The convolutional layer performs initial feature capture, and the captured result is then passed through a pooling window. The max pooling operation is used for downsampling, and the final output is a texture feature map.
[0091] In the above process, the batch normalization process and the extraction of micro-feature maps are included. The activation function introduces nonlinear results to accelerate model convergence, suppress gradient anomalies, and simultaneously introduce nonlinear expressive power. The convolutional layers capture high-frequency details in the local neighborhood, such as abrupt edge changes and subtle grayscale variations, making them suitable for detecting small-sized defect features. The 64 output channels are designed to enhance the representation of complex microstructures through multi-channel feature mapping, covering defect patterns of different shapes. Through the above process, the detection of small defects is improved, while noise interference is suppressed, the spatial location accuracy of defects is preserved, and high-resolution features are provided for subsequent segmentation or localization.
[0092] In the extraction of texture feature maps, the following methods are used: The convolutional layers are designed to expand the receptive region to capture long-range spatial correlations and identify local regions with texture repetition anomalies. These layers are then passed through a pooling window... Max pooling is used to downsample and enhance translation invariance to texture changes while reducing computational complexity. The 32 output channels are designed to balance feature dimensionality and computational efficiency, extracting abstract representations of key texture patterns such as direction and frequency. Through the above process, detection of large-scale irregular regions is achieved. Pooling operations reduce the impact of local noise or slight deformation, improving adaptability to changes in lighting and fabric stretching.
[0093] Please refer to the above embodiments. Figure 6 This refers to constructing a dual-stream feature extraction model based on the filtered image to extract microscopic feature maps, texture feature maps, and the fused feature map.
[0094] In the dual-stream feature extraction model, microscopic feature maps and texture feature maps constitute multi-scale perception, covering the full range of detection needs from pixel-level defects to regional-level texture destruction. The microscopic branch preserves high-resolution details, while the texture branch reduces the amount of data through pooling. The two are parallelized to reduce redundant computation.
[0095] The specific steps for generating the fused feature map are as follows:
[0096] The extracted micro-feature maps and texture feature maps are concatenated along the channel dimension to form a concatenated feature map. Global average pooling is performed on the feature map to obtain a set of description vectors. The description vectors are input into a multilayer perceptron. The output of the multilayer perceptron is then mapped through an sigmoid function to finally generate a channel attention weight coefficient between 0 and 1.
[0097] The extracted microscopic feature maps and texture feature maps are added element-wise to form a composite feature map, which is then input into... The convolution kernel performs calculations, mapping multi-channel features to a single-channel spatial response map. It performs an exponential operation on each pixel value of the spatial response map, globally sums the values of all pixels after exponential amplification, and divides the exponentialized result of each pixel by the result of the global sum to generate the spatial attention weight coefficient at the pixel.
[0098] The generation of intermediate feature maps mainly consists of two parts: the first part is to multiply the spatial attention weight coefficient corresponding to each pixel with the micro feature map element by element; the second part is to subtract the spatial attention weight coefficient from 1 and then multiply the result with the texture feature map element by element. The two parts are weighted and combined to form an intermediate feature map. The obtained channel attention weight coefficient is then multiplied with the intermediate feature map to generate the fused feature map.
[0099] The formula upon which the above process is based is:
[0100]
[0101] in, Indicates global average pooling; This represents a multilayer perceptron; Indicates a channel splicing operation; The channel attention weight coefficients of the image; This represents the microscopic feature map of the filtered image; This represents the texture feature map of the filtered image;
[0102] In the above process, The channel attention weight coefficients represent the differences in importance of different feature channels in the defect detection task, quantifying the contribution weights of each channel in the microscopic feature map and the texture feature map; the complementary information of microscopic and texture features is fused by the splicing operation to improve the ability to distinguish defects at multiple scales; global average pooling is used to compress the spatial dimension and extract global statistics of the channel dimension to characterize the overall importance of each channel; multilayer perceptron is used to learn the nonlinear interaction relationship between channels and generate a channel importance score that matches the task objective, i.e., defect detection.
[0103] The filtered micro-feature map and texture feature map Channel dimensions are concatenated to form a fused feature tensor, preserving the spatial correspondence between the two types of features. Global average pooling is then used to spatially compress the two-dimensional features of each channel, generating a channel description vector, which is then input into the multilayer perceptron. Perform nonlinear transformation, for Output application Functions that map numerical values to interval;
[0104] when When the output value increases, When the value approaches 1, the corresponding channel is significantly enhanced. When the output value decreases, As the value approaches 0, the corresponding channel is actively suppressed. The formula maps the output of the multilayer perceptron to a value using the sigmoid function. In this process, it is ensured that the weight coefficients are monotonically positively correlated with the importance of the input features and have normalization properties.
[0105] Generate spatial attention weights for each pixel:
[0106]
[0107] in, Indicates the height of the image; Indicates the width of the image; Represents the input feature map After using a convolution kernel of size 1×1, at pixel point Output value at; This indicates element-wise addition; Represents pixels Spatial attention weight coefficients at the location;
[0108] In the above process, This method is used to characterize the differences in importance of different spatial locations in an image for defect detection tasks. It dynamically focuses on high-defect areas through weight coefficients, suppressing interference from background or uniform texture areas. Based on the element-wise addition of micro-feature maps and texture feature maps, it captures spatial anomaly patterns under the combined effect of the two. A 1×1 convolutional kernel maps multi-channel features to a single-channel spatial response map through linear transformation, quantifying the significance of each location. That is, defective areas have higher response values. The response differences are amplified exponentially, and then a probability distribution is generated by global normalization, i.e., summing the denominators, to strengthen the weight of key areas. When the output value increases, the growth rate of the numerator exponent term is higher than that of the sum of the denominators, leading to... Significant increase;
[0109] Based on the channel attention weight coefficients and the spatial attention weight coefficients of pixels, a fused feature map is generated:
[0110]
[0111] in, This indicates element-wise multiplication; This represents the fused feature map.
[0112] In the above process, The adaptive fusion results of multi-scale features across channel and spatial dimensions are represented. By dynamically balancing the contribution ratio of microscopic feature maps and texture feature maps, optimized feature representations for defect detection are generated. Channel weights are utilized. Strengthen key channels, spatial weight Focusing on areas of defects, we achieve a refined integration of local details and overall structure; The larger the value, the more the characteristic response of the corresponding channel is amplified in the fusion result, thus enhancing the contribution of the key channel to the detection task. The larger, In position The higher the contribution percentage, the better. The smaller, In position The higher the contribution percentage, the more effective the linear combination becomes. Achieve spatially adaptive feature fusion, and then through Channel dimension calibration is performed, and the weight coefficients are monotonically correlated with the feature response intensity.
[0113] S3: Based on the fused feature map, a convolutional network is used to construct a heatmap of the feature map. Based on the mean and variance of the pixels in the heatmap, an adaptive threshold is constructed. Binarization is then used to compare the heatmap pixels of each pixel with the adaptive threshold to generate a binarized image of the defect and trigger an alarm.
[0114] The specific steps for constructing a heatmap of an image using a convolutional network are as follows:
[0115] Based on the fused feature map, a convolutional kernel with a size of [size missing] is used. A convolutional layer with 32 output channels performs feature transformation, followed by... The activation function introduces non-linearity, and finally a convolution kernel with a size of [size missing] is used. The convolutional layer with 1 output channel is subjected to channel compression and aggregation to generate a heatmap for highlighting key areas.
[0116] In the above process, the kernel size is The convolutional layer with 32 output channels preserves the spatial structure of the fused feature map while refining the edges and texture details of key regions, such as the boundaries of defects, through the local receptive field. At the same time, it expands the number of channels to 32 to improve the expressive power of the features. The activation function is used to introduce non-linear mapping, enhancing the model's ability to model complex feature patterns non-linearly, such as distinguishing subtle differences between defects and background, while suppressing negative response noise; a convolutional kernel with a size of [missing information] is used. The convolutional layer with a single output channel performs channel compression and aggregation to compress 32-channel features into a single channel. By aggregating cross-channel information, it highlights spatial responses strongly correlated with defect detection, generating an interpretable heatmap. High activation value regions correspond to locations with higher defect probabilities. Furthermore, the generated heatmap provides high-resolution guidance for defect localization. The heatmap directly maps the defect confidence of each region in the input image through the strength of single-channel activation values. High activation regions, such as values close to 1, correspond to potential defect locations such as fabric tears or stains, making the detection logic transparent.
[0117] Please refer to the above embodiments. Figure 7 This represents a heatmap of fabric with defects. The heatmap directly maps the defect confidence of each region in the input image by the strength of the single-channel activation value. High activation regions, such as values close to 1, correspond to potential defect locations such as fabric tears or stains, making the detection logic transparent.
[0118] The specific steps for generating the binarized image of the defect are as follows:
[0119] Based on the generated heatmap, the average and standard deviation of all pixel values in the heatmap are calculated. Then, combined with an empirical coefficient determined by expert experience, the product of the average, the empirical coefficient, and the standard deviation is added to form an adaptive judgment threshold. Specifically:
[0120]
[0121] in, Indicates the threshold for judgment; This represents the average pixel value of the heatmap; This represents the standard deviation of the pixel values in the heatmap. Represents the empirical coefficient;
[0122] Among them, the empirical coefficient Through expert analysis of historical defect samples, high-contrast defect sampling was obtained. Low contrast defect sampling ;
[0123] Traverse each pixel in the heatmap and compare its pixel value with the calculated adaptive threshold. If the value of the pixel is greater than or equal to the threshold, mark it as 1 at the corresponding binary mask position to indicate that there is a defect. Otherwise, mark it as 0 to indicate that the area is normal.
[0124] In the above process, the mean value of the heatmap pixel values reflects the overall activation intensity and the standard deviation reflects the dispersion, thus quantifying the global distribution characteristics of the heatmap. The threshold offset is dynamically adjusted by combining expert experience coefficients. For example, increasing the coefficient can suppress noise interference, while decreasing the coefficient can enhance the response to weak defects. This avoids the failure of fixed thresholds in different scenarios such as changes in lighting and differences in texture complexity, thus improving the generalization of the algorithm. Each pixel value of the heatmap is compared with the adaptive threshold to generate a high-resolution binary mask, preserving the fine-grained spatial structure of the defect area and avoiding information loss caused by global pooling or downsampling.
[0125] The specific steps for outputting the fabric detection results based on the binarized image of the defects are as follows:
[0126] Based on the binarized image, count the number of pixels marked as 1 and calculate their proportion of all pixels. If the proportion exceeds a preset proportion threshold, the area represented by the binarized image is determined to be a defective area.
[0127] In the above process, fabric defects such as holes, stains, and broken yarns usually form continuous or dense abnormal areas in local areas, while normal texture noise such as natural fiber undulations and tiny reflections are mostly isolated and scattered pixels. The existence of defects will inevitably lead to the physical coverage of abnormal areas. The area ratio directly quantifies the severity of the defect and directly counts the number of pixels, which is simpler and easier to operate.
[0128] The preset proportion threshold is obtained by collecting 5,000 normal fabric images, statistically analyzing the distribution of abnormal pixel proportions, calculating the mean and variance of the abnormal pixel proportions of all images, and using the sum of the mean plus 6 times the standard deviation as the preset proportion threshold. The multiplication by 6 is to expand the statistical coverage, buffer the risk of misjudgment caused by non-ideal distributions such as light fluctuations and fiber reflections, and minimize the possibility of normal textures being misjudged as defects.
[0129] Please refer to the above embodiments. Figure 8 This means comparing each pixel value of the heatmap with an adaptive threshold to generate a high-resolution binary mask image.
[0130] Please see Figure 10 The present invention also provides a machine vision-based fabric defect detection device, the detection device being used to perform the above-described detection method, comprising:
[0131] The data acquisition module is used to acquire grayscale images of the fabric surface, construct a differential filter, calculate the scale gradient magnitude of the image pixels based on the differential filter, use spatial adaptive parameter mapping to map the scale gradient magnitude of the pixels to parameter values, and generate a hybrid filter kernel based on the parameter values to filter the pixels of the image.
[0132] The feature fusion module is used to construct a dual-stream feature extraction model based on the filtered image to extract microscopic feature maps and texture feature maps. Based on the microscopic feature maps and texture feature maps, channel attention weight coefficients are constructed by channel splicing, and spatial attention weight coefficients of each pixel are generated at the same time. The channel attention weight coefficients and the spatial attention weight coefficients of the pixels are fused at multiple scales to generate a fused feature map.
[0133] The defect detection module is used to construct a heatmap of the feature map based on the fused feature map using a convolutional network. Based on the mean and variance of the pixels in the heatmap, an adaptive threshold is constructed. Binarization processing is used to compare the heatmap pixels of each pixel with the adaptive threshold to generate a binary image of the defect. The detection result of the fabric is output based on the binary image of the defect.
[0134] The above formulas are all dimensionless calculations. The formulas are derived from software simulations based on a large amount of collected data to obtain the most recent real-world results. The preset parameters in the formulas are set by those skilled in the art according to the actual situation.
[0135] The above embodiments can be implemented, in whole or in part, by software, hardware, firmware, or any other combination thereof. When implemented in software, the above embodiments can be implemented, in whole or in part, as a computer program product. Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented by electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution.
[0136] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment, depending on actual needs.
[0137] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application.
Claims
1. A machine vision-based method for detecting fabric defects, characterized in that the steps include... include: S1: Acquire grayscale images of the fabric surface, construct a differential filter, calculate the scale gradient magnitude of the image pixels based on the differential filter, use spatial adaptive parameter mapping to map the scale gradient magnitude of the pixels to parameter values, and generate a hybrid filter kernel based on the parameter values to filter the pixels of the image. S2: Based on the filtered image, a dual-stream feature extraction model is constructed to extract microscopic feature maps and texture feature maps. Based on the microscopic feature maps and texture feature maps, channel attention weight coefficients are constructed by channel splicing, and spatial attention weight coefficients of each pixel are generated at the same time. The channel attention weight coefficients and the spatial attention weight coefficients of the pixels are fused at multiple scales to generate a fused feature map. S3: Based on the fused feature map, a convolutional network is used to construct a heatmap of the feature map. Based on the mean and variance of the pixels in the heatmap, an adaptive threshold is constructed. Binarization is used to compare the heatmap pixels of each pixel with the adaptive threshold to generate a binarized image of the defect. The detection result of the fabric is output based on the binarized image of the defect. The extracted micro-feature maps and texture feature maps are concatenated along the channel dimension to form a concatenated feature map. Global average pooling is performed on the feature map to obtain a set of description vectors. The description vectors are input into a multilayer perceptron. The output of the multilayer perceptron is then mapped through an sigmoid function to finally generate a channel attention weight coefficient between 0 and 1. The extracted microscopic feature maps and texture feature maps are added element-wise to form a composite feature map, which is then input into... The convolution kernel performs calculations, mapping multi-channel features to a single-channel spatial response map. It performs an exponential operation on each pixel value of the spatial response map, globally sums the values of all pixels after exponential amplification, and divides the exponentialized result of each pixel by the result of the global sum to generate the spatial attention weight coefficient at the pixel. The generation of intermediate feature maps mainly consists of two parts: the first part is to multiply the spatial attention weight coefficient corresponding to each pixel with the micro feature map element by element; the second part is to subtract the spatial attention weight coefficient from 1 and then multiply the result with the texture feature map element by element. The two parts are weighted and combined to form an intermediate feature map. The obtained channel attention weight coefficient is then multiplied with the intermediate feature map to generate the fused feature map.
2. The machine vision method for detecting fabric defects according to claim 1, characterized in that, The specific steps for calculating the scale gradient magnitude of image pixels based on the differential filter are as follows: A differential filter is defined as a filter set containing three different differential filter structures, namely... of filter, of filter, The Gaussian derivative filter is used; each pixel of the grayscale image is convolved with the horizontal and vertical kernels of the difference filter, and the results are summed to obtain the gradient components in the horizontal and vertical directions of the pixel. Based on the gradient components, the gradient magnitude is calculated using the Pythagorean theorem. The gradient magnitudes generated by the three filters are compared, and the maximum value among the gradient magnitudes is selected as the final scale gradient magnitude of the pixel.
3. The machine vision method for detecting fabric defects according to claim 2, characterized in that, The specific steps for mapping the scale gradient magnitude of a pixel to parameter values using spatial adaptive parameter mapping are as follows: Based on the scale gradient magnitude of each pixel, a spatial adaptive parameter mapping is established to map the scale gradient magnitude of the pixel to parameter values, specifically: Multiply the magnitude of the pixel's scale gradient by an adjustable adaptive weight coefficient, then take its negative value as the input to the exponential function, calculate the value of the exponential function and add a constant 1, divide the sum by a constant 2, and subtract the constant 1 from the quotient. The result is the parameter value that the pixel will ultimately map to.
4. The machine vision method for detecting fabric defects according to claim 3, characterized in that, The process of generating a hybrid filter kernel based on parameter values to filter the pixels of the image involves the following steps: Based on the parameter values obtained by mapping the gradient magnitude of each pixel in the image, a spatially adaptive hybrid filter kernel is generated. The hybrid filter kernel consists of two parts: one part is the parameter value minus 1 and then multiplied by a 5×5 Gaussian kernel; the other part is the parameter value multiplied by a 5×5 bilateral kernel. The results of these two parts are weighted to obtain the spatially adaptive hybrid filter kernel for each pixel. For each pixel in the image, its surrounding pixels are... Each pixel value in the neighborhood is multiplied by the corresponding weight element in a hybrid filter kernel generated based on the pixel. All these weighted pixel values are summed to obtain the new pixel value of the filtered image at the pixel.
5. The machine vision method for detecting fabric defects according to claim 1, characterized in that, The specific steps for constructing the dual-stream feature extraction model to extract microscopic feature maps and texture feature maps are as follows: Based on the filtered grayscale image, a two-stream parallel structure is used to extract microscopic feature maps and texture feature maps. One branch is used to extract the microscopic feature maps. The processing flow of this branch is as follows: the filtered grayscale image is processed using a batch normalization process, and then passed through a... The activation function introduces non-linear results, and finally a convolutional kernel with 64 output channels and a size of [missing information] is used. Feature extraction is performed on the convolutional layers to obtain micro-feature maps; Another branch is used to extract the texture feature map of the image. The processing flow of this branch is to first perform batch normalization on the filtered grayscale image and... The activation function introduces a non-linear result, which is then processed by a convolutional kernel with 32 output channels and a kernel size of [missing information]. The convolutional layer performs initial feature capture, and the captured result is then passed through a pooling window. The max pooling operation is used for downsampling, and the final output is a texture feature map.
6. The machine vision method for detecting fabric defects according to claim 1, characterized in that, The specific steps for constructing a heatmap of an image using a convolutional network are as follows: Based on the fused feature map, a convolutional kernel with a size of [size missing] is used. A convolutional layer with 32 output channels performs feature transformation, followed by... The activation function introduces non-linearity, and finally a convolution kernel with a size of [size missing] is used. The convolutional layer with 1 output channel is subjected to channel compression and aggregation to generate a heatmap for highlighting key areas.
7. The machine vision method for detecting fabric defects according to claim 6, characterized in that, The specific steps for generating a binarized image of the blemish are as follows: Based on the generated heatmap, the average value and standard deviation of all pixel values in the heatmap are calculated. Then, combined with the empirical coefficient, the average value is added to the product of the empirical coefficient and the standard deviation to form an adaptive judgment threshold. Iterate through each pixel in the heatmap and compare its pixel value with the calculated adaptive threshold; If the value of the point is greater than or equal to the threshold, it is marked as 1 at the corresponding binarization mask position; otherwise, it is marked as 0, generating a binarized image of the defect.
8. The machine vision method for detecting fabric defects according to claim 7, characterized in that, The specific steps for outputting the fabric detection results based on the binarized image of the defects are as follows: Based on the binarized image, count the number of pixels marked as 1 and calculate their proportion of all pixels. If the proportion exceeds a preset proportion threshold, the area represented by the binarized image is determined to be a defective area.
9. A machine vision-based fabric defect detection device, characterized in that: The detection device is used to perform the detection method according to any one of claims 1-8, including: The data acquisition module is used to acquire grayscale images of the fabric surface, construct a differential filter, calculate the scale gradient magnitude of the image pixels based on the differential filter, use spatial adaptive parameter mapping to map the scale gradient magnitude of the pixels to parameter values, and generate a hybrid filter kernel based on the parameter values to filter the pixels of the image. The feature fusion module is used to construct a dual-stream feature extraction model based on the filtered image to extract microscopic feature maps and texture feature maps. Based on the microscopic feature maps and texture feature maps, channel attention weight coefficients are constructed by channel splicing, and spatial attention weight coefficients of each pixel are generated at the same time. The channel attention weight coefficients and the spatial attention weight coefficients of the pixels are fused at multiple scales to generate a fused feature map. The defect detection module is used to construct a heatmap of the feature map based on the fused feature map using a convolutional network. Based on the mean and variance of the pixels in the heatmap, an adaptive threshold is constructed. Binarization processing is used to compare the heatmap pixels of each pixel with the adaptive threshold to generate a binary image of the defect. The detection result of the fabric is output based on the binary image of the defect.