Identification of Cabbage Seedling Defects in a Fast Automatic Transplanter Based on the maxIOU Algorithm

The automatic identification of seedling defects is an important technology of an intelligent automatic transplanting machine, which effectively improves the quality of the transplanting machine’s operation. The accurate segmentation of seedling substrate and seedling region is the key to the success of the seedling defect recognition algorithm. This paper proposes the maxIOU algorithm to calculate the image segmentation threshold: The image G channel and excess green color space were selected as the color space for the segmentation of the substrate region and seedling region by analyzing the color histogram. Several images were randomly selected from the dataset to generate a training set and were labeled manually as the ground truth. The training set images were segmented using a threshold of zero to 255, and the intersection over union (IOU) were calculated using the algorithm segmented result and the ground truth. The threshold corresponding to the average IOU maximum was used as the segmentation threshold. After image segmentation, three features (area of the substrate, area of the seedling, and filling ratio of the lower part of the substrate) were obtained by the algorithm, and the image was identified for whether there was an empty conveyor belt, seedling deficiency, multiple seedlings, skew, and damaged substrate. The algorithm was tested on the automatic transplanter test platform. The experiment results were as follows: Firstly, the image segmentation threshold was calculated by the maxIOU method. The color component interval corresponding to the segmented substrate region was [0, 24] in the G channel, and the color component interval corresponding to the segmented seedling region was [21, 255] in the excess green channel. The average IOU of the substrate area was 0.854, and the average IOU of the seedling area was 0.820 in the verification experiment. Secondly, a dataset including 431 normal seedling images and 69 defective seedling images (empty conveyor belt, seedling deficiency, multiple seedlings, skew, and damaged substrate) was identified for defects. The accuracy, precision, and recall were 97.6%, 97.4%, and 99.8%. The processing time was 71.4 ms. The conclusion of the experiment was as follows: the maxIOU algorithm had high accuracy in the segmentation of the substrate and seedling region. The defect identification algorithm had high accuracy for defect identification of cabbage seedlings, and the algorithm had good real-time performance, which can be applied to high speed field transplanters.


Introduction
Precision agriculture is the trend of modern agriculture development. The promotion of automatic transplanting machines has improved agricultural efficiency and reduced agricultural production costs. It is an important embodiment of precision agriculture. The seedling defects seriously reduce the operation effect of the transplanting machine. Automatic identification and compensation technology for defective seedlings comprise an important engineering technique to improve the pass rate of mechanized transplanted seedlings. The automatic identification technology of defect seedlings is affected by unfavorable factors such as an unstructured transplanting environment and seedling morphology, which leads to identifiable defect types, causing the recognition accuracy and algorithm processing time to not be able meet the production requirement. The difficulty of the defect seedling identification algorithm lies in accurately segmenting seedlings from the image with huge differences in the unstructured transplanting environment and identifying them with appropriate features.
In recent years, the research on defect seedling identification technology included seedling information acquisition technology [1][2][3], seedling and background segmentation [4,5], and seedling defect identification technology [6][7][8]. Advanced technologies such as hyperspectral image sensors, near-infrared image sensors, machine learning techniques, and artificial neural network technologies have played an important role. Cheng [5] proposed a color image segmentation algorithm based on principal component analysis (PCA) and the variance (CV) model, which realized the extraction of corn seedling targets under a complex background. Liao [9] proposed a rice seedling segmentation algorithm based on the YCrCb color space (a color space where the Y channel means luminance, Cr means the difference between the R channel in the RGB color space and the Y channel, and Cb means the difference between the B channel in the RGB color space and the Y channel) and an improved Otsu method. The probability parameters were used to weight the background variance of the intraclass variance to improve the accuracy of the image segmentation algorithm for the bimodal and near-single peak histogram image. Li [10] proposed a cooperative segmentation algorithm based on the Markov random field optimization model to realize automatic segmentation of cotton leaves under natural lighting conditions. Wang [11] used the threshold segmentation method, region growing segmentation method, and region segmentation method to segment the pre-processed corn images and proposed a scoring based image processing effect evaluation method. Caicedo [12] used deep learning and traditional image processing algorithms to identify nuclei. The researchers proposed an algorithm evaluation framework that used measurement accuracy, error types, and computational efficiency as indicators and publicly released 23,165 manually annotated kernels and source code sets. The test results showed that: deep learning improved accuracy and could reduce the number of biologically relevant errors by half. Rodriguez-Lozano [13] proposed a simple and novel method to segment the forehead region and to extract the average temperature from this area solving this lack of non-user interaction tools. The results provided an accuracy of 90% compared to the manual segmentation using the coefficient of Jaccard as a metric of similitude. Moreover, the method could work with real-time constraints at 83 frames per second in embedded systems with low computational resources.
Segmentation of seedlings and background is one of the key steps to achieve defect seedling identification. Threshold segmentation is the most common method of image segmentation. It is divided into the artificial threshold and automatic threshold [14][15][16][17][18] according to the threshold selection method. The automatic threshold algorithm has better adaptability to the interference factors in the image, while the artificial threshold algorithm has advantages in robustness and algorithm time consumption. Chang [19] proposed an improved Otsu threshold segmentation method based on the resampling technique and ensemble learning technology, which was more accurate than the traditional Otsu method to extract blood vessels from MRA (magnetic resonance angiography) images. Based on the basic one-dimensional Otsu algorithm, Shao [20] proposed an improved two-dimensional multi-threshold method to achieve accurate segmentation of high temperature regions in the infrared image of petrochemical pipelines. Zhan [21] proposed an ore image segmentation algorithm based on the histogram accumulation moment, which realized the ore target location and recognition of multiple scenes. Qin [22] proposed an improved ant colony algorithm and applied it to the Otsu multi-threshold segmentation algorithm. Compared with the traditional Otsu algorithm and the traditional ant colony algorithm based Otsu algorithm, the new algorithm could get the optimal threshold faster and more accurately.
In this article, an algorithm, called maxIOU, which determines the optimal segmentation threshold by calculating the maximum intersection over union (IOU) between the image processing algorithm and the artificial image segmentation result, is proposed and used to segment the substrate and seedling region from the image. Then, the area of the substrate, the area of the seedling, and the filling of the lower part of the substrate were used as three characteristic indicators to determine whether the image contained defects or not.

