Anomaly Detection in Annular Metal Turning Surfaces Based on a Priori Information and a Multi-Scale Self-Referencing Template

To solve the problem of anomaly detection in annular metal turning surfaces, this paper develops an anomaly detection algorithm based on a priori information and a multi-scale self-referencing template by combining the imaging characteristics of annular workpieces. First, the annular metal turning surface is unfolded into a rectangular expanded image using bilinear interpolation to facilitate subsequent algorithm development. Second, the grayscale information from the positive samples is used to obtain the a priori information, and a multi-scale self-referencing template method is used to obtain its own multi-scale information. Then, the phase error and large-size anomaly interference problems of the self-referencing method are overcome by combining the a priori information with its own information, and an accurate response to anomalous regions of various sizes is realized. Finally, the segmentation completeness of the anomalous region is improved by utilizing the region growing method. The experimental results show that the proposed method achieves a mean pixel AUROC of 0.977, and the mean M_IOU of segmentation reaches 0.788. In terms of efficiency, this method is also much more efficient than the commonly used anomaly detection algorithms. The proposed method can achieve rapid and accurate detection of defects in annular metal turning surfaces and has good industrial application value.


Introduction
There are a lot of grooves, tapered surfaces, end faces and other annular turning surfaces in the manufacturing process of metal workpieces [1][2][3]. Surface anomaly detection in annular metal turning surfaces is an important process for the quality control of machining. In images, the annular metal turning surface is a circular structure, and its region of interest occupies only a small part of the whole image [4]. In grayscale images, the annular metal turning surface shows a uniform distribution of grayscale in the circumferential direction. Developing a general and efficient anomaly detection algorithm using the structure and grayscale characteristics of annular metal turning surfaces has important application value for automated anomaly detection in annular metal turning surfaces.
Two problems need to be overcome to achieve anomaly detection in annular metal turning surfaces: 1. large image resolution but small region of interest; 2. metal turning surface positive and negative samples are unbalanced, and negative samples are difficult to collect.
In an image containing a complete annular metal turning surface, the image size is determined by the outer diameter of the ring. The difference between the inner and outer diameter of the ring is usually not large, which results in the region of interest of the annulus occupying a small area in the image. Regarding the problem of high-resolution anomaly detection, in addition to the direct processing of the whole image [5,6], other common solutions are resizing [7] and cutting [8,9]. Zhao [10] et al. directly compressed the input image size, and experiments proved that such a strategy has little effect on the detection accuracy for large-size targets, but the recognition effect is poor for small-size targets, and the direct resize neutralizes or removes the pixels in small target regions. Some researchers have used a cutting method to cut large-size images into small-size images for processing and finally stitch the detection results together as the final output. The sliding window approach [11][12][13] has also been adopted by many researchers to transform the problem of detecting high-resolution images into a small sliding window detection problem. Coordinate transformation is also an effective way to extract the ring region of interest. Suo [4] and Deng [14] et al. directly unfolded the region of interest of annular workpieces to complete the inspection of the annular workpieces without reducing the resolution of the part being inspected. The conversion of the annular region into a rectangular region also facilitates subsequent algorithm development.
In terms of industrial anomaly detection needs, the collection of anomaly samples is more difficult due to the low product substandard rate. Meanwhile, anomalies have different manifestations, and it is difficult to cover all types of anomalies in the collection of anomaly samples, and unexpected anomalies are usually encountered in actual production. The difficulty of collecting negative samples leads to the difficulty of practical application of supervised target retrieval [15,16] or segmentation [17,18] methods. To address the problem of negative samples being difficult to collect, researchers have carried out extensive research work on positive-sample-based anomaly detection algorithms. Traditional anomaly detection algorithms can quickly achieve anomaly detection by manually designing the processing flow and using morphological processing [19,20] or template matching [21,22]. However, the traditional algorithm has high requirements for illumination, imaging angle and the state of the workpiece to be detected, and the selection of each parameter in the algorithm is complicated, and the algorithm is less robust. With the development of deep learning technology, the powerful feature extraction capability of deep networks has greatly enriched the technical methods of unsupervised anomaly detection. Positive sample anomaly detection methods using deep learning networks can be classified into two main categories [23]: image-reconstruction-based methods [24][25][26] and discriminativeembedding-based methods [27,28]. Image-reconstruction-based methods tend to learn the structural information of positive samples for image reconstruction and compare the reconstruction differences between the image to be detected and the abnormality-free map to achieve abnormality detection. Discriminative-embedding-based methods are more about learning the feature distribution of positive samples, and the determination of anomalous regions is achieved by calculating the distance between the features of the samples to be detected and the statistical positive sample features. Defard et al. [29] used VGG16 as a feature extraction layer, patch embedding for anomaly-free samples using multivariate Gaussian distribution to obtain a probabilistic representation of the normal class, and anomaly detection was achieved by calculating the Mahalanobis distance between the image to be detected and the normal class. Roth et al. [30] combined embeddings from ImageNet models with an outlier detection model to construct a maximally representative memory bank of nominal patch features. This method provides a further improvement in the speed of anomaly detection. The detection effect of deep-learning-based anomaly detection methods depends mainly on the learning effect of the network, and it is more difficult to improve the targeted effect of the trained network. Additionally, anomaly detection by deep learning methods generally results in too-large segmentation of small-sized anomalous regions.
Since the annular metal turning surface has a uniform grayscale in the circumferential direction, this grayscale characteristic is suitable for the self-referencing template method in the traditional anomaly detection method. The self-referencing method uses the periodic grayscale information of the image itself to construct an anomaly-free template, and the final anomaly detection result is obtained by the difference between the template and the image to be detected [31,32]. Ideally, the self-referencing template is anomaly free, but when there are large-size anomalous regions, the self-referencing template constructed using its own grayscale information contains a large amount of anomalous information, which causes serious misidentification problems [33]. Due to the use of grayscale differencing, the self-referencing template method may be incomplete in detecting anomalous regions with low contrast.
Combining the above analysis and the existing problems, this paper combines the circular structure of the annular metal surface and the characteristics of uniform imaging to unfold the annular region of interest, which not only maintains the pixel resolution of the region of interest but also reduces the amount of data processing and the background complexity of anomaly detection objects. Due to the lack of anomalous samples, this paper proposes to implement anomaly detection in annular metal surfaces based on a self-referencing algorithm. In this paper, we overcome the problem of the self-referencing template method failing to detect large-size anomalous regions by introducing a priori information and introduce the strategy of a multi-scale self-referencing template and region growth to improve the accuracy of low-contrast anomaly detection.
Our main contributions in this article are summarized as follows: (1) A bilinear interpolation expansion method based on the principle of transformation between polar and Cartesian coordinate systems is invoked for the problem of extracting the region of interest of annular metal turning surfaces; (2) For the problem of large-size anomaly detection, a general self-referencing template anomaly detection algorithm that fuses a priori information and its own grayscale information is proposed, and the anomaly segmentation effect is further improved by using a region growing method; (3) A more systematic and comprehensive experimental analysis and demonstration of the selection basis and performance effect of the parameters in the algorithm of this paper are conducted, and the generality of the parameters is proved through experiments.
The remainder of this paper is organized as follows. The flow of the developed algorithm and the object to be tested are briefly introduced in Section 2. Section 3 details the implementation of the entire algorithm. The following Section 4 describes the experimental design and results. Finally, this work is concluded in Section 5.

