Edges play a major role in both human and computer vision. We, as humans, can easily recognize many object types and their pose just by seeing a backlit silhouette or a rough sketch. Indeed, when art emphasizes edges and pose, it often seems to convey the idea of an archetype, like Rodin's The Thinker or Joe Shuster's Superman. Software, too, can reason about edges, poses, and archetypes. We will discuss these kinds of reasoning in later chapters.
For the moment, we are interested in a simple use of edges for artistic effect. We are going to trace an image's edges with bold, black lines. The effect should be reminiscent of a comic book or other illustration, drawn with a felt pen.
OpenCV provides many edge-finding filters, including Laplacian()
, Sobel()
, and Scharr()
. These filters are supposed to turn non-edge regions to black while turning edge regions to white or saturated colors. However, they are prone to misidentifying noise as edges. This flaw can be mitigated by blurring an image before trying to find its edges. OpenCV also provides many blurring filters, including blur()
(simple average), medianBlur()
, and GaussianBlur()
. The arguments to the edge-finding and blurring filters vary but always include ksize
, an odd whole number that represents the width and height (in pixels) of the filter's kernel.
A kernel
is a set of weights that are applied to a region in the source image to generate a single pixel in the destination image. For example, a ksize
of 7
implies that 49 (7 x 7)
source pixels are considered in generating each destination pixel. We can think of a kernel as a piece of frosted glass moving over the source image and letting through a diffused blend of the source's light.
For blurring, let's use medianBlur()
, which is effective in removing digital video noise, especially in color images. For edge-finding, let's use Laplacian()
, which produces bold edge lines, especially in grayscale images. After applying medianBlur()
, but before applying Laplacian()
, we should convert from BGR to grayscale.
Once we have the result of Laplacian()
, we can invert it to get black edges on a white background. Then, we can normalize it (so that its values range from 0 to 1) and multiply it with the source image to darken the edges. Let's implement this approach in filters.py
:
def strokeEdges(src, dst, blurKsize = 7, edgeKsize = 5): if blurKsize >= 3: blurredSrc = cv2.medianBlur(src, blurKsize) graySrc = cv2.cvtColor(blurredSrc, cv2.COLOR_BGR2GRAY) else: graySrc = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) cv2.Laplacian(graySrc, cv2.cv.CV_8U, graySrc, ksize = edgeKsize) normalizedInverseAlpha = (1.0 / 255) * (255 - graySrc) channels = cv2.split(src) for channel in channels: channel[:] = channel * normalizedInverseAlpha cv2.merge(channels, dst)
Note that we allow kernel sizes to be specified as arguments to strokeEdges()
. The blurKsize
argument is used as ksize
for medianBlur()
, while edgeKsize
is used as ksize
for Laplacian()
. With my webcams, I find that a blurKsize
value of 7
and edgeKsize
value of 5
look best. Unfortunately, medianBlur()
is expensive with a large ksize
like 7
. If you encounter performance problems when running strokeEdges()
, try decreasing the blurKsize
value. To turn off blur, set it to a value less than 3
.