Wood surface defect detection method based on feature memory bank and state space

CN122573969BActive Publication Date: 2026-09-11QILU UNIVERSITY OF TECHNOLOGY (SHANDONG ACADEMY OF SCIENCES) +2
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202611054863.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2026-07-16
Publication Date
2026-09-11
Estimated Expiration
2046-07-16

AI Technical Summary

Technical Problem

该类方法降低了对缺陷样本的依赖,但在木材表面缺陷检测场景中仍存在不足:一方面,常用卷积神经网络特征提取器主要依赖局部感受野,对木材纹理走向和长距离连续性建模不足;另一方面,正常木材区域本身存在亮度、对比度和局部灰度波动差异,若直接对全部正常特征统一压缩,容易造成部分正常纹理模式覆盖不足;此外,直接以最近邻距离或固定阈值作为异常判定依据,容易受到不同木材样本自然纹理波动和成像差异的影响,导致误检或漏检

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122573969B_ABST
    Figure CN122573969B_ABST
Patent Text Reader

Abstract

The present application belongs to the technical field of defect detection, and particularly relates to a wood surface defect detection method based on a feature memory bank and a state space. The method adopts state space visual encoder feature extraction, local perception patch feature construction, normal feature memory bank compression based on gray scale statistical grouping, and abnormal score calibration based on local perception patch reference distance distribution, etc. technologies, realizes abnormal detection, spatial positioning and visual output of wood surface defects. Based on the normal feature memory bank, the abnormal detection is carried out without relying on a large number of defect samples for classification or segmentation supervision training, and only the high-confidence normal area is screened by using the defect label in the library building stage, so that the defect sample size and the class integrity requirement are reduced. Through the cooperation of the above technologies, the present application can reduce the dependence on a large number of defect samples and fine annotation, and improve the accuracy, stability and positioning ability of defect detection in a complex wood grain background.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of defect detection technology, and particularly relates to a method for detecting defects on the surface of wood based on a feature memory library and a state space. Background Technology

[0002] Wood, as an important natural material, is widely used in construction, decoration, furniture manufacturing, packaging, and industrial processing. The surface quality of wood products directly affects their mechanical properties, service life, and appearance. Therefore, accurate and stable detection of surface defects in wood is of great significance during wood processing and production. However, due to differences in wood growth environment and processing conditions, wood surfaces often have various defects such as dead knots, live knots, bark, cracks, notches, dry scars, rotten heartwood, debarking, and burrs. These defects often manifest as localized abnormal textures, characterized by irregular shapes, large size variations, and high similarity to normal wood grain, posing a significant challenge to automated detection.

[0003] Current methods for detecting surface defects in wood still involve both manual visual inspection and machine vision inspection. Manual inspection is inefficient, and the results are easily affected by the inspector's experience, subjective judgment, and working conditions, making it difficult to guarantee consistency and stability. With the increasing automation in the wood processing industry, traditional manual inspection methods can no longer meet the real-time and reliability requirements of production lines.

[0004] To improve detection efficiency, some research and engineering practices have begun to introduce machine vision-based methods for detecting defects on wood surfaces. Early methods mostly used traditional image processing techniques such as grayscale analysis, edge detection, and texture feature extraction to identify defect areas. However, the texture structure of wood surfaces is complex and diverse, and normal wood grain exhibits significant long-distance continuity in space. Traditional methods based on local rules or fixed thresholds are easily affected by changes in lighting, texture interference, and imaging angle, making it difficult to achieve stable detection under complex conditions.

[0005] In recent years, with the development of deep learning technology, methods for detecting surface defects in wood based on convolutional neural networks have gradually attracted attention. Classification network-based methods can determine the presence of defects in an image or identify defect categories, but they typically only provide image-level results and struggle to accurately locate defect regions. Object detection network-based methods can output defect categories and approximate locations through bounding boxes, but require a large number of bounding box annotation samples, and their accuracy in locating irregularly shaped defects such as cracks, bark, and burrs is limited. Semantic segmentation network-based methods can obtain pixel-level defect contours, but require costly pixel-level annotation and are highly dependent on the quantity and quality of defect samples. Most of these methods rely on supervised training using defect samples; when defect types are incomplete, defect samples are scarce, or unknown defects are present, the model's generalization ability is easily limited.

[0006] To address the challenge of comprehensively acquiring defect samples, anomaly detection methods have been introduced into the field of industrial surface defect detection. These methods typically establish a normal feature distribution based on normal samples and determine whether the sample to be tested deviates from this distribution during the detection phase. Common methods include feature reconstruction, density estimation, and memory matching. Among these, memory-based anomaly detection methods acquire local features of normal samples through feature extraction networks and store these features, representing the normal texture distribution, in a feature memory. During detection, the features of the sample to be tested are matched with the features in the memory, and the degree of anomaly is determined by the distance. While this approach reduces reliance on defect samples, it still has shortcomings in wood surface defect detection scenarios: Firstly, commonly used convolutional neural network feature extractors primarily rely on local receptive fields, resulting in insufficient modeling of wood texture direction and long-distance continuity. Secondly, normal wood areas inherently exhibit differences in brightness, contrast, and local grayscale fluctuations; directly compressing all normal features uniformly can easily lead to insufficient coverage of some normal texture patterns. Furthermore, directly using nearest neighbor distance or a fixed threshold as the basis for anomaly judgment is easily affected by natural texture fluctuations and imaging differences among different wood samples, leading to false positives or false negatives.

[0007] Chinese patent document CN113610831A discloses a wood defect detection method based on computer image technology and transfer learning. This method first preprocesses wood image data, then constructs a multi-layer convolutional neural network transfer learning model, and uses this model to train the preprocessed data. Next, the trained data is processed to obtain new image data, which is then input into the transfer learning model for further training to obtain a critical value. Subsequently, defect features are searched on the new image data to construct a computer image technology model, and the defect features are quantified. Finally, the critical value is combined with a defect classification threshold to obtain a wood defect detection model, thereby achieving wood defect detection. However, this method mainly relies on the transfer learning model and defect classification threshold to complete wood defect identification. Essentially, it still needs to learn defect features from existing defect samples and use the critical value or classification threshold for judgment. When there are many types of wood surface defects, irregular defect shapes, insufficient defect samples, or unknown defects not appearing in the training data, the model is easily affected by the completeness of the training sample categories and the threshold setting, resulting in limited generalization ability. Meanwhile, this method mainly identifies defects from the perspective of image-level or defect feature classification. Therefore, in complex wood grain backgrounds, there may still be problems such as normal textures being misjudged as defects or small defects being missed.

[0008] Therefore, existing methods for detecting wood surface defects are still insufficient to fully adapt to real-world scenarios where wood grain is complex, normal areas vary greatly, defect morphology is irregular, and it is difficult to obtain comprehensive samples of unknown defects. It is still necessary to propose a method for detecting wood surface defects that can improve the accuracy, stability, and localization capabilities of defect detection against complex wood grain backgrounds. Summary of the Invention

[0009] To address the aforementioned technical problems, this invention provides a method for detecting wood surface defects based on a feature memory and state space. The method is based on the PatchCore anomaly detection framework, introduces a Mambavision encoder as a feature extraction network, and combines local perceptual patch feature construction, normal feature memory compression based on gray-level statistical grouping, and anomaly scoring calibration based on patch reference distance distribution to achieve the detection and localization of wood surface defects.