Pipeline
The process of manual anomaly detection is: first, find the region of interest; then, judge the rough anomaly region based on the a priori information in the mind; finally, finish the precise location of the anomaly region based on the information relating to texture or the grayscale of the image itself. Referring to the process of manual anomaly detection, this paper carries out the development of an anomaly detection algorithm for annular metal turning surfaces. The pipeline of the proposed method is illustrated in Figure 1, which is mainly divided into two parts: mean grayscale template construction and the defect detection process. Firstly, the annular region of interest (ROI) in positive samples I g is unfolded into rectangular image f g (corresponding to the manual detection of the search of interest).
In the mean grayscale template construction part, the grayscale value data of i columns in N normal expanded images are randomly extracted, and a total of N × i columns of waveform data are extracted, and the grayscale mean value of each row is calculated as the standard grayscale waveform to construct mean grayscale template MGT with the same size as the input image (corresponding to the a priori information of manual detection). In the defect detection process, for the expanded image to be detected f d , the significant anomalous regions are screened using the mean grayscale template and the multi-scale self-referencing template (corresponding to the coarse anomaly detection for manual detection); the significant anomalous regions are restored using MGT data to obtain the restoration map R; again, using the multi-scale self-referencing template method to obtain the final anomaly difference score map Score f inal , the high-response region in Score f inal is the anomaly region (corresponding to the precise location of the manually detected anomaly region); finally, the abnormal region is segmented by the region growth and morphology processing method, and the segmentation map Result is obtained.  Figure 1. Pipeline of anomaly detection algorithms based on a priori information and multi-scale self-referencing template.
In the mean grayscale template construction part, the grayscale value data of i columns in N normal expanded images are randomly extracted, and a total of N i × columns of waveform data are extracted, and the grayscale mean value of each row is calculated as the standard grayscale waveform to construct mean grayscale template MGT with the same size as the input image (corresponding to the a priori information of manual detection). In the defect detection process, for the expanded image to be detected d f , the significant anomalous regions are screened using the mean grayscale template and the multi-scale self-referencing template (corresponding to the coarse anomaly detection for manual detection); the significant anomalous regions are restored using MGT data to obtain the restoration map R ; again, using the multi-scale self-referencing template method to obtain the final anomaly difference score map final Score , the high-response region in final Score is the anomaly region (corresponding to the precise location of the manually detected anomaly region); finally, the abnormal region is segmented by the region growth and morphology processing method, and the segmentation map Result is obtained.

Sample Acquisition
Since there are relatively few public datasets for anomaly detection in annular metal turning surfaces, in this paper, as shown in Figure 2, a dataset containing three types of annular metal turning surfaces is constructed: a nuclear fuel rod groove dataset, rotary body thin-walled port dataset, and injector nozzle valve seat surface dataset. The objects to be detected in the datasets are all annular workpieces (yellow area), and their unfolded images are all long-bar images; the nuclear fuel rod groove has an unfolded size of 100 × 7000 pixels, the rotary thin-walled port has an unfolded size of 120 × 8700 pixels and the injector valve seat surface has an unfolded size of 440 × 10,000 pixels. The number of pos-

