© Vladimir Kovalevsky 2019
Vladimir KovalevskyModern Algorithms for Image Processinghttps://doi.org/10.1007/978-1-4842-4237-7_6

6. Edge Detection

Vladimir Kovalevsky1 
(1)
Berlin, Germany
 
Among the known methods of edge detection are simple gradient filters containing some kind of smoothing of the image. The Sobel filter is a common example. It is defined by two matrices with weights as shown in Figure 6-1.
../images/474294_1_En_6_Chapter/474294_1_En_6_Fig1_HTML.png
Figure 6-1

Weights of Sobel filter

The filter calculates two sums SH and SV of products of the gray values in a gliding 3 × 3 neighborhood of the actual pixel with corresponding weights WH and WV shown in Figure 6-1 and returns the sum of the absolute values |SH| + |SV|. Pixels in which this value is greater than a given threshold belong to the edge. The results are generally unsatisfactory because the edges are too thick.

Other known methods of edge detection are the zero crossing of the Laplacian Kimmel (2003) and the Canny (1986) filter. I present in what follows a comparison of these methods with our new rather simple and efficient method.

Laplacian Operator

An efficient method of edge detection is that of zero crossing of the Laplacian. The Laplacian operator is defined in mathematics as the sum of the second partial derivatives:

Lap(F(x, y)) = ∇2F(x, y) = ∂2F/∂x2 + ∂2F/∂y2.

Because a digital image is not differentiable, it is necessary to replace the derivatives by finite differences.

Definition 1: The expression D1(x, Δx, F) = F(x + Δx) - F(x) is called the first difference of F(x) with respect to x and with the increment Δx.