[0010] To achieve the above objectives, this invention provides a method for detecting wood surface defects based on a feature memory library and a state space, the technical solution of which is as follows: A method for detecting wood surface defects based on feature memory and state space includes the following steps: S1. Filter the original timber images in the dataset using defect region markers to construct a database sample; preprocess the database sample to obtain a set of normal database samples. The images to be detected are preprocessed to obtain standardized samples for detection. S2, Database Construction Phase: The pre-trained Mambavision encoder is used to process the normal library construction sample set in step S1. Feature extraction is performed to obtain hierarchical feature maps. After spatial alignment of the hierarchical feature maps, aligned feature maps are obtained. A local perceptual patch feature set is constructed in the aligned feature map space. Based on gray-level statistical grouping, the constructed local perceptual patch feature set is grouped to form a compressed normal feature memory. ; S3. Detection Stage: The standardized test samples obtained in step S1 are input into the pre-trained Mambavision encoder to generate a test patch feature set; based on the normal feature memory constructed in step S2, the nearest neighbor anomaly score is performed on the test patch feature vectors in the test patch feature set, and a calibration anomaly risk value is output; the anomaly risk values ​​of each test patch feature vector are reorganized according to their spatial position in the test sample to generate an anomaly risk map located in the feature map space; the anomaly risk map is post-processed to output a defect segmentation map; after anomaly region integration, the overall anomaly distribution result of the test sample is output.

[0011] Preferably, step S1 includes: S11. Extract the target wood region from the original wood images in the dataset to obtain the effective wood region image; S12. Mark the defect areas in the effective area image of the wood and extract the normal areas as samples for library construction. S13. Perform size normalization and data augmentation on the normal region samples in the database construction sample, and output the standardized normal region samples, i.e., the normal database construction sample set. The target wood region is extracted from the image to be detected to obtain the effective region image to be detected. The size of the effective region image to be detected is normalized, and the standardized sample to be detected is output.

[0012] Preferably, in step S12, defect regions are marked on the effective area image of the wood to form a defect-marked effective area image of the wood. Local candidate regions are extracted on the defect-marked effective area image of the wood using a sliding window method. For each local candidate region, if it overlaps with any defect region or the distance between it and the boundary of the defect region is less than a preset safety interval, it is removed. If it does not overlap with any defect region and has no occlusion, no severe reflection, and no exposure abnormality, it is retained as a normal region. The set of normal regions formed is used as a sample for building a normal feature memory library.

[0013] Preferably, in step S2, the pre-trained Mambavision encoder is used to process the normal library construction sample set from step S1. Feature extraction is performed to obtain hierarchical feature maps; the generated second-stage and third-stage feature maps are spatially aligned to obtain aligned feature maps. Where H′ represents the spatial dimension of the aligned feature map in the height direction, W′ represents the spatial dimension of the aligned feature map in the width direction, and C′ represents the number of channels of the aligned feature map.

[0014] Preferably, step S2, which involves constructing a locally perceptual patch feature set in the aligned feature map space, specifically includes: For each spatial location in the aligned feature map Construct its local neighborhood: (1) Where h represents the coordinates of the current center position in the height direction of the aligned feature map, w represents the coordinates of the current center position in the width direction of the aligned feature map; a represents the coordinates of a position in the local neighborhood in the height direction, b represents the coordinates of a position in the local neighborhood in the width direction, and p represents the size of the local neighborhood window.

[0015] Local aggregation is performed on the feature regions of the local neighborhood of each spatial location to obtain the corresponding local sensing patch feature vector; the local sensing patch feature vector is then L2 normalized to obtain the normalized local sensing patch feature vector.

[0016] The normalized local sensing Patch feature vectors corresponding to all spatial locations are combined to form the local sensing Patch feature set, denoted as: (2) in, Indicates the first A normalized local perceptual patch feature vector This indicates the number of patch features generated on the aligned feature map.

[0017] Preferably, step S2, which involves compressing the memory based on grayscale statistical grouping, groups the constructed local perceptual patch feature set to form a compressed normal feature memory. The details are as follows: (2) Based on the spatial position of each normalized local perceptual patch feature vector in the aligned feature map F, extract the corresponding local image region in the normalized normal region sample, and perform grayscale processing on the local image region to obtain the corresponding grayscale map; calculate the grayscale mean and grayscale standard deviation of the grayscale map; (2) Based on the calculated gray mean and gray standard deviation, the normalized local perception Patch feature vector is divided into different gray statistical groups; (3) The normalized local perception Patch feature vectors in each group are sampled from the core set, and the representative normalized local perception Patch feature vectors of each group are selected to form the representative feature subsets of the corresponding groups. (4) Combine the representative feature subsets obtained from each group to form a compressed normal feature memory.

[0018] More preferably, step (1), calculating the grayscale mean and grayscale standard deviation of the grayscale image, specifically involves: (3) (4) Among them, I i (x, y) represents the grayscale image I corresponding to the i-th normalized local perceptual patch feature vector. i The grayscale value at pixel coordinates (x, y), where x represents the pixel coordinate in the height direction of the grayscale image, y represents the pixel coordinate in the width direction of the grayscale image, and h... i and wi These represent the height and width of the grayscale image, respectively, in μ. i This represents the average gray level of the local image region corresponding to the i-th local perceptual patch feature vector; It represents the grayscale standard deviation of the local image region corresponding to the feature vector of the i-th local perceptual patch.

[0019] Step (2) is based on the grayscale mean. and grayscale standard deviation The normalized local perception patch feature vectors are divided into 4 groups; let the median grayscale mean of the grayscale image of the local wood region corresponding to the normalized local perception patch feature vector be... The median standard deviation of grayscale is The grouping rules are as follows: Corresponds to the low brightness - low contrast group; Corresponds to the low brightness - high contrast group; Corresponds to the high brightness - low contrast group; Corresponding to the high brightness-high contrast group.

[0020] In step (3), let the gray-level statistical group be numbered k, where k = 1, 2, 3, 4. The normalized local perceptual patch feature vectors contained in the k-th gray-level statistical group form the local perceptual patch feature set of the k-th gray-level statistical group, denoted as P. k : (5) Among them, P k p represents the normalized local perceptual patch feature set corresponding to the k-th gray-level statistical group. k , j Let n represent the j-th normalized local perceptual patch feature vector in the k-th gray-level statistical group. k This represents the number of normalized local perceptual patch feature vectors in the k-th gray-level statistical group.

[0021] The number of features retained in each group is calculated using the following formula: (6) in, The number of normalized local perceptual patch feature vectors in the k-th gray-level statistical group; Then, core set sampling is performed within each gray-level statistical group according to the feature space distance to obtain the representative feature subset of the k-th gray-level statistical group: (7) Among them, M k Let m represent the representative feature subset corresponding to the k-th gray-level statistical group. k , Let m represent the r-th representative normalized local perceptual patch feature vector selected from the k-th gray-level statistical group. k This represents the number of representative features that are pre-selected and retained in the k-th gray-scale statistical group; The representative feature subsets of each gray-level statistical group are merged to obtain the compressed normal feature memory: (8) in, This represents the compressed normal feature memory. This indicates the number of grayscale statistical groups.

[0022] Preferably, the nearest neighbor anomaly scoring in step S3 includes the calculation of the original anomaly score based on the nearest neighbor distance and the calibration of the anomaly score based on the reference distance distribution of the normalized local perception patch feature vector.

[0023] More preferably, step S3, which involves performing nearest-neighbor anomaly scoring on the feature vectors of the patch to be tested in the feature set of the patch to be tested and outputting a calibration anomaly risk value, specifically includes: For the The normalized feature vector of each patch to be tested, and its original anomaly score is defined as follows: (9) in, Represents the Euclidean distance function. Indicates the first The normalized feature vectors of each test patch. This represents the j-th feature vector in the compressed normal feature memory; Using the normalized local perceptual patch feature vectors that were not used in constructing the normal feature memory in step S2, their relative position to the normal feature memory is calculated based on the nearest neighbor distance method. The nearest neighbor distance is used to obtain the normal reference distance set: (10) in, Indicates the first The nearest neighbor distance of a normalized local perceptual patch feature vector relative to the compressed normal feature memory. This represents the number of normalized local perceptual patch feature vectors that did not participate in the construction of the normal feature memory.