Sample Acquisition
Since there are relatively few public datasets for anomaly detection in annular metal turning surfaces, in this paper, as shown in Figure 2, a dataset containing three types of annular metal turning surfaces is constructed: a nuclear fuel rod groove dataset, rotary body thin-walled port dataset, and injector nozzle valve seat surface dataset. The objects to be detected in the datasets are all annular workpieces (yellow area), and their unfolded images are all long-bar images; the nuclear fuel rod groove has an unfolded size of 100 × 7000 pixels, the rotary thin-walled port has an unfolded size of 120 × 8700 pixels and the injector valve seat surface has an unfolded size of 440 × 10,000 pixels. The number of positive and negative samples for the three datasets is shown in Table 1. thin-walled port Groove Valve seat

Unfolding of Annular Region of Interest
The region of interest of the annular turning surface only occupies part of the whole image. To reduce the number of data operations and make full use of the structural characteristics of the annular surface with uniform grayness in the circumferential direction, the annular region of interest needs to be extracted and unfolded into a long rectangular image.
In Figure 3, the schematic diagram of the annular region of interest unfolding is shown. The yellow area in Figure 3a is the annular region of interest, and its corresponding annular unfolding area is shown in Figure 3b

Unfolding of Annular Region of Interest
The region of interest of the annular turning surface only occupies part of the whole image. To reduce the number of data operations and make full use of the structural characteristics of the annular surface with uniform grayness in the circumferential direction, the annular region of interest needs to be extracted and unfolded into a long rectangular image.
In Figure 3, the schematic diagram of the annular region of interest unfolding is shown. The yellow area in Figure 3a is the annular region of interest, and its corresponding annular unfolding area is shown in Figure 3b. To facilitate the annular unfolding operation, two coordinate systems are constructed in the original image I(x, y): the image Cartesian coordinate system O − XY and the polar coordinate system o − rθ with the center of the annular region of interest as the origin. Let P(r, θ) be any point of the annular region in polar coordinates, and its corresponding point in the image Cartesian coordinate system is p(x, y), and the relationship between the image Cartesian coordinate system and the polar coordinate system can be established.
where 0 x and 0 y are the coordinates of the polar coordinate system o rθ − in the center circle of the image coordinate system.  The inner and outer rings of the annular region of interest intersect the 0 • axis of the polar coordinate system at the points A(r 1 , 0 • ) and B(r 2 , 0 • ), whose corresponding positions in the expanded image f (x, y) are A and B , respectively. Since the circumferences of the circles where points A and B are located are different, in this paper, the annular region of interest is unfolded along the circumference of the outer ring. Let the image f (x, y) size be n × m after the annular unfolding, which is defined as follows: where n is the number of rows of the expanded image, and m is the number of columns of the expanded image; r 1 and r 2 are the radius of the inner and outer rings, respectively, and their values are integers; and where n is the number of rows of the expanded image, and m is the number of columns of the expanded image; 1 r and 2 r are the radius of the inner and outer rings, respectively, and their values are integers; and     is a downward rounding symbol.
Let the point ( , ) P r θ in the polar coordinates have its corresponding position as where p n and p m are the coordinates of point P ′ in the expanded image; m is the number of columns of the expanded image; r is the radius coordinate of point P in the polar coordinate system ; θ is the angular coordinate of point P in the polar co-360 ° is a downward rounding symbol. Let the point P(r, θ) in the polar coordinates have its corresponding position as P (n p , m p ) in the expanded image. Then, the transformation equation of point P to P is: where n p and m p are the coordinates of point P in the expanded image; m is the number of columns of the expanded image; r is the radius coordinate of point P in the polar coordinate system r = r 1 , r 1 + 1, . . . , r 2 ; θ is the angular coordinate of point P in the polar coordinate where n is the number of rows of the expanded image, and m is the number of columns of the expanded image; 1 r and 2 r are the radius of the inner and outer rings, respectively, and their values are integers; and     is a downward rounding symbol.
Let the point ( , ) P r θ in the polar coordinates have its corresponding position as ( , ) p p P n m ′ in the expanded image. Then, the transformation equation of point P to P ′ is:   is a downward rounding symbol. To improve the unfolding effect of the annular turning surface and avoid the jagged effect generated by the image unfolding, the gray values in the unfolding map are calculated by bilinear interpolation when performing the unfolding. As shown in Figure 4, p(x, y) is the interpolation point corresponding to point P (n p , m p ) in the expanded image. Since the coordinates of p(x, y) may not be an integer, four integer points p 1 , p 2 , p 3 and p 4 around p are selected. The values of point M and point N are obtained by linear interpolation using p 1 and p 2 and p 3 and p 4 , respectively. Then, the grayscale value of point p is obtained by the linear interpolation of point M and point N.
where p n and p m are the coordinates of point P ′ in the expanded image; m is the number of columns of the expanded image; r is the radius coordinate of point P in the polar coordinate system ; and     is a downward rounding symbol. To improve the unfolding effect of the annular turning surface and avoid the jagged effect generated by the image unfolding, the gray values in the unfolding map are calculated by bilinear interpolation when performing the unfolding. As shown in Figure 4,   This completes the extraction and unfolding process of the annular region of interest. For any annular workpieces, we only need to determine the circle center o(x 0 , y 0 ), inner ring radius r 1 (in pixels) and outer ring radius r 2 (in pixels) and use the above Equations (1)-(4) in turn to achieve the counterclockwise unfolding process of the annular workpiece region of interest.

