1 Introduction

The Euler number or Euler characteristic is a feature that allows describing the topological structure of an image or an specific object in an image. As it is known, the Euler number has been used in many applications: industrial part recognition [1], real-time thresholding [2], object number calculation, [3], and real-time Malayan license plate recognition [4], to mention a few.

Mathematically speaking, the Euler number, \( e \), of a digital binary image \( I\left( {x,y} \right) \) can be obtained as follows:

$$ e = o - h $$
(1)

In this case, \( o \) is the number of objects or (binary regions) in the image and \( h \) is the number of holes (i.e., isolated regions of the image’s background).

Many methods have been developed to obtain the Euler number of a digital binary image. Some of these methods, can be found in [525].

The algorithm outlined in [5] was one of the first reported in literature. The most popular algorithm of this method is used by the MATLAB image processing tool. It calculates the Euler number of a binary image as:

$$ e = \frac{s1 - s3 - 2 \cdot x}{4}. $$
(2)

In this case:

  1. 1.

    \( s1 \) is the number of matrices \( \left\{ {\left[ {\begin{array}{*{20}c} 0 & 0 \\ 1 & 0 \\ \end{array} } \right],\left[ {\begin{array}{*{20}c} 0 & 0 \\ 0 & 1 \\ \end{array} } \right],\left[ {\begin{array}{*{20}c} 0 & 1 \\ 0 & 0 \\ \end{array} } \right],\left[ {\begin{array}{*{20}c} 1 & 0 \\ 0 & 0 \\ \end{array} } \right]} \right\} \);

  2. 2.

    \( s3 \) is the number of matrices \( \left\{ {\left[ {\begin{array}{*{20}c} 0 & 1 \\ 1 & 1 \\ \end{array} } \right],\left[ {\begin{array}{*{20}c} 1 & 0 \\ 1 & 1 \\ \end{array} } \right],\left[ {\begin{array}{*{20}c} 1 & 1 \\ 1 & 0 \\ \end{array} } \right],\left[ {\begin{array}{*{20}c} 1 & 1 \\ 0 & 1 \\ \end{array} } \right]} \right\} \), and

  3. 3.

    \( x \) is the number of matrices \( \left\{ {\left[ {\begin{array}{*{20}c} 0 & 1 \\ 1 & 0 \\ \end{array} } \right],\left[ {\begin{array}{*{20}c} 1 & 0 \\ 0 & 1 \\ \end{array} } \right]} \right\} \).

As we can see, before using (2), the MATLAB algorithm needs to perform up to 10 comparisons on each image pixel. Time complexity for this method is of \( O\left( {N^{2} } \right) \) for a \( N \times N \) image, which is linearly dependent on the number of pixels. For image processing tasks, where the data could be huge the constant term that is so often hidden in the big-Oh notation becomes important.

Artificial Neural Networks (ANN), on the other hand, have been successfully used in many tasks including signal analysis, noise cancellation, model identification, process control, object detection, and pattern recognition, and so on. Many ANN models have been reported in literature, since the very simple Threshold Logic Unit (TLU), introduced by McCulloch-Pitts [26] at the beginning of the 40’s, passing by the well-known Perceptron, presented to the world by Rosenblatt in the 50’s [27, 28] until the so called Morphological Neural Models with and without Dendritic Processing introduced by Ritter et al. in [29, 30, 31, 32], to mention a few.

In this paper, we show how an MLP can be used to compute the Euler number of a 2-D binary image. By making an analysis of the local results provided by a known formulation to compute the image Euler number we arrive at the specialized ANN architecture. We decided to use a MLP for its versatility since many years ago in a multitude of situations. This is the first, to our knowledge, that a MLP-based architecture is used to compute the Euler number of a 2-D binary image. It constitutes and original an interesting option to compute this topological describing feature.

