GainTKW: A Measurement System of Thousand Kernel Weight Based on the Android Platform

: Thousand kernel weight (TKW) is an important parameter for the evaluation of grain yield. The traditional measurement method relies on manual steps: weighing and counting. In this paper, we developed a system for the automated evaluation of thousand kernel weight that combines a weighing module and Android devices, called “gainTKW”. The system is able to collect the weight information from the weighing module through a serial port using the RS232-micro USB cable. In the imaging process, we adopt a k-means clustering segmentation algorithm to solve the problem of uneven lighting. We used the marker-controlled watershed algorithm and area threshold method to count the number of kernels that are touching one another. These algorithms were implemented based on the OpenCV (Open Source Computer Vision) libraries. The system tested kernel images of six species taken with the Android device under different lighting conditions. The algorithms in this study can solve the segmentation problems caused by shadows, as well. The appropriate numbers of kernels, of different species, are counted with an error ratio upper limit of 3%. The application is convenient and easy to operate. For the experiments, we can prove the efﬁciency and accuracy of the developed system by comparing the results between the manual method and the proposed application.


Introduction
As a complex agronomic trait, grain yield is multiplicatively determined by three components: the number of spikes per area, grain number per spike, and grain weight.Grain weight is usually represented by 1000-kernel weight.Thousand kernel weight (TKW) is an important parameter for the evaluation of variety breeding.TKW is not only directly related to the grain yield and milling quality of grain, but also has an impact on the seedling vigor and growth indirectly affecting the yield [1]. TKW was also found to be closely associated with kernel size traits as well, such as kernel length (KL), kernel width (KW), kernel thickness (KT), and the kernel length/width ratio (L/W) [2].As a result, TKW is frequently used in crop research as a measurement indicator [3].
The measurement of the thousand kernel weight includes weighing and counting the kernels.Weight data is obtained from scales and traditional methods based on manual counting are used to count the kernels [4].As manual counting is time-consuming and labor-intensive with subjective results, electronic counting devices [5] are also commonly used.The machine is accurate at slow speeds but cannot ensure precision at higher speeds.In addition, the machine cannot handle a wide range of kernel sizes and is noisy.The counting machine equipped with a laser can overcome these shortcomings but it is expensive, bulky and can only be used in laboratories.The operation is also complex and cumbersome.A less costly and easier to implement a method to count the kernels is image processing technology [6].It can achieve satisfactory results if the recognition algorithm is efficient enough.
This study solves the counting problem using the image processing method.Counting kernels using on image processing occurs in two steps.First is the image segmentation that separates the kernels from the image background.Then the kernels themselves are separated so that the number of kernels can be calculated.Good image segmentation is the foundation of the counting accuracy.There are many segmentation algorithms such as threshold [7], edge detection [8] and clustering [9].The touching kernels increase the difficulty of counting the kernels correctly.In order to solve this problem, different methods of separating touching kernels have been reported in the literature, such as the morphological operations [10], the ellipse fitting algorithm [11,12], the concave points matching algorithm [13,14] and the watershed segmentation algorithm [15].The morphological method is simple but the frequency of erosion and dilation is dependant on human experience [10].The ellipse fitting algorithm is time-intensive and does not perform well on elongated shapes [12].The concave points matching algorithm is prone to oscillation and the results presented are poor when separating more than three touching kernels [14].The watershed algorithm is a widely-used segmentation method that uses local gradient difference in touching areas and identifies dividing lines through a simulation process of water flooding, from kernel areas under the pixel gradient constraints.The traditional watershed algorithm is prone to over segmentation [16].Kernel segmentation and separating algorithms based on image processing are mostly performed on a computer or laptop, which can be inconvenient [17].With the development of mobile intelligent terminals, mobile devices have changed the way of living and people's behaviors.Modern mobile devices contain digital cameras with high resolution.The Android mobile phone platform provides third-party developers with a wide ranging, free environment, allowing users to capture and process images wherever necessary.A number of applications on mobile devices have been developed for agricultural applications, such as grain phenotyping [18], estimating citrus yield [19], identifying plant species [20], weed identification [21], estimating soil-color [22] and so on.
The goal of the present work was to develop a convenient, efficient and reliable system that was able to perform an automated calculation of the thousand kernel weight using a weighing module and Android devices, called "gainTKW".The system counts the number of the kernels and weighs them.The Android application will get the weight information from the weighing module through a serial port.In the counting part, to eliminate the influence caused by shadow, we chose the k-means clustering algorithm instead of the traditional threshold algorithm to segment the object from the background.For counting the touching kernels, we used the marker-controlled watershed algorithm to avoid over-segmentation.Because the complex touching conditions of kernels cannot be divided well by the improved watershed algorithm, we used an area threshold method to calculate the number of kernels in clusters.