Mean Grayscale Template Construction
For computational convenience, the images are converted to single-channel grayscale maps with grayscale values ranging from 0 to 1 during the subsequent algorithm development. As shown in Figure 5, for the expanded image f (x, y) of an annular workpiece without anomalies, it can be ideally considered to have equal gray values in each row (yellow region) and the same gray waveform in each column (green region).
In the actual industrial detection process, the design of the tooling fixture and imaging method can ensure that the imaging position of the annular workpiece is relatively fixed, and the imaging brightness and contrast of each position are relatively consistent, so it can be approximated that the distribution of the gray value of each row of the corresponding expanded image conforms to the normal distribution. For any annular turning surface, the (1)-(4) in turn to achieve the counterclockwise unfolding process of the annular workpiece region of interest.

Mean Grayscale Template Construction
For computational convenience, the images are converted to single-channel grayscale maps with grayscale values ranging from 0 to 1 during the subsequent algorithm development. As shown in Figure 5, for the expanded image ( , ) f x y of an annular workpiece without anomalies, it can be ideally considered to have equal gray values in each row (yellow region) and the same gray waveform in each column (green region). In the actual industrial detection process, the design of the tooling fixture and imaging method can ensure that the imaging position of the annular workpiece is relatively fixed, and the imaging brightness and contrast of each position are relatively consistent, so it can be approximated that the distribution of the gray value of each row of the corresponding expanded image conforms to the normal distribution. For any annular turning surface, the mean value of each row of grayscale can be counted to construct an anomalyfree mean grayscale template MGT equal to the size of the expanded image. If the MGT is directly used as a template to calculate the absolute value of the difference between MGT and the image f to be detected, the a priori anomaly score prior Score can be obtained. N anomaly-free expanded images I g are collected, and a positive-sample statistical matrix Ps of size n × (N * k) is constructed by randomly selecting k columns of grayscale waveforms in each expanded image. The mean grayscale value of each row of Ps is calculated, and the mean grayscale template MGT is constructed.
where MGT(i, :) is all the grayscale values in row i of the mean grayscale template; i is the row coordinates of Ps in the range of 1, 2,· · ·, n; and j is the column coordinate of Ps in the range of 1, 2,· · ·, N * k.
If the MGT is directly used as a template to calculate the absolute value of the difference between MGT and the image f to be detected, the a priori anomaly score Score prior can be obtained.
Score prior = abs(MGT − f ) As shown in Figure 6, the results of the a priori anomaly scores for the multiple images to be detected are shown. In Figure 6c, the a priori anomaly score has a strong response for anomalous regions, but the score map also has a high response for edges and contours. The reason for this is that the MGT template can only respond to the grayscale distribution trend of the image and cannot accurately respond to the phase shift of the image. Therefore, the a priori anomaly score cannot be directly applied to the anomaly detection task.

Anomaly Detection Algorithm Based on a Priori Information and Multi-Scale Self-Referencing Template
In practical imaging of annular turning surfaces, there are problems such as oil interference, surface texture, uneven lighting or unfolded center shift. As shown in Figure 7b, the grayscale of each row of the expanded image is the same; meanwhile, the grayscale waveforms of each column also produce phase deviation. The dashed box in Figure 7a represents the self-reference template construction area at the solid line, and the solid line is the center of the dashed box. The construction region of the self-referencing template has a total of w columns, and the row grayscale mean of the region is used as the grayscale template. Figure 7c shows the self-referencing template waveforms of an abnormal column, where the difference at the anomaly increases as the value of w is taken, but the disturbance in other regions increases. Figure 7d shows the self-referencing template waveforms of Sensors 2023, 23, 6807 9 of 21 a normal column. When w is small, there is a large amplitude difference between the constructed template and the normal waveform due to the influence of the anomalous region, and false recognition occurs at this time. When w takes the value of 2000, the amplitude of the template waveform is the same as that of the anomaly-free waveform.
As shown in Figure 6, the results of the a priori anomaly scores for the multiple images to be detected are shown. In Figure 6c, the a priori anomaly score has a strong response for anomalous regions, but the score map also has a high response for edges and contours. The reason for this is that the MGT template can only respond to the grayscale distribution trend of the image and cannot accurately respond to the phase shift of the image. Therefore, the a priori anomaly score cannot be directly applied to the anomaly detection task.