Experimental Site and Cabbage Cultivars
The test images in our paper were collected from the underground laboratory of the College of Engineering, China Agricultural University, which is located in Beijing, China (40.00° N, 116.35° E). The cabbage seedlings (Zhonggan No. 21) were selected as the experimental samples. The seedlings were planted in a standard 72 point tray. The height of the seedlings was 10-15 cm, and the number of euphyllia was 4-7. The height of seedling substrates was 3 cm, and the upper and lower surface areas of the seedling substrate were 3 × 3 cm and 1.5 × 1.5 cm.

Image Acquisition
The transplanter is shown in Figure 1. The transplanter used the ejector rod to eject the entire row of seedlings from the tray into the conveyor belt. An automatic image acquisition and processing system captured and analyzed seedling images on the conveyor. The system consisted of three parts: an industrial camera, a pair of broad area linear arrays, and an IPC (industrial personal computer). The cabbage seedling images were obtained using a MER-131-75GC network camera (Daheng Imaging, Beijing, China) with a network port. The focal length of the camera lens was 8 mm. The camera was mounted on a camera support at 0.3 m above the conveyor belt of the automatic transplanter (see Figure 1A). The exposure time was 0.5 ms. The device used a lamp house to block the incoming light and filled it with a strip light source. The light source (Daheng Imaging) was a pair of white strip light sources. The obtained images were transmitted to the computer through a gigabit network line and were saved as 24 bit color images in the RGB color space with the BMP format. The resolution of the original images was 1280 × 1024. Each image contained five cabbage seedlings, and the images of the middle three cabbage seedlings were clipped. The size of the clipped image was 265 × 1024. Finally, a dataset of 500 images was generated.

