Captured image + cv2.filter2D = approval?

I am trying to use OpenCV for convolution. In my algorithm, I need to flip the kernel before passing its functions. My first attempt was to use the Numpy and methods : filter2D()fliplr()flipud()

def test_np():
    im = np.random.uniform(size=(32, 32))
    k = np.ones((3, 3), dtype=np.float32) / 9.
    k = np.fliplr(np.flipud(k))                # Numpy-flipped kernel
    return cv2.filter2D(im, -1, k)

Surprisingly, during filtering, she gave me a statement error :

OpenCV error: statement failed (src.dims <= 2 & esz <= (size_t) 32) in the transpose, file / build / buildd / opencv -2.4.2 + dfsg / modules / core / src / matrix.cpp, line 1877 terminate call after calling the instance 'cv :: Exception' what (): / build / buildd / opencv-2.4.2 + dfsg / modules / core / src / matrix.cpp: 1877: error: (-215) src.dims <= 2 && & esz <= (size_t) 32 in the transpose function

However, if I changed the translation method to OpenCV flip():

def test_cv2():
    im = np.random.uniform(size=(32, 32))
    k = np.ones((3, 3), dtype=np.float32) / 9.
    k = cv2.flip(k, -1)                        # OpenCV-flipped kernel
    return cv2.filter2D(im, -1, k)

filter2D() works without problems .

I checked the results np.fliplr(np.flipud(...))and cv2.flip(...), and they match:

k_np = np.fliplr(np.flipud(k))
k_cv2 = cv2.flip(k, -1)
(k_np == k_cv2).all()    # gives True

So, we have 2 arrays that look the same but behave differently .

, , Numpy, - OpenCV? , ?

+4
1

, .

:

numpy, ndarray, , .. . OpenCV, . , filter2D(), transpose() . Python OpenCV Mat , .

copy().

, opencv. ( OpenCV 3.x, OpenCV, )

:

Numpy ndarray

Numpy , flip, .. , , . , , view. , .

OpenCV . OpenCV , Numpy, numpy strides, array. :

In [39]: x = np.ones((3,3),dtype=np.float32)

In [40]: x.strides
Out[40]: (12, 4)

In [43]: x.flags
Out[43]: 
  C_CONTIGUOUS : True    # Original array is continuous
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False

Numpy

In [41]: y = np.transpose(x)

In [42]: y.strides
Out[42]: (4, 12)         # transpose just change the strides

In [44]: y.flags
Out[44]: 
  C_CONTIGUOUS : False   # tranposed array is not continuous in numpy
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False

OpenCV

In [45]: z = cv2.transpose(x)

In [46]: np.all(z==y)   # result of numpy and OpenCV are same
Out[46]: True

In [47]: z.strides      # Check the strides
Out[47]: (12, 4)

In [48]: z.flags
Out[48]: 
  C_CONTIGUOUS : True   # OpenCV result is continuous.
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False

, numpy tranpose

In [53]: q = np.transpose(x).copy()

In [55]: np.all(z==q)
Out[55]: True

In [56]: q.strides     # Strides is same as OpenCV function
Out[56]: (12, 4)

In [57]: q.flags       
Out[57]: 
  C_CONTIGUOUS : True  # result is continuous also
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False

In [49]: %timeit y = np.transpose(x)
1000000 loops, best of 3: 701 ns per loop

In [50]: %timeit y = np.transpose(x).copy()
1000000 loops, best of 3: 1.48 us per loop

In [51]: %timeit y = cv2.transpose(x)
1000000 loops, best of 3: 1.04 us per loop

flipping numpy- . OpenCV .

In [58]: a = np.fliplr(x)

In [59]: a.strides
Out[59]: (12, -4)

In [60]: b = cv2.flip(x,-1)

In [61]: b.strides
Out[61]: (12, 4)

OpenCV python Mat. copy().

OpenCV . , , python. OpenCV.

OpenCV 3, OpenCV. :

In [62]: cv2.__version__
Out[62]: '3.0.0-dev'

In [63]: im = np.random.uniform(size=(32,32))

In [64]: k = np.ones((3,3), dtype=np.float32)/9.

In [65]: k = np.fliplr(np.flipud(k))

In [66]: z = cv2.filter2D(im, -1, k)

In [70]: print z[:5,:5]
[[ 0.65543429  0.53362787  0.45040413  0.52151458  0.61432061]
 [ 0.53666124  0.49690944  0.40779054  0.50330829  0.60923295]
 [ 0.39288601  0.42130001  0.41378173  0.5080897   0.58349994]
 [ 0.32685086  0.4340541   0.46039198  0.48272091  0.45093509]
 [ 0.25456175  0.40217766  0.4459138   0.49665956  0.4198618 ]]
+4

All Articles