[0024] For any test patch feature vector, the original anomaly score According to its normal reference distance set The location of the value is used to calculate the calibration anomaly risk value. : (11) in, This is an indicator function that takes the value 1 if the condition in parentheses is true, and 0 otherwise.

[0025] Compared with the prior art, the present invention has the following beneficial effects: (1) This invention employs techniques such as state-space visual encoder feature extraction, local perceptual patch feature construction, normal feature memory compression based on gray-scale statistical grouping, and anomaly scoring calibration based on local perceptual patch reference distance distribution to achieve anomaly detection, spatial localization, and visualization output of wood surface defects. Through the combination of the above techniques, this invention can improve the accuracy, stability, and localization capability of defect detection against complex wood grain backgrounds while reducing the dependence on a large number of defect samples and fine annotation.

[0026] (2) This invention introduces the Mambavision encoder into the PatchCore anomaly detection framework and utilizes its state space modeling capabilities to extract wood surface features. Compared with traditional CNN-based feature extraction methods, this invention can better characterize the long-range continuity and global structural information of wood texture, and improve the ability to distinguish normal textures from defect areas in complex wood grain backgrounds.

[0027] (3) This invention employs a locally perceptive Patch feature construction method to perform feature representation and anomaly analysis on local regions of wood images. Specifically, this invention does not extract features from a single spatial location as Patch features, but rather performs local aggregation on the aligned feature map, centering on the current spatial location and combining the feature vectors of its surrounding neighboring locations, to obtain locally perceptive Patch feature vectors. In this way, each Patch feature not only contains the texture information of the local wood region corresponding to the current location, but also integrates the contextual information of the surrounding neighboring regions, which can better characterize the local continuity and local abrupt changes of the wood texture. Thus, in the subsequent anomaly scoring and anomaly map reconstruction process, the Patch-level anomaly detection results can be mapped to the specific spatial location of the wood image to be detected, improving the precision of defect region localization and reducing the problem of small defects, slender defects, or local defects being weakened by the overall image features.

[0028] (4) This invention proposes a normal feature memory compression method based on gray-level statistical grouping. When constructing the normal feature memory, the gray-level mean and gray-level fluctuation characteristics of the normalized local perceptual patch feature vector are grouped, and representative features are selected in each group so that the memory can cover normal wood texture features under different brightness and contrast conditions, thereby reducing misjudgment caused by changes in the brightness of the wood surface and local imaging differences.

[0029] (5) The present invention adopts an anomaly scoring calibration method based on the reference distance distribution of the normalized local sensing patch feature vector. By using the normalized local sensing patch feature vector that has not participated in memory compression to construct a reference distance distribution, the nearest neighbor distance score of the patch to be detected is calibrated, so that the anomaly score can reflect its deviation from the normal distribution and improve the stability of anomaly judgment.

[0030] (6) This invention performs anomaly detection based on a normal feature memory bank, without relying on a large number of defect samples for classification or segmentation supervision training. It only uses defect labels to screen high-confidence normal regions during the database construction stage, which reduces the requirements for defect sample size and category integrity. Attached Figure Description

[0031] Figure 1 This is a complete flowchart of Embodiment 1 of the present invention; Figure 2 This is the original image of the wood used in Embodiment 1 of the present invention; Figure 3 This is a schematic diagram of the normal region extraction process in Embodiment 1 of the present invention; Figure 4 This is a flowchart illustrating the database construction and detection stages in Embodiment 1 of the present invention; Figure 5 This is a schematic diagram of the layered structure of the MambaVision encoder in Embodiment 1 of the present invention; Figure 6 This is a flowchart of the normal feature memory compression process based on gray-scale statistical grouping in Embodiment 1 of the present invention; Figure 7 This is a flowchart of the anomaly score calculation and calibration process in Embodiment 1 of the present invention; Figure 8 This is an integrated diagram of abnormal regions in Embodiment 1 of the present invention, where a is an abnormal risk diagram, b is a defect segmentation diagram, and c is a visualization result of the superimposed defects. Detailed Implementation

[0032] Example 1 Methods for detecting wood surface defects based on feature memory and state space, such as Figure 1 As shown, it includes the following steps: S1. Filter the original timber images in the dataset using defect region markers to construct a database sample; preprocess the database sample to obtain a set of normal database samples. The images to be detected are preprocessed to obtain standardized samples for detection. This embodiment uses a self-built wood surface image dataset. The images were acquired by an industrial camera with a resolution of 1960×1080. The dataset contains approximately 12,200 images. In addition to the wood surface area, the original wood images also include the conveyor belt, equipment edges, and the imaging background. A single image of the wood surface may contain normal texture areas, as well as defective areas such as dead knots, live knots, bark, cracks, notches, dry scars, rotten heartwood, bark layer, and burrs, specifically as follows: Figure 2 As shown.

[0033] S11. Extract the target wood region from the original wood images in the dataset to obtain the effective wood region image; The original wood image contains wood surface areas and non-wood background areas. To avoid background features being included in subsequent library construction and detection processes, the wood target region is extracted from the original image first.

[0034] Specifically, the original image is separated into its background regions. The outer contour of the wood region is determined based on the differences between the wood region and the background region in terms of grayscale, color, or edge continuity. The image is then cropped based on the outer contour. The remaining background edges and non-wood interference regions in the cropping result are further corrected and removed to obtain the effective wood region image.

[0035] S12. Mark the defect areas in the effective area image of the wood and extract the normal areas as samples for library construction. A single effective area image of wood may contain both normal texture areas and defect areas. Therefore, the entire effective area image of wood is not directly used as a sample for library construction. In the library construction stage, this invention uses defect area markers to filter wood images, removing local areas containing defects or close to defect boundaries, and retaining normal areas as samples for subsequent library construction.

[0036] The defect region markers are only used for screening normal samples and are not used as supervision labels for classification, detection, or segmentation in model training.

[0037] Specifically, a sliding window method is used to extract local candidate regions on the effective area image of the wood after the defect areas have been marked. The sliding window size is 224×224 pixels, and the sliding step size is 112 pixels. (See details below.) Figure 3 As shown.

[0038] For each local candidate region, if it overlaps with any defective region, or if the distance between it and the boundary of a defective region is less than a preset safety interval, it is discarded; if it does not overlap with any defective region, and has no occlusion, severe reflection, or exposure abnormalities, it is retained as a normal region. After screening, a set of normal regions is obtained, and this set of normal regions is used as the sample for constructing a normal feature memory database.

[0039] S13. Perform size normalization and data augmentation on the normal region samples in the database construction sample, and output a standardized normal region sample set, i.e., the normal database construction sample set. The target wood region is extracted from the image to be detected to obtain the effective region image to be detected. The size of the effective region image to be detected is normalized, and the standardized sample to be detected is output.

[0040] A unified preprocessing workflow is adopted for the normal region samples in the database construction stage and the images to be detected in the detection stage. First, the size of the normal region samples and the effective regions to be detected after extracting the wood target region from the images to be detected are normalized. By scaling while maintaining the original aspect ratio, the wood region samples are scaled and edge-padded to obtain a standard input image of 224×224.

[0041] During the database construction phase, random data augmentation is performed on normal region samples after size normalization. Random data augmentation includes horizontal flipping, vertical flipping, brightness adjustment, contrast adjustment, and color perturbation. Random data augmentation is not performed during the detection phase.

[0042] After preprocessing, standardized normal region samples are output during the library construction stage, and standardized samples to be tested are output during the detection stage.

