title: In OpenCV, what data types does cv2.filter2D() expect?
body: <p>I'm teaching myself about edge detectors, and I'm trying to use OpenCV's <code>filter2D</code> to implement my own gradient calculator, similar to <code>cv2.Sobel()</code>. In the Python interface to OpenCV, <code>cv2.filter2D()</code> allows users to convolve an image with a custom filter. In OpenCV nomenclature, this filter is called a "kernel."</p>
  
  <p>Using an image (<a href="http://dl.dropbox.com/u/35993/OpenCV_StackOverflow/per00001.png" rel="nofollow">per00001.png</a>) from the <a href="http://cbcl.mit.edu/software-datasets/PedestrianData.html" rel="nofollow">MIT pedestrian dataset</a>, I find that <code>cv2.Sobel()</code> produces a reasonable looking output. (Code is below, <a href="http://dl.dropbox.com/u/35993/OpenCV_StackOverflow/gradientX.png" rel="nofollow">output image is here</a>.)</p>
  
  <pre><code>#OpenCV's Sobel code (outputs nice-looking gradient)
  import cv2, numpy
  img = cv2.imread("./per00001.png")
  
  gradientX = cv2.Sobel(img, -1, 1, 0)
  
  compression_params = [cv2.cv.CV_IMWRITE_PNG_COMPRESSION, 9]
  cv2.imwrite("gradientX.png", gradientX, compression_params)
  </code></pre>
  
  <p><b>↑ GOOD</b></p>
  
  <hr>
  
  <p><b>↓ BROKEN</b></p>
  
  <p>When I try to implement my own <code>Sobel()</code>-like code (below), I get an <a href="http://dl.dropbox.com/u/35993/OpenCV_StackOverflow/gradientX_customSobel.png" rel="nofollow">all-black image</a>. I'm speculating that the problem is with the data type of the kernel parameter (<code>horizontalSobelMtx</code>) that I'm passing into <code>cv2.filter2D()</code>. However, I haven't been able to find any documentation about the kernel data type for <code>cv2.filter2D()</code>.</p>
  
  <pre><code>#Custom Sobel code (outputs all-black image)
  import cv2, numpy
  img = cv2.imread("./per00001.png")
  
  horizontalSobelMtx = [[-1,0,1],[-2,0,2],[-1,0,1]]
  horizontalSobelMtx = numpy.asanyarray(horizontalSobelMtx) #guessing about appropriate datatype.
  gradientX_customSobel = cv2.filter2D(img, -1, horizontalSobelMtx)
  
  compression_params = [cv2.cv.CV_IMWRITE_PNG_COMPRESSION, 9]
  cv2.imwrite("gradientX_customSobel.png", gradientX_customSobel, compression_params)
  </code></pre>
  
  <p>So, here are my questions:</p>
  
  <p>1) What data type does <code>cv2.filter2D(..., kernel, ...)</code> expect for the <code>kernel</code> parameter?</p>
  
  <p>2) If the data type of <code>kernel</code> isn't the problem here, then what's causing my custom Sobel code to output a blank image? </p>