Materials Preparation
To test the system's performance, we chose six species of kernels with differences in color, shape, and size.They were Indica rice (Oryza sativa L.), adzuki bean (Vigna angularis (Willd.)Ohwi & Ohashi), mung bean (Vigna radiata L.), soybean (Glycine max (L.) Merr.), peanut (Arachis hypogaea L.) and sesame (Sesamum indicum L.).The Indica rice had an average length of ~10 mm, a width of ~3 mm, and a yellow coloration.The adzuki bean had an average diameter of ~5 mm and a red coloration.The mung bean had an average diameter of ~4 mm and a green coloration.The soybean has an average diameter of ~6 mm and a yellow coloration.The peanut had an average length of ~13 mm, a width of ~7 mm and a red coloration.The sesame had an average length of ~3 mm, a width of ~2 mm and a black coloration.The experiments were carried out using a Nexus 7 on a clear day indoors.The devices were separately used to acquire and analyze 60 images of 6 different species (10 images per variety).

System Design
The whole system consists of two hardware parts: an Android device and a weighing module.The experimental setup is shown in Figure 1.In this study, as an example, we used the Google Nexus 7 tablet.The tablet incorporates a Qualcomm Snapdragon S4 Pro 1.3 GHz high-speed 4-core CPU and its memory is 2 GB.It also has a 120 megapixel video camera and an LED flashlight.To test the performance of the application, we used the Huawei P9 mobile phone purchased from Huawei Technologies Co., Ltd.(Shenzhen, China).The mobile phone incorporates a Hisilicon Kirin 955 2.5 GHz high-speed 8-core CPU and its memory is 3 GB.It has a 1200 megapixel video camera and an LED flashlight.
The whole system consists of two hardware parts: an Android device and a weighing module.The experimental setup is shown in Figure 1.In this study, as an example, we used the Google Nexus 7 tablet.The tablet incorporates a Qualcomm Snapdragon S4 Pro 1.3 GHz high-speed 4-core CPU and its memory is 2 GB.It also has a 120 megapixel video camera and an LED flashlight.To test the performance of the application, we used the Huawei P9 mobile phone purchased from Huawei Technologies Co., Ltd.(Shenzhen, China).The mobile phone incorporates a Hisilicon Kirin 955 2.5 GHz high-speed 8-core CPU and its memory is 3 GB.It has a 1200 megapixel video camera and an LED flashlight.
In order to gain an accurate measurement of grain weight, we designed the weighing module for high performance.Its structure diagram is shown in Figure 2. The resistance strain type pressure sensor is the core of the weighing system.It produces a voltage signal output when subjected to pressure.A digital signal processing circuit consisting of filters and an amplifier was designed to eliminate interference and amplify the signal.A microcontroller is then used to read the processed voltage signal through the embedded AD converter.Then the weight of the grains can be calculated.The measurement result is displayed by LED segment displays and output via USB interface.The electronic balance's maximum weighing is 110 g and the resolution is 0.01 g.

Development Tools
The application was developed using Google's own Android IDE, Android Studio.The IDE is based on the popular IntelliJ IDEA from JetBrains and is being offered by Google for free.Compared to Eclipse, there are several main differences.In order to gain an accurate measurement of grain weight, we designed the weighing module for high performance.Its structure diagram is shown in Figure 2. The resistance strain type pressure sensor is the core of the weighing system.It produces a voltage signal output when subjected to pressure.A digital signal processing circuit consisting of filters and an amplifier was designed to eliminate interference and amplify the signal.A microcontroller is then used to read the processed voltage signal through the embedded AD converter.Then the weight of the grains can be calculated.The measurement result is displayed by LED segment displays and output via USB interface.The electronic balance's maximum weighing is 110 g and the resolution is 0.01 g.

System Design
The whole system consists of two hardware parts: an Android device and a weighing module.The experimental setup is shown in Figure 1.In this study, as an example, we used the Google Nexus 7 tablet.The tablet incorporates a Qualcomm Snapdragon S4 Pro 1.3 GHz high-speed 4-core CPU and its memory is 2 GB.It also has a 120 megapixel video camera and an LED flashlight.To test the performance of the application, we used the Huawei P9 mobile phone purchased from Huawei Technologies Co., Ltd.(Shenzhen, China).The mobile phone incorporates a Hisilicon Kirin 955 2.5 GHz high-speed 8-core CPU and its memory is 3 GB.It has a 1200 megapixel video camera and an LED flashlight.
In order to gain an accurate measurement of grain weight, we designed the weighing module for high performance.Its structure diagram is shown in Figure 2. The resistance strain type pressure sensor is the core of the weighing system.It produces a voltage signal output when subjected to pressure.A digital signal processing circuit consisting of filters and an amplifier was designed to eliminate interference and amplify the signal.A microcontroller is then used to read the processed voltage signal through the embedded AD converter.Then the weight of the grains can be calculated.The measurement result is displayed by LED segment displays and output via USB interface.The electronic balance's maximum weighing is 110 g and the resolution is 0.01 g.