Anomaly Detection Algorithm Based on A Priori Information and Multi-Scale Self-Referencing Template
In practical imaging of annular turning surfaces, there are problems such as oil interference, surface texture, uneven lighting or unfolded center shift. As shown in Figure 7b, the grayscale of each row of the expanded image is the same; meanwhile, the grayscale waveforms of each column also produce phase deviation. The dashed box in Figure 7a represents the self-reference template construction area at the solid line, and the solid line is the center of the dashed box. The construction region of the self-referencing template has a total of w columns, and the row grayscale mean of the region is used as the grayscale template. Figure 7c shows the self-referencing template waveforms of an abnormal column, where the difference at the anomaly increases as the value of w is taken, but the disturbance in other regions increases. Figure 7d shows the self-referencing template waveforms of a normal column. When w is small, there is a large amplitude difference between the constructed template and the normal waveform due to the influence of the anomalous region, and false recognition occurs at this time. When w takes the value of The above analysis shows that, in the self-referencing template algorithm, the parameter w directly determines the performance of the algorithm. Too small a w value can create problems with false recognition and missed recognition, while too large a w value can introduce too much interference. To solve the above problems, with reference to the process of manual judgment of abnormal areas, this paper proposes a highly robust anomaly detection algorithm adapted to annular metal turning surfaces by combining abnormality-free mean grayscale templates (a priori knowledge) and a multi-scale self-referencing template (own grayscale information).
First, the image to be tested f (size n × m) is shrunk horizontally, and its height is kept unchanged. The shrunken image T is obtained.
where T is the shrunken image in the horizontal direction; i is the row coordinate of T which takes a value in the range of 1, 2, . . . , n; j is the column coordinate of T , which takes values in the range of 1, 2, . . . , m/sk ; j is the column coordinate of f , which takes values in the range 1, 2, . . . , m;  The above analysis shows that, in the self-referencing template algorithm, the parameter w directly determines the performance of the algorithm. Too small a w value can create problems with false recognition and missed recognition, while too large a w value can introduce too much interference. To solve the above problems, with reference to the process of manual judgment of abnormal areas, this paper proposes a highly robust anomaly detection algorithm adapted to annular metal turning surfaces by combining abnormality-free mean grayscale templates (a priori knowledge) and a multi-scale self-referencing template (own grayscale information).
First, the image to be tested f (size n m × ) is shrunk horizontally, and its height is kept unchanged. The shrunken image T ′ is obtained. After the operation of shrinking, the abnormal column in the original image is diluted by the data of the normal column, at which point it can be approximated that there is no abnormal data in the shrunken image T . To ensure size consistency between the template and the expanded image f , this paper adopts the bilinear interpolation method to enlarge T to the size of n × m to obtain the final self-referencing template T.
Calculate the absolute residual of the self-referencing template T and the expanded image f to obtain the residual map Res.
Res(x, y) = abs(T(x, y) − f (x, y)) where abs is the absolute value operation. By choosing different values of sk, a series of self-referencing algorithm residual maps of different scales can be obtained. Since different sk values have different anomaly detection effects for different sizes of anomalous regions, it can be assumed that the residual map corresponding to a series of sk values must contain all the anomalous regions. The mean value of these residual maps can be used to extract all the anomalous regions; this step is mainly to find all the anomalous regions without requiring all the anomalous regions to have a high response. The mean value of all residual maps is calculated to obtain the multi-scale self-referencing anomaly detection score Score ms .
where k is the index number of the scale factor k = 1, 2, · · · , Num; the scale factor takes values in the array [sk 1 , sk 2 , · · · sk Num ]; and Res k is the residual map obtained when the scale factor sk = sk k .
To perform the normalization operation on Score ms , introduce the normalization threshold t 1 .
Score ms = Score ms /max(Score ms ) Score ms /t 1 max(Score ms ) ≥ t 1 max(Score ms ) < t 1 (10) where max(Score ms ) is the maximum value of Score ms . At this time, Score ms contains not only the response of the anomalous region but also the response of the non-anomalous region, but there is less phase interference in it, and the information of the mean grayscale template MGT can be introduced to suppress the non-anomalous region. Since the a priori anomaly score Score prior has a high response to both the significant anomaly region and the phase disturbance, the a priori anomaly scores Score prior are operated with the multi-scale self-referencing anomaly scores Score ms for the dot product, and only the significant anomaly regions can be retained to obtain the scores of the significant anomaly regions Score S . Score S = Score prior . × Score ms (11) The parameter t 2 is introduced for contrast enhancement of Score S .
At this time, the interference caused by phase anomalies and large-size anomalies is removed from the Score S , but it is less effective in detecting the anomalous regions that are close to the grayscale of MGT. Here, the strategies of image restoration and, again, multi-scale self-referencing templates are introduced to solve the above problems. The main idea is to fill the anomalous position in the original image f with the mean grayscale template MGT corresponding position data according to the significant anomaly detection score Score S .
The dot product operation is performed on Score S and MGT to obtain the grayscale data MGT for restoration.
The locations to be reconstructed in f are removed with Score S to obtain the anomaly region mask map f mask . f The final restoration map R is obtained by adding MGT and f mask .
The final anomalous region score result Score f inal is obtained by using the multi-scale self-referencing template method in Equations (7)-(9) again for the restoration map R. In Score f inal , phase errors are eliminated, the effect of large size anomalies is overcome and the response to low-contrast anomalies is high.
Since the response of the anomalous region is usually not uniform, in order to achieve the complete segmentation of the anomalous region, the region growth method is introduced here, setting the region growth start point t 3 and the region growth end point t 4 . The final anomalous region segmentation result Result can be obtained.

Bilinear Interpolation Unfolding Effect
Image unfolding is a key part of the data processing of the algorithm in this paper, where the thin-walled annular region of interest needs to be unfolded into a long strip of images to facilitate the development of the algorithm. The region of interest of the ring is converted to a rectangular region, and the area of the region of interest increases so it involves interpolation of the pixels of the increased region in the unfolded image. Since the algorithm of this paper is based on the uniformity of grayscale in the horizontal direction of the unfolded image, if the interpolation method is not well chosen, it leads to the generation of a jagged texture in the horizontal direction of the unfolded image, as shown in Figure 8a, which affects the detection effect of the algorithm of this paper.  S Score (the significant anomaly detection score) processed by this paper's algorithm. As shown in Figure 8, the generation of jagged edges in the expanded image can be avoided by the bilinear interpolation method, and, for the algorithm in this paper, the anomalous response interference from jagged edges can also be effectively suppressed. As shown in the comparison results, the use of the bilinear interpolation method for image unfolding can effectively increase the degree of smoothness of the unfolding map to avoid the generation of jagged edges, which ensures the normal operation of the anomaly detection algorithm in this paper.  Figure 8 demonstrates the image unfolding results of the nearest-neighbor and bilinear interpolation methods and shows Score ms (the multi-scale self-referencing anomaly detection score) and Score S (the significant anomaly detection score) processed by this paper's algorithm. As shown in Figure 8, the generation of jagged edges in the expanded image can be avoided by the bilinear interpolation method, and, for the algorithm in this paper, the anomalous response interference from jagged edges can also be effectively suppressed.
As shown in the comparison results, the use of the bilinear interpolation method for image unfolding can effectively increase the degree of smoothness of the unfolding map to avoid the generation of jagged edges, which ensures the normal operation of the anomaly detection algorithm in this paper.