The rest of this paper is organized as follows. In Sect. 2 we describe our proposed methodology to derive at the end to the specialized MLP based architecture to compute the Euler number of a 2-D binary image. We devote Sect. 3 to report the experimental results that validate the applicability of the derived specialized MLP architecture as well as a comparison with other approaches reported in literature. In short, in Sect. 4 we reach our conclusions and directions for present and future research.

2 Our Proposal

In this section we describe how an MLP can be used to obtain the Euler number of a 2-D digital binary image. We decided it to do so, because as it is known MLPs have shown to be an excellent options to solve many problems in multiple areas where pattern classification is required.

To train a MLP to obtain the Euler number of a 2-D binary image we can proceed as usual by firstly selecting a set of \( P \) training samples, for example: \( M \times N \) 2-D binary images: \( {\mathbf{I}} = \left\{ {I_{1} ,I_{2} , \ldots ,I_{P} } \right\} \). Before training the ANN, suppose we divide set \( {\mathbf{I}} \) into \( q \) sub-sets of images such that each sub-set has the same Euler number, according to (1). With this in mind, we could proceed, for example, as illustrated in Fig. 1(a) by presenting, on the one hand, as input to a known Euler number computation method, that in turn outputs a correct value of \( e \) for each image: \( I_{k} ,k = 1,2, \ldots ,P \). As can be appreciated from this same figure, each image is also presented at the input of the untrained MLP, that produces a value: \( \hat{e} \), as an estimate of \( e \). The resulting error: \( E \), could be then used, in an iterative way, to adjust the MLP weights until it is ready to compute the Euler number of an unknown input image as illustrated in Fig. 1(b).

Fig. 1.
figure 1

A first alternative to train a MLP to compute the Euler number of a 2-D binary image. (a) Training of the MLP. (b) Testing of the MLP.

It is clear that if we apply the above described strategy, we would have several inconveniences. A first inconvenience would be the following: If we use a three layer MLP, the number of input neurons would be \( M \times N \) (the image size); the number of output neurons would be directly proportional to the number of values \( NV \) of \( e \) \( \left\{ { \ldots , - 3, - 2, - 1,0,1,2,3, \ldots } \right\} \) needed to be computed for an input image. In short, the correct number of hidden neurons to obtain the desired values for \( e \) would be certainly very big and rather difficult to find.

A second inconvenience would be that because the Euler number of an image is a function of the number of its objects and its holes, lots of training images would be required to reach good training results, this is because we have too many possibilities. Instead of using an architecture like this, we propose to derive a specialized one as follows. Let us first consider the following two expressions, introduced in [33] to compute the Euler number of a 2-D digital binary image in terms of only three comparisons. For the case of 4-connected regions (regions where their pixels are allowed to be connected only by their sides), the authors propose computing the image Euler number as:

$$ e = \# \left( {\begin{array}{*{20}c} 1 & 0 \\ 0 & 0 \\ \end{array} } \right) - \# \left( {\begin{array}{*{20}c} 1 & 1 \\ 1 & 0 \\ \end{array} } \right) + \# \left( {\begin{array}{*{20}c} 1 & 0 \\ 0 & 1 \\ \end{array} } \right) $$
(3)

On the other hand, for the case of 8-connected regions (regions where the pixels are allowed to be connected by their sides and corners), the authors propose to compute \( e \) by means of the following equation:

$$ e = \# \left( {\begin{array}{*{20}c} 1 & 0 \\ 0 & 0 \\ \end{array} } \right) - \# \left( {\begin{array}{*{20}c} 1 & 1 \\ 1 & 0 \\ \end{array} } \right) - \# \left( {\begin{array}{*{20}c} 0 & 1 \\ 1 & 0 \\ \end{array} } \right) $$
(4)

As referred in [33], these two equations seem to be the smallest expressions (in terms of the necessary operations) that allow computing the Euler number of a 2-D digital binary image, providing exact values as if (1) was used. To appreciate the validity of (3) and (4), let us consider the four academic examples shown in Fig. 2.