[0043] The wood surface defect detection method based on feature memory and state space further includes a library construction stage and a detection stage, such as... Figure 4 As shown, both stages share the same Mambavision encoder. In the database construction stage, normal region samples are input, and the encoder extracts feature maps, followed by local perceptual patch features. Based on this, grayscale statistical grouping and memory compression are performed to obtain the normal feature memory. Meanwhile, the normalized local perceptual patch feature vectors that were not involved in library compression are used to statistically analyze the reference distance distribution for subsequent anomaly scoring calibration.

[0044] In the detection phase, a local region of the sample to be detected is input, and feature maps are extracted using the same encoder to construct the test patch features. Subsequently, the test patch features are compared with the normal feature memory. Nearest neighbor distance matching is performed to obtain the original anomaly score. Then, the original anomaly score is calibrated by combining the reference distance distribution of the normalized local perception patch feature vector to obtain the anomaly risk value. The anomaly risk values ​​of each patch are recombined according to spatial location to generate an anomaly risk map, which is then upsampled and smoothed to obtain a defect segmentation map. Finally, the visualization result of the integrated anomaly region is output.

[0045] S2, Database Construction Phase: The pre-trained Mambavision encoder is used to process the normal library construction sample set in step S1. Feature extraction is performed to obtain hierarchical feature maps. After spatial alignment of the hierarchical feature maps, aligned feature maps are obtained. A local perceptual patch feature set is constructed in the aligned feature map space. Based on gray-level statistical grouping, the constructed local perceptual patch feature set is grouped to form a compressed normal feature memory. ; Traditional PatchCore anomaly detection methods typically employ convolutional neural networks (CNNs) as feature extractors, such as ResNet and WideResNet CNN encoders. These encoders primarily extract edge, color, texture, and local semantic features from images through multi-layer convolutional structures, and then use the extracted features for subsequent patch-level anomaly matching. In some industrial surface defect detection tasks, CNNs can effectively identify local anomalies such as scratches, stains, and damage.

[0046] However, the detection of surface defects in wood differs significantly from that in ordinary industrial surfaces. Normal wood grain typically extends continuously along the fiber direction, exhibiting strong directionality and cross-regional continuity. Simultaneously, the wood surface is also affected by growth patterns, processing marks, changes in lighting, and imaging angles, resulting in normal variations in brightness and localized grain fluctuations. Defects such as dead knots, live knots, bark, cracks, notches, dry scars, rotten heartwood, debarking, and burrs are often not simply bright or dark spots, but rather manifest as interruptions in grain direction, disruption of grain continuity, abrupt changes in local structure, or subtle variations inconsistent with the surrounding normal wood grain.

[0047] In this scenario, traditional CNN encoders have certain limitations. CNN feature extraction mainly relies on local convolutional kernels. Although the receptive field can be gradually expanded through multiple layers of the network, its feature response is still more biased towards local regions. For long-distance texture structures extending along the fiber direction on the wood surface, relying solely on local convolutional features is insufficient to fully express the texture continuity between different regions. When defective areas are similar to surrounding normal wood grain in color, grayscale, or local texture, the local patch features extracted by CNNs can easily mix normal texture fluctuations with real defects, leading to a decrease in the discriminative power of subsequent nearest neighbor anomaly scoring.

[0048] In addition, some wood defects have elongated, extended, or irregular shapes, such as cracks, bark, and burrs. The abnormality of these defects is not necessarily limited to a small, localized area, but rather manifests as changes in grain direction, disruption of structural relationships, or abnormal continuity over a larger area. If the feature extraction process only focuses on local appearance differences, two types of problems can easily arise: one is misjudging natural variations in normal wood grain as abnormalities, and the other is submerging small or extended defects in a complex wood grain background.

[0049] Therefore, feature extractors in wood surface defect detection not only need to preserve local texture details but also need to understand texture orientation and contextual relationships over a larger range. The state-space visual MambaVision encoder is well-suited for solving this problem. The state-space model can treat the spatial locations in the image feature map as a visual sequence with sequential relationships, establishing long-range positional dependencies with relatively low computational complexity.

[0050] The MambaVision encoder combines convolutional embedding, hierarchical feature extraction, and state space modeling capabilities. The convolutional embedding part can preserve the local edges, textures, and grayscale variations of the wood surface; the state space modeling part can enhance the contextual association between different spatial locations, so that the extracted patch features not only contain local appearance information, but also information about the continuity of the surrounding texture.

[0051] Therefore, introducing the MambaVision encoder into the PatchCore anomaly detection framework is not simply about replacing the original CNN encoder, but rather about improving the ability of patch features to express the continuity of wood texture and structural anomalies. Patch features extracted by the state-space visual encoder can better distinguish between natural fluctuations in normal wood grain and structural changes caused by defects, and provide a more stable feature foundation for subsequent normal feature memory construction, nearest neighbor distance matching, and anomaly scoring calibration.

[0052] As shown in Figure 5, the MambaVision-S-1K encoder employs a hierarchical visual feature extraction structure. The input image first passes through a convolutional embedding module to generate an initial feature map, followed by four encoding stages: Stage 1, Stage 2, Stage 3, and Stage 4. Each stage is connected by a downsampling module, which gradually reduces the spatial size of the feature map and gradually increases the number of channels, thereby obtaining feature representations at different scales.

[0053] The input size of the local wood image is set to 224×224×3. This size is consistent with the input specifications of the publicly available pre-trained weights, making it easy to use the pre-trained parameters directly; it is also consistent with the size of the local wood region extracted by the aforementioned sliding window, avoiding changes in the local texture ratio caused by additional scale transformations.

[0054] As shown on the left side of Figure 5, the convolutional embedding module uses two 3×3 convolutions, each with a stride of 2. The first convolutional layer outputs 64 channels, and the second convolutional layer outputs 96 channels. After two convolutions with a stride of 2, the input image is downsampled from 224×224 to 56×56. Convolutional embedding is used instead of direct linear slicing because wood texture has continuous edges and local grayscale variations; convolution can preserve the local relationships between adjacent pixels while downsampling. The number of channels is increased from 64 to 96 to establish a certain texture representation dimension in the initial stage, while controlling the subsequent computational load.

[0055] After convolutional embedding, the feature map enters four encoding stages. The output feature sizes of each stage are as follows: Stage 1 outputs 56×56×96; Stage 2 outputs 28×28×192; Stage 3 outputs 14×14×384; Stage 4 outputs 7×7×768.

[0056] This size variation comes from a 2x downsampling between stages. With each downsampling, the spatial size is halved, and the number of channels doubles. This setup allows shallow features to retain more spatial detail, while deeper features retain stronger regional expressiveness.

[0057] The network depth is configured as [3, 3, 7, 5], corresponding to the number of MambaVision Block modules in Stages 1 to 4, respectively. The first two stages are relatively shallow because they have high spatial resolution and require significant computation; too many modules would significantly increase computational overhead. Also, shallow layers primarily handle edge, color, and local texture representation, so a deep structure is unnecessary. Stage 3 has 7 modules because its feature map size is 14×14, making computation relatively manageable while still maintaining a certain spatial resolution, making it suitable as the main stage for mid-scale texture and region structure modeling. Stage 4 has 5 modules to further extract high-level contextual information, but due to its lower spatial resolution, the depth is not increased further to avoid over-compressing localization information.

[0058] The base feature dimension is set to 96, corresponding to the number of output channels in Stage 1. The number of channels in subsequent stages is expanded to 192, 384, and 768 respectively. This channel configuration follows the stepwise doubling design common in hierarchical visual encoders, keeping shallow layers with lower dimensions to control computational load, while deeper layers use higher dimensions to accommodate more abstract features. For memory-based matching methods, excessively low feature dimensions affect patch expressiveness, while excessively high feature dimensions increase memory retrieval overhead; therefore, a medium-sized channel configuration is adopted.