Development Tools
The application was developed using Google's own Android IDE, Android Studio.The IDE is based on the popular IntelliJ IDEA from JetBrains and is being offered by Google for free.Compared to Eclipse, there are several main differences.

Development Tools
The application was developed using Google's own Android IDE, Android Studio.The IDE is based on the popular IntelliJ IDEA from JetBrains and is being offered by Google for free.Compared to Eclipse, there are several main differences.

•
Build Tools: Android Studio utilizes the fast growing Gradle build system.Eclipse, on the other hand uses Apache Ant as its main build system.The Android operating system provides outstanding accessibility advantages.However, it still has certain limitations in terms of efficiency and versatility for the needs of this work, especially for the implementation of the image processing algorithms.In this respect, OpenCV (Open Source Computer Vision Library) is a highly optimized library written in C/C++, which offers a wide catalog of image processing and analysis functions.Android applications are mainly developed in Java, with an existing Android version of this library called OpenCV4Android (http://opencv.org/platforms/Android.html).
Figure 3 shows the "gainTKW" system implementation flow.The Android operating system provides outstanding accessibility advantages.However, it still has certain limitations in terms of efficiency and versatility for the needs of this work, especially for the implementation of the image processing algorithms.In this respect, OpenCV (Open Source Computer Vision Library) is a highly optimized library written in C/C++, which offers a wide catalog of image processing and analysis functions.Android applications are mainly developed in Java, with an existing Android version of this library called OpenCV4Android (http://opencv.org/platforms/Android.html).Figure 3. shows the "gainTKW" system implementation flow.

Kernels Segmentation
According to the traditional image processing steps, after obtaining the image we transformed it from RGB to gray.The gray image was smoothed by Gaussian filtering [23,24] (sigma = 1.5, template size = 4 × 4) to improve operational efficiency.Then the Otsu segmentation algorithm [7,25,26] was applied, by which a maximum contrast between the kernels and the image background was created to ensure the extraction of intact kernels.
Interference from uncontrolled or uneven lighting is the most basic challenge for smartphone optical sensing [27].Because of the problem caused by shadows, the traditional method could not segment the object appropriately.Hence, in this study, a K-means clustering-based algorithm was

Kernels Segmentation
According to the traditional image processing steps, after obtaining the image we transformed it from RGB to gray.The gray image was smoothed by Gaussian filtering [23,24] (sigma = 1.5, template size = 4 × 4) to improve operational efficiency.Then the Otsu segmentation algorithm [7,25,26] was applied, by which a maximum contrast between the kernels and the image background was created to ensure the extraction of intact kernels.
Interference from uncontrolled or uneven lighting is the most basic challenge for smartphone optical sensing [27].Because of the problem caused by shadows, the traditional method could not segment the object appropriately.Hence, in this study, a K-means clustering-based algorithm was utilized to segment the image.As the most widely used partition algorithm, K-means is one of several clustering algorithms.Algorithm 1 shows the flow diagram of the K-means clustering algorithm.K-means clustering needs to specify the number of clusters to be partitioned and a distance metric to quantify how near the two classes are to each other.The basic idea is that the given cluster number is k, and the result of clustering the sample set is k clustering centers.The final clustering result is done by minimizing the sum of squares of distances between the data objects and the corresponding region.The kernels have their own color, different from the background, thus the number of clusters should be two.

Algorithm 1 K-means algorithm
Input: K (number of clusters), Training set x (1) , x (2) , . . ., x (m)  Randomly initialize K cluster centroids µ 1 , µ 2 , . . ., µ K repeat // cluster assignment step: find closest centroid for i = 1 to m data points do c (i) : = index of cluster centroid closest to x (i) end for // update centroid step: compute means based on assignment for k = 1 to K centroid do µ: = average(mean) of points assigned to cluster k end for until N iterations Output: centroids assignments c (1) , c (2) , . . ., c (m) , and learned centroids µ 1 , µ 2 , . . ., µ K In order to minimize the effect caused by the light variation during capturing the original image, in this study, we processed the images in the CIE L*a*b color space [28].The 'L' means color space dimension for light, '*a' means color space dimension for red-green, '*b' means color space dimension for yellow-blue.The RGB values are transformed to CIE tristimulus values as follows: The CIE L*a*b equation is then applied.This involves the evaluation of cube roots.
First, the application read the image and transformed it into the CIE L*a*b color space, then applied the K-means clustering algorithm in 'a*b*' space to generate the different regions and finally selected the segmented image containing kernels to finish the binarization process.We applied the morphology opening operation to eliminate the noise existing in the binary image.Figure 4 shows the results of Otsu segmentation algorithm and K-means clustering segmentation algorithm.

Counting Method
After getting kernel binary images, the number of kernels was counted.In general, conventional kernel segmentation methods sometimes could not separate kernels completely because of the touching kernels.In this study, we chose the watershed segmentation algorithm to separate touched kernels.The watershed algorithm is a technique that makes use of image gradient cues to segment images.To begin with, the process of watershed segmentation started with the Euclidean distance map for each of the pixels followed by the determination of maxima points which are also called ultimate eroded points.Then we started flooding from each of these maxima points, to segment and separate the kernels from each other.Finally, we counted the number of kernels.
Though the watershed segmentation algorithm was relatively effective in separating the objects in the image, in some cases we did not have the most accurate characterization.We had two cases where the segmentation algorithm was inefficient.In the first case, a single kernel was split into several kernels.In the other case, multiple kernels which were touching each other were combined to form a single kernel.For the first case, we applied the marker-controlled watershed algorithm [29].The algorithm simulates flooding from the markers and inflates the regions according to the increasing values of a priority map until the regions extend to the watershed lines.In this method, the number of separated particles is the same as the number of labeled markers, so the simple touching kernels can be separated well.Figure 5 shows a representative binary image with traditional and marker-controlled watershed segmentation algorithm results (using mung beans images as an example).For the second case, we chose the area threshold method to segment the kernels.First, we calculated the area of all the contours in a binary image and saved them in a list.Then we sorted the list and found the median area of the contours.Because the image process might slightly destroy the contours edge and affect the original area, we chose the 1.2 times median area as the threshold.If the contour's area was smaller than the threshold, we thought of it as a single kernel, otherwise, we divided the area by the threshold and rounded the result, which was the number of the touching kernels.At the same time, the touching area was outlined in red and the number of the kernels was displayed in the center of the touching area.Combining the marker-controlled watershed algorithm and area threshold method, we can calculate the number of kernels in the image.Figure 6 shows the counting result after using the area threshold method.

Counting Method
After getting kernel binary images, the number of kernels was counted.In general, conventional kernel segmentation methods sometimes could not separate kernels completely because of the touching kernels.In this study, we chose the watershed segmentation algorithm to separate touched kernels.The watershed algorithm is a technique that makes use of image gradient cues to segment images.To begin with, the process of watershed segmentation started with the Euclidean distance map for each of the pixels followed by the determination of maxima points which are also called ultimate eroded points.Then we started flooding from each of these maxima points, to segment and separate the kernels from each other.Finally, we counted the number of kernels.
Though the watershed segmentation algorithm was relatively effective in separating the objects in the image, in some cases we did not have the most accurate characterization.We had two cases where the segmentation algorithm was inefficient.In the first case, a single kernel was split into several kernels.In the other case, multiple kernels which were touching each other were combined to form a single kernel.For the first case, we applied the marker-controlled watershed algorithm [29].The algorithm simulates flooding from the markers and inflates the regions according to the increasing values of a priority map until the regions extend to the watershed lines.In this method, the number of separated particles is the same as the number of labeled markers, so the simple touching kernels can be separated well.Figure 5 shows a representative binary image with traditional and marker-controlled watershed segmentation algorithm results (using mung beans images as an example).For the second case, we chose the area threshold method to segment the kernels.First, we calculated the area of all the contours in a binary image and saved them in a list.Then we sorted the list and found the median area of the contours.Because the image process might slightly destroy the contours edge and affect the original area, we chose the 1.2 times median area as the threshold.If the contour's area was smaller than the threshold, we thought of it as a single kernel, otherwise, we divided the area by the threshold and rounded the result, which was the number of the touching kernels.At the same time, the touching area was outlined in red and the number of the kernels was displayed in the center of the touching area.Combining the marker-controlled watershed algorithm and area threshold method, we can calculate the number of kernels in the image.Figure 6 shows the counting result after using the area threshold method.

Description of Application's Performance
"GainTKW" was designed to be used by all kinds of smartphone users.It was achieved by following two decisions.(1) Implementation of a simple and friendly graphical user interface.The interface shows only the relevant information to the users, using symbols or illustrations instead of descriptive text.(2) Linear execution.It allows users to utilize the application without any knowledge about its internal performance.
The interface is designed for simplicity and convenience.Figure 7 shows an illustrated flowchart diagram of the application, in which the application has three main functions.(1) Image source selection function.The button 'Open Camera' is used to capture real-time images.The button 'Open Album' is used to select local images to process.The selected image will be displayed on the top of the application.( 2) Kernel counting function.The button 'Count' is used to count the number of grains in the on-screen image and display the number.The image processing algorithm mentioned above will be run.(3) Thousand kernel weight calculating function.The button 'Calculate' is used to calculate the thousand kernel weight using the weight value and number value.Users can also manually enter the weight and number to the application and the result will be calculated.

Description of Application's Performance
"GainTKW" was designed to be used by all kinds of smartphone users.It was achieved by following two decisions.(1) Implementation of a simple and friendly graphical user interface.The interface shows only the relevant information to the users, using symbols or illustrations instead of descriptive text.(2) Linear execution.It allows users to utilize the application without any knowledge about its internal performance.
The interface is designed for simplicity and convenience.Figure 7 shows an illustrated flowchart diagram of the application, in which the application has three main functions.(1) Image source selection function.The button 'Open Camera' is used to capture real-time images.The button 'Open Album' is used to select local images to process.The selected image will be displayed on the top of the application.( 2) Kernel counting function.The button 'Count' is used to count the number of grains in the on-screen image and display the number.The image processing algorithm mentioned above will be run.(3) Thousand kernel weight calculating function.The button 'Calculate' is used to calculate the thousand kernel weight using the weight value and number value.Users can also manually enter the weight and number to the application and the result will be calculated.

Description of Application's Performance
"GainTKW" was designed to be used by all kinds of smartphone users.It was achieved by following two decisions.(1) Implementation of a simple and friendly graphical user interface.The interface shows only the relevant information to the users, using symbols or illustrations instead of descriptive text.(2) Linear execution.It allows users to utilize the application without any knowledge about its internal performance.
The interface is designed for simplicity and convenience.Figure 7 shows an illustrated flow-chart diagram of the application, in which the application has three main functions.(1) Image source selection function.The button 'Open Camera' is used to capture real-time images.The button 'Open Album' is used to select local images to process.The selected image will be displayed on the top of the application.( 2) Kernel counting function.The button 'Count' is used to count the number of grains in the on-screen image and display the number.The image processing algorithm mentioned above will be run.(3) Thousand kernel weight calculating function.The button 'Calculate' is used to calculate the thousand kernel weight using the weight value and number value.Users can also manually enter the weight and number to the application and the result will be calculated.

Evaluatio of Application's Performance
The way to calculate the thousand kernel weight is by counting and weighing the samples.In general, we selected some kernels from the sample randomly and recorded the weight as m0 and the amount as N.The thousand kernel weight  is calculated into the following formula (result in gram).

𝑚 =
× 1000  (4) Since the accuracy of the application was mainly determined by the accuracy of the counting result, we did experiments on the different species of kernels to evaluate the accuracy of the application.The error ratio ((estimated number − true number)/true number) was used to evaluate the accuracy of the proposed algorithm.The closer the error ratio is to zero, the higher the counting accuracy is.The designed kernel counting method and current main methods were using samples with different species and numbers of kernels.For all the datasets we have manually counted the number of kernels in each of the images and, compared it with the number of kernels detected by the proposed algorithm.
The application's performance was evaluated with a triple target: the assessment of its ability to accurately count different species of kernels under different light conditions and the comparison of its computational efficiency using the smartphone's cameras.

Performance Evaluation under Different Light Conditions
In order to test the counting accuracy of the application under different lighting conditions, we designed three experimental conditions.They were as follows: natural lighting (L1); Android device's flash lamp on (L2); an artificial lighting under the Android device (L3).The sheet was placed on a table with a dark top, and the experiments were performed in the room on a clear day.Details of the experimental conditions are listed in Table 1.The flow-chart diagram of the "gainTKW" application illustrated with the application's screenshots.

Evaluatio of Application's Performance
The way to calculate the thousand kernel weight is by counting and weighing the samples.In general, we selected some kernels from the sample randomly and recorded the weight as m 0 and the amount as N.The thousand kernel weight m H is calculated into the following formula (result in gram).
Since the accuracy of the application was mainly determined by the accuracy of the counting result, we did experiments on the different species of kernels to evaluate the accuracy of the application.The error ratio ((estimated number − true number)/true number) was used to evaluate the accuracy of the proposed algorithm.The closer the error ratio is to zero, the higher the counting accuracy is.The designed kernel counting method and current main methods were using samples with different species and numbers of kernels.For all the datasets we have manually counted the number of kernels in each of the images and, compared it with the number of kernels detected by the proposed algorithm.The application's performance was evaluated with a triple target: the assessment of its ability to accurately count different species of kernels under different light conditions and the comparison of its computational efficiency using the smartphone's cameras.

Performance Evaluation under Different Light Conditions
In order to test the counting accuracy of the application under different lighting conditions, we designed three experimental conditions.They were as follows: natural lighting (L1); Android device's flash lamp on (L2); an artificial lighting under the Android device (L3).The sheet was placed on a table with a dark top, and the experiments were performed in the room on a clear day.Details of the experimental conditions are listed in Table 1.We tested the application on two different Android devices (shown in Table 2) under three light conditions mentioned above.Figure 8 shows the counting results of the app under different light conditions.It indicates that using the algorithm in this study could segment the object well from the background.The binary images have nearly no difference.Since the counting process is based on the binary images, the same binary image could lead to the same count result.So the different lighting conditions have little effect on the counting error ratio.We tested the application on two different Android devices (shown in Table 2) under three light conditions mentioned above.Figure 8 shows the counting results of the app under different light conditions.It indicates that using the algorithm in this study could segment the object well from the background.The binary images have nearly no difference.Since the counting process is based on the binary images, the same binary image could lead to the same count result.So the different lighting conditions have little effect on the counting error ratio.

Performance Evaluation on Different Species of Kernels
The details about the datasets and the counting results are presented in Table 2.The equation of error ratio is as follows.The N 0 is the number of kernels calculated by the algorithm mentioned above.The N is the number of kernels counted manually.

Error ratio
Among different species of kernels, the error ratio increased as the number of kernels increased.The accuracy of the algorithm mentioned above mainly depends on the complexity of the touching kernels.When the number of kernels increased, the likelihood of the complex touching conditions rises too.Among these kernel species, the average error ratio of the sesame is the highest, because of its small area and irregular shape.We also found that the error ratios of the soybeans were lower than other kernels, which might be explained by its more regular shape than other species of kernels.
To determine the suitable cases for the algorithm to be used, we used a line chart to display the correlation between kernel number and error ratio, shown in Figure 9.We used the experiments under L1 lighting condition with HUAWEI P9.The number of kernels is from 1 to the maximum number that can be placed on the A4 (background-size) paper.The maximum number of Indica rice, adzuki bean, Mung bean and sesame is around 1200.The maximum number of soybean is around 900 and peanut is around 700.Because the error rates for image-based kernel counting typically increase as the number of kernels per image increase, because the frequency of overlap among the kernels increases.It is necessary to determine for each species the maximum number of kernels that can accurately be counted within a single image.In consideration of the accuracy and practical application, we chose 3% as the upper limit.In this case, the suitable number of kernels to count is different between species.The suitable number of the Indica rice is within 450, adzuki bean is within 500, mung bean is within 400, soybean is within 900, sesame is within 300, peanut is within 250.To ensure the credibility of the TKW parameter, the minimum number should be 100.The average TKW parameters of different species of kernels are shown in Table 3.The average TKW parameter can be calculated as following.
For every species of kernels, the tkw is the thousand kernel weight calculated by the "gainTKW" in an experiment, the n is the number of the experiments.

Application's Computational Efficiency Study
We tested the software running time on six species of kernels and the application was installed on Huawei P9.To accomplish a rigorous and accurate comparison, all devices analyzed exactly the same image.The kernel images were acquired with the Huawei P9 using the common Android camera application.For standardizing the state of the smartphones and minimizing the interference of other applications or services installed, the following testing protocol was defined and followed to carry out the test: 1. Closing all recent applications in the device.2. Re-starting the device.3. Selecting the flight mode.4. Waiting for 40 s for the operating system to completely load.5. Starting the "gainTKW" application.6.Running the tests 3 times.Finally, the definitive computation time for an image was calculated as the average time taken for its analysis in the three performed iterations.Results of linear regression are shown in Figure 10.

Application's Computational Efficiency Study
We tested the software running time on six species of kernels and the application was installed on Huawei P9.To accomplish a rigorous and accurate comparison, all devices analyzed exactly the same image.The kernel images were acquired with the Huawei P9 using the common Android camera application.For standardizing the state of the smartphones and minimizing the interference of other applications or services installed, the following testing protocol was defined and followed to carry out the test: 1.
Closing all recent applications in the device.

3.
Selecting the flight mode.

4.
Waiting for 40 s for the operating system to completely load.5.
Running the tests 3 times.
Finally, the definitive computation time for an image was calculated as the average time taken for its analysis in the three performed iterations.Results of linear regression are shown in Figure 10.On the one certain device, results indicate a positive moderate linear relationship between running time and the number of kernels.Since the slopes are nearly zero and the adjust R-squares are below 0.4.We did the one-way analysis of variance (ANOVA) to determine the effect of different species of kernels on running time.The result indicated that the running time was not significantly affected by the different kinds of kernels (p-value = 0.85).Statistical analyses were performed by SPSS 15.0 (SPSS Inc., Chicago, IL, USA).The running time of the Huawei P9 is about 6.8 s.

Discussion
This study aimed to develop a measurement system of thousand kernel weight based on the Android platform through image analysis.To reduce the effect of uneven lighting on the result, we used k-means clustering segmentation algorithm.To solve the difficulty of counting the touching kernels, we combined the marker-controlled segmentation algorithm and the area threshold method.
In the kernel segmentation stage of analysis, we transferred the image into a different color space to separate the luminance and chrominance signals.There are several color spaces that could be chosen like CIEL*a*b, YCbCr (Y-luminance, Cb-blue difference, Cr-red difference), HSV (Hue, Saturation, Value).The k-means clustering segmentation algorithm under these color spaces can segment the kernels from the background well.In the counting part, since the morphological operations would destroy the shape of kernels and the ellipse fitting algorithm is complex and timeconsuming, we proposed an algorithm that combines the marker-controlled watershed segmentation algorithm and the area threshold method.The method is simple, efficient and adaptable to separate the touching kernels.

Discussion
This study aimed to develop a measurement system of thousand kernel weight based on the Android platform through image analysis.To reduce the effect of uneven lighting on the result, we used k-means clustering segmentation algorithm.To solve the difficulty of counting the touching kernels, we combined the marker-controlled segmentation algorithm and the area threshold method.
In the kernel segmentation stage of analysis, we transferred the image into a different color space to separate the luminance and chrominance signals.There are several color spaces that could be chosen like CIE L*a*b, YC b C r (Y-luminance, C b -blue difference, C r -red difference), HSV (Hue, Saturation, Value).The k-means clustering segmentation algorithm under these color spaces can segment the kernels from the background well.In the counting part, since the morphological operations would destroy the shape of kernels and the ellipse fitting algorithm is complex and time-consuming, we proposed an algorithm that combines the marker-controlled watershed segmentation algorithm and the area threshold method.The method is simple, efficient and adaptable to separate the touching kernels.
Current kernel counting tools, besides manual counting, rely mainly on desktop counting equipment (http://www.shuliyi.com/html/products/71.html).The products include mainframe, stock pan, and feeder.The counting speed is 1000-2000 kernels per minute.The optical-electrical sensors are used to count the kernels.The equipment is expensive and the machine cannot be carried conveniently.It is suitable for large-scale kernel counting.Kernel counting based on image analysis is usually performed on a personal computer [30,31].These analyses pay more attention to phenotyping.When capturing images, users should minimize contact between grains.The background and the light conditions should also be controlled.Some mobile phone applications [18,32] have been developed but also did not solve the uneven lighting problem well.
The system "gainTKW" can get the thousand kernel weight information through mobile phone in an image and solve the uneven lighting problem as well as improve the counting accuracy.This system can reduce the cost of kernel counting and can be a valuable tool for farmers and researchers.

Conclusions
This paper presents a measurement system of thousand kernel weight based on the Android platform, called "gainTKW", which provides agricultural practitioners with a powerful tool for easily and automatically assessing thousand grain weight and providing useful information for yield estimation.
To test the accuracy of the algorithm, we performed a series of image analysis experiments using two types of Android devices with six species of kernels under three different lighting conditions.Since that error rates for image-based kernel counting typically increase as the number of kernels per image increase, because the frequency of overlap among the kernels increases.Because of this, and because of differences in kernel size and shape among different species, it is necessary to determine for each species the maximum number of kernels that can accurately be counted within a single image.Choosing 3% as the upper limit of the error ratio, the suitable number of kernels to count is different between species.The suitable number of the Indica rice is within 450, adzuki bean is within 500, mung bean is within 400, soybean is within 900, sesame is within 300, and peanut is within 250.There are several approaches suggested to improve image quality and counting accuracy, some of which require add-on devices (enclosed lighting and imaging attachments) to enhance the sensitivity of the camera.
Our results demonstrate that the accuracy and efficiency of "gainTKW" is comparable with those obtained using desktop PC/scanner/camera equipment, and even with modest devices, the application can be effective and reliably operated at high rates of applicability and performance.

Figure 1 .
Figure 1.The experimental setup for the system.

Figure 2 .
Figure 2. The structure diagram of the electronic balance.

Figure 1 .
Figure 1.The experimental setup for the system.

Figure 1 .
Figure 1.The experimental setup for the system.

Figure 2 .
Figure 2. The structure diagram of the electronic balance.

Figure 2 .
Figure 2. The structure diagram of the electronic balance.

Figure 3 .
Figure 3.The system Implementation Flow.From left to right: hardware, programming tools, image process algorithm, software interface.

Figure 3 .
Figure 3.The system Implementation Flow.From left to right: hardware, programming tools, image process algorithm, software interface.

Figure 4 .
Figure 4.The extraction of Indica Rice binary images using the Otsu segmentation algorithm and Kmeans clustering segmentation algorithm; (a) Original image of Indica rice; (b) Binary image of Indica rice based on Otsu algorithm; (c) Binary image of Indica rice based on K-means clustering algorithm.

Figure 4 .
Figure 4.The extraction of Indica Rice binary images using the Otsu segmentation algorithm and K-means clustering segmentation algorithm; (a) Original image of Indica rice; (b) Binary image of Indica rice based on Otsu algorithm; (c) Binary image of Indica rice based on K-means clustering algorithm.

Figure 5 .Figure 6 .
Figure 5.The result images of different watershed segmentation algorithms; (a) Binary image of Indica rice; (b) Output image of traditional watershed segmentation algorithm; (c) Output image of the marker-controlled watershed segmentation algorithm.

Figure 5 .Figure 5 .Figure 6 .
Figure 5.The result images of different watershed segmentation algorithms; (a) Binary image of Indica rice; (b) Output image of traditional watershed segmentation algorithm; (c) Output image of the marker-controlled watershed segmentation algorithm.

Figure 6 .
Figure 6.The result of the area threshold method; (a) Binary image of the Indica rice; (b) Indica rice grains that are not separated correctly; (c) The result image of algorithm (the touching area was outlined in red and the number of the kernels was displayed in the center of the touching area).

Figure 7 .
Figure 7.The flow-chart diagram of the "gainTKW" application illustrated with the application's screenshots.

Figure 7 .
Figure 7.The flow-chart diagram of the "gainTKW" application illustrated with the application's screenshots.

Figure 8 .
Figure 8.The counting results under different lighting conditions.

Figure 8 .
Figure 8.The counting results under different lighting conditions.

Figure 9 .
Figure 9.The relationship between error ratios and kernel number.

Agronomy 2018, 8 ,
x FOR PEER REVIEW 12 of 15 time and the number of kernels.Since the slopes are nearly zero and the adjust R-squares are below 0.4.We did the one-way analysis of variance (ANOVA) to determine the effect of different species of kernels on running time.The result indicated that the running time was not significantly affected by the different kinds of kernels (p-value = 0.85).Statistical analyses were performed by SPSS 15.0 (SPSS Inc., Chicago, IL, USA).The running time of the Huawei P9 is about 6.8 s.

Figure 10 .
Figure 10.The relationship between running time and kernel number on the same device.

Figure 10 .
Figure 10.The relationship between running time and kernel number on the same device.

•
Build Tools: Android Studio utilizes the fast growing Gradle build system.Eclipse, on the other hand uses Apache Ant as its main build system.• Advanced Code Completion/Refactoring: Both IDEs feature the standard Java code auto completion but in the case of Android Studio, Google has baked in deeper support for specific Android code and refactoring.Android Studio can refactor your code in places where it's just not possible using Eclipse and ADT.• Project Organization: Both IDEs work differently in an effort to help you manage and organize your projects.When using Eclipse you must be familiar with the concept of workspaces, but Android Studio introduces the concept of modules.• IDE Performance/Stability: Eclipse is a purely Java based software, and needs high amounts of Ram and CPU power to run reliably.Android Studio is a more stable and lightweight IDE.

Table 1 .
The light conditions for measuring the accuracy of the segmentation algorithm.

Table 1 .
The light conditions for measuring the accuracy of the segmentation algorithm.

Table 2 .
The performance evaluation of the accuracy of kernel counting using the "gainTKW" application on two different Android devices using six species of kernels (KN means kernel number).

Table 2 .
The performance evaluation of the accuracy of kernel counting using the "gainTKW" application on two different Android devices using six species of kernels (KN means kernel number).

Table 3 .
The average TKW (thousand kernel weight) parameters of different species of kernels.

Table 3 .
The average TKW (thousand kernel weight) parameters of different species of kernels.