Multi-Scale Parameter Determination
The scale factor sk is related to the horizontal dimension of the anomalous region to be detected in the expanded image. When the value of sk is too small, the algorithm becomes weak in detecting anomalies of large size, and, at the same time, it causes misrecognition, as in the green area of Figure 9b. If sk is too large, it introduces phase or light uneven interference. Under actual working conditions, the size of the anomaly region varies greatly, and here in this paper, the pixel range of the horizontal size of the anomalous region is defined as 5~500 pixels. As shown in Figure 9a, in this paper, the self-referencing residual map Res is calculated for different sizes of artificial anomalous regions with different sk values, and the mean response value v of the anomalous region (red region in Figure 9b) is counted to determine the choice of the scale factor sk. The statistical results of the scale factor sk versus the mean response value v for the anomalous region are shown in Figure 10. The mean response value v of the anomalous region increases with the scale factor sk . As can be seen from the size of the anomalous region, the scale factor sk corresponding to the convergence position of the average response value increases as the size increases, while the final convergence value becomes smaller, which is consistent with the calculation principle of the self-referencing template method. From the figure, it can be seen that when size takes the maximum value of 500, the mean response value v takes the basic convergence at =2500 sk . Since the residual response is a grayscale difference, and considering that the human eye has the highest discrimination ability for grayscale display systems with a grayscale level of 8, that is, in the residual map Res , from 0 to 1, when the residual value > 1/8 = 0.125, the corresponding position can be considered as an anomalous region. To avoid the interference caused by the large-scale factor and to ensure the effect of anomaly detection, this paper takes =1000 sk when the mean response value v is 0.37, which meets the reliability standard of anomaly detection. The statistical results of the scale factor sk versus the mean response value v for the anomalous region are shown in Figure 10. The mean response value v of the anomalous region increases with the scale factor sk. As can be seen from the size of the anomalous region, the scale factor sk corresponding to the convergence position of the average response value increases as the size increases, while the final convergence value becomes smaller, which is consistent with the calculation principle of the self-referencing template method. From the figure, it can be seen that when size takes the maximum value of 500, the mean response value v takes the basic convergence at sk = 2500. Since the residual response is a grayscale difference, and considering that the human eye has the highest discrimination ability for grayscale display systems with a grayscale level of 8, that is, in the residual map Res, from 0 to 1, when the residual value > 1/8 = 0.125, the corresponding position can be considered as an anomalous region. To avoid the interference caused by the large-scale factor and to ensure the effect of anomaly detection, this paper takes sk = 1000 when the mean response value v is 0.37, which meets the reliability standard of anomaly detection.
The grayscale value of the artificial anomalous region in this paper is 0, which does not represent the full true anomaly region. However, due to the high error tolerance of the chosen average response value v, combined with a large number of supplementary experiments, this paper suggests that the algorithm in this paper has a better anomaly detection effect when the scale parameter sk is taken to be twice the maximum size of the anomalous region to be detected. The grayscale value of the artificial anomalous region in this paper is 0, which does not represent the full true anomaly region. However, due to the high error tolerance of the chosen average response value v , combined with a large number of supplementary experiments, this paper suggests that the algorithm in this paper has a better anomaly detection effect when the scale parameter sk is taken to be twice the maximum size of the anomalous region to be detected.
In summary, for the anomaly detection demand of 5~500 pixels in a horizontal direction, the scale factor sk selected in this paper takes the value range of 10~1000, and, to ensure that sk is an integer, the number of sk is taken as 6 by equal difference interval, and 10, 208, 406,802,1000 sk = .

Determination of Algorithm Grayscale Parameters
There are four grayscale parameters in this algorithm, which are the normalization threshold 1 t for normalizing the multi-scale self-referencing anomaly detection score ms Score , the threshold 2 t for contrast enhancement of the significant anomaly detection score S Score , the anomalous region growth start point 3 t and end point 4 t .
The normalization threshold 1 t serves to prevent the multi-scale self-referencing template score ms Score from being too low, which, in turn, amplifies the phase and noise disturbances during normalization. Studies have shown that the human eye has a highresolution accuracy for regions with grayscale differences > 0.125, i.e., the human eye resolution threshold is 0.125. Here, 1 t is set to equal the human eye resolution threshold of 0.125. The main function of contrast enhancement threshold 2 t for the significant anomalous region is to improve the contrast of anomalous regions for subsequent image restoration. If its value is too large, it affects the low-contrast abnormality recognition; if its value is too small, it introduces too much noise interference. To ensure the detection effect, its value is also taken to be the human eye resolution threshold 0.125. In summary, for the anomaly detection demand of 5~500 pixels in a horizontal direction, the scale factor sk selected in this paper takes the value range of 10~1000, and, to ensure that sk is an integer, the number of sk is taken as 6 by equal difference interval, and sk = 10, 208, 406, 802, 1000.

