Development and validation of a visual image analysis for monitoring the body size of sheep

ABSTRACT In china, there is a great variety of sheep production systems, from the most traditional grazing types, mixed (grazing/stabling) to the most technologically advanced facility sheep. Body size parameters can reflect its growth development, production performance and genetic characteristics. So, monitoring body size is very important. In view of the problems or limitations of the present manual measurement, using the tools of measuring stick, tape measure, etc., the sheep having to stand on a flat place with correct posture, a non-contact method for measuring body dimensions of small-tailed Han sheep based on machine vision has been proposed and discussed. This approach is based on a position limit apparatus, computer-assisted visual capture, an automatic foreground extraction algorithm and a measuring point detection algorithm. The measured body sizes include withers height, back height, rump height, body length, chest depth, chest width, abdominal width and rump width. This approach has been examined in a specific farm for a case study. The errors of more than 90% measurements for sheep body size are within 3%. The results indicate that the method based on visual image analysis is effective, and it is especially suitable for the sheep feeding in an intensive and large-scale way.


Introduction
The sheep industry is a significant part in the animal production substructure in China, especially in Inner Mongolia and its surrounding west area, playing an important role in culture, income, employment as well as in nutrition of the household (CSY 2016). Nowadays, increased interest among animal scientists and producers in lifetime growth-age relationships has been stimulated by the recognition of the economic importance of maturing, rate of gain, mature size and related characters. Growth, usually defined as the increase in size or body weight at a given age, is one of the important considerations for the selection of improvement on meat animals like sheep (Mandal et al. 2011). During the course of growth, the animal's different body parts change in proportion. So, body dimensions or linear measurements have been used in conformation appraisal for many livestock species as a supplement to body weights when measuring the productivity. Scientists have achieved tremendous successes in phenotypic characterization study of growth-development production performance and genetic characteristics for sheep (Aziz and Sharaby 1993;Janssens and Vandepitte 2004;Zhou 2013;Zishiri et al. 2013). Various studies showed that the body dimensions of sheep can be used to study the interactions between heredity and environment (Dunlop and Young 1966), to assess the growth rate, feed utilization, carcass characteristics in farm animals (Wynn and Thwaites 1981) and to predict the live body weight based on the body measurements (Aziz and Sharaby 1993;Mohammed and Amin 1997;Varade and Ali 1999;Atta and Khidir 2004). In addition, linear size measurements have been suggested as the more objective measures for body conformation of animals (Janssens and Vandepitte 2004).
Therefore, in order to evaluate breeding efficacy, it is essential for feedlots to accurately measure and track live physical characteristics, growth and performance data of the animal. The most traditional measurements, using the tools of measuring stick, tape measure, etc., keeping the sheep on a flat place with correct posture, requiring another professional to measure and record body dimensions, have obviously problems or limitations, e.g. causing the sheepa injury and stress when forcing the animal into position for an accurate measurement. To avoid direct contact with animals, the measurement based on computer-assisted visual image and digital image has been studied and proposed in literatures, as in the review by Frost et al. (1997). At present, image processing technology has been applied more and more to such fields as industry measurement, control and guide, virtual reality, biologic pharmacy, etc., permeating rapidly through the scientific research and production in agriculture (Zwertvaegher et al. 2011;Liu et al. 2013;Guo et al. 2014;Zhu et al. 2014;Vieira et al. 2015;Wongsriworaphon et al. 2015). Obviously, the image body-measuring method is easier to be implemented than that by manual; also it has the advantages of less workload and impersonal impartiality due to the measurement not effected by operators and time.
In recent years, some scholars have studied image processing technology for sheep body dimension measurement ), but only two parameters were measured and a single-view image selected, with the result that the acquired data were not enough and the detection was inaccurate. In view of the problems or limitations of the present approach, a non-contact measuring method for a sheep's body dimensions based on a multiple-views machine vision has been proposed and discussed in this paper.

Animals
The study has been performed at Hailiutu sheep farm affiliated with Inner Mongolia Agricultural University of China, where a demonstration project of China-Canada Science and Innovation Center on Sustainable Agriculture was implemented. The farm is located in Beishizhou village, Tumd Left Banner, Hohhot, Inner Mongolia of China, longitude 111°22 ′ 30 ′′ , latitude 40°4 1 ′ 30 ′′ . Eleven small-tailed Han sheep (2 growing sheep and 9 adult sheep), aging from 6 to 36 months with mean weight of 66.3 ± 10.5 kg, which is a meat and fur sheep breed originated from Mongolian sheep in ancient north China (Ovis aries), were randomly chosen from a herd.
For this study, hundreds of top-view, left-side-view and rightside-view images of the sheep with various body postures were taken, and 160 good quality images were selected. When finishing image collection, the measured sheep was weighed by a self-designed 'walk-over-weighing' device.
All procedures involving animals were approved by the animal care and use committee at the respective institutions where the test was conducted.

