## 2 Materials and Methods
### Detection of Objects and Motion Estimation  
From the point of view of image segmentation and object detection, and due to the nature of the set up, (a biological experiment in a real, small-scale industrial environment) there were three main problems: noise, artefacts and occlusions.  
Two main sources of noise were identified: air bubbles and shadows. The main noise was generated by the air bubbles moving towards the surface. This creates a little wave system on the water surface, which makes the light penetrate the water in a nonlinear manner. The second noise source was the shadows of the fish on the bottom and the tank's walls. Although the lighting was placed on the ceiling above the tank to avoid this issue, the generation of some shadows was unavoidable (see Figure 3).  
Figure 3: Example of an artefact indicated by the arrow, caused by a feed pellet. Occasionally, the system also had difficulties to discriminate some fish from their shadows (circle).  
Figure 2: Recording procedure. The total recording time was 1h 30 min. The results presented here correspond to the analysis the 30 s pre- and 3 min post-event.  
There were three main types of artefacts (anomalies introduced in the signal or in the data by the equipment or the technique): the first was caused by the pellets used for feeding the fish. Some food pellets were suspended in the water and when fish swam around them, they spread off forming black holes in the images (see Figure 3). The second (very similar to the first but smaller in size) was caused by the faeces of the fish, and the third by the light's reflections on the skin of the fish.  
Occlusions are a well-known issue in tracking that occur when two or more tracked target images become one during a time period in the sequence. Occlusions are more frequent when target objects are similar to each other, as it happens in animal groups and fish [34]. Occlusions in fish tracking may lead to two types of misidentification: loss of fish identity and swapping identity between individuals [31]. Tracking problems take place both while the occlusion occurs and when the occlusion ends and the fish appear separately in the image. To solve this problem different solutions have been developed such as the use of 3D information [35, 36], using the animals' characteristics, such as its shape [37, 38, 39] or analyzing the special topology of the shape [40, 41, 42, 43]. Automatic scene calibrating systems are also very helpful tools and many approaches have been made in this field, for example an automatic calibrating camera system for tracking people [44].  
Given that the robustness of the system depends on how the motion detection takes place and all the problems listed above severely limit the election of the motion estimation algorithm, we decided to use an algorithm based on optical flow in order to eliminate noise, artefacts and occlusions in one step. This conventional approach is based on the calculation of the local relative motion [45] which is also used for space segmentation [46], and it has the additional advantage that it can work with a moving camera and/or with moving backgrounds. We applied this method to detect objects and estimate their motion by a simple process which consisted in identifying the differences between one of the images and the image obtained in the previous second, _i.e._, resting each frame from its 24 th predecessor (since we work at 24 fps). This made it possible to delete background, noise and artifacts common to all images, including objects that had not changed position in the previous second while keeping those objects whose position changes in 1 second intervals, _i.e._, the moving fish. Methods based on optical flow provide very valuable information but they are computationally intense and sometimes require specific hardware.  
After the optical flow, or motion, was calculated, the images were binarized using standard morphologic operations in order to be able to detect the elements in the image and their centers in each frame.