An Edge Detection Technique for Grayscale Images Based on Fuzzy Logic

Edge detection is a preliminary process in many image processing and computer vision applications such as object detection and object extraction. It detects important events in the image where sharp discontinuity in pixels intensity is found. Several edge detection techniques have been proposed including Sobel, Canny, Prewitt


INTRODUCTION
Edge detection refers to the process of extracting the sharp discontinuity in pixels intensity in a certain image. The importance of edge detection is that it is the first step in various image operations such as boundary detection, object recognition, and classification. Several edge detection techniques have been proposed like Sobel, Roberts, Canny, Prewitt, and Laplacian of Gaussian which are based on the calculation of the gradient [1].
Using the gradient, edge detection is performed by looking for the maximum and minimum in the first derivative of the image, whereas Laplacian method searches for zero crossing in the second derivative of the image [1]. Since the decision whether a pixel should be considered as an edge or not is uncertain, some techniques that use fuzzy logic to detect edges have been proposed [2,3].
The fuzzy logic set, first proposed by Zadeh in 1965, is defined as a set of elements with a degree of membership between 0 and 1. The mathematical function that describes the membership of elements in a fuzzy set is called a membership function [1]. The advantage of fuzzy logic is that it describes the problem in terms of linguistic variables which make it a powerful tool for managing the vagueness and uncertainty efficiently [4,5]. As a result, fuzzy logic theory has been successfully applied to many areas such as computer vision and image processing.
Fuzzy image processing is the collection of approaches that understand the images and their features, and process them as fuzzy sets. Fuzzy image processing typically consists of several steps; namely, image fuzzification, processing and modification of membership values, and if needed defuzzification [6]. The fuzzification step is necessary to process the image in the fuzzy domain as it transfers the image from gray level plane to the membership functions plane. All processing activities are performed in the image processing and membership values modification step. Finally, the output is transferred back to the gray level plane to be operated on by the defuzzification step [7,8].
In this paper, a new edge detection technique is proposed, where a 3χ3 mask is utilized and according to the relationship between mask pixels, a two-phase fuzzy system is applied to determine whether the pixel is an edge or not. In the first phase each pixel is tested to detect edges according to four directions; horizontal, vertical, diagonal, and inverse diagonal. The second phase aims to combine the results from the first phase in order to provide the final decision on a certain pixel. The details of each phase will be discussed later on. The proposed algorithm is implemented using MATLAB and the performance of the new technique is compared with those of Sobel, and Canny.
The rest of the paper is organized as the following, Section 2 reviews related works. Then, section 3 presents an overview on fuzzy logic. Section 4 discusses the proposed technique. Section 5 exhibits experimental evaluation of the proposed technique. Finally, section 6 concludes the paper.

RELATED RESEARCH
Many techniques have been proposed for image edge detection. Sobel method [9] uses two kernels, shown in Fig. 1, to obtain edge intensities in the vertical and horizontal directions. The kernels can be applied separately to the input image to produce separate measurements of the gradient components in each direction then they can be combined to find the magnitude of the gradient at each pixel. Eq.
(1) shows how to calculate the magnitude of the gradient.
Where G x and G y are the gradient in directions x, y respectively. If the pixel intensity exceeds a specific threshold, the pixel will be regarded as an edge point.

Fig. 1. Sobel's vertical and horizontal masks
Canny method [10] finds the edge strength by taking the gradient of the image using the same kernels used by Sobel operator to find the gradient in both directions, i.e. x and y, and then the magnitude is calculated. Next, it computes the edge direction according to the gradient in the x and y directions using Eq. (2) Once edge direction is known, the next step is to resolve the orientation of the edge into one of the four directions shown in Fig. 2, based on which direction is closest toߠ. Finally, a Non-Maxima Suppression (NMS) method is used to extract the edge point with the largest edge intensity along its direction which gives a thinner edge line in the output image.

