title: Get Laplacian pyramid using opencv
body: <p>I'm trying to get a layer of the <strong>Laplacian pyramid</strong> using the opencv functions: <code>pyrUp</code> and <code>pyrDown</code>.</p>
  
  <p>In the <a href="http://docs.opencv.org/modules/imgproc/doc/filtering.html?highlight=pyrup#cv2.pyrUp" rel="nofollow">documentation</a> and in more detail in this <a href="http://books.google.be/books?id=seAgiOfu2EIC&amp;pg=PA131&amp;lpg=PA131&amp;dq=laplacian%20pyramid%20opencv&amp;source=bl&amp;ots=hSJ97feFKg&amp;sig=g7UTzcN8ALGzAFLtfNhmoM-n5Ig&amp;hl=en&amp;sa=X&amp;ei=M2OcUf2qJKHi4QTJ4YHwAQ&amp;redir_esc=y#v=onepage&amp;q=laplacian%20pyramid%20opencv&amp;f=false" rel="nofollow">book</a>, I found that the <em>i-th</em> Laplacian layer should be obtained by the following expression:</p>
  
  <p>Li = Gi - pyrDown(Gi+1)</p>
  
  <p>where Gi is the i-th layer of the Gaussian pyramid.</p>
  
  <p>What I've tried is:</p>
  
  <pre><code>def get_laplacian_pyramid_layer(img, n):
      gi = img
      for i in range(n):
          gi_prev = gi
          gi = cv2.pyrDown(gi_prev)
      pyrup = cv2.pyrUp(gi)
      return cv2.addWeighted(gi_prev, 1.5, pyrup, -0.5, 0)
  </code></pre>
  
  <p>But I get different sizes of the images involved in the substraction. I don't understand it because <code>pyrUp</code> is suppposed to invert the process of the Gaussian pyramid, i.e. pyrDown (with a lose of information of course but that should not affect the size, right?).</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I refactored my code to:</p>
  
  <pre><code>def get_laplacian_pyramid_layer(img, n):
  '''Returns the n-th layer of the laplacian pyramid'''  
      currImg, i = img, 0
      while i &lt; n: # and currImg.size &gt; max_level (83)
          down, up = new_empty_img(img.shape), new_empty_img(img.shape)
          down = cv2.pyrDown(img)
          up = cv2.pyrUp(down, dstsize=currImg.shape)
          lap = currImg - up
          currImg = down
          i += 1
      return lap
  </code></pre>
  
  <p>As you can see, I force the destination image to be of the same size of the source with the parameter <code>dstsize</code> of the <code>pyrUp</code> function.</p>
  
  <p>However, this code also gives me an error when when executing the <code>pyrUp</code> function. The message of the error is:</p>
  
  <blockquote>
    <p>OpenCV Error: Assertion failed (std::abs(dsize.width - ssize.width*2) == dsize.width % 2 &amp;&amp; std::abs(dsize.height - ssize.height*2) == dsize.height % 2) in pyrUp_,</p>
  </blockquote>
  
  <p>In debug mode I checked the expression of the assertion with:</p>
  
  <pre><code>up.shape[1]-down.shape[1]*2 == up.shape[1] %2 and up.shape[0]-down.shape[0]*2 == up.shape[0] %2
  </code></pre>
  
  <p>and it is satisfied.</p>
  
  <p>So, I don't have a clue of what is happening.</p>