getPerspectiveTransform or findHomography

1,080 views
Skip to first unread message

Johannes Larsch

unread,
Mar 23, 2017, 6:26:10 AM3/23/17
to Bonsai Users
Hi,

I was hoping to use openCV getPerspectiveTransform or findHomography to calibrate a projector with a camera. Are these functions available in openCV.net? How can I access them in a python node?

Johannes
Message has been deleted

Johannes Larsch

unread,
Mar 23, 2017, 8:15:42 AM3/23/17
to Bonsai Users
In the meantime, I calculated the transformation matrix outside of bonsai to go ahead and use the result to transform points.
Ultimately, this would be OK because I expect I will have to calibrate only once per day or so and then I can save the transformation for use in bonsai.

Next, I want to transform (x,y) points according to this matrix (not an image).
Outside of bonsai, I can use the following python code:

h, status = cv2.findHomography(src, dst)
src_transformed=cv2.perspectiveTransform(np.array([src.astype('float32')]), h)[0]

this results in 'corrected' src coordinates.


In bonsai, I am again stuck figuring out the appropriate data types:

in a python node, I tried the following

clr.AddReference("OpenCV.Net")
from OpenCV.Net import *

  values = Array[float]([  9.48648092e-01,   1.53330831e-02,   1.97974600e+01,
      -2.32614180e-02,   9.52525799e-01,   5.03506020e+01,
      -6.44805411e-06,   6.25636499e-06,   1.00000000e+00])

  h = Mat.FromArray(values, 3, 3, Depth.F32, 1)

  values = Array[float]([207,243])
  point = Mat.FromArray(values, 3, 3, Depth.F32, 1)
  out = point

  CV.PerspectiveTransform(point,out,h)


This fails, because PerspectiveTransform expects 'vec' and 'out' to be of data type 'Arr' and I can't figure out how to generate that type.

Johannes Larsch

unread,
Mar 23, 2017, 10:32:25 AM3/23/17
to Bonsai Users
OK, I wrote a function that does the  transformation of (x,y) coordinates according to a matrix using CV.PerspectiveTransform. Maybe some of my conversions between data types can be omitted?
It would still be nice to get the homography matrix in bonsai but for now, this is working.

clr.AddReference("OpenCV.Net")
from OpenCV.Net import *


def transf(x,y):
  values = Array[float]([  9.48648092e-01,   1.53330831e-02,   1.97974600e+01,
      -2.32614180e-02,   9.52525799e-01,   5.03506020e+01,
      -6.44805411e-06,   6.25636499e-06,   1.00000000e+00])

  h = Mat.FromArray(values, 3, 3, Depth.F64, 1)

  values1 = Array[float]([x,y])
  values2 = Array[float]([1,1])

  point = Mat.FromArray(values1, 1, 1, Depth.F64,2)
  out = Mat.FromArray(values2, 1, 1, Depth.F64,2)

  CV.PerspectiveTransform(point,out,h)

  x=out.Item[0].Val0
  y=out.Item[0].Val1

  return x,y

Gonçalo Lopes

unread,
Mar 23, 2017, 1:54:34 PM3/23/17
to Johannes Larsch, Bonsai Users
Hi Johannes,

Sorry for the late reply, I guess you already figured out most of the stuff. GetPerspectiveTransform and FindHomography do exist in OpenCV.NET and you can access them as you access the other functions, e.g.:

CV.GetPerspectiveTransform(src,dst,mapMatrix)

I'm assuming you tried this before, but do bear in mind that src and dst need to be managed arrays of type Point2f for the corners, which means that lists of doubles and floats will not work.

In general, documentation for OpenCV.NET can be found in NuDoq:


Let me know if you have any specific difficulties converting the types.


--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/dcb71bd9-b29b-44f1-bcb2-a3503af10487%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Johannes Larsch

unread,
Mar 23, 2017, 4:10:59 PM3/23/17
to Bonsai Users, johanne...@gmail.com
thanks for the link. I was briefly looking at the bitbucket files for opencv and noticed that the source file that has PerspectiveTransform didn't have GetPerspectiveTransform so I got confused. figuring out these types is a bit painful, i.e. Mat.Item[0].Val1 was not intuitive to me for accessing a matrix... But it's great that many things are possible in the end.
Reply all
Reply to author
Forward
0 new messages