Color Space Analysis
The necessary pre-step for defect identification is to segment the two parts of the seedling accurately: the substrate and the seedlings. Image segmentation methods include threshold segmentation [23,24], edge detection segmentation [25,26], clustering algorithm segmentation [27,28], mathematical morphology based segmentation, and semantic segmentation [29,30]. In the images collected on the conveyor belt, there were interference factors such as reflection, shadow, and substrate debris and the morphological differences of cabbage seedlings were large. Edge detection and clustering algorithms were not applicable. Semantic segmentation based on a full convolutional neural network can be applied to the extraction of complex targets; however, sample labeling is too cumbersome, and the algorithm is not real time. The threshold segmentation algorithm is a segmentation algorithm suitable for fixed backgrounds and known foreground target colors. The algorithm is stable and real time. The mathematical morphology algorithm is a supplement to the image segmentation algorithm, mainly used to remove noise points. Therefore, the combination of threshold segmentation and the mathematical morphology algorithm was selected to segment the matrix blocks and seedlings.
The implementation of the threshold segmentation algorithm depends on the appropriate color space. In this article, a grayscale histogram was used to determine the color space. Image color spaces include RGB, HSV, YIQ, etc. (see Figure 2). The color histogram peak feature analysis method was used to determine the image segmentation color space: Ten images of cabbage seedlings were randomly selected and converted into the RGB, HSV, and YIQ spaces respectively. Then, each color space gray histogram was accumulated (see Figure 3). As is shown in Figures 2(A1-A3) and 3A, three channels of the RGB space showed peak characteristics, wherein the green channel peak characteristics of the G green channel were the most stable and the peak region corresponded to the substrate region. As is shown in Figures 2(B1-B3) and 3B, among the three channels of the HSV space, the S channel showed peak characteristics, but the peak area corresponded to the background area. As is shown in Figures 2(C1-C3) and 3C, neither of the three channels in the YIQ space showed a peak corresponding to the substrate region. Therefore, the RGB space G channel was selected as the color space for substrate segmentation. The excess green color space (see Equation (1)) is commonly used in green target segmentation. The color component of the seedling area in the sample image was significantly higher than other areas (see Figures 4 and 5), and the threshold segmentation method could realize the segmentation of seedling regions. Therefore, the excess green color space was used to segment the seedling area.
where R, G, and B are the pixel values in the RGB color space. ExG is the pixel value in the excess green color space.

Segmentation Threshold Calculated by the maxIOU Algorithm
This section analyzes the algorithm for image segmentation threshold calculation. Traditional image segmentation threshold calculation methods, such as the Otsu method and the gray histogram method, are applicable to double peak image segmentation. Although the G channel of the sample image had double peaks, the double peaks contained more background pixel interference, and the segmentation threshold obtained by the Otsu method was not accurate enough (see Figure 3A). The valley region between the double peaks of the image G channel was too wide (see Figure 3A), and the gray histogram method also had difficult searching for a suitable segmentation threshold. The seedling area did not form double peaks in the super green color space ( Figure 5) and was not suitable for the Otsu method and the gray histogram method.
In this article, the maxIOU algorithm is proposed (Equation (2)), which could be used to calculate the image segmentation threshold with an inconspicuous double peak feature. The maxIOU method drew on the idea of the Otsu method enumerating all segmentation threshold comparison effects, but made two important improvements: (1) Inter-class variance was replaced by intersection over union (IOU) as a function for evaluating the image segmentation effect.
(2) The threshold corresponding to the maximum average IOU of the training set image was used as the image segmentation threshold instead of the Otsu adaptive threshold.
where IOU is the intersection over union of algorithm segmented image and labeled image; i is the color component between 0 and 255. maxIOU was applied to segment the substrate and seedling region. Firstly, several images were selected as Training Set 1 from the dataset. The substrate region and the seedling region of the image were manually segmented and labeled as the ground truth. Secondly, all training set images in the G channel and excel green color space were segmented with a 0-255 threshold and were repaired by morphological methods. IOU was calculated with the algorithm segment result and ground truth. Finally, the average value of the IOU corresponding to each threshold was calculated, and the maximum average IOU corresponding threshold was selected as the image segmentation threshold.
IOU is a standard for measuring the accuracy of detecting corresponding objects in a specific dataset. The IOU is a number between 0 and 1, and a larger IOU indicates a better segmentation result. The IOU calculation method is shown in Figure 6 and Equation (3). C1 is the artificial labeled image segmentation result, which is called the ground truth. C2 is the image segmentation result by the algorithm. Si is the shadow in Figure 6A, which is the intersection of C1 and C2. Su is the shadow in Figure 6B, which is the union of C1 and C2.

