● The first component is a luminosity function that
converts an RGB pixel to a gray level intensity
● We will use this function as we iterate through an RGB
image and compute the luminosity coefficients of various
● Computing edges is a pixel-level operation: a given pixel
(pivot pixel) either belongs to an edge or not
● The edges can be detected via gradients, i.e., changes
in luminosity between two horizontal and two vertical
neighbor pixels of the pivot pixel at (x, y).
● What to do with border pixels that do not have a
complete set of neighbors?
● Two simple approaches are
1) padding the image and
2) ignoring the border pixels.
We will choose (2) for the sake of simplicity
Calculate dx and dy
● The function rgb_pix_dx computes the luminosity
change in the x direction as the difference between the
two luminosity levels of the pivot pixel's horizontal
● The function rgb_pix_dy computes the luminosity
change in the y direction as the difference between the
two luminosity levels of the pivot pixel's vertical
● The magnitude of the gradient is the length of the
hypotenuse of the right triangle whose vertical side is
returned by rgb_pix_dy and whose horizontal side is
computed by rgb_pix_dx.
● The direction of the gradient is computed as
math.atan2(pdy,pdx)*(180/math.pi), where pdy and pdx
are the values returned by rgb_pix_dy and rgb_pix_dx,
● The function math.atan2(pdy,pdx) returns values
between -pi and +pi. If pdy and equal to pdx, are both
equal to the default_delta value (i.e., there is no change
in the y and x intensities) we return -200, an arbitrary
value outside of [-180, +180].
● Python generators are used to iterate through image
and detect edges.
● A pixel belongs to an edge if its direction is within [-90,
+90] and its magnitude is at least 20.
● Edge pixels are set to 255 (white); non-edge pixels to 0