Three-dimensional inversion of knot defects recognition in timber cutting

The comprehensive utilization of wood is the main goal of log cutting, but knot defects increase the difficulty of rationally optimizing cutting. Due to the lack of real shape data of knot defects in logs, it is difficult for detection methods to establish a correlation between signal and defect morphology. An image-processing method is proposed for knot inversion based on distance regularized level set segmentation (DRLSE) and spatial vertex clustering, and with the inversion of the defects existing relative board position in the log, an inversion model of the knot defect is established. First, the defect edges of the top and bottom images of the boards are extracted by DRLSE and ellipse fitting, and the major axes of the ellipses made coplanar by angle correction; second, the coordinate points of the top and bottom ellipse edges are extracted to form a spatial straight line; third, to solve the intersection dispersion of spatial straight lines and the major axis plane, K-medoids clustering is used to locate the vertex. Finally, with the vertex and the large ellipse, a 3D cone model is constructed which can be used to invert the shape of knots in the board. The experiment was conducted on ten defective larch boards, and the experimental results showed that this method can accurately invert the shapes of defects in solid wood boards with the advantages of low cost and easy operation.


Introduction
Comprehensive utilization of wood is the main goal of log cutting optimization. The existence of knot defects in the log not only increases the difficulty of wood processing, but also affects the visual and mechanical properties of the boards after log cutting (Chang and Lin 2021). At present, researchers have carried out detection based on ultrasonic, X-ray, stress wave and other methods, attempting to establish a correlation between electrial signals and internal defects and invert the internal defects of logs (Qin et al. 2018;Bertolin et al. 2020;Cheng et al. 2020). However, acquisition of the spatial data of the deects in the log is still the primary task and a difficult problem of modeling and analysis. The spatial morphology of knot defects can be inversed by the integration of the boards after log sawing. In other words, through the relative positions of the internal defects of the board, the information of knot defects can be inverted. For the defect detection in the board, Olsson et al. (2013) used laser imaging technology to calculate the microfibril angles of the surface, and then described the internal defects of the board. Hittawe et al (2015b) used X-ray technology to describe the shapes of knot defects in solid wood boards, and to predict their mechanical parameters by calculating the sectional area and spatial depth of knot defects. Huber et al.

Abstract
The comprehensive utilization of wood is the main goal of log cutting, but knot defects increase the difficulty of rationally optimizing cutting. Due to the lack of real shape data of knot defects in logs, it is difficult for detection methods to establish a correlation between signal and defect morphology. An image-processing method is proposed for knot inversion based on distance regularized level set segmentation (DRLSE) and spatial vertex clustering, and with the inversion of the defects existing relative board position in the log, an inversion model of the knot defect is established. First, the defect edges of the top and bottom images of the boards are extracted by DRLSE and ellipse fitting, and the major axes of the ellipses made coplanar by angle correction; second, the coordinate points of the top and bottom ellipse edges are extracted to form a spatial straight line; third, to solve the intersection dispersion of spatial straight lines and the major axis plane, K-medoids clustering is used to locate the vertex. Finally, with the vertex and the large ellipse, a 3D cone model is constructed which can be used to invert the shape of knots in the board. The experiment was (2022) presented a method for reconstructing the geometry, pith, knots, and local fiber orientations in timber boards, based on X-ray computed tomography scans. Yu (2019) used near infrared spectroscopy to construct the relationship between edge angles and spectral characteristics and used inclination values to describe the positions of knots. Despite the ability to detect the morphology of knots, these methods have the disadvantages of high equipment cost and difficult technology.
Image processing has the advantages of simple system composition, strong ability in information acquisition and flexibility in detection. In the process of image processing, the defect regions can be segmented effectively through the effective fusion between algorithms (Li et al. 2020). Hittawe et al. (2015a) extracted the LBP & SURF features of the sheet, used an image processing process combining contrast enhancement, entropy maximization and image filtering to detect potential defect areas.  proposed a region growing segmentation method based on image fusion to realize fast and accurate segmentation of defect regions. Luo and Sun (2019) proposed an optimization algorithm for image binarization based on local thresholds to solve the problem of uneven background of wood defect images. In recent years, level set methods have ovecome the shortcomings of complex steps and cumbersome calculations required in the fusion process of various methods. These level set methods naturally change the topology with continuous evolution curves to obtain more detailed features, and thus can segment target objects with complex shapes (Ramu et al. 2021;Gao et al. 2022). Li et al. (2010) proposed the model of distance regularized level set evolution (DRLSE), and the internal energy of the distance regular term is used to reduce the deviation between the level set function and the distance sign function based on the level set. The regulaized level set model has higher segmentation accuracy compared with the traditional level set methods.
Although the segmentation algorithm can capture the defect contour of the board, how to use image processing technology to inverse the internal shape of defects has not been fully studied. Guindos and Guaita (2013) held that knots exist in the shape of 3D cones. In the processing of boards, elliptical knots usually show on the top and bottom surfaces. However, as wood is a heterogeneous and anisotropic biomaterial, such ellipses will often be warped and distorted, which brings difficulties to the image inversion of the cone. This paper used DRLSE to segment the edge region of the defect, thus eliminating the influence of noise caused by texture and depression in the background, and then fitted the defect boundary by elliptic equation. In addition, angle correction was carried out to solve the problem of the non-coplanar major axes of the ellipses. Aiming at the deviation in proportion to the major and minor axes of the ellipses that cause the non-intersection of the edge lines and difficulty in vertex determination, this paper employed a clustering method, K-medoids, eliminating interference points to locate the vertices. Finally, the shapes of knots in the boards were inverted according to the bottom ellipse and vertex position information, which provided data support for the inversion of internal knots of the log.