IOU =
The original images and the artificially labeled substrate area and seedling area images are shown in Figure 7 and the corresponding G channel, and excess green color space gray histogram are shown in Figure 8. As is shown in Figure 8(A3,B3), the gray histogram of the artificially labeled substrate region had a peak coincidence with the gray histogram of the complete image in the G channel, and the overlap interval was [0, selected threshold]. Therefore, it was feasible to use the threshold segmentation method to achieve the segmentation of the substrate region and the background region. As is shown in Figure 8(C3,D3), the grayscale histogram of the artificially labeled seedling region had a higher degree of coincidence with the higher color component of the grayscale histogram of the complete image. Therefore, it was feasible to use the threshold segmentation method to realize the segmentation of the seedling area and background area. Si Su As is shown in Figure 8, the segmentation result of the threshold segmentation algorithm was partially misjudged because of the interference of the broken substrate, vermiculite, and shadow. Image segmentation results needed to be repaired using morphological algorithms. The morphological algorithms used in this paper included dilation (see Equation (4)), blob analysis (see Equation (5)), and area filling surrounded by convex hull points [31,32] (see Equation (6)).
where A is the object to be dilated; B is the dilated structural element; D(x,y) is the center of the structural element. The image was convolved using the dilated structural element as the convolution mask and be converted to a dilated image.
where A is the area of the connected domain; T is the threshold for connected domain selection; B is the output of blob analysis. If the area of a connected domain is greater than the threshold, the connected domain will be preserved, otherwise the connected domain will be removed.
where booli indicates whether point Pi is a convex hull point; the minimum row coordinate point in the region edge points is selected as base point P0, and other points are sorted from small to large at the angle of the line vector with P0 and recorded as Pi; P0 and P1 are the default as the convex hull points; Ps is the previous convex hull point of Pi; Ps-1 is the previous two convex hull points of Pi.

Identification of Seedling Defects
Seedling defects are inevitable in automatic transplanter operations. Seedling defects that may occur during ram-type transplanters' operation include the following: empty conveyor belt, seedling deficiency, skew, multiple seedlings, and substrate damage (see Figure 9 and Tables 1 and 2).

Seedling defects Defects' characterization Empty conveyor belt
There is no seedling on the conveyor belt Seedling deficiency There is substrate on the conveyor belt, but no seedling Skew The stem of the seedling spans the conveyor baffle Multiple seedlings There is more than 1 seedling in a conveyor belt slot Substrate damage Serious damage to the substrate structure leads to an irregular substrate shape Empty conveyor belt Seedling deficiency Skew Multiple seedlings Substrate damage After the segmentation of substrate and seedling area, the region area was selected as an indicator to evaluate whether the image had the defects of empty conveyor belt, seedling deficiency, skew, and multiple seedlings. The area of the normal image substrate area should be larger than the area of the empty conveyor belt, seedling deficiency, and skew image substrate area and smaller than the area of the multiple seedlings image substrate area. Therefore, the upper threshold and the lower threshold (see Equation (7)) can be used to identify whether or not there is a defect in the image.
where arease is the area of seedling region; areasu is the area of the substrate region; thresholdsed is the seedling area lower threshold; thresholdsud and thresholdsuu are the substrate region lower threshold and upper threshold. The defect of substrate damage needs to be identified by area shape analysis. The complete substrate is trapezoidal in shape and is consistent with the groove, while the damaged substrate is rectangular in shape and fills the entire conveyor belt slot. The following method is used to determine whether the substrate is damaged (see Figure 10 and Equation (8)): The substrate is equally divided into upper and lower parts by a 1/2 substrate height. The area of the lower part (A-1,B-1) and the outer frame area is calculated, and the ratio of lower part area to the frame area is recorded as c to determine whether the substrate is damaged (see Equation (9)).
where c is the index for evaluation of whether the substrate is damaged; aread is the area of the lower part of the substrate; a is the area of the outer frame.
where cu is the index c upper threshold; cd is the index c lower threshold. In this article, the method of feature scatter plot analysis was used to obtain the optimal defect identification threshold. Some of the sample images were randomly selected as Training Set 2 and manually labeled as the normal group and defect group, including the defects of the empty conveyor belt, seedling deficiency, skew, multiple seedlings, and substrate damage. The image processing method was used to calculate the feature values of the image: arease, areasu (Formula (8)), and c (Formula (9)). Feature scatter plots were used to analyze the normal and defect group sample characteristics, and the threshold with the highest classification accuracy rate was used as the defect identification threshold.

