1 Introduction

Fabric is one of the materials that is used extensively in daily life. By textile technology development and high competition in the industry, quality products play an important role in satisfying the consumers. Reduction in the fabric defects is one of the significant factors to improve textiles quality. Defects [1] may reduce the fabric price by 45 to 65%. Also, approximately 85% of defects in garment industry, are caused by defects in the fabrics. So quality control is essential and very important in the textile industry. The traditional inspection procedure is based on the human visual inspection, which is slow and depends on the operator's accuracy, attention and experience. It is also difficult for humans, to inspect 1.6–2 m of fabric width. Therefore, the human-based inspection system has limited performance in terms of time, cost and quality. As a result, it is necessary to use an automated fabric inspection system in the textile industry. However, at least 70 types of fabric defects are introduced by the textile industry [2]. The variety of fabric defects, noise influence, vast variety of fabric patterns and similarity in defects shape and background texture are still serious challenges in automated fabric inspection. So far, researchers have proposed many techniques to address these challenges. These techniques can be divided into two general categories as motif-based and non-motif-based methods. The motif-based method has been developed in [3], which can detect defects on the most of 2D patterns. The non-motif-based methods are classified into methods for un-patterned and patterned fabrics. Defect detection of un-patterned fabrics has been extensively investigated, and researchers have presented various methods, such as auto-correlation [4], co-occurrence matrix [5], morphological operations [6], fractal method [7], Fourier [8] and wavelet [9] transforms. These methods can successfully detect defects on plain fabrics and are mainly designed for un-patterned fabrics defect detection. However, patterned fabric inspection is still a difficult problem because of complex textured structures. So far, a few methods, such as hash function [10], golden image subtraction [11], lattice segmentation and template statistics [12], Bollinger [13] and Regular [14] Bands have been developed for patterned fabric inspection. These methods can naturally be applied to detect the defects on repetitive patterns. Among patterned texture inspection, defect detection on irregular patterned fabrics is much more difficult. According to [2], model-based approaches have been proposed for randomly textured fabrics. However, these methods are weak to detect small defects, and they are also sensitive to lighting setup and noise. It should be noted that irregular fabric texture may vary randomly in shape, orientation and position. These variations make analysis more difficult. However, Dennis in [15] concluded that the Gabor filter (GF) outputs which are derived from non-uniformly textured images, exhibit many local variations that can be useful for texture segmentation. So, the first objective of our proposed algorithm is to consider an approach that can be applied to all the fabric groups in our database.

Recently, many techniques have been proposed for texture analysis that are based on the Gabor filter. Studies on human perception ability have led many researchers to use the multichannel GF for feature extraction. A detailed design of a fabric inspection model was presented in [16], based on the GF bank. An algorithm was also developed using the GF bank and the Principal Component Analysis (PCA) on uniform fabric defects [17]. However, applying a filter bank generates large feature vectors whose massive computing cost cannot be ignored. Therefore, an optimal set of one or two filters is used to extract features. These optimized filters are tuned in a particular direction and frequency range. The tuned GF requires minimum computation, but correct selection of its parameters is crucial. Recently, a number of optimization methods are used to optimize the GF parameters. Jing et al. [18] adopted a genetic algorithm (GA) for GF optimization to detect various colourful fabric defects. They also presented an effective scheme based on the optimal GF via GA [19]. In [20], the optimal GF is designed by GA, and combined with the golden image subtraction (GIS) to detect defect on patterned fabrics. Other optimization algorithms such as simulated annealing [21], differential evolution [22] and random drift particle swarm [23] have been used to optimize the GF parameters. Our second objective is to consider a robust optimization method to select the GF parameters, as the performance of GF-based models depends on correct selection of GF parameters.

Cuckoo optimization algorithm (COA), which is inspired by life style of a bird called Cuckoo, is introduced in [24]. Finding the best optimal point, increasing probability of achieving the global optimal solution and fast convergence are some of the advantages of the COA. In this paper, optimization of GF parameters based on COA is proposed which can lead to an optimal feature extraction from the image. The proposed method consists of training and defect detection steps. In the training step, the GF is adjusted to the texture features of a defect-free fabric image via the COA. In the defect detection step, the test fabric image is filtered by the optimal GF. Then, in order to segment the defective pixels from the background, the image is divided into blocks. An adaptive local binarization method is proposed that plays a significant role to boost the algorithm performance. For local binarization, various statistical properties of all blocks of the image are calculated. Then, a change-point detection algorithm [25] is used to determine threshold and find defective blocks.

