title: Implementation of image dilation and erosion
body: <p>I am trying to figure out an efficient way of implementing image dilation and erosion for binary images. As far as I understand it, the naive way would be:</p>
  
  <ul>
  <li>loop through the image</li>
  <li>if pixel is 1</li>
  <li>loop through the neighborhood based on the structuring element's
  height and width</li>
  <li>(dilate) substitute each pixel of the image with the value in the
  corresponding location of the SE</li>
  <li>(erode) check if all neighborhood is equal to the SE, if so keep all
  the pixels, else delete the centre</li>
  </ul>
  
  <p>so this means that for each pixel I have to loop through the SE as well making this a O(N<em>M</em>W*H).</p>
  
  <p>Is there a more elegant way of doing this?</p>