Fig. 2. Edge directions
Chung-chia kang and Wen-June Wang [9] proposed an edge detection technique for both gray and color images. They use a 3χ3 mask divided into two sets S 0 and S 1 , which are used to compute objective functions corresponding to four directions as per Fig. 3. The objective function that corresponds to edge direction j is defined by Eq. (3.a), Eq. (3.b) and Eq. (3.c).
Where L is the gray level of the digital image, ‫ݓ‬ ଵ = 90, ‫ݓ‬ ଶ = 40, ݉ and ݉ ଵ are the average of pixels' intensities in S 0 and S 1 respectively. ‫‬ , ‫‬ are two pixels' intensities in the same set such that m ≠ n and m > n. For each direction j, the objective function f j is calculated and then edge intensity is calculated by Eq. (4). Finally, a non-maxima suppression method is applied for pixels with intensity ≥ specific threshold to extract edge points along the direction, which is used to obtain a thinner edge along the direction.

Fig. 3. Directions' sets
Another Edge detection technique is proposed by Jiang et al. in which a mathematical model is used to extract thin edges in low-contrast images [11]. They developed a quad-decomposition edge enhancement process, a thresholding process, and a mask-based noise filtering process to enhance thin edge features, extract edge points and filter out some meaningless noise points.
In [2] the authors proposed a fuzzy edge detection approach in which each pixel is compared with neighbors' intensities by applying a 3x3 mask on the image and then all mask pixels' intensities are entered to the fuzzy system. Each intensity value will be fuzzified according to two membership functions; black and white. Afterward, and according to inference rules the center pixel will be considered as black, white, or edge pixel. Another fuzzy approach proposed in [3], which uses a 3x3 mask over the image and for each mask, the standard deviation and the gradient values are calculated. Then, these two values are entered in to the fuzzy system, where both standard deviation and gradient values can be low, medium, or high. According to these values the center pixel can have low, medium, or high edge intensity.
Liang et al. [4] proposed an algorithm which operates on gray images via three passes on image pixels. In the first pass, a fuzzy classifier is used to classify the pixels according to gray level variation in various directions to 6 classes; edge in direction-1, edge in direction-2, edge in direction-3, edge in direction-4, background, and speckle. The second pass applies a competitive process to compare the edge pixels with its neighbors to obtain a thinner edge. Finally, the last pass is to de-speckle the speckle pixels.
Anver et al. [5] proposed a fuzzy system that decides on image edges based on different mask sizes. They use 3x3, 5x5, and 7x7 masks and for each mask they calculate the line-edginess and step-edginess for each pixel in the image by entering the pixel and its neighbors' intensities to a fuzzy system. According to the differences in the intensities between the center pixel and its neighbors, the step-edginess and the lineedginess strengths are computed. At the end of this step, each pixel will have six values; stepedginess 3x3 , step-edginess 5x5 , step-edginess 7x7 , line-edginess 3x3 , line-edginess 5x5 , and lineedginess 7x7 . Then, the two values for each mask are compared and the larger wins. For example, for the 3x3 mask compare the step-edginess 3x3 and the line-edginess 3x3 for each pixel in the image and the winner is called edginess 3x3 . After this step, each pixel in the image will have three values; edginess 3x3 , edginess 5x5 , and edginess 7x7 .
The three values are entered in to a fuzzy system to find the final edginess strength.
Another edge detection technique is proposed by Dachasilaruk [12]. This technique is used for speckle reduction in SAR images by using the standard deviation of the squared amplitude method. A filtering method is used to remove the large wavelet coefficients generated by edge region and the speckle noise in small wavelet coefficient or non-edge region. The despeckled image is obtained by reconstruction from the filtered coefficients.
Although fuzzy systems have been used in some previous techniques such as in [2][3][4][5], the proposed technique considers the relation between pixels' intensities in four different directions within the same mask, not only mask neighbors' intensities (e.g. 8 neighbors' intensities in the 3x3 mask) as in [2,3,5]. In [4] although the edges are considered in four directions, two additional steps are needed to obtain thinner edges and remove speckle pixels which are not needed in the proposed technique.

FUZZY LOGIC OVERVIEW
Fuzzy logic was introduced by Lotfi Zadeh in 1965 [13]. The power of fuzzy logic is that it presents set membership as a value between 0 and 1 rather than traditional crisp value which can be 0 or 1. Fuzzy logic can be used in many systems to reflect the uncertainty by define the degree to which an element belongs to a particular set [1,14]. The difference between fuzzy value and crisp value is illustrated in Fig. 4.

Fig. 4. Difference between crisp and fuzzy sets
Fuzzy inference systems have three main components; inputs, output(s), and rules. Two main fuzzy inference styles are known; Mamdani and Sugeno. In this section we will discuss Mamdani-style inference since it has been used for the proposed fuzzy system.
Each input and output is described as linguistic variable such as Pressure. The values of input or output variable are represented by linguistic terms. Each linguistic term is represented by one membership function [14]; in our example the Pressure can be Low or High. Both Low and High are linguistic terms which can be presented by a membership functions such as Trapezoidal, Triangular, or Gaussian. Fuzzy rule is an If-then rule that consists of two parts; antecedent(s) and consequent(s). The general format of fuzzy rule is:

If Input-variable is Value n Then Output-Variable is Value m
For example: If Pressure is High Then Volume is Small. After defining the inputs, and outputs along with the membership functions and the needed fuzzy rules, the fuzzy system is ready to be used. The evaluation process can be performed in four steps illustrated in Fig. 5.
Firstly, the fuzzification process in which the crisp input value is mapped into a fuzzy value using the corresponding membership functions. Secondly, in rules evaluation process the fuzzified inputs are applied to the antecedents of the fuzzy rules. If a given fuzzy rule has multiple antecedents, the fuzzy operator AND or OR (correspond to minimum and maximum respectively) is used to obtain a single number that represents the result of the antecedent evaluation. This number, called antecedent truth, is then applied to the consequent membership function by clipping the consequent membership function at the level of the antecedent truth. Next is the rules aggregation process which is the process of unification of the outputs of all rules by taking the membership functions of all rule consequents previously clipped and combine them into a single fuzzy set. Finally, the output value is mapped from fuzzy value to crisp value in the defuzzification step [15].

PROPOSED TECHNIQUE
In this paper a new edge detection technique for grayscale images is proposed. Our technique consists mainly of two steps; the first step is the pre-processing step in which the parameters that are needed for the fuzzy system, in the next step, will be calculated. These parameters represent the distances in pixels' intensity in the 3x3 mask.
In the second step, the calculated distances will be evaluated by two-stage fuzzy system to decide whether the mask center pixel is an edge pixel or not. Fig. 6. illustrates the general structure of the proposed fuzzy system along with the needed parameters for each stage. In the next subsections the proposed technique will be discussed in more details.

Step 1: Intensity Distances Calculations
In this step all fuzzy parameters are calculated. For each pixel in the gray image, a 3x3 mask is applied and for each mask the needed parameters are calculated depending on the pixels' intensity variation in the mask according to four directions; vertical, horizontal, diagonal, and inverse diagonal, as shown in Fig. 2. For each direction two sets are defined; S 0 and S 1 which are shown in Fig. 3. Each set consist of a number of pixels which varies according to a specific direction, for example, Direction-1 has S 0 = {p1, p2, p4, p5, p7, p8}, and S 1 = {p3, p6, p9}.
Two different intensity distances are defined to recognize the relation between the sets' pixels. The first one is the Set Intensity Distance (SID) which characterizes the inter-set intensity distance between S 0 and S 1 . It is calculated using Eq. (5.a), Eq. (5.b), and Eq. (5.c).
(5.c) തതത and തതത are the intensity average for pixels in S 0 and S 1 respectively. Pi S0 is the i th pixel's intensity in S 0 set, and Pi S1 is the i th pixel's intensity in S 1 set. As the value of SID increases, the possibility to have an edge in this direction increases too since high value means high intensity deference between the two sets. The second distance type is the Pixels Intensity Distance (PID) which describes the pixels' intensity variation between the pixels within the same set (Intra-set intensities variance). Two PID are defined; PID 0 and PID 1 corresponding to the sets S 0 and S 1 respectively. To find PID 0 and PID 1 we calculate the Standard Deviation (SD) for pixels' intensities within each set as per Eq. (6.a) and Eq. (6.b) respectively. Lower standard deviation implies smaller intensities variance and thereby lesser possibility to have an edge.

Step 2: Intensity Distances Evaluation
After finding intensity distances, we use a fuzzy system to evaluate these distances. As mentioned before, the proposed fuzzy system is applied to decide on edge pixels using two stages. Firstly, decide if the center pixel, in the mask, is an edge according to each direction. Secondly, combine the effect of all directions and decide whether to consider the center pixel as an edge or not.

First stage fuzzy system
For each direction, the first fuzzy stage is applied to find the edge intensity which reflects the possibility to have an edge in that direction. It is a three inputs one output fuzzy inference system. It takes SID, PID 0 , and PID 1 as inputs and returns the edge intensity as an output. This intensity reflects the possibility to have an edge in that direction. The SID variable has three values; Small, Medium, and Large. Each value is represented by a membership function as shown in Fig. 7(a). Both PID 0 and PID 1 have also three membership functions; Small, Medium, and Large. These functions are illustrated in Fig. 7(b).
Each input parameter will be fuzzified according to its corresponding membership functions. The trapezoidal and triangular functions are used to represent the membership functions. These functions are represented in Eq. (7) and Eq. (8), respectively. After fuzzification process all values will be evaluated according to the Mamdani fuzzy rules of this stage which are listed in Table 1. Finally, the output is defuzzified according to the rules evaluation result, and the value is represented by Low, Medium, and High membership functions as in Fig. 7(c). Note that the range values for the membership functions are tuned according to the conducted experiments.

Second stage fuzzy system
After finding the edge intensities for all directions, these values are entered to the second stage which is a four to one fuzzy inference. As first stage the second stage uses Mamdani fuzzy inference to evaluate its inputs with rules listed in Table 2Table . The inputs are the four outputs from the first phase (one output for each direction) and the output is the final edge intensity according to the four directions results. Both input and output are represented as Low, Medium, and High membership functions shown in Fig. 8.

Complete Example
The following example shows how the system works for the mask shown in Fig. 9. We calculate the SID, PID 0 and PID 1 for each direction.

EXPERIMENTAL RESULTS
In this section, we present the results of the proposed technique and compare them with those obtained from Sobel, and Canny. Moreover, the results are evaluated using Mean Square Error (MSE), and Peak Signal to Noise Ratio (PSNR).

Experimental Results
We tested the proposed technique on various images which can be categorized as images that contain many details (Fig. 12 to Fig. 20) and text images (Fig. 21) As illustrated in the figures the proposed technique generally lays between Sobel and Canny operators in that it provides more detailed edges than Sobel but not as many details as Canny. When comparing between our results and that of Canny we notice that the provision of more detailed edges by Canny is not always appropriate such as the case with Butterfly, Linda, and the Rose images. This is due to giving more detailed edges could scramble the general shape of the original image. Considering the results one can notice that our technique gives results that are better than Sobel's ones. Moreover the proposed technique provides better results than Canny's results for Baboon and Matches images. In Fig. 21 that represents text images, clearly, the three techniques give comparable results.

Results Evaluation
In order to quantitively evaluate the obtained resuslts, we compute the MSE and the PSNR for the tested images and compare the results to those obtained by Canny and Sobel.
The MSE is calculated using the following equation [16]: Where I 1 is the original image and I 2 is the edge detection result. Usually, lesser MSE values means better results. But, in the case of edge detection, higher values for MSE means more edges points [16].
For PSNR calculation Eq.(10) is used. Usally, higher values for PSNR means better quality for the results. However, for edge detection lesser values are required to achieve proper result [16].
Where R is the highest intensity in the image which is 255 for grayscale images. Table 3 and Table 4 show the ressults of MSE and PSNR for the tested images, respectively. As can be noteced, the proposed fuzzy method has the higest MSE and the lowest PSNR values among other methods. Concequently, better results have been achieved. Average MSE and average PSNR are illustrated in Fig. 10 and Fig. 11, respectively.

CONCLUSION
Fuzzy logic is a powerful tool that can be used to manage ambiguity and uncertainty. Since the decision whether to consider a pixel as an edge or not is based on uncertainty, fuzzy logic can be used to detect image edges. In this paper a new fuzzy approach is proposed in which two fuzzy phases are used to detect edges. Images pixels are evaluated according to four directions; horizontal, vertical, diagonal, and inverse diagonal. A 3x3 mask is applied on the image and for each mask we define two types of intensity distances. According to these distances the fuzzy inference will locate edges according to the four directions. Experimental results show the merits of the proposed approach compared with Sobel and Canny especially as it tends to show more details than Sobel and less edges, which could be noisy, than Canny. Moreover, the proposed method has higher MSE and lower PSNR values compared with Canny and Sobel which means better results have been achieved.