[0059] The number of heads for each stage is configured as [2, 4, 8, 16]. This setting corresponds to the progressively increasing number of channels: as the number of channels increases, the features are divided into more modeling subspaces, allowing different stages to handle texture and structural information of different granularities. Shallow layers have fewer heads and mainly process local textures; deep layers have more heads and are used to handle more complex regional relationships.

[0060] The window parameters are configured as [8, 8, 14, 7]. Stage 1 and Stage 2 feature maps have larger sizes, so the window size is set to 8 to limit the scope of single-step local modeling and control computational load. Stage 3 feature maps are 14×14, and the window size is set to 14, effectively covering the entire feature map of this stage, which is beneficial for establishing a larger range of contextual relationships at the mesoscale level. Stage 4 feature maps are 7×7, and the window size is set to 7, corresponding to covering the entire feature map of this stage. This setting matches the feature map size of each stage, avoiding excessive computation in shallow layers while allowing deeper layers to obtain a wider range of information interactions.

[0061] like Figure 5As shown, the Mamba Vision Block in the MambaVision-S-1K encoder mainly includes layer normalization, a selective state space modeling module, a depthwise separable convolution module, an MLP channel expansion module, and residual connections. Input features are first normalized using LayerNorm, then fed into the Mamba state space modeling module to model long-distance dependencies between different spatial locations in the feature map. Subsequently, a 3×3 depthwise separable convolution further enhances the local texture representation, allowing the encoder to simultaneously preserve local texture details and a large range of structural continuity information on the wood surface. Afterward, the features are again processed by the LayerNorm and MLP modules for channel-dimensional information mixing and expansion, and then added to the input features through residual connections to maintain feature transfer stability. The Drop Path marked in the figure represents the stochastic depth strategy used during training to reduce the risk of model overfitting. In this method, the encoder uses publicly pre-trained weights and keeps the parameters frozen; therefore, stochastic depth training is not triggered during the detection and library construction phases.

[0062] The MLP ratio is set to 4. This parameter determines the channel expansion ratio within the module and is a commonly used medium expansion configuration in visual encoders. A ratio that is too small will limit the channel blending capability, while a ratio that is too large will increase the number of parameters and computational cost. For the current input size and medium-sized encoder, a value of 4 can maintain a balance between expressive power and computational overhead.

[0063] We initialize the model using weights from the MambaVision-S-1K model pre-trained on the ImageNet-1K dataset, with approximately 50.1M parameters and a computational cost of approximately 7.5 GFLOPs. ImageNet-1K is a commonly used large-scale natural image classification dataset, and pre-trained models can learn general visual features such as edges, textures, colors, and local structures. Although this pre-training dataset is not specifically designed for wood defects, our method does not directly learn defect categories using an encoder. Instead, it leverages the general visual feature extraction capabilities of ImageNet-1K to extract representations of local wood regions, and then uses a normal feature memory to determine whether the test region deviates from the normal texture distribution. Therefore, using ImageNet-1K pre-trained weights reduces the dependence on labeled wood defect samples and provides a stable initial feature space for subsequent memory construction and anomaly matching.

[0064] During pre-training, preprocessing parameters included the input wood image size, normalized mean, normalized standard deviation, and cropping ratio. The input image size was 224×224×3, consistent with the input specifications of the selected MambaVision-S-1K pre-trained model. The normalized mean was [0.485, 0.456, 0.406], and the normalized standard deviation was [0.229, 0.224, 0.225], corresponding to the commonly used RGB three-channel normalization parameters in ImageNet-1K pre-trained models, ensuring the pixel distribution of the input wood image remained consistent with the input distribution during pre-training. The cropping ratio `crop_pct` was 0.93, indicating that approximately 93% of the effective area in the central region of the image was retained during preprocessing before being adjusted to the standard input size, reducing the impact of edge-irrelevant regions on feature extraction. In this method, the same size normalization and channel normalization parameters are used in both the library construction stage and the detection stage to ensure that the features of normal samples and the features of samples to be tested are in the same feature space.

[0065] Subsequent feature construction selects the output features from Stage 2 and Stage 3, instead of directly using features from all stages. Although Stage 1 has the highest spatial resolution, it reflects more of the underlying edges and brightness variations, making it susceptible to noise and normal wood grain fluctuations. While Stage 4 contains strong contextual information, its spatial resolution is only 7×7, making it unsuitable as the primary basis for fine-grained localization. The 28×28 features of Stage 2 retain more spatial details, and the 14×14 features of Stage 3 contain certain regional context; therefore, these two stages are selected as inputs for subsequent Patch feature construction.

[0066] After encoder initialization, the parameters are frozen, and the encoder is no longer trained or fine-tuned using wood data. The same set of fixed encoder parameters is used to extract features during both the database construction and detection phases. This is because this method does not learn specific defect categories by training a classifier or segmentation network; instead, it constructs a normal feature memory using normal wood samples and determines the degree of anomaly based on the distance between the features of the sample to be detected and the normal feature memory. Continuing to train the encoder on wood defect data would require introducing defect category labels, bounding box labels, or pixel-level defect labels, which would not only increase annotation costs but also potentially cause the encoder to over-adapt to existing defect types, affecting its ability to detect unknown or rare defects.

[0067] Specifically, a pre-trained MambaVision encoder is used to extract features from the normal library sample set obtained in step S1, resulting in hierarchical feature maps. In terms of feature level selection, the second and third stage feature maps output by the encoder are used for subsequent local perceptual patch feature construction. The second and third stage feature maps are spatially aligned to obtain aligned feature maps. , which serves as the input for subsequent calculations. Here, H′ represents the spatial dimension of the aligned feature map in the height direction, W′ represents the spatial dimension of the aligned feature map in the width direction, and C′ represents the number of channels in the aligned feature map.

[0068] The spatial alignment involves upsampling the feature map from stage 3 to a size of 28*28, then concatenating the feature map from stage 2 and the upsampled feature map from stage 3 along the channel dimension, and finally performing channel fusion through a 1×1 convolution to obtain the aligned feature map. .

[0069] The construction of a locally perceptual patch feature set in the aligned feature map space specifically involves: For each spatial location in the aligned feature map Construct its local neighborhood: (1) Where h represents the coordinates of the current center position along the height direction of the aligned feature map, w represents the coordinates of the current center position along the width direction of the aligned feature map; a represents the coordinates of a position within the local neighborhood along the height direction, b represents the coordinates of a position within the local neighborhood along the width direction; p represents the size of the local neighborhood window, where the neighborhood size is taken as... .

[0070] Local aggregation is performed on the feature regions of the local neighborhood of each spatial location to obtain the corresponding local sensing patch feature vector; the local sensing patch feature vector is then L2 normalized to obtain the normalized local sensing patch feature vector.

[0071] The normalized local sensing patch feature vectors corresponding to all spatial locations are combined to form a local sensing patch feature set, denoted as . (2) in, Indicates the first A normalized local perceptual patch feature vector, This indicates the number of patch features generated on the aligned feature map.

[0072] In this context, the local neighborhood feature region refers to the feature region on the aligned feature map F, centered at the current spatial position (h, w), composed of feature vectors corresponding to the surrounding p×p spatial positions. Since each spatial position in the aligned feature map F corresponds to a local receptive region in the input wood image, this local neighborhood feature region can represent the texture context information of the current patch and its surrounding neighboring regions. In this embodiment, the neighborhood size is p=3, that is, the 3×3 feature region formed by the current spatial position and its surrounding neighboring positions is locally aggregated to obtain the corresponding local receptive patch feature vector; the local receptive patch feature vector is then L2 normalized to obtain the normalized local receptive patch feature.

[0073] Specifically, local aggregation is performed using local pooling.

[0074] The step size parameter during patch feature construction is taken as follows That is, to generate a set of Patch features at all spatial locations.

[0075] During the database construction phase, the set constitutes a locally perceptual patch feature set.