Identification of Cabbage Seedling Defects Procedure
In this subsection, the detailed steps of the proposed algorithm for identification of cabbage seedling defects are described:

•
Step 1: Some images were randomly selected as Training Set 1 from the dataset and manually labeled as the substrate and seedling region.

•
Step 2: The RGB color space G channel and the excel green color space (Formula (1)) of Training Set 1 were used as the image segmentation color space.

•
Step 3: The images in Training Set 1 were segmented using a threshold of 0-255 and morphologically repaired. IOU was calculated with image segmentation results and labelled images. The threshold corresponding to the maximum IOU was the segmentation threshold.

•
Step 4: Some of the images in the dataset were randomly selected as Training Set 2 and manually labeled as the normal group and defect group (including the defects of empty conveyor belt, seedling deficiency, skew, multiple seedlings, and substrate damage).

•
Step 5: The image processing method was used to calculate the feature values of the image: arease, areasu (Equation (8)), and c (Equation (9)). Feature scatter plots were used to analyze normal and defect group sample characteristics, and the threshold with the highest classification accuracy rate was used as the defect identification threshold.

•
Step 6: Identification of cabbage seedling defects: The images in the G channel and excess green color space were segmented with the threshold obtained in Step 3. The values of arease, areasu (Equation (8)), and c (Equation (9)) were calculated, and cabbage seedling defects were identified based on the threshold calculated in Step 5.

Image Segmentation Threshold Calculation and Result Analysis
In this section, the optimal threshold for matrix and seedling region segmentation is obtained by experimental methods. From the dataset consisting of 500 images, 10 images were randomly selected as Training Set 1, and the image segmentation threshold was calculated using the maxIOU method. The experiment result is shown in Figure 11A,B: The maximum IOU of the substrate region was 0.873, and the corresponding segmentation threshold was 24. The maximum IOU of the seedling region was 0.790, and the corresponding segmentation threshold was 21. Combined with the analysis in Section 2.4 (see Figures 7 and 8), the color component interval corresponding to the segmented substrate region was [0,24] in the G channel, and the color component interval corresponding to the segmented seedling region was [21,255] in the excess green color space.
(A) (B) Figure 11. Mean IOU of the substrate (A) and seedling region (B).
A verification experiment was designed to verify the generalization performance of the maxIOU method. Fifty images that were different from Training Set 1 were randomly selected from the dataset to verify the algorithm's segmentation effect. The segmentation threshold calculated in the previous paragraph was used to segment the images. The experiment result is shown as follows: The average IOU of the substrate area was 0.854, and the average IOU of the seedling area was 0.820. Image segmentation results are shown in Figure 12. The experiment obtained the conclusion that the thresholds searched by the maxIOU method could well achieve the segmentation of the substrate and seedling region.

Defect Identification Threshold Calculation and Result Analysis
Firstly, some images were selected from the dataset to calculate the defect identification threshold. Two-hundred normal seedling images were randomly selected from the dataset as the normal group, and 25 defective seedling images were selected as the defect group, including 5 empty conveyor belt images, 5 seedling deficiency images, 5 skew images, 5 multiple seedlings images, and 5 substrate damage images.
According to Equation 7, a scatter plot was used to determine the lower threshold of seedling area (see Figure 13). According to the defect feature analysis in Table 2, the empty belt, lack of seedlings, and skewed seedling images were selected for comparison with the normal group images. The area minimum of the normal group seedlings was 19,275 pixels, followed by 29,230 pixels. The area maximum of the empty belt, lack of seedlings, and skewed seedling was 23,688, followed by 18,806. The classification results of different segmentation thresholds are shown in Table 3. As shown in Table 3, when the classification threshold interval was (18,806,19,275), there was one misclassification in the empty conveyor belt images, while the seedling deficiency, skew, and normal images were all classified correctly. When the classification threshold interval was (19,275,23,688), there was one error respectively in the normal images and the defect images' (empty conveyor belt, seedling deficiency, and skew images) classification result. When the classification threshold interval was (23,688,29,230), there was one misidentification in normal images' classification, and the defect images (empty conveyor belt, seedling deficiency, and skew images) were all classified correctly. As the effect of defect seedling misidentification was greater than normal seedling misidentification, the seedling defect identification threshold interval should be selected as (23,688,29,230). Finally, the threshold was taken as the intermediate value of 26,459 in the experiment.   According to Equation (7), a scatter plot was used to determine the upper and lower threshold of the seedling area (see Figure 14). According to the defect feature analysis in Table 2, the empty conveyor belt, multiple seedlings, and substrate damaged images were selected for comparison with the normal images. The area maximum of the normal group substrate was 46,912 pixels, and the area minimum of the normal group substrate was 21,888 pixels. The area maximum of the multiple seedlings and substrate damaged images was 51,931 pixels. All the area of the empty conveyor belt images was zero. There was no overlapping interval between the normal group and the defect images. The substrate identification lower threshold interval was selected as  According to Equation (9), a scatter plot was used to determine the threshold of the c value of the substrate's lower part (see Figure 15). The c value maximum of the normal group images was 0.661, and the c value minimum of the substrate damaged images was 0.688. There was no overlapping interval between the normal group and the substrate damaged images. The c value threshold interval was selected as (0.661, 0.688). Finally, the c value threshold was taken as the intermediate value of 0.675 in the experiment.