Materials
Ten larch boards with knots on top and bottom surfaces were selected as experimental materials. Camera JHUM130m-E and lens HM5014MP5 were used to collect images of the defects in sufficient light. Examples of board defect images are shown in Fig. 1. In the experiment, the image resolution is 762 × 176 pixels. The boards varied in length and width. In order to facilitate analysis, the fitting accuracy was measured by pixel scale.

DRLSE algorithm for defect segmentation
Compared with the traditional active contour models, DRLSE adds an internal energy function which can automatically adjust the deviation between the level set function and the signed distance function. As a result, there is no need to repetitively initialize the level set function in the iterative process. The energy functional is set as in Eq. 1:

Fig. 1 Defects on a top and b bottom surfaces of boards
Where ▽ is the derivative operator; > 0 and is a constant; ∫ Ω p(|∇ |)dx is the internal energy function; p is the energy density function, defined as in Eq. 2.
∫ Ω g ( )|∇ |dx + ∫ Ω gH (− )dx is the external energy function, which controls the zero level set curve to move towards the target boundary, where, > 0 ; Ω refers to the image region; (x) is the Dirac formula, as shown in Eq. 3; H (x) is the Heaviside formula, as shown in Eq. 4; g(I) is the edge detection function of the input image I(x, y) , as shown in Eq. 5.
Where ε represents the width of the function, usually 1.5.
where ▽ is the space derivation operator; G σ indicates the Gaussian filter with the standard deviation σ; I represents the image; * represents the convolution operation.
The goal of the DRLSE model is to minimize the energy function E( ) , and the iterative process of the level set function is solved by using the partial differential equation, as shown in Eq. 6.
Where is (x, y, t) , representing the level set function; div indicates divergence; div d p (|∇ |)∇ is the internal energy function, > 0 ; ( )div g(I) ∇ |∇ | is the energy determinant of the function, the value of which keeps getting smaller in the iterative process until reaching the edge contour of the detected region, > 0 ; g(I) ( ) can determine the change direction of the level set function contour in the iterative change process (Zhang and Zhang 2018). When < 0 , the contour curve expands outward; when > 0 , the contour curve shrinks inward; when = 0 , the contour curve will not move but makes the contour smoother.

