Unity-Technologies / obstacle-tower-challenge

Starter Kit for the Unity Obstacle Tower challenge
Apache License 2.0
119 stars 39 forks source link

Camera parameters? #33

Open biggzlar opened 5 years ago

biggzlar commented 5 years ago

Any chance we could get the results of a camera calibration with the ingame 3rd-person camera. Specifically the camera matrix (focal lengths and optical center) would be of interest for some approaches seen in robotics.

kwea123 commented 5 years ago

You can do it with the floor... it's a grid! And by further supposing the camera is perfectly centered... you can get the intrinsics. That's what I did.

biggzlar commented 5 years ago

@kwea123 thanks for your reply and the tip! Won't it be a problem that the tiles seen in the texture of the floor are uneven, though?

kwea123 commented 5 years ago

I labelled the corners (by finding the floor lines and computing the intersections) by myself, there is no problem. a

biggzlar commented 5 years ago

Gotcha, now I get it! Thanks a lot!

biggzlar commented 5 years ago

@kwea123 one more question. Have you been using opencv for the calibration or a custom implementation? Because it doesn't seem to work with less than 3x3 image points.

kwea123 commented 5 years ago

I use opencv, and it worked with only 6 points. After that you need to guess a little about the parameters because 6 points are too few to get precise parameters.

Basically it's just cv2.calibrateCamera(objpoints, imgpoints, ... and replace the objpoints and imgpoints that you found.

biggzlar commented 5 years ago

Alright, that's good to know. I get a weird error that I can't find any docs on and was wondering if the number of points was an issue (because it is if you were to try and detect e.g. (2,3) points with cv2.findChessboardCorners).

Feel free to ignore this though, since it goes past the original issue. Just for completeness' sake here is the code and error:

ret, mtx, dist, rvecs, tvecs = cv.calibrateCamera(objp.reshape(-1,1,3
     ...: ).astype(np.float32), imgp.reshape(-1,1,2).astype(np.float32), gray.
     ...: shape[::-1],None,None)
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-147-30b0df05894d> in <module>
----> 1 ret, mtx, dist, rvecs, tvecs = cv.calibrateCamera(obj.reshape(-1,1,3).astype(np.float32), crns.reshape(-1,1,2).astype(np.float32), gray.shape[::-1],None,None)

error: OpenCV(4.0.0) /Users/travis/build/skvark/opencv-python/opencv/modules/calib3d/src/calibration.cpp:1475: 
error: (-211:One of arguments' values is out of range) The number 
of points in the view #0 is < 4 in function 'cvCalibrateCamera2Internal'

with

objp.reshape(-1, 1, 3).shape
(6, 1, 3)

imgp.reshape(-1, 1, 2).shape
(6, 1, 2)

Thank you, again.

kwea123 commented 5 years ago

Try to use list and pay attention to the shapes. I use cv2.calibrateCamera([objpoints], [imgpoints],...) with objpoints with shape (6, 3) and imgpoints with shape (6, 1, 2).

By the way, you need to set flags=cv2.CALIB_FIX_ASPECT_RATIO+cv2.CALIB_FIX_PRINCIPLE_POINT to make it converge to good parameters. I tried many flags and these work best.

biggzlar commented 5 years ago

Awesome, thanks for the generous support. I'm sure I'll figure it out eventually.

Edit: Got it!

awjuliani commented 5 years ago

Hi @biggzlar

With the open source release this summer, we will make sure that this is possible natively from Unity. I am glad to hear though that @kwea123 has provided a solution which works in the mean time 😃

biggzlar commented 5 years ago

Hey @awjuliani

Great to hear!