Ray Wu
Ray Wu

Reputation: 1023

cv.filter2D return TypeError

My platform is Mac OS X 10.6 and install python 2.7.2/opencv by homebrew. I want to use cv.filter2D to do Lanczos resampling, here is my code:

import cv

src = cv.imread('img.jpg')
dst = cv.CloneMat(src)
kernel = cv.CreateMat(7, 7, cv.CV_32FC1)

for i in range(-3, 4):
    for j in range(-3, 4):
        kernel[i+3, j+3] = L(i, 3) * L(j, 3)

cv.filter2D(src, dst, kernel)
cv.imshow(dst)

My problem is cv.filter2D. If I use

cv.filter2D(src, dst, kernel) 

, which wrote in OpenCV Reference Manual, it will get error message.

Traceback (most recent call last):
  File "lanczos.py", line 27, in <module>
    cv.filter2D(src, dst, kernel)
TypeError: only length-1 arrays can be converted to Python scalars

Under python I type

cv.filter2D.__doc__

, it said the function prototype is

'filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]]) -> dst'

if I use

cv.filter2D(src, -1, kernel, dst)

instead, the error message become

Traceback (most recent call last):
  File "lanczos.py", line 27, in <module>
    cv.filter2D(src, 1 , kernel, dst)
TypeError: <unknown> is not a numpy array

seems no people encounter this problem before?

Upvotes: 0

Views: 2864

Answers (1)

HYRY
HYRY

Reputation: 97291

what is the version of your opencv, my version is:

>>> import cv2 as cv
>>> cv.__version__
'$Rev: 4557 $'

I can use a ndarray as the kernel, and don't need to create an empty array for the result, filter2D() will return a new ndarray.

import cv2 as cv
import numpy as np
src = cv.imread('img.jpg')
kernel = np.ones((7,7), dtype=np.float)/49.0
dst = cv.filter2D(src, -1, kernel)
cv.imwrite("result.jpg", dst)

Upvotes: 3

Related Questions