A pre-processing technique to decrease inspection time in glass tube production lines

In case of glass tube for pharmaceutical applications, high-quality defect detection is made via inspection systems based on computer vision. The processing must guarantee real-time inspection and meet increasing rate and quality requirements. Defect detection in glass tubes is complicated by aspects that hamper the efﬁciency of state-of-the-art techniques. This paper presents a pre-processing algorithm which excludes portions of the image where defects are surely absent. The approach decreases the time for defect detection and classiﬁcation phases (any detection algorithm can be applied), as they are applied only in high-probability candidate sub-image. We derive a methodology to get robust values of algorithm’s parameters during production. The algorithm relies on detrended standard deviation and double threshold hysteresis, which solve issues related to the misalignment between illuminator and acquisition camera, and enable a robust detection despite rotation, vibration, and irregularities of tubes. We consider Canny, MAGDDA, and Niblack algorithms. The solution keeps the detection quality of such algorithms and reaches a 4.69 × throughput gain. It represents a methodology to obtain defect detection in time-constrained environments through a software-only approach, and can be exploited in par-allel/accelerated solutions and in contexts where a linear camera is utilized on both ﬂat and uneven surfaces.


INTRODUCTION
Glass tubes, to be used as containers for pharmaceutical use such as vials, syringes, and carpules, are typically been made of borosilicate glass, which is widely considered the most traditional and effective choice for parenteral drug containers or delivery systems [1,2]. Such kind of glass can present production defects like knot inclusions (blobs), lines of air, or flexible fragments called lamellae, and these defects can produce subsequent problems and pharmaceutical recalls [1,3,4]. Glass issues have been related to numerous product recalls, and over 100 million units of drugs packaged in borosilicate glass containers have been withdrawn from the market [5][6][7]. Therefore, pharmaceutical glass manufacturers must accurately inspect the quality of the tubes, to accept only the ones having defects, if any, below the threshold that prevents them from degrading the final quality of the product. An automatic inspection system can be used, based on a machine vision system with a back illuminator ( Figure 1). On average, glass has no patterns and it has monochromic and translucency dominant characteristics. However, glass defects produce variations in refraction properties around them and can cause local variations in light intensity in glass images, which image processing techniques [8] aim to detect.
Machine vision has been successfully adopted for performing inspections in many industrial processes [8,[9][10][11][12][13][14][15][16]. The classical approach consists in extracting the image parts containing the object of inspection [9,11]. Subsequently, algorithms are applied to identify image regions whose pixels may belong to a defect [8,14,17,18]. The final phase consists of the classification of defects, in which characteristics of the identified regions are derived as to eventually assign defects to predetermined classes.
Current evolution of glass production process involves both high accuracy in defect detection and faster production lines. It is necessary to design new systems aiming at doubling the current production speed and improving current defect-detection quality. Therefore, the throughput FIGURE 1 The inspection system requirement of the detection is getting more and more critical over time.
From the vision standpoint of defect detection, we must consider the following major production issues: (a) The cylindrical shape of the glass produces darker areas near the edges of the tube profile image, (b) the tube is partly suspended, it rotates and vibrates and, (c) in some instants, the illuminator is not perfectly centred with the tube, (d) the glass is not perfectly cylindrical but has irregularities (it has a "sausage-like" shape). All these aspects prevent using techniques known in the literature [8,[17][18][19]20] effectively.
With the aim of reducing the processing time required by defect detection and classification phases, in this paper we propose a fast preprocessing approach to exclude from detection the portions of the image where no defects are present. To exploit such an approach, we develop an algorithm (DSDRR, Detrended Standard Deviation Region of interest Reduction algorithm), based on detrended standard deviation (DSD) and double threshold hysteresis. The detrended standard deviation detects the image portions that may contain a defect, solving the issues related to alignment between the illuminator and the acquisition camera. Double thresholds with hysteresis allow robust detection of the image portions despite rotation, vibration, and geometric irregularity of the tube. The algorithm relies on parameters that affect the detection quality and throughput. We have refined a methodology, to be applied in the setup process of a new production batch, which determines the values that ensure robustness in the detection at the cost of a minimum temporary degradation of the throughput. These values are then kept constant throughout the full-throughput production batch.
The comparative evaluation of DSDRR has been carried out considering three different detection algorithms available in literature: Canny, MAGDDA, and Niblack. Results in all the cases show that the algorithm keeps the same inspection quality, reduces the inspection area by 88% on average, and achieves a throughput improvement by at least 1.97× and up to 4.69× in more computational demanding cases.
The proposal represents a methodology to obtain defect detection in time constrained environment by acting only on software, so it can be extended to application domains where computation and/or energy resources are limited. Differently from other proposal in literature, this technique does not reduce the resolution of the images, which could prevent or severely hamper defect detection. It can also be useful in environments where high-performance resources are available, as it reduces computation time, the communication cost and power consumption.

THE INSPECTION SYSTEM
The most relevant classes of defects for pharmaceutical glass production [9,21], are the following, and their criticality derives from the size of defect features as well as the considerable impact on the final quality of the product. Specifically: 1. Knot inclusion (Blobs), caused by imperfections in the raw materials used in the furnace; they appear on the tube surface as circular lenses, while they appear on the image with a back illuminator as dark patches ( Figure 2). 2. Air lines, caused by air bubbles in the furnace which are pulled by the drawing machine; they appear as long dark lines (Figure 3), having their ends thinner than the central part.
Tubes cannot have air lines in their used section for some pharmaceutical applications, such as in vaccine syringes, because if they are too close to the surface of the tube, they can break (open line), and cause contamination of the content. The blob is related to the cosmetic look of the glass container, and, in the Such defects can be detected by an Inspection System, which has two macro-components: Image Acquisition Subsystem and Host Computer (Figure 1, [9]). The Image Acquisition Subsystem acquires the digitized images (frames). It includes a LEDbased illuminator, a line scan camera in front of the illuminator, and a frame grabber, which (a) assembles individual sequential lines, captured by the camera, into a whole frame, and (b) transfers the frame to the Host Computer. Our system utilizes three cameras, with one illuminator each, as to cover all 360 • of the tube's external surface.
The inspection is performed while the tube is moving from the furnace to the Drawing Machine. The subsequent Cutting and Discarding Machine (CDM) cuts the tube in slices, each about 1.5 m long. The Host Computer implements defect detection and classification algorithms and can sends a discard command to the CDM. Discard policies are based on various factors, depending on the production order, and may include the number of air lines and blobs present in a tube slice, their (cumulative) length or area, and, the maximum size of defects.
The inspection task is made particularly challenging by the following factors: (i) The darker areas near the edges of the tube profile; (ii) the rotation and vibration of the tube, (iii) illuminator not aligned with the centre of the tube, (iv) the tube-shaped more like a "sausage" (the right side of Figure 2).

Performance requirements
The line scan camera consists of a single array of sensors; each sensor in the line array covers a virtual rectangle on the glass tube. The virtual rectangle represents a pixel in the frame. The virtual rectangle has a dimension along the orthogonal direction of the tube (orthogonal size) that depends on sensor size and optical magnification, as in Equation (1) [22][23][24]. The dimension along the longitudinal direction (longitudinal size) can be derived from tube speed and line acquisition period, as in Equation (2) [22,24,25]. The frame size in the orthogonal direction is given by the orthogonal size of the virtual rectangle and the number of sensors (pixels) in the line array Equation (3) [23,25].
orthogonal size = sensor size × magnification (1) longitudinal size = tube speed × line acquisition period (2) orthogonal frame size = orthogonal size × #pixelsinlinearray The minimum orthogonal resolution required depends on the tube diameter. Higher resolution is required for smaller diameter tubes due to their higher curvature. For tubes having a diameter less than 30 mm, an orthogonal resolution of 15 µm is considered acceptable by experts. These requirements give a lower bound to the number of sensors in the line array. In fact, the number of sensors must be greater than the ratio between the orthogonal frame size and the orthogonal size Equation (4). #pixelsinlinearray 3 orthogonal frame size∕orthogonal size As orthogonal size is 15 µm and maximum orthogonal frame size is 30 mm, we obtain that: Therefore, at least a 2k linear camera must be adopted to satisfy the requirement on orthogonal resolution.
In production, the Drawing Machine controls the tube speed, and, in our scenario, the speed is within the range 0.5-4 m/s (depending on the tube diameter). A value of 2 pixels per mm is considered by experts an acceptable value of longitudinal resolution. To obtain this longitudinal resolution at maximum tube speed (4 m/s), we utilize a 2k CCDs camera, with sensor size of 7 µm with a scan frequency of 8 kHz (the corresponding line acquisition period is 125 µs). According to Equation To guarantee real-time inspection, the system must satisfy the following constraint Equation (5): processing time for a frame < frame acquisition period (5) Acquisition period depends on the number of lines in a single frame and the line scan frequency of the camera Equation (6). frame acquis. period = #linesinaframe × line acquisition period (6) In our case, frames are made up of 1000 lines so, according to Equation (6), the acquisition period for each frame is 125 ms and the processing time per frame must be less than 125 ms (5).

RELATED WORK
In inspection system, each frame is typically subjected to three processing steps [14,26,27]: Pre-processing, defect detection and classification.
In the pre-processing phase, the image is elaborated for the subsequent phases, applying techniques for reducing acquisition noise [28] and allowing the acceleration of next phases through the exclusion of zones outside the Region of Interest (ROI, extraction) [29]. In glass tube inspection, ROI extraction consists in detecting the usable part of the tube within the frame, and comprises also the elimination of the tube's edges [9].
The defect detection phase aims at identifying the image regions whose pixels may belong to a defect, which is then classified in the following phase. The detection relies on segmentation techniques [14], which in turn are based on thresholds [8] or edge detection [17,18]. In classification, a series of characteristics are extracted from the segmented regions, possibly including them within predetermined classes of defects.
Edge detection techniques [17] try to detect points in a digital image where the image brightness changes sharply compared to the surroundings. Canny algorithm [18] represents one of the best solutions for images with noise [17] and the algorithm has been successfully used in various applications domains ( [10][11][12][13]).
In threshold-based techniques, the threshold can be global (a single threshold for the image) or local (different thresholds for different areas of the image [19]). Many global thresholds techniques are derived from the OTSU algorithm [30] that determines threshold values that maximize the inter-class variance of the image histogram [14]. Otsu algorithm and its variants have been exploited to perform inspection in various application domains [8,14,15].
As for local thresholds, the Niblack's [19] binarization method is a local adaptive technique, based on varying threshold over the image by using local mean value and the standard deviation of grey level evaluated in a window centred in each pixel. The application of Niblack has produced good results in segmenting text from the background and has been used also in vision system for industrial processes [16].
By considering the circular shape of the tube, the detection of defects cannot be obtained with the use of constant thresholds, since the luminous intensities of pixels relative to a defect in an area have values similar to the pixels not related to defects in another area ( Figure 6(a)). Besides, tube vibration and the its not perfect circular shape prevent using procedures based on background subtraction or template matching [8,20]. In [31], a method is proposed for detecting defect in surface textures based on single dimensional local binary patterns, based on two steps. First, in training step, a single dimensional local binary patterns is applied on full defect-less surface images and a basic feature vector is calculated. Then, by image windowing and computing the non-similarity amount between these windows and the basic vector, a threshold is computed for defect-less surfaces. Finally, in the detection step, by using the defect-less threshold the defects are detected on real images. Anyway, by considering that tube is in continuous movement (web inspection) and rotates and vibrates, and that the illuminator is in some instant not perfect centred with the tube, it may be difficult to build an efficient training and detection step in the case of tube inspection. In [9], a Canny-based algorithm has been proposed to perform inspection in a system similar to the ones presented in this paper, and it was able to meet the performance requirements of that time.
Currently, a general trend in this type of image processing applications is the need to reduce the processing time of the algorithms used, due to the time and quality requirements of modern production lines, the increase in resolution required and the availability of high resolution data sources [32]. Image processing algorithms must then perform demanding computation on substantial amount of data, with high throughput, and often, with the need to meet real time requirements [32].
Techniques for reducing processing time and for achieving real-time performance adopted in literature are essentially the following: • Reduction of the data elaborated by the algorithm • Design of parallel implementations of the algorithms and execution on parallel machines • Use of special hardware accelerators like FPGAs For instance, processing time have been reduced by resizing the image under inspection [33]. The technique has been utilised in [34] to build an automated inspection system of surface defects based on machine vision. The authors utilize a pattern matching technique that is applied to images whose size is reduced via a Gaussian Pyramid decomposition. A similar technique has been utilized in [23] to reduce the computation time of one stage of their processing pipeline. In [33], for defect detection in web offset printing, a one dimensional down-sampling of the image is utilized to speed-up the identification of the first row of web offset images.
Parallel implementations can unleash significant speedups, but specific techniques must be adopted to code effective parallel algorithms that yield similar performance gains as the invested computational and storage resources [35][36][37]. Parallel algorithm may be executed on conventional or GPGPU architectures (General Purpose Graphics Processing Units). For instance, Intel OpenCV library contains an optimized version of Canny detector, that exploits multithreading and multicores [38]. Different parallel implementation of Canny and other edge detection algorithms on GPGPU have been proposed [23,39]. In [23], a high-resolution inspection platform for the detection of surface defects in touch panel glass, has been implemented based on GPGPUs. High-resolution acquisition systems generate large image data, and ad-hoc GPU kernels, that perform all the step of detections, allow achieving acceptable elaboration time. In general, performance of the developed algorithm depends on the size of the acquired image [39,40], as also highlighted in [23].
An alternative solution to GPGPU is represented by the design of FPGA-based accelerators (Field Programmable Gate Array). A FPGA device is an array of logic blocks that can implement arbitrary parallel and pipelined operations, without the constraint of conventional multicores or GPGPUs [41,42]. These devices are typically suitable for exploiting the specific parallelism of low-level image processing algorithms [43][44][45]. There are many implementations of defect and edge detection algorithms in literature [43,44,46], but also in the case of FPGA the processing time of an image depends on the image size [44].
As a summary, both in case of conventional, GPGPU or FPGA solutions, image resizing constitutes an important step for satisfying real-time requirements of the application, as it reduces processing time, and also communication costs of parallel solutions.
In our specific application, standard down-sampling techniques [33] cannot be adopted, as the reduced resolution degrades the quality of defect detection below mandatory requirements. The technique proposed in this paper reduces the size of the ROI image by excluding areas where no defects are present, and this approach differs from all other works in literature. A preliminary version of the idea appears in [47], where the effects of ROI reduction on processing resources are discussed.

RATIONAL
In the ROI Extraction stage, two areas are excluded from the image: (i) The ones outside the tube image and (ii) dark areas near the edges of the tube itself. In general, the ROIs may contain large portions without any defect. Application of the detection algorithm on these portions is unnecessary and wastes elaboration time. Within the overall processing time of all the steps, the largest quote (about 77%) is spent in the detection, when Canny algorithm is applied. By applying Canny only to the areas containing defects, the time spent in detection is reduced by about 64%. In principle, if an image contains no defects, removing defect-free columns from the ROI implies that the processing time of defect detection drops to zero. Since our principal purpose is to decrease total processing time, our proposal consists in removing portions from the ROI where it is easy (fast) to assess the absence of defects. We call the remaining ROI area Reduced ROI (RROI).
It's about understanding as identify the areas without defects without spending much time. We can observe that the luminous intensity along a column has small variation (column a, Figures 2  and 4) except in pixels where there are irregularities (column c) or defects (column b). The presence of the blob (column b) produces a significant impulsive variation, while the irregularity of the geometric shape (column c) produces non-impulsive variations of the pixel values. The standard deviation of each column can be used to detect the presence of defects in the column (Figure 5(a)), provided that the effects of non-perfect alignment of the illuminator with the acquisition camera are eliminated. In this case, in fact, there is a growing trend of the standard deviation from a side of the tube to the other ( Figure 5(b)). This effect can be eliminated by removing the linear trend values from the values of the standard deviation (Detrended Standard Deviation, DSD). Another significant requirement is implementing a robust detection strategy as to maintain its quality across various conditions. Rotations and vibrations soften the intensity and curve the shape of lines, making their ends less evident. The same phenomenon occurs in blobs causing a blurred outline along the longitudinal axis. Therefore, the luminous intensity of the defects is high in the proximity of the centre of the defect but tends to drop on the end. However, these final parts are those that determine the true size of the defect. The result is that only the central part produces an impulsive trend of the standard deviation, while the final parts might not be highlighted by the detection process.
To solve such issue, we use a hysteresis with two thresholds as to identify columns that may belong to a defect (t L and t H with t L < t H ). The algorithm includes in the RROI: (i) Columns with DSD values greater than t H , (ii) columns with DSD values between t L and t H adjacent to the columns already in the RROI ( Figure 6).  Proposed algorithm applied to the frames of Figures 5(a,b). The columns belonging to the resulting RROIs are highligthed in green. The blue line represents the tH value, the red line represents the tL value, the yellow line represents the DSD Hysteresis with two thresholds has been utilized also in Canny [17], for managing weak edges that are connected to strong edges. The main differences with our approach are: (1) In Canny hysteresis is applied to the gradient magnitude of a pixel, while, in our approach, it is applied to the DSD of a column; and (2) points that pass the "hysteresis test" in Canny are marked as edges, while in our approach if a column passes the "hysteresis test" it does not mean that some pixels of the column belong to the defect. The column is indeed included in the RROI, and then processed in the next step of defect detection to determine if a defect is present on the RROI.
In [48,49] is presented an image based automatic road crack detection method, in which image areas belonging to normal road surface and crack road surface are distinguished by using image sample variance (where a sample is a window of the image), and only image samples belonging to crack road surface are further elaborated for crack detection, improving also the accuracy of detection [49]. The variance is calculated for the set of sample windows that constitute the image, and a threshold is applied to detect the samples that include cracks. Unlike these works, in our proposal we utilized DSD of a column together with hysteresis. This is because data on a column are acquired by a single sensor, so it is expected that the acquired signal along is almost constant for a defect free column, but, as stated above, we have to manage the effect of non-perfect alignment of the illuminator, and, due to rotations and vibrations, only the central part of the defects produces an impulsive trend of the standard deviation, while the final parts might not be highlighted by the detection process, that instead must be detected. The use of the image sample variance with a threshold could be not adequate, as end parts of the air lines or some portion of the blob may result below the threshold in this case, while they are marked as "weak defective column" by our algorithm. Besides, the aim of our technique is to improve the execution time, and not to improve the quality of detection.

ALGORITHM
The algorithm proposed for defect detection (DSDRR, DSD Roi Reduction) computes the DSD of each column of the internal part of the tube (ROI). Afterward, the columns whose DSD values are greater than t H and the ones adjacent to the RROI columns whose DSD values are greater than t L are assigned to the RROI. The algorithm logic is shown in Figure 7. The acquired frame is the input_image and the other inputs are the two thresholds (t L and t H ). The algorithm returns a list of intervals of adjacent columns that represents the image RROI.
For simplicity, the code of some functions is not reported: Function extract_internal (input_image) extracts the internal part of the tube from the input_image using the algorithm proposed in [9]; std_column (image) returns the array of standard deviation values calculated for each column of the input image; detrend (input_array) calculates the difference between the input_array and its linear trend by applying the linear least squares method; length() member function returns the number of components of an array. Function adjacent_RROI_column (v,i,t,dir) finds the adjacent columns that belong to a RROI (i.e. their DSD is higher than t) given a column belonging to the RROI. Given an array v, an index i, a threshold t and a direction dir (1 for right and -1 for left), the function moves, from the index i and with the direction dir, on the array until a value lower than the threshold t is found. It returns the last index, that is, the last column in the direction dir that meets the condition v [column ] > = t.
The algorithm DSDRR (function rroi_extraction) performs the following steps: a. extracts the internal part of the image, invoking function extract_internal and assigns it to the variable internal_image (classic ROI, line 9); b. calculates the standard deviation for columns of the inter-nal_image and assigns it to variable std (line 10); c. calculates the DSD of the image and assigns it to the array ds = detrend(std) (line 11); d. scans ds starting from index i = 1 until ds[i] is higher than threshold t H (lines [13][14]. Column i belongs to the RROI; e. starting from i, it determines the columns adjacent to i that belong to the same RROI using threshold t L . This is per-

ALGORITHM PARAMETERS
Threshold values of the proposed algorithm affect the quality of detection and throughput: in a real production environment there is the need to identify the values that provide the best quality and maximum throughput. An inappropriate choice of thresholds can exclude defects from the RROI and generate false negatives, or it can widen the RROI which induces loss of performance. To ensure that all defects belong to the RROI, threshold t H must be lower than at least a DSD value of the column of each defect. Therefore, the threshold t H must be chosen lower than the maximum value of the DSD of the columns of each defect, otherwise the algorithm may exclude areas that include defects. Too low values of t H imply the inclusion in RROI of areas that do not include defects, resulting in unneeded higher computational load and thus decreased performance.

FIGURE 8
The algorithm for determining upper bound to t L and t H To ensure that the entire shape of each defect belongs to the RROI, and in order not to limit the quality of detection in terms of size, threshold t L must be lower than the minimum value of the DSD of the columns of each defect. Too low values of t L imply that areas without defects are included in RROI, resulting again in performance loss.
In conclusion, the values of t H and t L must satisfy the conditions: t H ≤ min (maxDSD_defects_columns) (8) Many parameters related to the production batch (glass tube diameter, size, opacity, thickness, etc.) influence the choice of the optimal thresholds. Since these parameters change with different productions, we have defined a method that determines the best values for the thresholds depending on each condition. The method is performed at the change of production and consists in the collection of image frames during this phase and for each frame performs the following steps:  The best value is obtained with t H = 2 and t L = 0 (optimal threshold), for which the RROI is reduced to 12.13% of the whole ROI. The red line represents combinations of t H and t L values for which the RROI size is equal to 20% of the whole ROI size Such values represents the best value for t L and t H , so the DSDRR algorithm can work with such thresholds, that is, t L = t LUPP and t H = t HUPP . We must observe that the tuning algorithm always identifies optimal values for the thresholds, since during the change of the production batch the glass tube is in a transitory state which determines the generation of a large number of defects (and in this phase the tubes are always discarded). When the phase ends, the tube is in a steady state and there is no need to change the thresholds until the next batch.
However, it is extremely useful to have a more robust choice of thresholds as to avoid false negative (t H < t HUPP ) and to improve the quality of defect size (t L < t LUPP ), even if it produces a limited reduction in the maximum throughput. So in the following, we characterize the dependence of the RROI size on t H and t L (for values lower than the upper bound identified by the algorithm) and we evaluate, in addition to the optimal solution obtained by the algorithm, also three cases of more relaxed thresholds. Figure 9 shows the percentage of the RROI size determined by DSDRR for the experimental set of images described in Section 7.1, when varying the t L and t H parameters below the t LUPP and t HUPP bounds. Each image has a resolution of 2048 × 1000 pixels, and the Canny algorithm is applied for defect detection. Figures 10 and 11 show the relationship between each threshold and the size of RROI, while maintaining the other threshold fixed: results highlight that the size of RROI is particularly sensitive to t L . Table 1 shows the average processing time for (a) identifying the RROI (RROI column in Table 1, which includes the processing time to extract the ROI and the processing time to detect the RROI), (b) for defect detection (column Detect Detection in Table 1) and classification (column Class in Table 1) and (c) the maximum total processing time (i.e. the sum of the previous components, evaluated as explained in Section 7.1), in case of the optimal thresholds (UB). Results are also shown for 3 "relaxed" thresholds on the red line depicted in . Choice A is more robust in measuring size (t L < t LUPP ), C is useful to minimize false negatives (t H < t HUPP ). B is a compromise solution (t L < t LUPP and t H < t HUPP ). FPS results underline the cost of more robust setups in terms of slightly reduced performance in case of the three "relaxed" solutions.

Experimental setup
The prototype inspection system of Figure 1 has been implemented and tested in a glass tube foundry. The image acquisition subsystem is composed by a Basler Racer RaL2048 linear camera [50], and a Matrox Solios eCL/XCL-B Camera Link frame grabber with PCI-X and PCIe connections [51]. Illumination is obtained with a COBRA Slim LED Line based on red light [52] in backlight configuration [53]. The illuminator guarantees a uniform source of illumination so that changes in the environmental conditions determine no effects on the light on the sensors. Three sensors, illuminators and cameras compose the system in order to reliably cover all 360 • around the tube under inspection.
The Host computer is a PC based on an Intel i7-940 CPU at 2.93 GHz. The algorithm has been implemented in OpenCV [38] and the communication between the Host Computer and the Cutting and Discarding machine relies on a CIFX 50E-DP board [54].
Evaluations are made using a dataset of 30 frames of glass tubes captured in production. Each frame is composed of 1000 lines taken by the line scan camera sensor with 2K pixels. The dataset contains ten impurities (blobs) and six air lines; one frame contains two air lines, another three impurities. 17 frames have no defects.
The evaluation is performed according to four metrics: processing time, number and type of defects detected, pixel-length of air lines and blob area. The evaluation of processing times has been obtained by executing 400× the complete dataset of images [55]; in each test, frames are randomly chosen, and three processor cores (each one devoted to a camera) perform the elaboration on images preloaded in RAM to reproduce real working condition and interference. We have measured the average processing time of each step (ROI extraction and preprocessing -ROI, which may include also the DSDRR preprocessing depending on the experiment, as explained in Section 7.2, Defect detection, Defect classification and the maximum total processing time (in the following MTPT) and these can be assumed as an estimate of the Worst-Case Execution Times [55].
For the number and type of defects, we considered the number of true positives (TP), that is, the number of defects that are properly classified by the algorithm; the number of false positives (FP), that is, defect detections not corresponding

Considered algorithms and configurations
We have applied proposed DSDRR to three algorithms: Canny [18], MAGDDA [57], and Niblack's [19] (Table 2). In case of Canny, a Gaussian filter is also applied to smooth the image and then the magnitude of gradient at each pixel location is calculated. Pixels belonging to a defect are determined by non-maximum suppression, double thresholds and hysteresis. MAGDDA works at row level and uses a moving average filter with windows size WS. A fixed threshold (k) is applied to label the pixel. Niblack algorithm uses local thresholds, calculated as the local average of values, multiplied by a factor (k), the standard deviation of luminous intensity measured in a square window (WS size) around each pixel.
To evaluate the effectiveness of the technique, we considered two configurations (Table 2). In the first configuration (Without DSDRR), the ROI is extracted with the algorithm described in [9] and one of the three defect detection algorithms is applied to it. In the second configuration (DSDRR), after extracting the ROI with the technique [9], the preprocessing technique DSDRR is utilized to determine the RROI (Reduced ROI), to which the defect detection algorithms indicated above are applied.
In the classification phase, an algorithm connects adjacent labelled pixels using [56] and calculates, for each group, the smallest rectangle containing them. Defect classification is straightforward: (i) The ratio between height and base of the rectangle containing a defect, and (ii) the area size allows to classify the type of defect.

Evaluations
The results show that the accuracy of Canny and MAGDDA algorithms, in terms of defect detection, does not change with the application of the DSDRR algorithm (Table 3). For Niblack, the introduction of the DSDRR algorithm improves detection as Niblack detects FP in areas removed from the RROI by DSDRR.
The DSDRR algorithm dramatically reduces the size of ROI. DSDRR detects only 17 RROIs: 14 of them contain defects and one contains three defects. RROI is on average the 2% of the original ROI, and at most the 18% and at least the 0% (in frames without defects). Table 4 and Figure 12 show the processing time and the throughput obtained by applying the considered algorithms, with or without the application of DSSDRR in the ROI elaboration preprocessing step. In Table 4, the best and worst execution times for each processing step are highlighted in green and red, respectively. A smaller RROI size decreases the required processing time in the core phases of elaboration (detection and classification) and on the total processing time ( Figure 12 and Table 4). Moreover, for frames that do not include defects, the time for detection and classification is, in practice, zero.
Average detection time of Canny with no DSDRR preprocessing is around 62 ms while for Canny with DSDRR this execution time is about 11 ms (−82%). For MAGDDA, the average detection time with no DSDRR preprocessing is 8.33 ms while is 1.44 ms with DSDRR (−83%). For Niblack, the average detection time with no DSDRR preprocessing is 2.3 s while with DSDRR is about 551 ms (−76%). Relevant enhancements are reported also for the classification phase, due to the exclusion of noisy pixels from the RROI.
DSDRR algorithm increases the processing time (Table 4) for determining ROI (from 7.6 to 10.912 ms). Despite this, the MTPT in Canny goes from 89.8 to 29.7 ms (the corresponding throughput increases by 3.02×), and for MAGDDA from 33.9  In the previous evaluations we excluded from each ROI the regions near the edges of the tube, as they are inspected by the adjacent cameras. We have evaluated also the scenario in which DSDRR analyses and includes such areas, obtaining higher processing times (the RROI size usually increases), without altering the quality of inspection. The number of RROI areas becomes 70 (instead of 17 areas) and RROI average size increases from 2% to 12%, with a maximum of 31% and a minimum of 4%, instead of 0%. The throughput loss is about 21% for Canny, 13% for MAGDDA, 30% for Niblack.
Results of the evaluation highlight that DSDRR maintains the defect detection quality of each algorithms and in some cases can even improve it. Besides, it is effective in reducing the inspected area, inducing a throughput increase by at least 1.97× and up to 4.69×. On average, the benefits in throughput are higher for more complex algorithms (e.g. Niblack achieves the highest increase). The requirement of doubling the tube speed in the future production, while keeping the longitudinal size unchanged, imposes a scan frequency of 16 kHz, that is, a processing time/frame not exceeding 62.5 ms.
Results show that Canny algorithm, with DSDRR technique, can fulfil the performance targets of such scenario. Furthermore, also MAGDDA with DSDRR can keep up with the performance requirements, while doubling both speed and precision.

CONCLUSIONS
The quality of pharmaceutical glass tubes can be checked in real-time during production by a vision system. Improvements in the production process and the need to improve accuracy in detection determine the need for fast solutions. The largest part of the elaboration (about 77%) is spent in defect detection and at least 64% of this time is spent looking for defects in defect-free areas. Our proposal is to reduce the ROI size, by removing the zones without defects through an additional fastpreprocessing phase relying on Detrended Standard Deviation (DSD) and a double threshold with a hysteresis. DSD compensates the effects of not perfect alignment of camera and illuminator, while the double threshold algorithm solves the issues related to vibration and rotation of the tube. The obtained algorithm (DSDRR) is effective in reducing the image area, by 98% on average, and in reducing the time required for defect detection and classification phases. The algorithm has operating parameters that affect the quality of detection and throughput. We have tuned a methodology for identifying tradeoff values that ensure robust detection and optimal throughput and can be adopted during the setup process of a new production run.
DSDRR is orthogonal to the detection algorithm and so can be used in conjunction with any of them. We have applied DSDRR to three different state-of-the-art algorithms: Canny, MAGDDA, and Niblack. Results of the experimental evaluation suggest that DSDRR preserves the detection quality and significantly reduces the computational needs of the entire system. The only overhead resides is a slight increase in the RROI calculation time. The overall throughput is improved by at least 1.97× up to 4.69× in the most computationally demanding cases. As a general remark, reduction in processing time potentially allows either increasing of the detection quality by using higher samplerate cameras or increasing the production speed without loss of precision and accuracy. Canny algorithm, with DSDRR preprocessing, fulfils the performance requirement deriving from doubling tube speed or doubling the resolution. MAGDDA with DSDRR can sustain the doubling of both.
With our proposal, the requirements imposed by the evolution of production processes are solved at software level. The proposal represents a methodology to obtain defect detection also in time-constrained environments, so it can be potentially extended also to other application domains where computation resources are limited. Likewise, it can be useful in environments where high performance resources are available, because it can decrease computational effort and/or power consumption.
DSDRR approach can be utilized also in other application environments where a linear camera is used on both flat and uneven surfaces. As for future works, we are planning to apply it to satellite vision systems, where hardware resources can be quite constrained both in terms of computational capabilities and energy envelope. Another potential application of the proposed algorithm is in content based image retrieval systems, as a preprocessing technique to be used in the object cropping phase [58].

ACKNOWLEDGMENTS
This work has been partially supported by the Italian Ministry of Education and Research (MIUR) in the framework of the CrossLab project (Departments of Excellence -LAB Advanced Manufacturing and LAB Cloud Computing, Big data & Cybersecurity).