The remaining of this paper is organized as follows. The GF is explained in Sect. 2. The proposed defect detection algorithm is described in Sect. 3. Section 4 provides the experimental results and related analysis to evaluate the performance of the proposed algorithm. Finally, the paper is concluded in Sect. 5.

2 Gabor filter

The 2D Gabor filter [26] is a complex exponential which is modulated by a 2D Gaussian function. In the spatial domain, it is defined as:

$$f\left( {x,y} \right) = \frac{1}{{2\pi \sigma_{x} \sigma_{y} }}exp\left[ { - \frac{1}{2}\left( {\frac{{x^{{\prime^{2} }} }}{{\sigma_{x}^{2} }} + \frac{{y^{{\prime^{2} }} }}{{\sigma_{y}^{2} }}} \right)} \right].exp\left( {2\pi jf_{0} x^{\prime} } \right)$$
(1)

where \(x^{\prime} = x \cos \theta - y \sin \theta\), \(y^{\prime} = x \sin \theta + y \cos \theta\), f0 is the filter central frequency, \(\left( {x^{\prime},y^{\prime} } \right)\) is the rotated coordinates \(\left( {x,y} \right)\) by the rotation angle of the filter θ, \({\upsigma }_{x}\) and \({\upsigma }_{y}\) are the Gaussian envelope along the x and y axes which are given by (2) and (3).

$$\sigma_{x} = \frac{1}{{\pi f_{0} }}\sqrt {\frac{\ln 2}{2}} \times \frac{{2^{B} + 1}}{{2^{B} - 1}}$$
(2)
$$\sigma_{y} = \lambda \sigma_{x}$$
(3)

The value of \(\sigma_{x}\) is determined by the half-peak bandwidth B. In (3),\(\lambda\) is the aspect ratio between \(\sigma_{x}\), and \(\sigma_{y}\), and it is used to adjust the output of the Gaussian. \(f_{0}\),\(\sigma_{x}\), \(\lambda\) and θ are the GF parameters which provide enough degree of freedom to design filters.

Based on (1), the Gabor filter \(f_{ }\) can be decomposed into real and imaginary parts as follows

$$f_{r} \left( {x,y} \right) = \frac{1}{{2\pi \sigma_{x} \sigma_{y} }}exp\left[ { - \frac{1}{2}\left( {\frac{{x^{{\prime^{2} }} }}{{\sigma_{x}^{2} }} + \frac{{y^{{\prime^{2} }} }}{{\sigma_{y}^{2} }}} \right)} \right].\cos \left( {2\pi jf_{0} x^{\prime} } \right)$$
(4)
$$f_{I} \left( {x,y} \right) = \frac{1}{{2\pi \sigma_{x} \sigma_{y} }}exp\left[ { - \frac{1}{2}\left( {\frac{{x^{{\prime^{2} }} }}{{\sigma_{x}^{2} }} + \frac{{y^{{\prime^{2} }} }}{{\sigma_{y}^{2} }}} \right)} \right].\sin \left( {2\pi jf_{0} x^{\prime} } \right)$$
(5)

It is found that only the real part of the Gabor filter is sufficient for inspecting fabric defects [19, 26]. The imaginary part requires nearly 50% of the total computation time and its effect on detection results is less pronounced. Thereby, only the real GF is adopted in our proposed algorithm. So, for a sample image Ii (x, y), the magnitude response of the filtered image is obtained by (6). Asterisk (*) denotes 2D convolution operation.

$$I\left( {x,y} \right) = \left\{ {{ }\left[ {f_{r} \left( {x,y} \right){*}I_{i} \left( {x,y} \right)} \right]^{2} { }} \right\}^{\frac{1}{2}}$$
(6)

3 Proposed work

3.1 Defect detection scheme

The defect detection can be considered as segmenting an image into defective and defect-free regions. Texture segmentation is classified into two categories, i.e. supervised and unsupervised segmentation [27]. The design of unsupervised inspection approach is difficult, and usually excessive computation is needed. In this paper, a supervised inspection problem is considered. In this case a priori knowledge about the textures is obtained from the defect-free fabric sample. Figure 1 shows our proposed defect detection scheme which consists of training and defect detection steps. In the training step, a defect-free fabric image is used as a template image for the optimization algorithm. The COA with the Fisher criterion [27] is adopted to find the optimal Gabor filter parameters. In the defect detection step, the optimal filter which is obtained in the offline stage, and thresholding are employed to detect defects. More details are presented in the following.

Fig. 1
figure 1

Block diagram of the proposed method

3.2 Fisher criterion