Definition 2: The expression Dn(x, Δx, F) = D1(x, Δx, Dn-1(x, F(x)) with n > 1 is called the nth difference of F(x) with respect to x and with the increment Δx. The second partial differences with respect to x and y are equal to

D2(x, Δx, F) = F(x – Δx, y) - 2·F(x, y) + F(x + Δx, y)

D2(y, Δy, F) = F(x, y – Δy) - 2·F(x, y) + F(x, y + Δy)

and the finite Laplacian with Δx = Δy = 1 is equal to

2F(x, y) = F(x – 1, y) + F(x + 1, y) - 4·F(x, y) + F(x, y – 1) + F(x, y + 1).

It can be calculated in digital images with the filter shown in Figure 6-2.
../images/474294_1_En_6_Chapter/474294_1_En_6_Fig2_HTML.png
Figure 6-2

The filter for calculating the finite Laplacian

An application of this filter to a digital image with the gray values F(x, y) yields the following value:

2F(x, y) = F(x – 1, y) + F(x + 1, y) - 4·F(x, y) + F(x, y – 1) + F(x, y + 1)(6.1)

Equation 6.1 can be rewritten as follows:

2F(x, y) = F(x – 1, y) + F(x + 1, y) + F(x, y – 1) + F(x, y) + F(x, y + 1) - 5·F(x, y)

= 5·M(x, y) - 5·F(x, y) = 5·(M(x, y) - F(x, y))(6.2)

where M(x, y) is the mean value of F(x, y) of the five pixels in the neighborhood of the pixel with coordinates (x, y). It is also possible to calculate the finite Laplacian in the pixel P = (x, y) by subtracting the gray value of P from the local mean value in the neighborhood of P. It is appropriate to use the fast averaging filter (Chapter 2) when using a greater neighborhood.

The Method of Zero Crossing

The method of zero crossing is based on detecting locations where the Laplacian changes its sign. Consider the cross-section through a row of a digital grayscale image shown in Figure 6-3.
../images/474294_1_En_6_Chapter/474294_1_En_6_Fig3_HTML.png
Figure 6-3

Cross-section through a row of a digital image

The Laplacian has positive values on one side of an edge and negative values on its other side. The absolute values of Laplacian near the edge are great as compared with its values on locations far away from the edge. It is more practical using the negative value of Laplacian—that is, the value (F(x, y) – mean value) instead of the value (mean value – F(x, y))—because these values are greater at greater gray values.

Locations at which the Laplacian changes its sign are called zero crossings. Some of these locations correspond to the location of the edge. A zero crossing always lies between two pixels. These indications of the location of an edge are therefore always quite thin.

Are Zero Crossings of Laplacian Closed Curves?

One advantage of the Laplacian put forward in some textbooks on image processing is that the edges produced by Laplacian are always closed lines. However, this is true only if we threshold the values of Laplacian; for example, if we replace the positive values of the Laplacian by 1 and the negative ones by 0. The fact that the boundaries of sets of pixels with the value 1 in such an image are closed curves is, however, the advantage of binary images and not that of Laplacian. Such edges are shown in Figure 6-4b. Most edges in an image of the Laplacian binarized with a threshold equal to 0 are irrelevant.
../images/474294_1_En_6_Chapter/474294_1_En_6_Fig4_HTML.jpg
Figure 6-4

(a) Original image, and (b) Laplacian binarized with threshold 0; edges are shown as white lines

As you can see, the overwhelming majority of the white lines in Figure 6-4b are completely meaningless and cannot be used as edges. These are the so-called irrelevant zero crossings.

Figure 6-5 shows an explanation for the irrelevant zero crossings of the Laplacian. The black lines shows the gray values of the original image. The green line represents the local mean value of the gray values, and the red line is the Laplacian. Red arrows show the irrelevant zero crossings. The values of the Laplacian at these locations are small, whereas at the relevant crossing the absolute value of the Laplacian is great.
../images/474294_1_En_6_Chapter/474294_1_En_6_Fig5_HTML.png
Figure 6-5

Irrelevant zero crossings of the Laplacian (red arrows)

We explain in the next section how to eliminate the irrelevant zero crossings.

How to Eliminate Irrelevant Crossings

Irrelevant crossings can be distinguished from the relevant ones by means of two thresholds: Only transitions from a Laplacian value greater than a positive threshold to a Laplacian value less than a negative threshold should be recognized as relevant edges. Most irrelevant edges disappear, but some relevant ones are interrupted due to the noise in the image. The image should be filtered to reduce the noise, for example, with the simplest averaging filter (Chapter 2). The image should be filtered two times: with a small and with a greater gliding window. The differences between these two filtered images can then be regarded as a good approximation of the Laplacian.

Consider Figure 6-6b, which shows as a blue line the gray values in one row of the image. The Laplacian values are shown as a green line. The black line is the x axis; red lines show the thresholds used to distinguish between relevant and irrelevant zero crossings. A zero crossing is relevant if it lies between two such pixels that one of them has a Laplacian value greater than the positive threshold and the other pixel has a Laplacian value less than the negative threshold.
../images/474294_1_En_6_Chapter/474294_1_En_6_Fig6_HTML.jpg
Figure 6-6

(a) Edges found with two thresholds (white lines), and (b) explanation

Figure 6-7 explains how the irrelevant zero crossings can be eliminated.
../images/474294_1_En_6_Chapter/474294_1_En_6_Fig7_HTML.png
Figure 6-7

An example of the values of the Laplacian

Noise Reduction Before Using the Laplacian

When using the averaging filter for noise reduction, the edges become blurred. As a result, the difference between the positive and negative values of Laplacian becomes small; Laplacian values change gradually. This makes it difficult to distinguish between relevant and irrelevant zero crossings. We suggest using the sigma filter (Chapter 2) instead of averaging with a small window. Then the edges remain precipitous; the absolute amounts of the Laplacian become larger.

Black lines in Figure 6-8a show the gray values filtered with averaging with a small window. In Figure 6-8b, the black line represent the gray values filtered with the sigma filter. For both, green lines represent the local mean values with the greater window and red lines show the Laplacian values.
../images/474294_1_En_6_Chapter/474294_1_En_6_Fig8_HTML.png
Figure 6-8

The gray and Laplacian values after filtering with (a) averaging, and (b) sigma filter

Blur During the Digitization and Extreme Value Filter

Even an ideal edge can become somewhat blurred during the digitization of an image. The reason for this is that the boundary between a light and a dark area in an image projected to the set of light-sensitive elements (e.g., CCD, charge-coupled devices, electronic light sensors used in various devices including digital cameras) can fall occasionally near the center of an element, as shown in Figure 6-9.
../images/474294_1_En_6_Chapter/474294_1_En_6_Fig9_HTML.jpg
Figure 6-9

The CCD matrix illuminated by a dark and a light area

This element obtains a light amount, which we call the middle value, lying between the values that are proper for the light and dark areas. The corresponding pixel is located in the middle of the edge and the Laplacian obtains a small value or even a zero value. A gap in the detected edge occurs, as illustrated in Figure 6-10.
../images/474294_1_En_6_Chapter/474294_1_En_6_Fig10_HTML.png
Figure 6-10

Gray values (black line), local mean (green line), and Laplacian (red line)

We suggest using the extreme value filter to avoid Laplacian gaps due to middle values. This filter applied to a grayscale image calculates the maximum and the minimum gray value in a small gliding window. It also calculates the differences between the gray value in the central pixel and the maximum and minimum values and decides which of these two values is closer to the central value. The closer value is assigned to the central pixel in the output image. The edges become sharp and the Laplacian gaps disappear. Here is the source code of the extreme filter for grayscale images.
int CImage::ExtremVar(CImage &Inp, int hWind)
{ N_Bits=8; width=Inp.width; height=Inp.height;
  Grid=new unsigned char[width*height];
  int hist[256];
  for (int y=0; y<height; y++) // ================================
  { int gv, y1, yStart=__max(y-hWind,0), yEnd=__min(y+hWind,height-1);
     for (int x=0; x<width; x++) //============================
    { if (x==0) //-------------------------------------------------------
      { for (gv=0; gv<256; gv++) hist[gv]=0;
         for (y1=yStart; y1<=yEnd; y1++)
         for (int xx=0; xx<=hWind; xx++)
                                  hist[Inp.Grid[xx+y1*width]]++;
      }
      else
      { int x1=x+hWind, x2=x-hWind-1;
         if (x1<width)for (y1=yStart; y1<=yEnd; y1++)
             hist[Inp.Grid[x1+y1*width]]++;
         if (x2>=0)
          for (y1=yStart; y1<=yEnd; y1++)
          { hist[Inp.Grid[x2+y1*width]]--;
             if (deb && hist[Inp.Grid[x2+y1*width]]<0) return -1;
          }
      } //---------------- end if (x==0) ---------------------------------
      int gMin=0, gMax=255;
      for (gv=gMin; gv<=gMax; gv++)
        if (hist[gv]>0) { gMin=gv; break; }
      for (gv=gMax; gv>=0; gv--)
        if (hist[gv]>0) { gMax=gv; break; }
      if (Inp.Grid[x+width*y]-gMin<gMax-Inp.Grid[x+width*y])
         Grid[x+width*y]=gMin;
      else Grid[x+width*y]=gMax;
    } //=============== end for (int x... ================
  } //================ end for (int y... =================
  return 1;
} //******************* end ExtremVar *********************
Here is the source code of the universal extreme method for color and grayscale images.
public int ExtremLightUni(CImage Inp, int hWind,Form1 fm1)
/* The extreme filter for color or grayscale images with variable hWind. The filter finds in the (2*hWind+1)-neighborhood of the actual pixel (x,y) the color "Color1" with minimum and the color "Color2" with the maximum lightness. "Color1" is assigned to the output pixel if its lightness is closer to the lightness of the central pixel than the lightness of "Color2". --*/
{
 byte[] CenterColor = new byte[3], Color = new byte[3], Color1 =
  new byte[3], Color2 = new byte[3];
  int c, k, nbyte = 3, x;
  if (Inp.N_Bits == 8) nbyte = 1;
  for (int y = 0; y < height; y++) // ====================================
  {
    if (y % jump == jump - 1) fm1.progressBar1.PerformStep();
    for (x = 0; x < width; x++) //======================================
    {
      for (c = 0; c < nbyte; c++) Color2[c] = Color1[c] = Color[c] =
                                 CenterColor[c] = Inp.Grid[c + nbyte * (x + y * width)];
      int MinLight = 1000, MaxLight = 0;
      for (k = -hWind; k <= hWind; k++) //==============================
      {
        if (y + k >= 0 && y + k < height)
          for (int i = -hWind; i <= hWind; i++) //==========================
          {
            if (x + i >= 0 && x + i < width) // && (i > 0 || k > 0))
            {
              for (c = 0; c < nbyte; c++)
                           Color[c] = Inp.Grid[c + nbyte * (x + i + (y + k) * width)];
              int light;
              if (nbyte == 3) light= MaxC(Color[2], Color[1], Color[0]);
              else light = Color[0];
              if (light < MinLight)
              {
                MinLight = light;
                for (c = 0; c < nbyte; c++) Color1[c] = Color[c];
              }
              if (light > MaxLight)
              {
                MaxLight = light;
                for (c = 0; c < nbyte; c++) Color2[c] = Color[c];
              }
            }
          } //=============== end for (int i... =======================
      } //=================== end for (int k... ======================
      int CenterLight = MaxC(CenterColor[2], CenterColor[1], CenterColor[0]);
      int dist1 = 0, dist2 = 0;
      dist1 = CenterLight - MinLight;
      dist2 = MaxLight - CenterLight;
      if (dist1 < dist2)
        for (c = 0; c < nbyte; c++)  Grid[c + 3 * x + y * width * 3] = Color1[c]; // Min
      else
        for (c = 0; c < nbyte; c++)  Grid[c + 3 * x + y * width * 3] = Color2[c]; // Max
    } //================== end for (int x... ==========================
  } //==================== end for (int y... ==========================
  //fm1.progressBar1.Visible = false;
  return 1;
} //********************** end ExtremLightUni ***************************
Consider an example of edges detected by the Laplacian zero crossing method (threshold = 10) after using the sigma filter and the extreme value filter. Figure 6-11 shows the gray values of the original image (Figure 6-11a) and the images after successive steps of processing the image with the filters: the image processed with the sigma filter (Figure 6-11b), with the extreme value filter (Figure 6-11c), and the Laplacian with edges (Figure 6-11d). Positive Laplacian values are shown in red, negative values in blue, and zero crossings as white lines.
../images/474294_1_En_6_Chapter/474294_1_En_6_Fig11_HTML.jpg
Figure 6-11

Example of edge detection with Laplacian zero crossing: (a) original image, (b) sigma filtered, (c) extreme filtered, and (d) Laplacian with edges (white lines)

As you can see, the edge detection is rather successful. However, there are some gaps in the edges as explained in the next section.

Fundamental Errors of the Method of Zero Crossing in the Laplacian

Laplacian has a fundamental property that the sequences of its zero crossings must have gaps in the neighborhood of points where three or four sequences meet. Figure 6-12 provides an example. Figure 6-12 shows an artificial image (a), the edges detected by the method of zero crossing (b), the desired ideal edges (c), and the values of the sign of the Laplacian (d). The red pixels are the pixels with positive values of the Laplacian and the blue pixels are those with negative values. Zero crossings are shown in Figure 6-12d as white lines.
../images/474294_1_En_6_Chapter/474294_1_En_6_Fig12_HTML.png
Figure 6-12

(a) Artificial image with noise, σ = 5; (b) zero crossings over the image after sigma and extreme filtering; (c) ideal edges; and (d) zero crossings over the Laplacian (red are positive values)

Notice the missing pieces of the edges occurring due to fundamental errors in Figure 6-12 marked by green arrows. These are considered fundamental errors because it is impossible to eliminate them. These errors occur because the Laplacian is the difference between the local mean and the actual gray value. A zero crossing lies between a pixel with a positive value and one with a negative value of the Laplacian. One of these pixels has a gray value greater than the local mean and the other one a gray value less than the local mean. If, however, there are more than two different gray values in the gliding window, then the local mean can lie only between two of gray values. There can be no zero crossing for the third gray value. The edge therefore has a gap. Due to these gaps, the edges produced by means of Laplacian zero crossings contain no crotches.

These errors are very important because edges are often used to produce polygons representing boundaries of areas with a constant color. Missing crotches make these polygons incomplete, as they do not describe the boundaries of regions correctly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.226.105