MatLab transformPointsForward equivalent in Python
<p>I need an equivalent function to <a href="http://www.mathworks.com/help/images/ref/projective2d.transformpointsforward.html" rel="nofollow noreferrer">transformPointsForward</a> (from MatLab) in Python. I  have a code from: <a href="https://github.com/jwyang/face-alignment" rel="nofollow noreferrer">https://github.com/jwyang/face-alignment</a>, which I'm trying to re-write in Python using openCv.</p>
<hr />
<p>I have a following function in MatLab:</p>
<pre><code>[pixel_a_x_lmcoord, pixel_a_y_lmcoord] = transformPointsForward(Tr_Data{s}.meanshape2tf{k}, pixel_a_x_imgcoord', pixel_a_y_imgcoord');
</code></pre>
<p>where</p>
<ul>
<li><code>Tr_Data{s}.meanshape2tf{k]</code> is a geometric transformation, specified as an <code>affine2d</code> geometric transformation object.</li>
<li><code>pixel_a_x_imgcoors</code> is vector 1000x1</li>
<li><code>pixel_a_y_imgcoors</code> is vector 1000x1</li>
</ul>
<hr />
<p><strong>My attempts</strong></p>
<p>I'm searching for an equivalent function, but I'm not succesfull. So far I've found <a href="http://matplotlib.org/devel/transformations.html#matplotlib.transforms.Affine2D" rel="nofollow noreferrer">matplotlib.transform.Affine2D</a>, so I have all the inputs ready.</p>
<p>The problem is, I can't find the forward geometric transform function in Python. I've tried <a href="http://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#void%20perspectiveTransform%28InputArray%20src,%20OutputArray%20dst,%20InputArray%20m%29" rel="nofollow noreferrer">cv2.perspectiveTransform()</a>, but it gives me completely different output:</p>
<p><strong>Example:</strong></p>
<p><strong>MatLab</strong></p>
<pre><code>A = [1 2 3; 4 5 6; 0 0 1]';
pixelx = [1 5 9]';
pixely = [7 5 3]';
obj = affine2d(A);
[x,y] = transformPointsForward(obj,pixelx',pixely');
</code></pre>
<p>Output:</p>
<pre><code>x = [18 18 18]
y = [45 51 57]
</code></pre>
<p><strong>Python</strong></p>
<pre><code>A = np.transpose(np.array([[1,2,3],[4,5,6],[0,0,1]], dtype='float32'))
v = np.array([np.transpose(np.array([[1,5,9],[7,5,3]],dtype='float32'))])
cv2.perspectiveTransform(v,A)
</code></pre>
<p>Output:</p>
<pre><code>array([[[ 0.63043481,  0.80434781],
        [ 0.54347825,  0.76086956],
        [ 0.45652175,  0.71739131]]], dtype=float32)
</code></pre>
<hr />
<h3>Edit:</h3>
<p>I've tried the transform function, but the output from <a href="http://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#transform" rel="nofollow noreferrer">cv2.transform</a> is wrong.</p>
<p>Code:</p>
<pre><code>A = np.transpose(np.array([[1,2,3],[4,5,6],[0,0,1]], dtype='float32'))
v = np.array([np.transpose(np.array([[1,5,9],[7,5,3]],dtype='float32'))])
cv2.transform(v,A)
</code></pre>
<p>Output:</p>
<pre><code>array([[[ 29., 37., 45.], 
        [ 25., 35., 45.], 
        [ 21., 33., 45.]]], dtype=float32)
</code></pre>
<hr />
<h3>Edit2:</h3>
<p>I've tried to used the matrix without the line <code>[0,0,1]</code>, but the result is still incorrect.</p>
<p>Code:</p>
<pre><code>A = np.transpose(np.array([[1,2,3],[4,5,6]], dtype='float32'))
v = np.array([np.transpose(np.array([[1,5,9],[7,5,3]],dtype='float32'))])
cv2.transform(v,A)
</code></pre>
<p>Output:</p>
<pre><code>array([[[ 29.,  37.,  45.],
        [ 25.,  35.,  45.],
        [ 21.,  33.,  45.]]], dtype=float32)
</code></pre>