Fig. 2.
figure 2

Examples to numerically validate the functioning of (3) and (4).

Table 1 summarizes the results obtained for these four binary images by means of (3) and (4). In the case of the image (a), all three binary regions, as can be appreciated, are 4-connected, thus the computed results by (3) and (4) are the same. In the case of image (b), some of the pixels are only 8-connected and some others are 4-connected, the reader can see that the obtained results are different by the application of both equations is different. In the case of image (c), when (3) is applied, all pixels are considered as disconnected, that is why an “8” is obtained, however when (4) is used a “0” is obtained due to for this equation the eight pixels as considered as a connected object with a hole. Finally, in the case of image (d), if 4-connectivity is considered, we see that we have seven connected regions; that is why by means of (3) we obtain a “7”. On the other side, if 8-connectivity is considered, as can be appreciated from Fig. 2(d), all the pixels are taken as connected forming a spiral, thus the value for \( e \) in terms of (4) is “1”, as expected.

Table 1. Application of (3) and (4) to the four example of Fig. 2.

Suppose now we want to design two specialized MLP architectures that allow computing the Euler number of a 2-D binary image, one based on (3) and the other based on (4). To accomplish this goal, let us represent the four numbers of each of each of the three terms of (3) and (4) by the four variables: \( v_{1} ,v_{2} ,v_{3} \) and \( v_{4} \). It is not difficult to see that the three arrangements used by (3) and (4) are three of the sixteen possibilities depicted in Table 2.

Table 2. Values for (3) and (4).

From (3) we can also see that the first and third resulting values are both positive (rows 9 and 10), while the second term is negative (row 15). The remaining combinations, according to Table 2, sixth column, are zero. However, from (4) we can appreciate that the first term is positive as shown in row 9, while the second and third terms are both negative as depicted in rows 7 and 15, respectively. Column 6 summarizes the results for (3), while column 7 resumes the results for (4).

In both cases, we propose to use these 16 values to train a MLP that allows producing as output one of the three values: \( \left\{ { - 1,0,1} \right\} \), that in turn allow us determining the Euler number of a 2-D binary image in both cases of 4 and 8 connectivity. Figure 3 depicts a sketch of the specialized MLP architecture. It has four input neurons, nine hidden neurons and one output neuron. All activation functions of hidden neurons were chosen as sigmoidal, however the activation function for the output neuron is a hyperbolic tangent to approach at the end of training the three desired values: \( \left\{ { - 1,0,1} \right\} \). For adjusting the connections weights among neurons, we have used standard Backpropagation rule with a learning rate of \( \alpha = 0.1 \). All the synaptic weights were initialized to random values between 0.0 and 1.0. At 5000 iterations good output values were obtained, but because these three values never correspond to the values of \( - 1,0,1 \), we took output \( y \) of the MLP and if \( y > 0.5 \) then \( r = 1 \), else if \( y \ge - 0.5 \) and \( y \le 0.5 \) then \( r = 0 \) else if \( y \le - 0.5 \) then \( r = - 1 \). Training was attained at 0.7442 s.

Fig. 3.
figure 3

Sketch of the specialized MLP architecture to compute the Euler number of a 2-D binary image.

The reader can easily verify that all 16 values shown in Table 2 are correctly classified into their corresponding three classes; \( - 1,0,1 \), in both cases of 4 and 8 connectivity. This guaranties that at the moment of computing the image Euler number by means of this specialized ANN, we will obtain a correct value as if (1) was used in both cases of 4 and 8-connected images.

To locally compute the Euler number of a 2-D binary image \( I\left( {x,y} \right) \) we proceed sequentially as illustrated in Fig. 4. As can be seen from this figure, the generated MLP is displaced from the upper left position down to the right position inside the image, obtaining each time one of the three values: 1, 0 or 1. At the end of the application of this very simple procedure, we should obtain the Euler number of any 2-D binary image \( I\left( {x,y} \right) \) as follows:

$$ e = \mathop \sum \limits_{k = 1}^{T} r\left( k \right) $$
(5)
Fig. 4.
figure 4

Sequential way to apply (5) over an image.

In this case, \( T \) is the number of times the trained MLP is applied to image \( I\left( {x,y} \right) \); \( r\left( k \right) = - 1,0,1 \) is the local result output by the MLP applied to a sector of \( I\left( {x,y} \right) \).

3 Experimental Results

To numerically validate the correct functioning of the two derived ANNs, we first took 100 binary images of \( 256 \times 256 \) pixels. In the case of the first the 50 images, objects are 4-connected; in the remaining 50 images objects are 8-connected. Due to space limitations, results for only 16 of these images are shown in Fig. 5. Table 3 depicts the values of \( e \) for these 16 images by the application of (5) as illustrated in Fig. 4. Only the results with the MLP for the 4-connected cases are shown in this table. As expected, in all cases, the correct Euler number for the 16 images was correctly calculated. The reader can demonstrate that for any other 2-D binary image, the desired \( e \) should be correctly computed.

Fig. 5.
figure 5

16 of the binary images used to validate the functioning of the derived MLPs.

Table 3. Values of e for the 16 images shown in Fig. 5.

All the experiments were run on a desktop computer with an Intel (R) Core(TM) i7 950 CPU 3.07 GHZ × 8 cores and 18 GB of RAM; operating system: Windows 7 Ultimate ×64.

The average time to process an image of \( 256 \times 256 \) pixels was of 0.7808 (4-connected case) and 0.7352 (8-connected case) seconds, respectively if the MLP is sequentially applied as depicted in Fig. 4. If (3) or (4) are applied over an image the average time reduces to 0.1557 and 0.1456 s, respectively. As can be seen more time is needed to obtain the Euler number of a binary image if the ANN based method is applied. This is normal due to more processing time is required to attain the same goal. What we want to show in this paper is that it is possible to compute the binary image Euler number by means of a Multi-layered Perceptron.

Compared with other standard formulations to compute the Euler number of a 2-D binary image reported in literature in terms of time, we observe in Table 4 that our proposals are, of course, not the fastest but neither the slowest. Both MLP-based proposals are slower that the methods reported in [5, 14, 22, 17] but faster that the methods reported in [9, 11].

Table 4. Cpmparisson with other proposals.

Compared with other ANN implementations, such the one reported in [34] where a Morphological Neural Network with Dendritic Processing (MNNDP) is trained to accomplish the same task, the corresponding average times over the set of 100 images used in this paper were of 402.0 s (MNNDP based implementation) and 75.8 s (MLP based implementation), respectively.

4 Conclusions and Future Trends

In this paper we have shown that an MLP can be used to correctly determine the Euler number of a 2-D binary image. Through an analysis of the local operations implied in the application of known formulations, (3) and (4), we have derived a specialized architecture.

Although our proposed MLP based implementation is slower that the sequential implementation of (3) and (4), it constitutes an original and interesting alternative for the automatic computation of the 2-D binary image Euler number by means of an MLP.

Although our proposed methodology can be adapted to any ANN model, we have presented results with the MLP in both cases of 4 and 8 connectivity. A parallel work in this same direction with morphological neural networks is reported in [34].

Nowadays, we also are working toward the natural extension of our proposal in the case of 3-D binary images where objects will now be represented by voxels and not by pixels as usual.

We are also working to obtain an efficient implementation of the MLP to be run a GPU platform under CUDA. For this we are first implementing or MLP in matrix form. Because CUDA allows to manage the execution threats in matrix form, each execution threat can be a value inside a matrix, this way it will not be necessary to iterate over an image as illustrated in Fig. 4. Each threat will execute over each image pixel the necessary operations of lecture, its processing until the MLP output. In theory, the execution time will be equivalent to only iteration of the ANN.