Exploring Methods To Improve Edge Detection With Canny Algorithm
Exploring Methods To Improve Edge Detection With Canny Algorithm
Canny Algorithm
Abstract
The objective of this report is to analyze and design algorithms that when
used in conjunction to the Canny Edge Detector, gives better edge detection
performance and minimizes influence of external noise which leads to detection of false edges. All the algorithms have been designed using OpenCV
libraries.
Contents
1 Introduction
1.1 Edge Detector Types . . . . . . . . . . . . . . . . . . . . .
1.1.1 Gradient Based Edge Detection . . . . . . . . . . .
1.1.2 Second Ordered Derivative Based Edge Detection .
1.1.3 The Canny Edge Detector . . . . . . . . . . . . . .
1.2 Problems with Standard Edge Detection Method . . . . .
.
.
.
.
.
.
.
.
.
.
2 Literature Survey
2
2
3
5
6
7
8
3 Methods Explored
3.1 Recursive Method for Edge Detection . . . . .
3.1.1 Basics of Recursion . . . . . . . . . . . .
3.1.2 Using Recursion with Canny Algorithm
3.2 Contour Based Approach for Edge Detection .
3.2.1 Basics of Contours . . . . . . . . . . . .
3.3 Results . . . . . . . . . . . . . . . . . . . . . . .
4 Future Work
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
10
11
11
12
13
13
16
17
Chapter 1
Introduction
Edge detection is a process to find the set of pixels that represent the boundary of an disjoint regions in an image.Edges characterize the boundary of
objects in an image. Edge detection is important for image segmentation,
since many image processing algorithms require first to identify the objects
and then process them. Edge detection allows users to observe features of
an image showing an abrupt change in gray level or texture indicating a
boundary between two different regions in an image. It finds a lot of practical applications in medical imaging, face detection, fingerprint verification,
traffic control system and many more. The innumerable applications of edge
detection has made it one of the most researched topics in image processing.
Several edge detection algorithms have been developed for extracting edges
from an image. The purpose of edge detection is to localize the variations
in an image and must be efficient and reliable to reduce the error in the
subsequent processing stages of the application.
However edge detection is not without its challenges. The image on with
the detection is carried out has noise, due to random variation of brightness
or color information in images due to electronic noise. There are several
forms of noise like Gaussian noise, salt pepper noise and shot noise to name
a few. With noise the edge detection algorithm is more likely to detect false
edges which degrades the quality of edges detected. A good edge detector
increases the probability of of marking an actual edge pixel while decreasing
the probability of marking a false edge.
1.1
Many Edge detection methods have been developed for extracting edges
from a digital image. The can be broadly classified into two categories:
Gradient based and Laplacian based. The gradient based approach detects
edges by looking for the maxima or minima of the first derivate of the image.
The Laplacian method on the other hand tries to detect edges by searching
for the zero crossing of the second derivate of the image.
1.1.1
Consider an image as a function of two variables i and j such the I(i,j) returns
the intensity value of the image at point (i,j). The point (i,j) is the pixel
of the image. Depending on the image a I(i,j) can be a single value (Gray
scale images) or be multivalued(like RGB and other multichannel images).
Hence for the function I(i,j), the derivative of the function can be written as:
I = i
I(i, j) I(i, j)
+j
i
j
Where :
I(i,j)
i
I(i,j)
j
However the image is not a continuous function, rather a discrete one. Hence
we must define the derivative of a discrete function. There are several classical operators that do so which we will describe below.
Roberts Operator
This gradient based operator is computed by convolution of the image using
two 2 x 2 matrices. The two kernels are given as:
1 0
0 1
Dx =
Dy =
0 1
1 0
3
Sobel Operator
Sobel operator is used to compute an approximation of the gradient of the
image intensity function of the edge detection. At each pixel of an image the
Sobel operator gives either the corresponding gradient vector or the normal
vector. It uses the following 3 x 3 kernels:
1 0 +1
1 0 +1
Dx = 2 0 +2
Dy = 2 0 +2
1 0 +1
1 0 +1
Prewitt Operator
The Prewitt operator is similar to a sobel operator but has a different kernel
and gives better performance the Sobel. The kernel used is given below:
1 0 +1
+1 +1 +1
0
0
Dx = 1 0 +1
Dy = 0
1 0 +1
1 1 1
Algorithm for Detecting Edges using Gradient Operators
The algorithm for identifying if a pixel is an edge point or not is done using
the simple algorithm given below:
1. Preprocess the image using filters to reduce the effect of noise
2. Convolve the image in the horizontal direction using the x-Mask of the
gradient operator (Sobel, Roberts, Prewitt etc)
3. Convolve the image in the vertical direction using the y-Mask of the
gradient operator
4. Determine the magnitude of the gradient for each pixel and compare
it to a threshold
(a) If the magnitude is greater than the threshold, mark the pixel as
an edge image
(b) Else the pixel is not an edge image
5. Repeat step 4 for all pixels in the image
Note however that the algorithm does not make use of the gradient direction
operator.
1.1.2
In this method of Gradient Detection we compute the second order derivative of the function I(i,j) and we mark a pixel as an edge point if the second
order derivative at that point is zero. The generalized formulae for this approach is given below:
2 I =
2 I(i, j) 2 I(i, j)
+
2i
2j
2 I(i, j)
= I(i + 1, j) 2I(i, j) + I(i 1, j)
2j
The Laplacian can be implemented
0
1
0
1 0
4 1
1 0
The good thing about the Marr Hildrith Operator is that it is an isotropic
in nature and utilized only one mask, hence cheaper to implement. Also
since it differentiates twice it is more sensitive to noise. Since a Gaussian
Filter is applied to the image, we can combine the two operations to a single
Laplacian of Gaussian matrix (LoG). A 5x5 example of the matrix is given
below:
0
0 1 0
0
0 1 2 1 0
1 2 16 2 1
0 1 2 1 0
0
0 1 0
0
1.1.3
The Canny edge detector is one of the most sophisticated and popular edge
detectors available. It is a gradient based edge detector, though it offers
much more sensitivity to noise. The algorithm for the canny algorithm is
given below:
1. Apply noise reduction using Gaussian Filter on the image. This will
make the image smooth and hence offer better edge detection
2. The next step is finding the gradients of the image. This is done by
finding the first order derivatives of the Image using operators like
Sobel operator. This can be achieved by convolving the kernel over
the image pixel by pixel.
3. The algorithm then carries out non maximal suppression. This is done
as follows: For every pixel (i,j)
(a) Round the gradient of (i,j) to the nearest multiple of 450 . Now
compare the pixel to the neighboring pixels in the direction of
the gradient.
(b) If the pixel is the strongest(highest intensity) in the direction of
the gradient, then keep the gradient else remove it.
4. The final step is hysteresis thresholding. For a pixel (i,j) having gradient G:
(a) IF G < tlow then discard the edge point
(b) IF G > thigh then keep the edge
(c) Otherwise IF tlow < G < thigh and any of its neighbors in the 3x3
region around it have gradient greater than thigh , keep the edge
(d) IF such a neighboring pixel cannot be found in the 3x3 region,
search the 5x5 region. If a pixel is found with G > thigh , then
keep the edge
(e) Else discard the edge
Canny offer optimal edge detection due to the following reasons:
1. It is less sensitive to noise, since the application of the Gaussian filter
smoothens out the image
2. It removes streaking. Streaking means that if an edge is just below
the set threshold it will remove it leaving disconnected final edges.
However since Canny uses two thresholds thigh andtlow , the edge detector can connect the discontinuities by observing the edges in the
lower threshold image.
6
1.2
Chapter 2
Literature Survey
Edge Detection is a very popular area of research in the field of image processing. It is fundamental step in several image processing step and hence
has been studied extensively. However despite years of research it still holds
a lot of potential for improvement and active research. A variety of edge
detection algorithms have been devised which differ in their purpose and
algorithmic properties[2][3][6]. Most of the methods in edge detection can
be grouped into two categories, first gradient based methods and zero crossing or second gradient based methods. Canny[1] proposed an optimal edge
detector based on the derivative of the Gaussian Filter, along with nonmaximal suppression and double thresholding. Deriche[9] implemented a
fast recursive implementation of Cannys algorithm. Maar and Hildreth[4]
designed edge algorithms based on the second order derivative of the image.
More modern techniques include using mathematical morphology, wavelet
transforms and fuzzy logic. Lei Zhai et al[8] presents a overview of all these
methods. Wavelet transform is a powerful tool for time-frequency analysis,
and allows analysis of local properties of functions in terms of their spectral properties at a particular time. Wavelets act similar to kernel used in
Canny edge detection but are more selective in nature and hence provide
better detection of edges. However it is not always easy to determine the
wavelet function to be used. It is also used for detecting edges in medical
images[16].Mathematical morphology provides an innovative way to detect
edges. Structured elements of different shapes can be used to perform operations that dilate, erode, close or open an image. Deng Caixia et al[13].
proposed an improved edge detection algorithm using morphology, wherein
they use two structural elements,one of size 3x3 and the other of size 5x5
and orient them along different directions. This gives them two sets of edges
and then fuse the edges to generate the edge image. Senthikumarna N and
Kirubakarn C[11] give the application of morphology in segmentation for
MRI brain images. Research has also been carried out to determine the use
of concepts like fractal analysis and fuzzy sets for edge detection in particular applications.
There has been a lot of research on the Canny Edge detector itself. T
Rupalatha et al[15] and Qian Xu et al propose the implementation of a distributed Canny Edge detector on Field Programmable Gate Array(FPGA)
based architecture. Ogawa K has proposed an efficient Canny Edge Detection algorithm on CUDA(Compute Unified Device Architecture). Philip
Worthington proposes an enhanced Canny Edge detection using a concept
called curvature consistency. The algorithm tries to form regions of genuine
curvature without over smoothing the genuine edges. This is done by adjusting the gradient estimates to reduce the difference between shape-index
labels, proposed by Koenderink and van Doorn, of neighboring pixels. Ping
Zhou et al[5] propose an improved canny edge detection algorithm, using
Otsu method for adaptive thresholding. Other methods include Ant Based
Approach proposed by Aleksandar Jevtic and Bo Li[7] for adaptive edge
detection. The algorithm randomly distributed ants on the pixels of the
image. Each ant will then compute the probability of moving in a certain
direction based on the pheromone value of the neighboring pixels. There
is also a negative feedback through pheromone evaporation. These steps
are iterated in a loop till the maximum number of iterations is reached and
eventually the ants settle along the edges of the image. Piotr Dollar and
C.Lawrence Zitnick[10] propose edge detection algorithm using structured
forests. This includes structured learning which classifies a sample by recursively branching left or right down the tree until a leaf node is reached.
It can be seen that the Canny edge detector is a very popular and fast edge
detector, having many practical applications and an algorithm that can be
used in conjunction with other methods to provide better performance for
edge detection in an image.
Chapter 3
Methods Explored
Canny is one of the most popular edge detectors. It is innovative in the
sense that it uses techniques likes non maximal suppression and hysteresis
thresholding to improve edge detection. However there are certain factors
that it can control. These include external factors like noise and illumination
which greatly affects the performance of the edge detector. Many times it
leads to detection of false edges. For example, consider the following image
and the resultant image after applying Canny edge detection(threshold=75).
It can be seen here that for multiple objects closely spaced with one another,
it is very difficult for the edge detector to perform optimally and leads to
many false edges. Illumination problems create reflections in objects which
are mistaken for edges and hence generate false edges. We hence try to
explore methods that will process the image in certain way that will make
the output generated by the Canny algorithm better.
10
3.1
As
Figure 3.3: Size of image and output of the algorithm(Left to right from
top)(a)Original Image of size 300x400 (b)Canny Edge Output of image resized to 300x300 (c)Canny Edge Output of image resized to 400x400 (d)Edge
Output of image resized to 600x600
it can be seen from the images, Canny seems to work better with smaller
size images. This might suggest that we may use Canny on a smaller part
of an image and the join the results to provide better edge detection. Hence
we can apply recursion to divide the image into subimages and apply Canny
edge detection on that. Once done we can merge the results into a single
image.
3.1.1
Basics of Recursion
Recursion is a method of solving a problem, wherein the solution to a problem depends on the solutions to the smaller instances of the same problem.
In other word a problem can be divided into multiple independent problems(note that the problems are independent unlike dynamic programming).
11
logb a ),
O(n
T (n) = O(nk .logn),
O(nk ),
3.1.2
(3.2)
(3.3)
Figure 3.4: Using recursion an image will be divided into four quadrants
and each quadrant will go about the same process
The algorithm can be generalized as follows:
EDGEDETECTION(Image):
1. Divide the image into four regions into Si , i {0, 1, 2, 3}
2. For each region do:
12
3.2
Objects of interest in an image usually are closed in shape. Since one of the
aims of Edge detection is identifying objects, contours can be very useful for
finding edges of the image effectively. Contours can also be useful to remove
the small closed regions that are formed due to noise in the image. However
care must be taken that the contours do not detect edges that do not close
in a loop but that is usually not part of the object in interest.
3.2.1
Basics of Contours
Contours are both open and closed in nature. Contours represent edges
that are linked together to form a boundary of the object. Mathematically
contours can be represented by curves while for a discrete function like an
image, they represent set of pixels forming a closed region.
Geometry of Contours
A planar curve can be represented in several forms, as a 1-D function y =
f (x) or as a 2-D function f (x, y) = 0. Another popular way of representing
a curve is in parametric form (x(u), y(u)). The length of a curve can be
given by:
s
Z u2 2 2
dx
dy
+
du
du
du
u1
Digital curves like those in images are represented as as sent of pixel points
that represent an edge. Consider n edge points (x1 , y1 ), ..., (xn , yn ), then the
length of the digital curve can be estimated as:
S=
n p
X
(xi xi1 )2 + (yi yi1 )2
i=2
For an image chain codes are used to record the edge points that represent
a contour. By noting the starting edge point and recording the direction in
which one should move to the next edge point, chain codes represent very
compact way to represent a contour. Other approaches include polyline
13
Figure 3.5: Direction matrix for 8-connectivity and a chaincode for the curve
splitting. It uses the polyline representation i.e a sequence of line segments
joined end to end. There are two approaches to polyline fitting: top-down
splitting and bottom-up splitting. The Hop-Along Algorithm approximates
approximates a contour by a sequence of line segments and works on short
sublists of edges. The algorithm is given below:
1. Start with the first k edges from the list
2. Fit a line segment between the first and last edges in the list
3. If the normalized maximum error is too large, shorten the sublist to
the point of maximum error. The normalized maximum error is given
by:
= maxi |di |/D
, where di is the distance between the line and the ith edge in the edge
list and D is the distance between the edge points. Repeat step 2.
4. If the line fit succeeds compare its orientation with the orientation of
the previous line, if they are oriented in the same direction replace the
two line segments with a single line segment.
5. Make the current line the previous line segment and advance to the
next k edges in the sublist. Repeat step 2.
Another popular method is the Hough transform, which uses a voting mechanism to estimate parameters(used in regression) to be selected, and in turn
reduces the number of parameters required for regression. The voting is
done by points on a curve. Consider a linear line
y = mx + c
. Here x and y represent observed values while m and c represent the
parameters. The line can also be written as c = xm + y. Here c and
m represent the variable and x and y the parameters. All the (x,y) on the
14
line in the x-y space represents a family of lines in m-c space. Thus if we
are interested in the straight line that best fits given n-points, we can work
in the m-c domain instead of the x-y domain and apply hough tranform.
The point through which maximum number of lines pass through gives the
m and c of the best fitting line. Hough transform becomes inefficient as the
number of parameters increase, especially for 2-D curves. The performance
can be improved by using gradient information on the boundary of the image
to reduce the work in the parameter space. Consider the curve to be fitted
is a circle given by the following two equations:
(x a)2 + (y b)2 = r
. Then converting it into parametric form
x = r cos + a
y = r sin + b
Thus a and b can be written as: a = x r cos andb = y r sin . Thus
we can record the gradient angles i at edge point(which is precomputed
during edge detection) and then measure the distance r from a fixed base
point using the euclidean distance. This can be generalized for all types of
curves.
Using Coutours with Canny Edge Detection
Though contours internally use Edge detectors, we can use them along with
other edge detectors to improve the performance of an image obtained from
them. The algorithm can be stated as follows:
1. Perform Edge Detection using Canny algorithm on an image.
2. Detect Contours in the image using a Contour Algorithm
3. Select only those contours that are greater than certain threshold
Tthreshold . This will remove the small curves formed due to the noise
in the image.
4. Subtract the contour image from the original edge image. This will
leave out the noise present in the original edge image (and the objects
that do not form a closed loop).
5. Use this result to subtract the noise from the original image. Perform
thinning on the image
However this algorithm will not be effective if all objects are necessary for
the application, since this will only detect closed objects in the edge image.
15
3.3
Results
The initial results of the two algorithms have not been quite upto the mark.
While the recursive method works well in removing noise from blocks. However the contour based method does not work very well and the quality of
edges is degraded from that of canny edge detection algorithm. Nevertheless
it does remove noisy edges. The initial results are shown:
Figure 3.6: From Left to Right: (a)Original Image (b)Result from Canny
Algorithm (c)Result by implementing Recursion and Canny algorithm
(d)Result by using Contours and Canny algorithm
16
Chapter 4
Future Work
The initial results show that the algorithms are still not as sophisticated
as the Canny edge detector. However they show encouraging results in
eliminating noisy edges. There are several areas that can be explored to
improve edge detection, such as thresholding parameter of the Canny edge
detector. Histograms of the image show interesting properties about the
image and can be used to apply adaptive thresholding for iteratively improve
performance of the edge detectors. The illumination of the image can also be
adjusted so as to increase the chance of detecting true edges and decreasing
the probability of marking false edges. The neighborhood of the pixel can
also be explored and devise an algorithm similar to the voting mechanism
to distinguish noisy edge points from true edge points.
17
Bibliography
[1] John Canny, A Computational Approach to Edge Detection,IEEE
Transactions on Pattern Analysis And Machine Intelligence, Vol.PAMI8, NO.6, November 1986
[2] G.T.Shrivakshan, Dr.C Chandrasekar, A Comparison for various Edge
Detection Techniques used in Image Processing,International Journal
of Computer Science Issues,Vol 9,Issue 5,No 1, Sept 2012
[3] N. Senthilkumaran, Edge Detection Techniques for Image
Segmentation-A survey of soft computing approach,International
Journal of Recent Trends in Engineering, Vol. 1, No.2, May 2009
[4] D.Marr,
E.Hildreth,Theory of Edge Detection,Proceedings
of
the
Royal
Society
of
London,Series
B,Biological
Sciences,Vol.207,No.1167,pp.187-217,Feb 29,1980
[5] Ping Zhou, Wenjun Ye, Yaojie Xia , Qi Wang,An Improved Canny
Algorithm for Edge Detection,Journal of Computational Information
Systems 7,2011
[6] Rashmi,Mukesh Kumar,Rohini Saxena,Algorithm and Technique on
various Edge Detection: A Survey,Signal and Image Processing:An
International Journal,Vol.4,No.3,June 2013
[7] Aleksander Jevatic, Bo Li, Ant Algorithms for Adaptive Edge Detection
[8] Lei Zhai, Shouping Dong, Honglian Ma,Recent Methods and Applications on Image Edge Detection,2008 International Workshop on Education Technology and Training and 2008 International Workshop on
Geoscience and Remote Sensing
[9] Rachid Deriche,Using Cannys Criteria to Derive a Recursively Implemented Optimal Edge Detector,International Journal of Computer
Vision,pp.167-187, 1987
[10] Piotr Dollar,C.Lawrence Zitnick,Structured Forest for Fast Edge Detection,Microsoft Research
18
19