Ellipse fitting and angle correction
After image segmentation of the knots, the top and bottom defect boundary coordinates are obtained. Since the defects are not ideal ellipses, the least squares-based ellipse fitting algorithm is used to obtain the coefficient equation, as shown in Eq. 7: Where x and y are elliptic plane coordinates, while A, B, C, D, E and F are the coefficients of the equations.
The center of elliptic parameters O(x 0 , y 0 ) and plane inclination angle θ (angle between the major axis and X axis of the ellipse in the horizontal plane), semi major axis a and semi minor axis b can be obtained from the elliptic equation. The relationship between elliptic equation coefficients and elliptic parameters is shown in Eq. 8 (Flores and Rivera 2020): As the inclination angles θ 1 and θ 2 of the ellipses fitted from the top and bottom surfaces are different, the major axes of the two ellipses are not coplanar, and the data of the cone vertex coordinate set is highly dispersed. Eq. 9 is introduced to correct the inclination angles. After angle correction, two parallel ellipses with different heights and sizes are formed. The large ellipse is selected as the cone bottom, and the small ellipse is regarded as the cross-section between the cone and the top plane of the board.

Vertex location based on K-medoids
Taking the major axis points of the ellipse as the start, the edge points of the corresponding angles of the two ellipses every 15° are connected to form edge lines, totaling 22 edge lines (excluding two major axis points). Theoretically after angle correction, both the top and bottom ellipses have ) sin cos C = a 2 cos 2 + b 2 sin 2 D = −2(a 2 sin 2 + b 2 cos 2 ) * x 0 + 2(a 2 − b 2 ) sin cos * y 0 E = −2(a 2 cos 2 + b 2 sin 2 ) * y 0 + 2(a 2 − b 2 ) sin cos * x 0 F = −2(a 2 − b 2 ) sin cos * x 0 y 0 + (a 2 sin 2 + b 2 cos 2 ) * x 2 0 +(a 2 cos 2 + b 2 sin 2 ) * y 2 0 − a 2 b 2 (9) 3 = 1 + 2 2 1148 Y. Zhang et al. identical length-width ratios, and the edge lines and the major axis plane would intersect at the same point, i.e., the vertex of the cone. However, in fact, there would be deviation in length-width ratios of the top and bottom ellipses, and in the calculation during angle correction of the ellipses, so the edge lines and the major axis plane will intersect at different points within a certain range and an ideal cone cannot be formed. To locate the vertex position of the cone, a clustering algorithm is introduced to eliminate the influence of interference points and obtain more accurate vertex position. Twenty-two edge lines form a vertex dataset, yet with extreme values further away from the main cluster. The K-means algorithm is a popular cluster analysis method, and the clustering algorithm takes Euclidean distance as the measurement standard, which is subject to significant influence of the extreme values and thus is not conducive to the accurate selection of clustering center. Instead, K-medoids uses Manhattan distance as the measurement standard, and the sample points are taken as the center which is less affected by extreme values (Yu et al. 2018). Therefore, the K-medoids was selected as the clustering method for vertex coordinates, its loss function shown in Eq. 10.
where K is the number of clusters; N is the number of elements contained in each cluster; r kn indicates that when the data point n is classified into class k, n = 1, otherwise n = 0; | | x n − k | | indicates the Manhattan distance from the element to the center of cluster.
The specific process of this method is shown in Fig. 2.

RLSE-based defect segmentation
In the iterative process of the DRLSE algorithm, the time step τ is positively correlated with the update speed and convergence state of level set curve (Zhang and Guo 2015).
Here, τ = 6. To maintain the stability of level set evolution curve, it is necessary to meet τ*μ < 0.25 (Shen and Zhu 2015). After a number of experiments, the parameters of DRLSE are set as follows: μ = 0.04, λ = 4.2, α = 2.2, ε = 1.5, and Gaussian filter parameter σ = 2.3. Figure 3 shows the result of board defect image segmentation by DRLSE and that by the traditional edge detection Canny. By Canny, the segmentation result contains a large number of interferences from texture and groove on board surface. On the contrary, the DRLSE will gradually eliminate the influence of wood surface dent and texture in the iterative process and finally accurately capture the boundary of the knot defect in the image. Table 1 shows the ellipse parameters fitted on the top and bottom surfaces of 10 boards. For the convenience of calculation, all ellipses with a large area are set as bottom ellipses to ensure the positive ordinates of the cone points. All experimental boards are of the same 96-pixel length, recorded as h = 96. According to the data in the table, there is a certain deviation in the length-width ratio of the bottom surface and section ellipse, but as the deviation is small, it can approximately form a cone.