Determination of Algorithm Grayscale Parameters
There are four grayscale parameters in this algorithm, which are the normalization threshold t 1 for normalizing the multi-scale self-referencing anomaly detection score Score ms , the threshold t 2 for contrast enhancement of the significant anomaly detection score Score S , the anomalous region growth start point t 3 and end point t 4 .
The normalization threshold t 1 serves to prevent the multi-scale self-referencing template score Score ms from being too low, which, in turn, amplifies the phase and noise disturbances during normalization. Studies have shown that the human eye has a highresolution accuracy for regions with grayscale differences >0.125, i.e., the human eye resolution threshold is 0.125. Here, t 1 is set to equal the human eye resolution threshold of 0.125. The main function of contrast enhancement threshold t 2 for the significant anomalous region is to improve the contrast of anomalous regions for subsequent image restoration. If its value is too large, it affects the low-contrast abnormality recognition; if its value is too small, it introduces too much noise interference. To ensure the detection effect, its value is also taken to be the human eye resolution threshold 0.125.
The region growth parameters t 3 and t 4 are the key parameters that control the final segmentation results of the algorithm. Figure 11 shows the region growth start point t 3 comparison experiment. The starting value is taken as the human eye resolution threshold 0.125 for t 3 = 0.125, 0.1875 and 0.25, at which t 4 = 0.1. From the results of the experiment, it can be seen that if t 3 is too low, too much interference is introduced, and the noise is significantly suppressed with the increase in t 3 . Therefore, this paper selects the starting point of regional growth as t 3 = 0.25. the starting point of regional growth as 3 t = 0.25. From Figure 12, it can be seen that if the region growth end point 4 t is too low, too much noise interference is introduced, and, as 4 t increases, the noise interference decreases but the integrity of the segmentation is reduced. Considering the suppression of noise interference and the integrity of the abnormal region segmentation, this paper selects the region growth end point as 4 =0.1 t , whose value is slightly lower than the human eye resolution threshold. From Figure 12, it can be seen that if the region growth end point t 4 is too low, too much noise interference is introduced, and, as t 4 increases, the noise interference decreases but the integrity of the segmentation is reduced. Considering the suppression of noise interference and the integrity of the abnormal region segmentation, this paper selects the region growth end point as t 4 = 0.1, whose value is slightly lower than the human eye resolution threshold.

Effect of the a Priori Information on Detection
The self-referencing template method is less effective in detecting large-size anomalous regions but has a good suppression effect on phase errors, while the mean grayscale template is more effective in detecting large-size anomalies but introduces excessive phase errors. To verify the detection effect of the method in this paper for large-size anomalies and the suppression effect of phase errors, three different samples of large defects in the annular metal turning surface are selected in this section to compare the detection effect of the self-referenced template, mean grayscale template and the method in this paper. Figure 13 shows the comparison experiment of large-size anomaly detection effect. The figure shows that the direct use of the mean grayscale template works well for detecting large-size anomalous regions, but phase interference is introduced; self-referencing methods are effective in suppressing phase interference but have a low response for large sizes, while also introducing erroneous responses in non-anomalous regions; the a-priori-informationbased and multi-scale self-referencing template algorithm proposed in this paper combines the advantages of the mean grayscale template and self-referencing template to ensure the detection of large-size anomalies while effectively suppressing phase interference.

Effect of the A Priori Information on Detection
The self-referencing template method is less effective in detecting large-size anomalous regions but has a good suppression effect on phase errors, while the mean grayscale template is more effective in detecting large-size anomalies but introduces excessive phase errors. To verify the detection effect of the method in this paper for large-size anomalies and the suppression effect of phase errors, three different samples of large defects in the annular metal turning surface are selected in this section to compare the detection effect of the self-referenced template, mean grayscale template and the method in this paper. Figure 13 shows the comparison experiment of large-size anomaly detection effect. The figure shows that the direct use of the mean grayscale template works well for detecting large-size anomalous regions, but phase interference is introduced; self-referencing methods are effective in suppressing phase interference but have a low response for large sizes, while also introducing erroneous responses in non-anomalous regions; the a-prioriinformation-based and multi-scale self-referencing template algorithm proposed in this paper combines the advantages of the mean grayscale template and self-referencing template to ensure the detection of large-size anomalies while effectively suppressing phase interference.

Comparison of Commonly Used Unsupervised Anomaly Detection Methods
The algorithm in this paper is tested against three commonly used unsupervised anomaly detection algorithms, CFLOW-AD, PatchCore and PaDiM, which perform well