The Result of Seedling Defects' Identification
Finally, all images in the dataset were analyzed for defects. The experiment was implemented on an HP PC with an Intel Core-i7 processor and 8 GB RAM. In the experiment, the original image with a size of 265 × 1024 shown in Figure 2I was selected as the test image. The algorithm was written in the Halcon library. All pictures in the dataset were image segmented using the thresholds calculated above, and the corresponding features were identified for defects. The experiment result is shown in Table 4.  Table 4. Experiment result. Normal  431  420  Empty conveyor belt  20  20  Seedling deficiency  15  14  Multiple seedling  6  6  Skew  16  16  Seedling damaged  12  12  Total  500  488 Accuracy (see Equation (10)), precision (see Equation (11)), and recall (see Equation (12)) were used as indicators for analyzing the effect of defect identification. The analysis result is shown in Table 5. The normal images were taken as positive samples, and the defective seedling images were taken as negative samples.

Group The Total Number The Number of Samples Correctly Identified
where TP is the number of positive samples that are correctly identified; TN is the number of negative samples that are correctly identified; FP is the number of positive samples that are misidentified; FN is the number of negative samples that are misidentified. The experiment analysis indicated that: the accuracy was 97.6%; the precision was 97.4%; the recall was 99.8%; and the progress time was 71.4 ms. The algorithm had good accuracy in identifying seedling defects. The defect seedling misidentification rate was extremely low (recall was 99.8%). The algorithm had good real-time performance and could be applied in high speed cabbage transplanters.

Conclusions
In this article, the maxIOU algorithm was proposed (Equation (4)), which could be used to calculate the image segmentation threshold with inconspicuous double peak feature. The maxIOU method drew on the idea of the Otsu method enumerating all segmentation threshold comparison effects, but made two important improvements: (1) Inter-class variance was replaced by intersection over union (IOU) as a function for evaluating the image segmentation effect. (2) The threshold corresponding to the maximum average IOU of the training set image was used as the image segmentation threshold instead of the Otsu adaptive threshold. The maxIOU method was applied in the substrate and seedling region segmentation. After image segmentation, the area of the substrate area, the area of the seedling area, and the filling ratio of the lower part of the substrate area were calculated, and the defect identification threshold was calculated using the feature scatter plot. Finally, the five defects of empty conveyor belt, seedling deficiency, multiple seedling, skew, and seedling damaged were identified by the algorithm. Based on the condition of the automatic cabbage transplanter, an experiment was designed to test the segmentation effect of the maxIOU algorithm on the substrate and seedling region and the defects' identification result. The conclusion of the experiment is shown as follows: the maxIOU algorithm had high accuracy in the segmentation of the substrate and seedling region. The defect identification algorithm had high accuracy for defect identification of cabbage seedlings, and the algorithm had good real-time performance. which could be applied to high speed transplanters. The image acquisition system used a light box to block external light sources and used a strip light source to illuminate the field of view. Field external light does not affect field brightness. The industrial camera adopted fast exposure mode to reduce the impact of conveyor movement and mechanical vibration on image acquisition sharpness. The algorithm in this article could be well applied to the detection of defective seedlings in high speed field transplanters.