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. (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. Zhang et al. (2015) 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 and methods

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.

Fig. 1
figure 1

Defects on a top and b bottom surfaces of boards

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:

$$E(\phi ) = \mu \int_{\Omega } {p\left( {\left| {\nabla \phi } \right|} \right)dx} + \lambda \int_{\Omega } {g(I)\delta_{\varepsilon } (\phi )\left| {\nabla \phi } \right|} dx + \alpha \int_{\Omega } {g(I)H_{\varepsilon } ( - \phi )} dx$$
(1)

Where ▽ is the derivative operator; \(\mu > 0\) and is a constant; \(\int_{\Omega } {p\left( {\left| {\nabla \phi } \right|} \right)dx}\) is the internal energy function; p is the energy density function, defined as in Eq. 2. \(\lambda \int_{\Omega } {g\delta_{\varepsilon } (\phi )\left| {\nabla \phi } \right|} dx + \alpha \int_{\Omega } {gH_{\varepsilon } ( - \phi )} dx\) is the external energy function, which controls the zero level set curve to move towards the target boundary, where, \(\lambda > 0\); \(\Omega\) refers to the image region; \(\delta_{\varepsilon } (x)\) is the Dirac formula, as shown in Eq. 3; \(H_{\varepsilon } (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.

$$p(s) = \left\{ {\begin{array}{*{20}c} {\frac{1}{{4\pi^{2} }}(1 - \cos (2\pi s)),s < 1} \\ {\frac{1}{2}(s - 1)^{2} ,s \ge 1} \\ \end{array} } \right.$$
(2)
$$\delta_{\varepsilon } (x) = \left\{ {\begin{array}{*{20}c} {\frac{1}{2\varepsilon }\left[ {1 + \cos \left( {\frac{\pi x}{\varepsilon }} \right)} \right],\left| x \right| \le \varepsilon } \\ {0,\left| x \right| \ge \varepsilon } \\ \end{array} } \right.$$
(3)
$$H_{\varepsilon } (x) = \left\{ {\begin{array}{*{20}c} {\frac{1}{2}\left( {1 + \frac{x}{\varepsilon } + \frac{1}{\pi }\left( {\frac{\pi x}{\varepsilon }} \right)} \right),\left| x \right| \le \varepsilon } \\ {1,x > \varepsilon } \\ {0,x < - \varepsilon } \\ \end{array} } \right.$$
(4)

Where ε represents the width of the function, usually 1.5.

$$g(I) = \frac{1}{{1 + \left| {\nabla G_{\sigma } *I(x,y)} \right|^{2} }}$$
(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(\phi )\), and the iterative process of the level set function is solved by using the partial differential equation, as shown in Eq. 6.

$$\frac{\partial \phi }{{\partial t}} = \mu {\text{div}}\left( {d_{p} \left( {\left| {\nabla \phi } \right|\phi } \right)\nabla \phi } \right) + \lambda \delta_{\varepsilon } (\phi )div\left( {g(I)\frac{\nabla \phi }{{\left| {\nabla \phi } \right|}}} \right) + \alpha g(I)\delta_{\varepsilon } (\phi )$$
(6)

Where \(\phi\) is \(\phi (x,y,t)\), representing the level set function; div indicates divergence; \(\mu {\text{div}}\left( {d_{p} \left( {\left| {\nabla \phi } \right|} \right)\nabla \phi } \right)\) is the internal energy function, \(\mu > 0\); \(\lambda \delta_{\varepsilon } (\phi )div\left( {g\left( I \right)\frac{\nabla \phi }{{\left| {\nabla \phi } \right|}}} \right)\) 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, \(\lambda > 0\); \(\alpha g(I)\delta_{\varepsilon } (\phi )\) can determine the change direction of the level set function contour in the iterative change process (Zhang and Zhang 2018). When \(\alpha < 0\), the contour curve expands outward; when \(\alpha > 0\), the contour curve shrinks inward; when \(\alpha = 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:

$$Ax^{2} + Bxy + Cy^{2} + Dx + Ey + F = 0$$
(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):

$$\left\{ \begin{gathered} A = a^{2} \sin^{2} \theta + b^{2} \cos^{2} \theta \hfill \\ B = - 2(a^{2} - b^{2} )\sin \theta \cos \theta \hfill \\ C = a^{2} \cos^{2} \theta + b^{2} \sin^{2} \theta \hfill \\ D = - 2(a^{2} \sin^{2} \theta + b^{2} \cos^{2} \theta )*x_{0} + 2(a^{2} - b^{2} )\sin \theta \cos \theta *y_{0} \hfill \\ E = - 2(a^{2} \cos^{2} \theta + b^{2} \sin^{2} \theta )*y_{0} + 2(a^{2} - b^{2} )\sin \theta \cos \theta *x_{0} \hfill \\ F = - 2(a^{2} - b^{2} )\sin \theta \cos \theta *x_{0} y_{0} + (a^{2} \sin^{2} \theta + b^{2} \cos^{2} \theta )*x_{0}^{2} \hfill \\ + (a^{2} \cos^{2} \theta + b^{2} \sin^{2} \theta )*y_{0}^{2} - a^{2} b^{2} \hfill \\ \end{gathered} \right.$$
(8)

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.

$$\theta_{3} = \frac{{\theta_{1} + \theta_{2} }}{2}$$
(9)

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 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.

$$J = \min \sum\limits_{k = 1}^{K} {\sum\limits_{n = 1}^{N} {r_{kn} } } \left| {x_{n} - \mu_{k} } \right|$$
(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; \(\left| {x_{n} - \mu_{k} } \right|\) indicates the Manhattan distance from the element to the center of cluster.

The specific process of this method is shown in Fig. 2.

Fig. 2
figure 2

Flow chart of experiment

Results

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.

Fig. 3
figure 3

Comparison of edge segmentation results between a canny operator and b DRLSE

Ellipse fitting and correction results

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.

Table 1 Parameters of top and bottom ellipses

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 Kp 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.

Fig. 4
figure 4

K-medoids clustering results of some vertex datasets for Board No. 1−3 ac

Analysis of defect inversion results

The cone morphology is related to three parameters: let bottom area of the model be (fS1), sectional area of the model be (fS2) and the midpoint of the model section be (l2) (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 S1, S2 and O2 of the original image, as shown in Fig. 5. Their calculations are shown in Eqs. 1113.

$$f_{s1} = \frac{{S_{1} ^{\prime} - S_{1} }}{{S_{1} }}$$
(11)
$$f_{s2} = \frac{{S_{2} ^{\prime} - S_{2} }}{{S_{2} }}$$
(12)
$$l_{2} = \sqrt {[O_{2} ^{\prime}(x) - O_{2} (x)]^{2} + [O_{2} ^{\prime}(y) - O_{2} (y)]^{2} }$$
(13)
Fig. 5
figure 5

Schematic diagram of model verification

As shown in Table 2, the deviation rates of the bottom area of the model compared with the actual area, fS1 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 \(\overline{f}_{s1}\) = 2.80%, and the deviation distance of section midpoint \(\overline{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.

Table 2 Comparison between bottom area of the model S1′ and actual image area S1
Table 3 Comparison between sectional area of the model S2′ and actual value O2 with different methods
Table 4 Comparison between model section center O2′ and actual position O2 with different methods

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.