# Measurement of Fish Morphological Features through Image Processing and Deep Learning Techniques
## 2 Materials and Methods
### Image Processing Approach
Scan \(\mathbf{edgesgm}\) and remove pixel islands with less than min_pix adjacent edge pixels
```  
**Algorithm 1** Custom edge detection algorithm  
The first image processing approach toward the estimation of the fish contour and the localization of the fish parts exploits the _edgesgm_ image produced after the edge detection step, as described in the previous paragraph. This approach is in the path that leads to the \"Contour1\" output in Figure 1. This path has low latency but produces valid results only if the background is simple (e.g., sea water). The system can validate whether this approach can be successfully followed using an image segmentation such as the one shown in Figure 2. Taking into consideration that the resolution of the resized images was 80 \(\times\) 160 pixels, the size of each one of the segments shown in Figure 2, was 20 \(\times\) 40 pixels. Assuming that the fish did not extend to the whole picture, it was expected that in most of the border segments B11-B21-B31-B41-B42-B43-B44-B34-B24-B14-B13-B12, the number of edges was small, unless the background was complex. The number of unconnected edges and the mean pixel value in each segment were estimated from the _edgesgm_ matrix. Remember that a pixel in the _edgesgm_ binary mask is one if it belongs to an edge, and it is 0 otherwise. If the number of disjointed edges in each segment was less than 10 and the mean pixel value in this segment was lower than 0.25, the segment was considered \"empty\" of edges; otherwise, it was characterized as \"crowded\". An image displaying a fish with a simple background was expected to have a small number of crowded boundary segments, since only the boundary segments that contained a fish part would be crowded. If less than 6 segments were found to be crowded, the system assumed that the path toward \"Contour1\" in Figure 1 led to acceptable results. Figure 3 shows an example of an image that was rejected because 7 of the boundary segments were found to be crowded. Once the system decides that the \"Contour1\" path in Figure 1 is not acceptable, it attempts to determine the fish contour based on the edges found in the matrix _edgesgm_. A connection of disjoint edges takes place as described in Figure 4. In Figure 4a, the contents of a part of the _edgesgm_ binary mask are displayed, assuming that the ones belong to the fish contour. A second matrix _edgesgm2_ is constructed as follows. Each row (from top to the bottom) of _edgesgm_ is scanned from left to right. An identity _id_ is initialized to 0. All neighboring pixels are assigned with the same _id_. Figure 4b shows the form of _edgesgm2_ after its construction. In order to connect the disjoint edges, the matrices _edgesgm_ and _edgesgm2_ are scanned again. If 1 is found in the pixel (_i,j_) of _edgesgm_, the corresponding pixel in the _edgesgm2_ matrix is set to _id_ if one of the pixels (\(i-1\),\(j-1\)), (\(i\),\(j-1\)) in _edgesgm2_ is already set to _id_. If none of these pixels have been set to a non-zero _id_, then the maximum value of _id_ is increased by 1 and this value is assigned to _edgesgm2_(\(i\),\(j\)). Figure 4c shows the matrix _edgesgm2_ after this step. As can be seen from this figure, the edges have been connected and thickened. To avoid having adjacent pixels with different identities in _edgesgm2_, a third scan takes place to merge neighboring identities (Figure 4d). After this merging process, the larger identity value corresponds to the number of disjoint edges. The larger edge (i.e., the one that contains the larger number of pixels) is selected as the contour of the bigger fish that is displayed in the photograph. In this way, although multiple fish can exist in the image, the analysis will proceed with the largest one. Complicated shapes in the body of the fish may confuse the proposed method. Consequently, a solid representation of the fish is generated by setting to 1 all the pixels within the (assumed) fish contour. The border of this solid fish body is now used as the fish contour in the pattern matching.