In our work, in order to tune the GF parameters to match a particular texture background, a cost function based on the Fisher criterion is adopted. This criterion [21, 27] has shown a significant performance in solving the supervised segmentation problems. For a given M × N template textile image T, the Fisher cost associated with the Gabor filter parameters S is defined according to (7).

$$F\left( {S,T} \right) = - \left( {\frac{{\mu^{S,T} }}{{\sigma^{S,T} }}} \right)^{2}$$
(7)

where \(\mu^{S,T}\) and \(\sigma^{S,T}\) are mean and standard deviation of the filtered image that are computed by (8) and (9), respectively. Filtered image \(I_{i,j}^{S,T}\) is obtained by (6).

$$\mu^{S,T} = \frac{1}{MN}\mathop \sum \limits_{i = 1}^{M} \mathop \sum \limits_{j = 1}^{N} I_{i,j}^{S,T}$$
(8)
$$\left( {\sigma^{S,T} } \right)^{2} = \frac{1}{MN - 1}\mathop \sum \limits_{i = 1}^{M} \mathop \sum \limits_{j = 1}^{N} (I_{i,j}^{S,T} - \mu^{S,T} )^{2}$$
(9)

In optimization process, the goal is to find the optimal parameter S* which minimizes the Fisher cost function \(F\left( {S,T} \right)\). Since \({\text{min}}\left\{ {F\left( {S,T} \right)} \right\}\) has a good discriminatory behaviour, the Gabor filter with parameter values S* provides good detection results. Since the Fisher cost function includes many local optimal solutions, it is difficult to find the optimal Gabor filter parameters manually. So, the optimization algorithm is an appropriate solution. In this way, a robust optimization algorithm can determine the optimal parameters by solving a constrained minimization problem based on the Fisher criterion. There exists a diverse range of optimization algorithms. One of the most powerful evolutionary algorithms that has ever been introduced is COA. In this paper, the COA is used which is described as follows.

3.3 Gabor filter optimization based on COA

The COA optimization technique was introduced in [23]. The special life style of cuckoos and their unique features in egg laying are the basis of this optimization algorithm. Researchers have demonstrated superior performance of the COA compared to other optimization algorithms in various optimization problems. Finding the best optimal point, increasing the probability of achieving the global optimal solution, fast convergence and handling any type of a cost function for large scale problems are some advantages of the COA. Obviously, we cannot claim that an optimization algorithm has better performance than other algorithms in different fields. However, the aforementioned characteristics and better performance on the tested problems have been the basic motivation to adopt the COA for our optimization problem.

The COA starts by an initial population of cuckoos. The values of problem variables form an array. This array is called “habitat” which represents current living position of cuckoo. In our optimization problem by using the binary encoding scheme, arrays of 0 s and 1 s are generated randomly. Each of them represents a candidate solution to the problem. The required GF parameters are encoded using 8 bits as shown in Fig. 2. So the population of cuckoos is composed of the binary strings with 24 bits length. Each of the encoded parameters in the binary strings changes into a decimal number by a linear mapping and the cost function is calculated. The optimization process is repeated until the number of iterations reaches to a preset value. Finally, in the last subsection, thresholding step is presented which is very crucial to success of the inspection system.

Fig. 2
figure 2

Habitat structure in the COA

3.4 Local thresholding

To distinguish defective pixels from textured background, an adaptive local binarization method is proposed. In this procedure, threshold values are obtained by extracting local features from the filtered image as follows. First, the filtered image of \(M \times N\) pixels is divided into non-overlapping 64 × 64 blocks. Next, the local features such as mean, standard deviation and difference of the maximum and the minimum intensity are calculated for each block. The experimental results illustrate that these statistical features are able to simply discriminate between normal and defective blocks. Then, the extracted mean values and the difference of intensity values from all blocks are sorted in an increasing order. Then a change-point detection algorithm [25] is applied to estimate two thresholds to find defective blocks. Actually, this method identifies the change-point value where a deviation in the feature values is caused by fabric defect occurrence. Now, if the features values do not change significantly, this method returns a null matrix. In this case, average of the blocks means and average of blocks intensity differences are considered as the thresholds. Figure 3 shows an example of the change-point detection on a defective fabric sample. The estimated change-point is \(1.9107 \times 10^{3}\). Finally, in order to binarize the defective texture blocks and localize defects, two threshold values are calculated according to Eq. (10):

$$T_{1,2} = {\text{mean}}\left[ {{\text{mu}}\left( {i,j} \right)} \right] \pm w \times {\text{mean}}\left[ {{\text{std}}\left( {i,j} \right)} \right]$$
(10)
Fig. 3
figure 3