Apparatus
Using the digital image-based method for measuring a sheep's body dimensions normally requires the sheep at an appropriate position and quite stationary, which is not practical in a farm because it is difficult to set up an animal in a required position and keep it stationary. Some of the previous studies sought to develop the methods for volumetric and dimensional measurement of livestock while it was limited to a specific area with apparatus (Kriesel and Paul 2007). Sheep, belonging to large livestock and having more joints, compared to other large animals, such as cattle, pigs, et al., have better flexibility, more variable posture and a high-level behaviour accompanied with psychology. So, it is necessary to design an apparatus to limit sheep to a certain space.
Normally, there are two limitations in practice when using the image-processing techniques for body dimension measurement. First, images should be taken from an individual sheep, and second, a suitable environment, such as a blue background (Yan et al. 2009;Tian and Peng 2016), is needed to distinguish the sheep body from the surroundings. To address these problems, an alleyway assembled in front of the entrance and three blue backdrops (left-side-backdrop, right-side-backdrop, ground backdrop) were designed in this study.
Ideally, the physical and growth traits of each animal should be known at each stage of its stay in the feedlot in order to achieve an optimum management; thus, the accurate measurement of physical dimensions of each sheep is required regularly. Since most feedlots in northern China may house one hundred to one thousand sheep, as aforementioned, it is essential to accurately and rapidly acquire the repeated measurements as part of an automated tracking system.
For the above reasons, non-contact automatic measuring apparatus are obviously preferred for measuring sheep both accurately and rapidly (see Figure 1).
The apparatus includes an RFID identification system, automated gates, a position limiting device, a weighing system and an image acquisition device.
During the test, when sheep passed the alleyway (chute) and approached the range of around 30 cm from the entrance, the RFID ear tag reader started to read the identification tag from a distance, while the door was opened automatically. Once the signal of the ear tag reader disappeared, the door of the entrance was closed, and the sheep was positioned in the limiting device. First the sheep was weighed, the weight was read until it stabilized, then the three digital cameras (top, left, right) were started to capture images and the data were added to the database, also the exit was opened. When the weight data are reset, the door of the exit will be closed. The alleyway (chute) and gate system are used for sorting and grouping the sheep according to weight.

Camera number and layout
The most current state-of-the-art techniques for measuring animals rely upon the acquisition of images showing silhouettes or profiles of the animal. In any view, it provides only a record of the target animal's shadow with a loss of any three-dimensional shape within the silhouette outline. In general, the selection of the number and location of cameras required to accurately reproduce a target is largely dependent upon the complexity of the target surface.
Generally, the parameters of body size consist of four type indicators: body length, body width, body height and circumference (Mayaka et al. 1996;Chen et al. 2008;Shi et al. 2008;Chacón et al. 2011;Wang et al. 2011;Bingöl et al. 2012). In order to achieve sufficient accurate measurements, multiple views from different angles must be used.
Generally, it is believed that the animal's body is symmetrical. So, side and top views provide sufficient accuracy in achieving the desired linear measurement or calculation. However, the object is a live animal; it may often stand out of symmetry of the plane, which will potentially cause measuring errors (see Figure 2).
In order to avoid reduced precision caused by the disadvantage of the object deviating from the camera's optical axis when using a single side camera, a three camera configuration as shown in Figure 3 was designed. Orthogonal orientations were arranged for the cameras, aligning from the right side to the left side and the top.