Vertex location using K-medoids
Vertex clustering is carried out for the ellipse parameters (Table 1), and the clustering results of cone vertex data for some board defects are given in Fig. 4. The vertex dataset has a linear spatial distribution to a certain extent, featured as "convex", and presents the distribution characteristics of one central cluster with two lateral clusters. The data in the central cluster are valid, while those in both lateral clusters are invalid; the cluster number K p is set to be 3. Cluster analysis is made by using K-medoids, with the average value of the cluster center of the intermediate cluster as the cone vertex.

Analysis of defect inversion results
The cone morphology is related to three parameters: let bottom area of the model be (f S1 ), sectional area of the model be (f S2 ) and the midpoint of the model section be (l 2 ) (the midpoint of model bottom coincides with the midpoint of actual defect image by default). These three parameters of the cone model are compared with the parameters S 1 , S 2 and O 2 of the original image, as shown in Fig. 5. Their calculations are shown in Eqs. 11−13.
As shown in Table 2, the deviation rates of the bottom area of the model compared with the actual area, f S1 are  between 1 and 4%, with an average value of 2.4%, which is a minor deviation from the actual value, indicating that the data is reasonable. Tables 3, 4 show the comparison between the results of K-medoids and K-means algorithms.
In the case of K-medoids algorithm, the deviation rate of sectional area f s1 = 2.80%, and the deviation distance of section midpoint l 2 = 0.281 pixel length, which is slightly better than 2.98% and 0.363 pixel length, respectively, in the case of K-means algorithm, but the difference is not obvious.   Further analysis of the reasons for the insignificant improvement with K-medoids compared with K-means showed that: most of the defective boards selected in the experiment are small, which leads to more concentrated vertex dataset and the minor influence of extreme values. As a result, compared with K-means, the K-medoids algorithm does not contribute to an obvious improvement. However, with larger test-pieces, such as Boards no. 6 and 7 in the Table 4, the vertex data will be more dispersed and the extreme values will have greater influence, so the deviation is greatly reduced with K-medoids compared with K-means, thus making greater improvement. In conclusion, K-medoids clustering is better than K-means clustering.

Conclusion
In view of the complexity and high cost of existing inversion methods for knot defects of boards, this paper proposed to segment the knot by DRLSE and realize the inversion of knot defects through ellipse fitting, angle correction, spatial line generation and vertex clustering. The experimental results showed that the level set image segmentation alogrithm has the advantages of strong anti-interference ability, good robustness and high segmentation accuracy. Ellipse fitting and angle correction were carried out to generate the fitting ellipses of the top and bottom surfaces with coplanar major axes. The vertex dataset was obtained by spatial line fitting, and the influence of interference points was eliminated by clustering the vertex dataset of the model through K-medoids. In this way, the defect vertex can be accurately located, the defect shape can be inverted, and then we can obtain the spatial coordinates of the defect in the log. Our proposed method is capable of detection and inverting the knots' shape inside the plate with defects existing on the top and bottom.
Open Access This article is licensed under a Creative Commons Attribution 4.0 International License, which permits use, sharing, adaptation, distribution and reproduction in any medium or format, as long as you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons licence, and indicate if changes were made. The images or other third party material in this article are included in the article's Creative Commons licence, unless indicated otherwise in a credit line to the material. If material is not included in the article's Creative Commons licence and your intended use is not permitted by statutory regulation or exceeds the permitted use, you will need to obtain permission directly from the copyright holder. To view a copy of this licence, visit http://creativecommons.org/licenses/by/4.0/.