Comparison of Commonly Used Unsupervised Anomaly Detection Methods
The algorithm in this paper is tested against three commonly used unsupervised anomaly detection algorithms, CFLOW-AD, PatchCore and PaDiM, which perform well in the MVTec AD dataset. Since the object processed by the algorithm in this paper is a long expanded image, and the commonly used unsupervised anomaly detection algorithms CFLOW-AD, PatchCore and PaDiM process images that are square, to compare the performance of this paper with the above commonly used methods, in this part of the experiment, this paper intercepts the expanded images as samples of equal length and width to construct the comparison experimental dataset, as shown in Table 2. The programming environment of the algorithm in this paper is MatlabR2021b, and the programming environment of the comparison algorithm is Python, and the experiments are conducted in a Windows 10 system. The slave computer is configured with an Intel(R) Core (TM) i5-11400F @ 2.60 GHz 2.59 GHz and a memory of 16 G. The model of graphics card is an NVIDIA GeForce RTX 3060 with 12 GB memory.
Two evaluation indicators, AUROC and IOU, are selected. AUROC is a common indicator in the field of anomaly detection and is used to evaluate the anomaly response of each algorithm, which avoids the influence of subjective assumptions in threshold selection by summarizing the overall performance of the model under all possible thresholds. The IOU indicator is used to evaluate the anomalous region segmentation capability of each algorithm.
As can be seen from the Table 3, in terms of the Image AUROC and Pixel AUROC indicators, this paper's method achieves the optimal mean indicator for all three data. In terms of the Image AUROC indicator, the advantage of this method is more obvious compared with that of other methods. The reason for this is that this method has a higher response score for anomalous regions based on the grayscale difference, but the response score is not uniform over the same anomalous region. As a result, compared with other methods, this paper is better in terms of the Image AUROC indicator but slightly stronger in the Pixel AUROC indicator. The goal of anomaly detection is to segment the anomalous region, and the AUROC indicator more greatly reflects the divisibility of the anomalous region response score obtained by the algorithm. The ease of partitioning of the anomalous region response scores also needs to be considered when performing algorithm performance evaluation. In this section, we compare the image-level mean IOU indicator M_IOU of the anomalous region segmentation results of the method in this paper with those of other classical unsupervised methods, and the experimental results are shown in Table 4. From the M_IOU indicator values in the table, it can be seen that the M_IOU of all algorithms exceeds 0.5, indicating that the algorithms all have excellent anomaly segmentation ability. Compared with the Pixel AUROC indicator, the advantage of this paper's method is more obvious in the M_IOU indicator. The reason is that the Pixel AUROC indicator is affected by the area of the anomalous region; if the large anomalous region is correctly located, the Pixel AUROC indicator is greatly improved, while if the small anomalous region is incorrectly located, there is only a small decrease in the Pixel AUROC indicator. However, when the IOU is counted from each image, and the mean value is calculated, the effect of anomalous region size on the indicator is effectively overcome. The experimental results further demonstrate the effectiveness of the method in this paper.
Some of the experimental results are shown in Figure 14. Comparing the anomalous response results of the algorithms, the edges of the anomalous response region of the commonly used unsupervised anomaly detection methods are fuzzy, and the anomalous response region of this paper's method is more precise, responding only to the anomalous region and not to the non-anomalous region. From the segmentation results in the figure, it can be seen that the anomaly detection method proposed in this paper achieves the optimal anomaly localization results in contrast to Ground Truth. As shown in Figure 14b,g, the method in this paper still realizes the complete segmentation of defects in the case of an inhomogeneous anomalous response due to the segmentation strategy of region growing. For large-size anomalous regions, as shown in Figure 14c,f,i, the response of this paper's method to the anomalous regions is more significant compared with that of other comparison methods, and it avoids the problem of the response of the anomalous regions not being significant. For small-size anomalous regions, as shown in Figure 14j, the method in this paper still achieves good detection results based on the grayscale difference, while the commonly used anomaly detection methods have a very low response (CFLOW-AD and PaDiM) or no response (PatchCore).
In this paper, the operational efficiency of each algorithm is also analyzed. As shown in Table 5, the image sizes processed by each algorithm differ slightly when speed tests are performed due to the different input data sizes required by the different algorithms. In terms of efficiency, the algorithm in this paper achieves the highest detection speed.  In this paper, the operational efficiency of each algorithm is also analyzed. As shown in Table 5, the image sizes processed by each algorithm differ slightly when speed tests are performed due to the different input data sizes required by the different algorithms. In terms of efficiency, the algorithm in this paper achieves the highest detection speed.  The experimental results in this section demonstrate that the algorithm in this paper is suitable for annular metal turning surfaces with both good anomalous region segmentation capability and high speed, while the algorithm in this paper has no excessive configuration requirements for processing units and is easy to deploy for applications.

Conclusions
This paper focuses on solving the problem of anomaly detection in annular metal turning surface in industrial application scenarios. In this paper, the imaging characteristics of annular metal turning surfaces are utilized to unfold the annular region of interest into a rectangular region, which reduces the amount of data processing and simplifies the development of subsequent algorithms. This paper introduces statistical grayscale a priori information, which effectively improves the detection of large-size anomalous regions.
Through the acquisition of self-referencing multi-scale templates, combining the image's own information with the a priori information, the interference caused by contours and large-size anomalous regions is effectively suppressed. The segmentation operation of the response map using the region growing method improves the completeness of anomalous region segmentation. Compared to existing unsupervised anomaly detection algorithms, the method in this paper achieves the best average detection results in the three datasets of groove, thin-walled ports and valve seat data. The experimental results show that this method achieves a mean Pixel AUROC of 0.977, and the mean M_IOU of segmentation reaches 0.788. In terms of efficiency, this method is also much more efficient than the commonly used anomaly detection algorithms. The method proposed in this paper is versatile for annular metal turning surfaces and has lower hardware requirements and superior economics for industrial applications.
It is worth noting that the anomaly detection algorithm proposed in this paper is mainly for annular turning surfaces with uniform grayscale, and the algorithm is not applicable to objects with uneven grayscale in the expanded image, such as flange surfaces with holes and gear end faces. The algorithm in this paper still relies on grayscale differences and currently performs poorly for textural anomalies with very low grayscale differences (e.g., rippling). In future research, we intend to incorporate texture information into the development of the algorithm, as well to improve the detection of texture anomalies, and we will also explore the optimization of the algorithm in this paper to accommodate objects to be detected with uneven grayscale in the horizontal direction.