Problem about "delta" argument of filter2D

19 views
Skip to first unread message

HYRY

unread,
Jun 2, 2010, 1:24:31 AM6/2/10
to ctypes-opencv
I don't know whether this is a problem of OpenCV or pyopencv.
Following is the source code

----------------------------------------------

import pyopencv as cv
import numpy as np

kernel = np.array([[1,1,1],[1,2,1],[1,1,1.0]]) * 0.1
k1 = cv.asMat(kernel, force_single_channel=True)
a = cv.Mat(5,5,cv.CV_8UC1)
a[:] = 0

b = cv.Mat()
cv.filter2D(a, b, cv.CV_8U, k1, delta=10.0)
print "==== source image ===="
print a
print "==== kernel ===="
print k1
print "==== output image ===="
print b

----------------------------------------------

It create an all zero Mat object, and filter2D() it with a kernel.
I supporse the result should be a Mat filled by 10(the delta argument
of filter2D).

But here is the output, all zeros:

==== source image ====
Mat(rows=5, cols=5, nchannels=1, depth=0):
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]], dtype=uint8)
==== kernel ====
Mat(rows=3, cols=3, nchannels=1, depth=6):
array([[ 0.1, 0.1, 0.1],
[ 0.1, 0.2, 0.1],
[ 0.1, 0.1, 0.1]])
==== output image ====
Mat(rows=5, cols=5, nchannels=1, depth=0):
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]], dtype=uint8)



if I changed:
cv.filter2D(a, b, cv.CV_8U, k1, delta=10.0)
to
cv.filter2D(a, b, cv.CV_16U, k1, delta=10.0)

the result is correct:

==== output image ====
Mat(rows=5, cols=5, nchannels=1, depth=2):
array([[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]], dtype=uint16)



or changed:
kernel = np.array([[1,1,1],[1,2,1],[1,1,1.0]]) * 0.1
to
kernel = np.array([[1,1,1],[1,2,1],[1,1,1.0]])
the result is correct:

==== source image ====
Mat(rows=5, cols=5, nchannels=1, depth=0):
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]], dtype=uint8)
==== kernel ====
Mat(rows=3, cols=3, nchannels=1, depth=6):
array([[ 1., 1., 1.],
[ 1., 2., 1.],
[ 1., 1., 1.]])
==== output image ====
Mat(rows=5, cols=5, nchannels=1, depth=0):
array([[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]], dtype=uint8)

Minh-Tri Pham

unread,
Jun 2, 2010, 1:45:09 AM6/2/10
to ctypes...@googlegroups.com
Hi HYRY,

I think the problem may lie in OpenCV's code. PyOpenCV calls cv::filter2D
directly without transforming any function argument at all. Here's the
wrapping code for filter2D() (from file pyopencvext_free_functions_f.pypp.cpp):

{ //::cv::filter2D

typedef void ( *filter2D_function_type )( ::cv::Mat const &,::cv::Mat
&,int,::cv::Mat const &,::cv::Point,double,int );

bp::def(
"filter2D"
, filter2D_function_type( &::cv::filter2D )
, ( bp::arg("src"), bp::arg("dst"), bp::arg("ddepth"),
bp::arg("kernel"), bp::arg("anchor")=cv::Point_<int>(-0x000000001,
-0x000000001), bp::arg("delta")=0,
bp::arg("borderType")=int(::cv::BORDER_REFLECT_101) ) );

}

If you have time, perhaps you can write equivalent C++ code and see if it
produces similar results.

Cheers,
Minh-Tri

--
Dr. Minh-Tri Pham
Research Fellow
Surrey Space Centre, University of Surrey, Guildford, GU2 7XH, UK
Mob: +44 (0) 778 774 1089 | Tel: +44 (0) 148 368 2224

Reply all
Reply to author
Forward
0 new messages