[0076] The original PatchCore method typically compresses the entire set of locally perceptual patch features as a whole, without further distinguishing the differences in brightness, contrast, and local texture patterns among the different locally perceptual patch feature vectors. However, in wood surface images, different regions can exhibit significant differences in grayscale distribution and texture representation, even if they are all normal areas, due to the influence of wood texture, lighting conditions, surface color depth, and local imaging differences. Directly compressing the entire set of locally perceptual patch features can easily lead to insufficient coverage of some normal texture patterns, resulting in misjudgments in subsequent detection stages.

[0077] Therefore, after obtaining the local perception Patch feature set, this invention does not directly perform uniform compression on all features. Instead, it first groups the features according to the gray-scale statistical features of the local wood region corresponding to each Patch, then selects representative features within each group, and finally merges the results of each group into a compressed normal feature memory.

[0078] The memory compression based on gray-scale statistical grouping groups the constructed local perceptual patch feature set to form a compressed normal feature memory. ,like Figure 6As shown, specifically: based on the spatial position of each normalized local perceptual patch feature vector in the aligned feature map F, the mean gray level and standard deviation of the local image region in the corresponding standardized normal region sample are extracted; then, based on the mean gray level and standard deviation of the gray level, the normalized local perceptual patch feature vector is divided into different gray level statistical groups; subsequently, core set sampling is performed within each gray level statistical group to obtain the representative features of each group; the representative features are merged to obtain the compressed normal feature memory.

[0079] Specifically as follows: (1) Based on the spatial position of each normalized local perceptual patch feature vector in the aligned feature map F, extract the local image region in the corresponding normal region sample, and perform grayscale processing on the local image region to obtain the corresponding grayscale map; calculate the grayscale mean and grayscale standard deviation of the grayscale map; (2) Based on the calculated gray mean and gray standard deviation, the normalized local perception Patch feature vector is divided into different gray statistical groups; (3) The normalized local perception Patch feature vectors in each group are sampled from the core set, and the representative normalized local perception Patch feature vectors of each group are selected to form the representative feature subsets of the corresponding groups. (4) Combine the representative feature subsets obtained from each group to form a compressed normal feature memory.

[0080] As can be seen from Figure 6, this invention does not perform indiscriminate compression of the local perceptual patch feature set. Instead, it first divides the feature set into layers according to grayscale statistical features, and then retains the representative normal features in each layer, thereby improving the memory's ability to cover different normal texture patterns.

[0081] Specifically, in step (1), the local image region of the i-th normalized local perceptual patch feature vector in the aligned feature map F is determined based on its spatial position, and the local image region is then grayscaled to obtain grayscale image I. i Let the grayscale image I be... i The size is h i ×w i Then calculate its gray mean and gray standard deviation: (3) (4) Among them, I i (x, y) represents the grayscale image I corresponding to the i-th normalized local perceptual patch feature vector.i The grayscale value at pixel coordinates (x, y), where x represents the pixel coordinate in the height direction of the grayscale image, y represents the pixel coordinate in the width direction of the grayscale image, and h... i and w i These represent the grayscale image I. i Height and width; μ i This represents the average gray value of the local image region corresponding to the feature vector of the i-th local perceptual patch, used to characterize the average brightness of the local image region. It represents the grayscale standard deviation of the local image region corresponding to the feature vector of the i-th local perceptual patch, and is used to characterize the grayscale fluctuation and contrast difference of the local image region.

[0082] Step (2) is based on the grayscale mean. and grayscale standard deviation The normalized local perceptual patch feature vectors are divided into 4 groups. Let the median grayscale mean of the grayscale image of the local wood region corresponding to the normalized local perceptual patch feature vectors be... The median standard deviation of grayscale is The grouping rules are as follows: Corresponds to the low brightness - low contrast group; Corresponds to the low brightness - high contrast group; Corresponds to the high brightness - low contrast group; Corresponding to the high brightness-high contrast group.

[0083] In step (3), let the gray-level statistical group be numbered k, where k = 1, 2, 3, 4. The normalized local perceptual patch feature vectors contained in the k-th gray-level statistical group form the local perceptual patch feature set of the k-th gray-level statistical group, denoted as P. k : (5) Among them, P k p represents the set of local perceptual patch features corresponding to the k-th gray-level statistical group. k , j Let n represent the j-th normalized local perceptual patch feature vector in the k-th gray-level statistical group. k This represents the number of normalized local perceptual patch feature vectors in the k-th gray-level statistical group.

[0084] As shown in the "Intra-Group Feature Compression" section of Figure 6, this invention does not uniformly filter all patch features, but instead filters representative features separately within each gray-scale statistical group. The number of features retained in each group is calculated using the following formula: (6) In other words, each group retains 10% of the features in that group; when 10% is less than 30, the group retains a fixed 30 features.

[0085] Then, core set sampling is performed within each gray-level statistical group according to the feature space distance to select representative features that can cover the feature distribution within the group from the local perceptual patch features of that group. Specifically, for the k-th gray-level statistical group, its corresponding normalized local perceptual patch feature set is P. k The preset number of representative features to be retained is m. k .

[0086] First, from P k A normalized local perceptual patch feature vector is selected as the initial representative feature, and a representative feature subset M is added. k ; In each subsequent round of sampling, P is traversed. k China has not yet been selected for M k The normalized local sensing patch feature vector. For any unselected normalized local sensing patch feature vector, calculate its relationship with M. k The Euclidean distances between each selected representative feature in the set are calculated, and the minimum value is taken as the distance from the unselected local perceptual patch feature to the current representative feature subset M. k The nearest distance. For the first round, the calculated features of any unselected local perceptual patch will be compared with M. k The Euclidean distance between the initial representative features is used as the distance from the unselected local perceptual patch features to the current representative feature subset M. k The nearest distance; the larger the nearest distance, the more obvious the difference between the local perceptual patch feature and the currently selected representative features, indicating that its corresponding normal texture pattern has not been fully covered by the current representative feature subset.

[0087] Therefore, in each round of sampling, from those not yet selected into M... k Select the local sensing patch feature with the largest nearest distance from the local sensing patch features and add it to M. k Repeat the above sampling process until M... k The number of features in the data reaches the preset retention quantity m kThus, the representative feature subset of the k-th gray-level statistical group is obtained: (7) Among them, M k Let m represent the representative feature subset corresponding to the k-th gray-level statistical group. k , Let m represent the r-th representative normalized local perceptual patch feature vector selected from the k-th gray-level statistical group. k This represents the number of representative features that are pre-selected and retained in the k-th gray-scale statistical group.

[0088] Finally, as shown at the bottom of Figure 6, the representative features selected from the low brightness-low contrast group, low brightness-high contrast group, high brightness-low contrast group, and high brightness-high contrast group are merged to obtain the compressed normal feature memory: (8) in, This represents the compressed normal feature memory. This indicates the number of grayscale statistical groups.

[0089] The normal feature memory obtained through the above method is no longer just a uniform compression of all local perceptual patch features, but simultaneously covers the local features of normal wood under different brightness levels and contrast modes. In the subsequent detection stage, when the patch feature to be tested is matched with the nearest neighbor of this memory, it can reduce misjudgments caused by differences in brightness on the wood surface, local imaging fluctuations, and natural variations in normal texture.

[0090] S3. Detection Stage: The standardized test samples obtained in step S1 are input into the pre-trained Mambavision encoder to generate a test patch feature set. Based on the normal feature memory constructed in step S2, the nearest neighbor anomaly score is performed on the test patch feature vectors in the test patch feature set, and a calibration anomaly risk value is output. The anomaly risk values ​​of each test patch feature vector are reorganized according to their spatial position in the test sample to generate an anomaly risk map located in the feature map space. The anomaly risk map is post-processed to output a defect segmentation map. After anomaly region integration, the overall anomaly distribution result of the test sample is output.

