Driver
Driver

Reputation: 13

Converting openCV python function to C++

Following fragment is stolen from openCV python samples:

def motion_kernel(angle, d, sz=65):
    kern = np.ones((1, d), np.float32)
    c, s = np.cos(angle), np.sin(angle)
    A = np.float32([[c, -s, 0], [s, c, 0]])
    sz2 = sz // 2
    A[:,2] = (sz2, sz2) - np.dot(A[:,:2], ((d-1)*0.5, 0))
    kern = cv2.warpAffine(kern, A, (sz, sz), flags=cv2.INTER_CUBIC)
    return kern

It is part of a program that recover images blurred by some kind of noise. I'm trying to convert this code to C++ but I have only a very superficial knowledge of python. So I am in troubles in particular with line:

A[:,2] = (sz2, sz2) - np.dot(A[:,:2], ((d-1)*0.5, 0))

I understand remaining code.

Upvotes: 0

Views: 84

Answers (2)

Ash
Ash

Reputation: 4718

Let's write your matrix A as

A=[R | T]

where R is a 2*2 matrix and T is the last column of A. The notation A[:,2] in the code refers to T, and A[:,:2] refers to R. So, the line

A[:,2] = (sz2, sz2) - np.dot(A[:,:2], ((d-1)*0.5, 0))

is is just computing this:

R*v + u //v in R^2 is [(0.5(d-1)), 0]^T
        //u in R^2 is [sz2, sz2]^T

and then it stores it in T (the last column of A).

Upvotes: 1

Anatoliy R
Anatoliy R

Reputation: 1789

Not sure, because I do not see some code, but most likely:

A[:,:2] 

is the matrix

[[c, -s]
 [s, c]]

Then, with

np.dot(A[:,:2], ((d-1)*0.5, 0))

we multiply that matrix by vector

[(d-1)*0.5, 0]

Actually it's tuple, not vector, but meaning is the same.

After multiplication we get some vector of two elements and we subtract that vector from

[sz2, sz2]

Finally, we replace zeros in 2x3 matrix A with the result of subtraction

Upvotes: 1

Related Questions