Camera configuration and positions
Since live animals are seldom still, in order to fulfil the primary goals of this apparatus, the camera technology should exhibit the following characteristics: resistance to motion artefacts and a high animal throughput rate. A secondary goal is to accomplish the complete output at a video rate of 30 frames per second. For a camera, motion artefact resistance means the accurate acquisition of each individual image acquired by  a camera. A fast shutter speed or short image acquisition time, used in conjunction with a narrow flash pulse, is able to 'freeze' motion to avoid a blur in that individual image. A high animal throughput results from efficient processing algorithms in computing the desired measurements. These goals benefit from a minimal number of images to compute measurements and a high-speed DSP (digital signal processor). Minimizing the number of cameras also improves the processing speed.
Excluding the application of extraordinary resolution enhancement techniques, the resolution of the camera is a key factor. The resolution of the camera is greatly reduced by motion artefacts and blurring. The required shutter speed is related to the desired resolution and the motion speed in the following manner: where R is the desired resolution on the surface of the target in inches; V is the velocity of the target in inches per second; and S is the shutter speed or image acquisition time in seconds. On this account, image recording for the visual image analysis (VIA) was performed by three small gigabit Ethernet industrial cameras 'MV-EM120C' (mounting COMPUTAR_H0514-MP2 lens and 1280 × 960 pixel resolution).
Considering that the sheep is always moving, the apparatus must be capable of freezing such movement for achieving an accurate measurement. From literature (Sun et al. 2016), it is known that body height, body length, rump width and chest width of finishing small-tailed Han sheep are about 80, 92, 20 and 24 cm, respectively. Referring to the above body size data and leaving some spaces, the apparatus dimension is 1400 × 600 × 1100 mm (length × width × height).
The relationship between focal length and work distance is defined by: where f is the focal length, W D is the work distance, H o is the height of field of view, H i is the effective height of the camera imaging plane, i.e. sensor image size.
If the sensor size (SS, 4.8 × 3.6 mm), focal length (f , 5 mm) and the minimum focal distance (D min , 10 mm) are known, the minimum field of view (FOV min ) will be obtained by using the following equation: (3) According to the above formulas, the measuring accuracy is 0.0075 mm, and the maximum distance from left camera or right camera to enclosure was designed to be 1500 mm (see Figure 3).

Data acquisition
The data of the apparatus were acquired with an equipment layout similar to that shown in Figure 4. In this layout, when the target sheep enters the alleyway from the chute, the RFID ear tag reader mounted on one side of the alleyway will detect and identify the ID of the sheep. Then an actuator will open the entrance door and allow the sheep to proceed into the central region, standing on the weighing platform which the cameras are capable of accessing. The live weight of the sheep will be collected by a weighing system and the data will be stored automatically in an indicator; also the images of both sides and the top will be obtained by three cameras in the different locations. Upon the appropriate images being acquired, they will be saved in a computer for displaying and processing.

Image pre-processing
It is clear that the accuracy of the measurement of a sheep's body dimensions is dependent on the quality of the digital images and the efficiency of the image segmentation processing. Image pre-processing can significantly increase the reliability of an optical inspection, in which the techniques of lighting compensation and median filtering are commonly applied to eliminate noise and protect image edge in order to gain satisfactory recovery.
Image segmentation, which is the process of partitioning a digital image into multiple segments, is typically used to locate objects and boundaries in images. One of the most efficient ways of image segmentation is superpixels, which has been commonly used in recent years (Shi and Malik 2000;Vedaldi and Soatto 2008;Levinshtein et al. 2009;Veksler et al. 2010). Superpixel algorithms group pixels into perceptually meaningful atomic regions which can be used to replace the rigid structured pixel grid. They can capture image redundancy, provide a convenient primitive from which to compute image features and greatly reduce the complexity of subsequent image processing tasks. Achanta et al. (2012) introduced a superpixel algorithm, so-called simple linear iterative clustering (SLIC), adapting a k-means clustering approach to efficiently generate the superpixels. This method adheres to boundaries as well as or better than previous methods. At the same time, it runs faster, saves more memory, improves segmentation performance and is straightforward to extend to supervoxel generation.
By default, the SLIC algorithm has only one parameter k, the desired number of approximate equal-sized superpixels. For colour image with pixels N in the CIELAB colour space, the clustering procedure begins with an initialization step where k initial cluster centres C i = [ l i a i b i x i y i ] T with i = [1, k] are sampled on a regular gird spaced S pixels apart, where, l i , a i and b i are the pixel colour vectors in CIELAB colour space, and x i , y i are the pixel positions. To produce roughly equally sized superpixels, the grid interval is S = N/k . The centres are moved to seek locations corresponding to the lowest gradient position in a 3 × 3 neighbourhood. Next, in the assignment step, each pixel i is associated with the nearest cluster centre whose search region is overlapped with its location. The nearest cluster centre for each pixel is determined by a distance measure D, as depicted in Equations (4)-(6).
By defining D in this manner, m can be in the range of [1, 40] when using the CIELAB colour space, allowing us to weigh the relative importance between colour similarity and spatial proximity. When m is large, spatial proximity is more important and the resulting superpixels are more compact. When m is small, the resulting superpixels adhere more tightly to image boundaries, but have less regular size and shape. The results of sheep image pre-processing and image segmentation are shown in Figure 5.
The image was segmented into 300 superpixels using a weighting factor 10, relating spatial distances to colour distances; the resulted superpixels of area less than 10 pixels are eliminated and superpixel attributes were computed from the median colour values.