[0091] As shown in Figure 7, the nearest neighbor anomaly scoring process in the detection phase includes two main parts: the first part is the calculation of the original anomaly score based on the nearest neighbor distance, and the second part is the anomaly score calibration based on the reference distance distribution of the normalized local perceptual patch feature vector. The upper part of Figure 7 shows the feature of the test patch obtained after feature extraction of the wood image under test, and the nearest neighbor matching of this feature with the features in the normal feature memory to obtain the original anomaly score; the lower part of Figure 7 shows the calibration of the original anomaly score using the distance distribution of the normal reference patch, thereby converting the simple distance value into an anomaly risk value with statistical reference significance.

[0092] Specifically, the same encoder is used in both the database construction and detection phases to ensure that the features of normal samples and the features of samples to be tested are in the same feature space.

[0093] During the detection phase, standardized samples to be detected are input into the Mambavision encoder to generate a set of features for the patch to be tested. (9) in, This represents a standardized sample to be tested. This represents the encoder feature extraction process. This describes the process of constructing local perceptual patch features. Each feature vector in the patch feature set corresponds to a local region in the standardized sample to be detected. For the detection stage, feature extraction and spatial alignment, as well as the construction of the patch feature set, are the same as in the database construction stage, and will not be described in detail here; the set constitutes the patch feature set to be tested.

[0094] The feature vectors of each test patch are compared with the compressed normal feature memory. The nearest neighbor distance is used to retrieve the feature vectors in the dataset. For the first feature vector... The normalized feature vector of each patch to be tested, and its original anomaly score is defined as follows: (10) in, Represents the Euclidean distance function. Indicates the first The normalized feature vectors of each test patch. This represents the first normal feature memory in the compressed database. Each feature vector is a feature vector. The greater the distance between a test patch and its nearest neighbor features in the memory, the more the patch deviates from the normal wood texture distribution, and the higher the corresponding original anomaly score. This process corresponds to... Figure 7The section on "Anomaly Score Calculation (Nearest Neighbor Distance)" is included.

[0095] The original PatchCore method typically uses the nearest neighbor distance as the anomaly score and a fixed threshold for judgment. However, the fixed threshold only utilizes the absolute magnitude of the distance and does not take into account the distance distribution information of the locally perceptual patch feature vectors in the current memory. Because there are variations in brightness, natural texture fluctuations, and local imaging differences among wood samples, the nearest neighbor distance of the locally perceptual patch feature vectors themselves will also change. Therefore, the fixed threshold is easily affected by changes in sample distribution, leading to false positives or false negatives.

[0096] To address this issue, this invention further statistically analyzes the reference distance distribution of normalized local sensing patch feature vectors that did not participate in library construction and compression within the current memory, and uses this distribution to calibrate the original anomaly score of the patch to be tested. Instead of building a new memory, this invention utilizes the normalized local sensing patch feature vectors from step S2 that did not participate in constructing the normal feature memory, and calculates their distance relative to the compressed normal feature memory using the same nearest neighbor distance calculation method as in the detection phase. The nearest neighbor distance is used to obtain the normal reference distance set: (11) in, Indicates the first The nearest neighbor distance of a normalized local perceptual patch feature vector relative to the compressed normal feature memory. This represents the number of normalized local perceptual patch feature vectors that did not participate in the construction of the normal feature memory. This reference distance set reflects the range within which the distance typically falls when a patch matches the current memory under normal circumstances. This process corresponds to the "Scoring Calibration (Based on the Reference Distribution of Normalized Local Perceptual Patch Feature Vectors)" section in Figure 7. Figure 7 The normal patch in the text is the normalized local perceptual patch feature vector.

[0097] For any patch to be tested, the original anomaly score According to its normal reference distance set The location of the value is used to calculate the calibration anomaly risk value. : (12) in, This is an indicator function that takes the value 1 if the condition in parentheses is true, and 0 otherwise.

[0098] This indicates that the current original anomaly score is less than or equal to the normal reference distance. The proportion. In other words, Used to measure the relative position of the distance value of the current patch under test in the normal reference distance distribution. The larger the value, the further back the distance of the current patch to be tested is in the normal reference distribution, meaning that such a distance occurs less frequently under normal circumstances, and the corresponding anomaly risk is higher; The smaller the value, the lower the risk of anomaly, indicating that the distance of the current patch under test is still within the common range of the normal reference distribution.

[0099] As shown in Figure 7, through this calibration process, the original nearest neighbor distance output by the memory bank is no longer used merely as a simple distance value, but is further converted into an anomaly risk score with statistical reference to normal samples. This anomaly risk score can more stably reflect the degree to which the tested area deviates from the normal wood texture distribution, thereby improving the stability of defect judgment under different wood images, different brightness conditions, and different texture backgrounds.

[0100] After calculating the anomaly scores for all patches, the anomaly risk values ​​of each patch are reorganized according to their spatial location in the sample to be detected, generating an anomaly risk map located in the feature map space.

[0101] The post-processing of the abnormal risk map includes upsampling and smoothing.

[0102] Because the spatial resolution of the anomaly map is lower than that of the input image, an upsampling process is performed on the anomaly risk map to restore it to the original size of the sample to be detected. Bilinear interpolation is used for upsampling.

[0103] After upsampling, the anomaly risk map is smoothed to obtain the defect segmentation map. In this invention, Gaussian smoothing is used, with a Gaussian kernel width of σ=4 and a threshold T. P =0.60.

[0104] The defect segmentation map obtained after upsampling and smoothing is used to characterize the spatial distribution of abnormal regions in the sample to be tested. The larger the value in the segmentation map, the less frequently the corresponding region appears in the normal reference distance distribution, and the higher the risk of anomaly.

[0105] Subsequently, a local anomaly risk threshold is set. When the anomaly risk value corresponding to a pixel location is greater than or equal to T... p If the defect is detected, the location is marked as an abnormal area; otherwise, it is marked as a normal area, thus obtaining a defect segmentation map, as shown below: Let the local anomaly risk threshold T be... P The anomaly risk value is 0.60 when the anomaly risk value of a certain patch feature vector satisfies: When the feature vector of the patch to be tested is used, the region image corresponding to it is recorded as the defect candidate region. When the feature vector of the patch to be tested is used, the region image corresponding to it is recorded as the normal region.

[0106] Finally, the abnormal areas in the defect segmentation map are overlaid on the original wood image using color markings to obtain the defect visualization result.

[0107] Specifically, after obtaining the anomaly risk values ​​for all patches, the anomaly risk values ​​of each patch are integrated with their spatial positions in the original image and compared with the defect segmentation map after upsampling and smoothing to obtain the overall anomaly distribution result of the sample to be detected. After connecting and merging adjacent or continuously distributed high-risk areas, a complete defect area display result can be formed on the original image.

[0108] The final output includes a defect segmentation map, the location of abnormal regions, and a defect visualization result overlaid on the original image, such as... Figure 8 As shown, a is the anomaly risk map, b is the defect segmentation map, and c is the overlaid defect visualization result map. This method integrates local patch-level anomaly detection results into a comprehensive anomaly distribution map on the entire image.

Claims

