Figure 5-22 and those of a dilation operation on the same image are shown in
Figure 5-20. The erode operation is often used to eliminate “speckle” noise in an image. The idea here is
that the speckles are eroded to nothing while larger regions that contain visually significant content are not
affected. The dilate operation is often used when attempting to find connected components (i.e., large
discrete regions of similar pixel color or intensity). The utility of dilation arises because in many cases a
large region might otherwise be broken apart into multiple components as a result of noise, shadows, or
some other similar effect. A small dilation will cause such components to “melt” together into one.
To recap: when OpenCV processes the cv::erode() function, what happens beneath the hood is that
the value of some point p is set to the minimum value of all of the points covered by the kernel when
aligned at p; for the dilation operator, the equation is the same except that max is considered rather than
min:
Figure 5-22: Results of the erosion, or “min,” operator: bright regions are isolated and shrunk
𝑒𝑟𝑜𝑑𝑒 𝑥, 𝑦 = min
! , ! ∈!"#$"%
𝑠𝑟𝑐 𝑥 + 𝑖, 𝑦 + 𝑗
𝑑𝑖𝑙𝑎𝑡𝑒 𝑥, 𝑦 = max
(!,!)∈!"#$"%
𝑠𝑟𝑐 (𝑥 + 𝑖, 𝑦 + 𝑗)
You might be wondering why we need a complicated formula when the earlier heuristic description was
perfectly sufficient. Some ...