Image post-processing
Superpixels are commonly used as a pre-processing step in segmentation algorithms. In order to extract the object from the background, clustering approaches, which have emerged as well-liked techniques for unsupervised pattern recognition, were applied. Fuzzy c-means (FCM) clustering (developed by Dunn in 1973 and improved by Bezdek in 1981), which first optimally partitions a dataset into a given set of classes, is frequently used in machine learning (Alpaydin 2004). In our work, the SLIC superpixels' cluster centres were chosen as the clustering dataset factors [ l j a j b j ], which have the dominant impacts on formation and development of image.
After a sheep's body in image is revealed, other image postprocessing algorithms are extracted: region filling, morphological image processing, hole filling, boundary detection et al. The notations and image post-processing algorithms are described as follows.  Algorithm image post-processing 1. Begin. 2. fcm(data,2). %Data is computed by two categories, one is the background, the other is the sheep's body. 3. According to R values of FCM's cluster centres, the cluster with a larger R value is defined as the sheep's body, filled with white. The other cluster is defined as the background, filled with black. 4. Imopen, imclose. % Morphological image processing to eliminate noise. 5. Hole filling. 6. Removal of small areas of image. 7. Hough transform to detect straight line (vertical or horizontal) for filling fences. 8. Canny. 9. End. Figure 6. Figure 7 shows the measuring parameters in side view for determining the sheep's morphometric characteristics. By tradition, the body size measurement of sheep including withers height (WH), back height (BH), rump height (RH), body length (BL) and chest depth (CD) was carried out with a tape by the same technician (Figure 7(a)). In consideration of the limitations of computer vision measurement, the measurements were conducted on the surface of the animal (Figure 7(b)), meaning that the thickness of fleece was included in the body size measured by this method. The thickness of fleece in sheep would result in biased results, and body dimension would be overestimated. However, the change of body size is of more concern than the body size itself while the body size parameters are primarily used to study the sheep's growth development, production performance and genetic characteristics. Fleece growth is slower than the growth of body size; so the changing trend of body size would not be submerged. For the flocks, ovulation synchronization, single fixed-time artificial insemination and induced parturition lead to lambing synchronization, which facilitates supervision because of the individual animals studied at the same time (Abecia et al. 2012). In this situation, body size parameters including the fleece will have no significant impact on the prediction of group performance, because the growth of fleece was similar within a population/group.

Algorithm for the measurement of body height and body length
A flowchart for the measurement of a sheep's body dimension is shown in Figure 8.
Positioning withers was based on peak detection algorithm, and positioning rump was conducted by using the maximum curvature theory. The curvature k is depicted in Equation (7).
where yis the fitting function for the back curve of the sheep. The anterior shoulder point was determined by searching the maximum distance from a measuring point to a specific  line, and the distance can be calculated by Equation (8).
(x, y) is the point on the anterior shoulder edge, ax + by + c is the equation of the line.

Algorithm for the measurement of body width
Sheep body width was measured by the top view. Figure 9 shows the measuring parameters for the sheep's morphometric characteristics in top view. In top view, the sheep's body can be regarded as symmetrical. Therefore, the body width can be determined by calculating the distance of the symmetry points on the outline of the sheep's body. However, the sheep's body posture is variable because of multi-joints; so it is difficult to directly find the symmetrical centre line. The proposed algorithms for the measurement of the sheep body width have been summarized as follows: (1) Find the centroid (x 1 ) of the sheep in the image, as shown in Figure 10  (4) Small-tailed Han sheep has a long neck. When it is craned forward, it will lead to forward centroids. In order to compensate for regional changes caused by the centre of gravity forward, three centroids (x 8 , x 9 , x 10 ) are added, where, x 8 is the centroid of the x 5 to x 1 region, x 9 is the centroid of the x 6 to x 3 region, and x 10 is the centroid of the x 7 to end region, as shown in Figure 10 (7) In order to lessen the task of calculation, approximate broken-line is used to replace the fitting curve. The method repositions the centroids referred to in the fitting curve and connects the new centroids as the central axis of the sheep's chest, as shown in Figure 10(g). (8) Calculate the distance between two symmetrical points across the central axis, and then fit the data. First, the neck is positioned on the fitting curve by searching the point where the curvature is minimum. Then, the measuring points of chest width are determined on the fitting curve except the neck section by searching the point where the curvature is maximum. The result is shown in Figure 10(h). (9) In a similar way, approximate symmetrical centre line of the abdomen (Figure 10(i)) and the distance of opposite edge points are calculated. The measuring points of abdominal width are positioned based on the maximum distance on the fitting curve, as shown in Figure 10(j). (10) When centroids are moved forward, the rump region is determined by x 3 →x 7 →x 10 ( Figure 10(k)), else the region is determined by x 9 →x 3 →x 7 . Then, measuring points of rump width are positioned based on the fitting curve by searching the point where the curvature is maximum, as shown in Figure 10(l).