Change-point detection from feature values of a defective sample

where \({\text{mean}}\left[ {{\text{mu}}\left( {i,j} \right)} \right]\) and \({\text{mean}}\left[ {{\text{std}}\left( {i,j} \right)} \right]\) are average of the blocks means and standard deviations, respectively. \(\left( {i,j} \right)\) represents the block in the ith row and the jth column. \((i = 1,2,3, \ldots,{\text{M}}/64\) and \(j = 1,2,3, \ldots,{\text{N}}/64)\). \(w\) is a constant parameter and is determined by trial and error on different fabric textures. Finally, thresholding is carried out on defective blocks to generate a binary image D(x, y) by:

$$D\left( {x,y} \right){ } = \left\{ {\begin{array}{*{20}l} {1, \quad I^{\prime}\left( {x,y} \right) > T_{2}\; {\rm or}\; I^{\prime}\left( {x,y} \right) < T_{1} } \\ {0,\quad T_{1} \le I^{\prime}\left( {x,y} \right) \le T_{{2{ }}} } \\ \end{array} } \right.$$
(11)

where \(I^{\prime}\left( {x,y} \right)\) denotes intensity values of defective blocks. After binarization, a median filter is applied as post-processing to eliminate various noises.

4 Experimental results

The performance of the proposed algorithm is evaluated using two datasets, namely the public TILDA dataset [28] and the online Fabric Stain Dataset (FSD) [29]. TILDA contains fabric images of four classes. Each class has two kinds of fabrics. In our experiments, all kinds of fabrics with four different defect classes (e1–e4) and one defect-free class (e0) are considered. All images have the size of 512 × 768 pixels and 8-bit grey level. The Fabric Stain Dataset consists of polyester and cotton fabric images and the defects include ink, oil and dirt stains. The proposed algorithm is implemented in MATLAB R2018a-64 bit and windows 8.1 operating system. The programs are run on a personal computer with Intel(R) core(TM) 2 Duo CPU @ 2.53 GHz, 4 GB RAM. In our implementation, the mask size of the Gabor filter is set 15 × 15. The initial number of cuckoos is 5, and the minimum and the maximum number of eggs for each cuckoo are 2 and 4, respectively. The number of clusters is 1, and the maximum number of iterations is set to 15. Also, in the optimization process of the filter, the half-peak bandwidth B is set to 0.5 octaves and value ranges of other filter parameters are given in Table 1.

Table 1 Value ranges of the Gabor filter parameters

Performance evaluation of the proposed method is carried out based on the criteria which are defined according to (12), (13) and (14) [22]:

$${\text{Recall}}\left( {{\text{Sensitivity}}} \right) = \frac{{{\text{TA}}}}{{\left( {{\text{TA}} + {\text{FN}}} \right)}}$$
(12)
$${\text{Specificity}} = \frac{{{\text{TN}}}}{{\left( {{\text{TN}} + {\text{FA}}} \right)}}$$
(13)
$${\text{Accuracy}} = \frac{{\left( {{\text{TA}} + {\text{TN}}} \right)}}{{\left( {{\text{TA}} + {\text{TN}} + {\text{FA}} + {\text{FN}}} \right)}}$$
(14)

where True Abnormal (TA) shows correct detection including good defect localization. It means that only the defective pixels are white in the binary output image. True Normal (TN) and False Abnormal (FA) mean that no white pixels and white pixels, respectively, appear in the binary output of a defect-free sample. False Normal (FN) means that there are no white pixels in the binary image, although the original image is defective.

4.1 Performance evaluation

Based on the regularity of the texture of fabric images, the TILDA database is divided into four classes, namely C1, C2, C3 and C4. C1 includes plain fabrics with very fine invisible structure. C2 contains plain fabrics with visible texture and grid-like structure. C3 and C4 include patterned fabrics with very visible periodic structure and without clear periodic structure, respectively. Also all the fabric images in the Fabric Stain Dataset are plain.

4.1.1 Defect detection results

Figures 4 and 5 show some detection results of fabric samples in the two databases. The fabric images with small defects are successfully detected by using the proposed approach. Figure 4 clearly shows the example of these defects and the successful segmentation. The plain fabric images of C1 and FSD dataset are smooth and uniform, in which the defects are accurately localized with high detection success rate. The grid-like textiles of the C2 class, i.e. C2r2 and C2r3, have a low variance stochastic structure, which causes the reduction of performance. Actually, some of the defect-free samples of C2 have acceptable imperfections, which should not be considered as defects, as shown in Fig. 6. Especially in C2r3, there are some small black holes that make the fabric inspection difficult.

Fig. 4
figure 4

Detection results on some sample images with small defects

Fig. 5
figure 5

Some detection results on sample images with different types of texture and defects

Fig. 6
figure 6

Some defect-free fabric images of C2 and defective fabric images of C4r1 from e2 and e3 error classes. The defective regions are emphasised in the last two images

Comparing to plain fabrics, patterned textiles are more difficult to inspect automatically because they have more complicated texture background. C3 comprises two kinds of regular patterned fabrics, namely r1 plaids and r3 stripes. Our algorithm can successfully detect the defects of various shapes, sizes and position in C3. The algorithm has also been tested with irregular patterned fabrics C4, namely C4r1 arabesquitic and C4r3 sunflower. In C4, the background and defects are difficult to distinguish and their automated inspection is more complicated. Thus, very little research has directly investigated this case. However, as can be seen in Figs. 4 and 5, our algorithm detects defects in C4 accurately. In C4r1, there exist some defective fabric images whose defects are similar to the natural pattern variations and the human may fail to detect them, as shown in Fig. 6. So our study is not included these images. The quantitative results on plain and patterned fabrics are listed in Tables 2 and 3 and graphically presented in Fig. 7.

Fig. 7
figure 7

Graphical representation of the proposed algorithm performance

Table 2 Performance evaluation of the proposed defect detection algorithm on plain fabrics
Table 3 Performance evaluation of the proposed defect detection algorithm on patterned fabrics

4.2 Computational complexity and defect detection time consumption analysis

Assuming the image size as \(M \times N\) and the filter size as \(n \times n\), the computation complexity of filtering operation on image is \(O\left( {n^{2} MN} \right)\). Suppose that in our algorithm we apply a Gabor filter of size \(n \times n\) and a median filter of size \(l \times l\), the computational complexity of our algorithm will be \(O\left( {\left( {n^{2} + l^{2} } \right)MN} \right)\). Compared to methods that are based the neural networks or Gabor filter bank, the computation cost of our algorithm is much lower.

To verify success of our algorithm in real-time inspection, average of detection time is measured after parameters optimization. It is found that the proposed method took less than 1 s to process all types of the fabric samples in TILDA dataset. During this time, an image of 512 × 768 size is scanned. Image resolution is 30 × 8 dpi which is equivalent to a fabric with width of 243.84 cm and height of 43.45 cm. Consequently, our method is able to inspect at least 26 m of fabric height per minute. In industry, the fabric production speed from weaving machine is about 30–45 cm/min [30, 31]. Our database basically consists of low resolution images. Even if the images are captured with higher resolution, the proposed method is of great competence and reliability for real time operation.

4.3 Comparison of the proposed method

Comparison of the proposed method with other state of the art methods that have used the TILDA database, is presented in Table 4. The number of sample images tested by each method is reported in parentheses. It can be seen that fabric samples in C3 and C4 are not included in the most of the past studies. The dual-scale sparse coding [32] used all the defective images of C1, C2 and C3 and has a high defect detection rate. However, this method is inefficient on C4. Therefore, the versatility of the proposed algorithm on various fabric textures is higher than other algorithms and the experiments verify its effectiveness for fabric defect detection. Furthermore, so far, to the best of our knowledge, such an automatic inspection of irregular pattern fabrics with more than 100 testing samples has not been investigated.

Table 4 Detection rate comparison of the proposed algorithm. Numbers in parentheses are number of sample images as a test images

The ability of the COA in tuning the parameters of GF to extract features of the texture and the efficiency of the change-point algorithm in the binarization process causes our algorithm to perform well on plain and patterned fabrics, including fabrics with irregular patterns.

5 Conclusions

In this paper, an effective algorithm for automated texture defect detection in plain and patterned fabrics has been presented. The proposed algorithm is based on the optimal Gabor filter which is designed on basis of the Fisher cost function. The robust cuckoo optimization algorithm is an efficient way to search for the optimal Gabor filter parameters that match texture features of a defect-free fabric image. The proposed algorithm does not need any prior information about defects, and it is computationally efficient. So, its practical applicability is boosted for real-time inspection process. A thresholding is then proposed which plays an important role in the success of the algorithm. According to the obtained results, the proposed algorithm is accurate, effective and robust for variety of defective and defect-free fabric images. Furthermore, the results are compared with other competitors, which demonstrates higher versatility of our research.