1. A method for detecting wood surface defects based on feature memory and state space, characterized in that, Includes the following steps: S1. Use defect region markers to filter the original wood images in the dataset and construct a database sample; Preprocessing the database construction samples yields a normal database construction sample set. The images to be detected are preprocessed to obtain standardized samples for detection. S2, Database Construction Phase: The pre-trained Mambavision encoder is used to process the normal library construction sample set in step S1. Feature extraction is performed to obtain hierarchical feature maps; After spatial alignment of the hierarchical feature maps, aligned feature maps are obtained. A local perceptual patch feature set is constructed in the aligned feature map space. Based on gray-level statistical grouping, the constructed local perceptual patch feature set is grouped to form a compressed normal feature memory. ; The memory compression based on gray-scale statistical grouping groups the constructed local perceptual patch feature set to form a compressed normal feature memory. The details are as follows: (1) Based on the spatial position of each normalized local perception Patch feature vector in the aligned feature map F, extract the local image region in the corresponding normal region sample, and perform grayscale processing on the local image region to obtain the corresponding grayscale map. Calculate the mean and standard deviation of the grayscale image. (2) Based on the calculated gray mean and gray standard deviation, the normalized local perception Patch feature vector is divided into different gray statistical groups; (3) The normalized local perception Patch feature vectors in each group are sampled from the core set, and the representative normalized local perception Patch feature vectors of each group are selected to form the representative feature subsets of the corresponding groups. (4) Merge the representative feature subsets obtained from each group to form a compressed normal feature memory; S3. Detection Stage: The standardized test samples obtained in step S1 are input into the pre-trained Mambavision encoder to generate a test patch feature set; based on the normal feature memory constructed in step S2, the nearest neighbor anomaly score is performed on the test patch feature vectors in the test patch feature set, and a calibration anomaly risk value is output; the anomaly risk values ​​of each test patch feature vector are reorganized according to their spatial position in the test sample to generate an anomaly risk map located in the feature map space; the anomaly risk map is post-processed to output a defect segmentation map; after anomaly region integration, the overall anomaly distribution result of the test sample is output.

2. The method for detecting wood surface defects based on feature memory and state space according to claim 1, characterized in that, Step S1 includes: S11. Extract the target wood region from the original wood images in the dataset to obtain the effective wood region image; S12. Mark the defect areas in the effective area image of the wood and extract the normal areas as samples for library construction. S13. Perform size normalization and data augmentation on the normal region samples in the database construction sample, and output the standardized normal region samples, i.e., the normal database construction sample set. The target wood region is extracted from the image to be detected to obtain the effective region image to be detected. The size of the effective region image to be detected is normalized, and the standardized sample to be detected is output.

3. The method for detecting wood surface defects based on feature memory and state space according to claim 1, characterized in that, In step S2, the pre-trained Mambavision encoder is used to process the normal library construction sample set from step S1. Feature extraction is performed to obtain hierarchical feature maps; Spatial alignment is performed on the generated feature maps from stages 2 and 3 to obtain aligned feature maps. Where H′ represents the spatial dimension of the aligned feature map in the height direction, W′ represents the spatial dimension of the aligned feature map in the width direction, and C′ represents the number of channels of the aligned feature map.

4. The method for detecting wood surface defects based on feature memory and state space according to claim 1, characterized in that, Step S2, which involves constructing a locally perceptual patch feature set in the aligned feature map space, specifically includes: For each spatial location in the aligned feature map Construct its local neighborhood: (1) Where h represents the coordinates of the current center position in the height direction of the aligned feature map, w represents the coordinates of the current center position in the width direction of the aligned feature map; a represents the coordinates of a position in the local neighborhood in the height direction, b represents the coordinates of a position in the local neighborhood in the width direction; p represents the size of the local neighborhood window. Local aggregation is performed on the feature regions of the local neighborhood of each spatial location to obtain the corresponding local perception patch feature vector; L2 normalization is performed on the local perception patch feature vector to obtain the normalized local perception patch feature vector. The normalized local sensing Patch feature vectors corresponding to all spatial locations are combined to form the local sensing Patch feature set, denoted as: (2) in, Indicates the first A normalized local perceptual patch feature vector This indicates the number of patch features generated on the aligned feature map.

5. The method for detecting wood surface defects based on feature memory and state space according to claim 1, characterized in that, Step (1) involves calculating the mean and standard deviation of the grayscale image, specifically as follows: (3) (4) Among them, I i (x, y) represents the grayscale image I corresponding to the i-th normalized local perceptual patch feature vector. i The grayscale value at pixel coordinates (x, y), where x represents the pixel coordinate in the height direction of the grayscale image, y represents the pixel coordinate in the width direction of the grayscale image, and h... i and w i These represent the height and width of the grayscale image, respectively, in μ. i This represents the average gray level of the local image region corresponding to the i-th local perceptual patch feature vector; This represents the grayscale standard deviation of the local image region corresponding to the feature vector of the i-th local perceptual patch; Step (2) is based on the grayscale mean. and grayscale standard deviation The normalized local perception patch feature vectors are divided into 4 groups; let the median grayscale mean of the grayscale image of the local wood region corresponding to the normalized local perception patch feature vector be... The median standard deviation of grayscale is The grouping rules are as follows: Corresponds to the low brightness - low contrast group; Corresponds to the low brightness - high contrast group; Corresponds to the high brightness - low contrast group; Corresponds to the high brightness-high contrast group; In step (3), let the number of the gray-level statistical group be k, where k = 1, 2, 3, 4; the normalized local perceptual patch feature vectors contained in the k-th gray-level statistical group form the local perceptual patch feature set of the k-th gray-level statistical group, denoted as P. k : (5) Among them, P k p represents the normalized local perceptual patch feature set corresponding to the k-th gray-level statistical group. k , j Let n represent the j-th normalized local perceptual patch feature vector in the k-th gray-level statistical group. k This represents the number of normalized local perceptual patch feature vectors in the k-th gray-level statistical group; The number of features retained in each group is calculated using the following formula: (6) in, The number of normalized local perceptual patch feature vectors in the k-th gray-level statistical group; Then, core set sampling is performed within each gray-level statistical group according to the feature space distance to obtain the representative feature subset of the k-th gray-level statistical group: (7) Among them, M k Let m represent the representative feature subset corresponding to the k-th gray-level statistical group. k , Let m represent the r-th representative normalized local perceptual patch feature vector selected from the k-th gray-level statistical group. k This represents the number of representative features that are pre-selected and retained in the k-th gray-scale statistical group; The representative feature subsets of each gray-level statistical group are merged to obtain the compressed normal feature memory: (8) in, This represents the compressed normal feature memory. This indicates the number of grayscale statistical groups.

6. The method for detecting wood surface defects based on feature memory and state space according to claim 1, characterized in that, The nearest neighbor anomaly scoring in step S3 includes the calculation of the original anomaly score based on the nearest neighbor distance and the calibration of the anomaly score based on the reference distance distribution of the normalized local perception patch feature vector.

7. The method for detecting wood surface defects based on feature memory and state space according to claim 6, characterized in that, Step S3 involves performing nearest-neighbor anomaly scoring on the feature vectors of the patch to be tested within the feature set of the patch to be tested, and outputting a calibration anomaly risk value. Specifically: For the The normalized feature vector of each patch to be tested, and its original anomaly score is defined as follows: (9) in, Represents the Euclidean distance function. Indicates the first The normalized feature vectors of each test patch. This represents the first normal feature memory in the compressed database. 1 eigenvector; Using the normalized local perceptual patch feature vectors that were not used in constructing the normal feature memory in step S2, their relative position to the normal feature memory is calculated based on the nearest neighbor distance method. The nearest neighbor distance is used to obtain the normal reference distance set: (10) in, Indicates the first The nearest neighbor distance of a normalized local perceptual patch feature vector relative to the compressed normal feature memory. This represents the number of normalized local perceptual patch feature vectors that did not participate in the construction of the normal feature memory. For any test patch feature vector, the original anomaly score According to its normal reference distance set The location of the value is used to calculate the calibration anomaly risk value. : (11) in, This is an indicator function that takes the value 1 if the condition in parentheses is true, and 0 otherwise.

Citation Information

Patent Citations

  • Wood defect detection method based on computer image technology and transfer learning

    CN113610831A

  • Lightweight unsupervised defect detection method based on reconstruction and memory fusion

    CN120912500A

  • Wood surface defect detection method based on multi-view coding and feature memory bank

    CN121708008A