Results and discussion
In this section, the performance of the proposed sheep's body dimension measuring method is assessed.

Measurements of body height and body length
To test the influencing factors of the measuring method, a sheep was imaged five times at different times and in different postures during its stay in the apparatus. The body height and body length values of the sheep for each test were measured by using a measuring stick as the reference values (WH: 76 cm, BH: 74cm, RH: 79 cm, BL: 85cm, CD: 39 cm) and they were correspondingly determined by the proposed measuring algorithm as well. The results of the tests are shown in Table 1. From the experimental results, it was found that the measured value of the WH was 75.9 cm, the BH was 72.9 cm, the RH was 78.4 cm, the BL was 87.0 cm and the BL was 39.0 cm, while the error for each value ranged from −10.3 to 6.4 cm,−8.9 to 3.1 cm, −6.6 to 2.8 cm, −5.8 to 11.0 cm and −5.4 to 3.9 cm in monocular vision, respectively; and the error for each value ranged from −3.2 to 2.0 cm, −2.8 to 1.9 cm, −2.3 to 1.3 cm, −0.3 to 3.9 cm and −0.7 to 1.8 cm in the twocameras model, respectively. These measuring errors were mainly caused by the sheep's different postures or the influence of the relative open-air environment. However, these factors are inherent characteristics of the researched object or apparatus. For increasing the accuracy of the measurement, repetition of  the test and use of the smoothing filter are helpful. The rootmean squared errors of WH, BH, RH, BL and CD when filtered were 1.98, 1.79, 1.25, 3.01 and 0.92, respectively, having a high accuracy in comparison with the actual values. The errors of BL are the biggest because the change of body posture has a greater influence on body length.
To examine the repeatability of the measuring method, ten sheep aged 6-36 months were selected. Each sheep was imaged five times at different times and in different postures in the apparatus. The results are shown in Table 2.
The measuring errors of different parameters for each sheep are quite dispersed (e.g. the maximum errors for WH, BH, RH,BL and CD occurred to the No. 5,No. 3,No. 8,No. 9 and No. 1 sheep, respectively) and deviated highly. However, over 84% of the errors are limited to within 3%. The errors of body length are mainly caused by the sheep's different postures, and the errors of chest depth are mainly due to the low position of the lowest point of the sternum when the sheep turned its head.

Measurements of body width
The measurements were carried out on the same sheep in the same way as above. The results of body width measurement are shown in Table 3.
It can be seen that the proposed algorithms can accurately measure the sheep's body dimensions with a deviation of less than 2.1% on average. The greater errors for CW, AW, RW occurred for the No. 4 sheep. Compared with original image and experimental data, errors are caused by lower boundary  The mean value was computed by removing a maximum value and a minimum value.  segmentation accuracy caused by shadow between the body and the apparatus, as well as the sheep's body height being far beyond the calibration height.

Conclusion
The objective of this study is to develop an automatic measuring system for detecting a live sheep's body size which can be practically implemented in a farm with less disturbance to the animals. The proposed system based on computer-assisted VIA consists of four modules: background filtering, boundary detection, measuring points extraction as well as parameter calculation. For identifying an image's edge, a method based on SLIC segmentation algorithm and FCM clustering was suggested, by which each sheep's image was used to obtain the pixel locations of the sheep outline and convert them from pixel coordinates to body size values. This apparatus and methods applied selected features of advanced machine vision technology to the noninvasive quantification of the sheep's dimensions, require not only accurate measurements, but automation and rapid data acquisition as well. The experimental evidence suggests that the method is effective. The errors of more than 90% measurements for sheep's body size by VIA are within 3%. Meanwhile, there are at several advantages in using the image method to measure a sheep's body size: (1) it is a noncontact measuring method with less disturbance to the animals; (2) with less workload and less stress than the manual measurement; and (3) gets rid of the shortcomings of measuring standard changing with operator and time. Furthermore, with the attraction of application of machine vision technique to non-contact measurement, and the development of image processing technique, it can be pointed out that these will be the trend in future body size measurement of livestock since it prevents the animal's stress actions and anthropozoonosis. This will provide a basis for the rational management and production efficiency improvement in sheep breeding. Even with multiple views from many angles, researchers can attempt volumetric measurement.