- Home /

# How to simulate Unity Pinhole Camera from its intrinsic parameters

We have a real camera for which its intrinsic parameters are estimated, based on the calibration toolbox of Bouget: http://www.vision.caltech.edu/bouguetj/calib_doc/ We assume that there is no a non-linear optical distortion and estimate parameters: Focal length: The focal length in pixels is stored in the 2x1 vector fc. Principal point: The principal point coordinates are stored in the 2x1 vector cc as described in http://www.vision.caltech.edu/bouguetj/calib_doc/htmls/parameters.html

According to the classic theory presented in the book of Zisserman "Multiple View Geometry in Computer Vision Second Edition" (chapter 6), the projection matrix in this case is given by:

P = [fc(1), 0, cc(1), 0; 0, fc(2), cc(2), 0; 0, 0, 1, 0] This matrix is 3x4 and it acts on the homogenous coordinates (X,Y,Z,1)' to get projective homogenous coordinates; (x,y, z)' = P*(X,Y,Z,1)'. The corresponding image pixel coordinates are then calculated based on xp= x/z and yp=y/z.

Does anyone can help us to simulate exactly the same camera in the Unity. We would like to know how the classic matrix P above is related to Camera.projectionMatrix that is of dimensionality [4x4] and we would like to save the obtained rendered scene that has the same number of pixels as images from the real camera.

Or another way around, what is the mathematical expression that relates between (XYZ)-coordinates in the camera coordinate system and (xp,yp)- projective pixel coordinates and Camera.projectionMatrix of the Unity?

Unity Forums might be a better place for this question. If you ask there, consider editing this question to include a link to the forum question.

Hi, did you solve this problem? I want to calculate physical camera's intrinsics parameters but couldn't find proper APIs. If you find the solution, could you share with us?

**Answer** by jungguswns
·
Apr 27, 2019 at 10:54 AM

It's late answer, but there is nothing to do with projection matrix.

you have to play with Sensor Size & Lens Shift by using Physical Camera option

ax 0 px

0 ay py

0 0 1

say this is Intrinsic matrix K, then

SensorSizeX = f * resolution_width / ax

SensorSizeY = f * resolution_height / ay

LensShiftX = - (px - width / 2.0f) / width // somehow this is apposite sign ..

LensShiftY = (py - height / 2.0f) / height

Here, you have to set screen resolution as yout image's resolution (if you work in Unity editor you can do this by changing player option or in script and set Game view as Standalone).

I tested this by setting these camera value with given intrinsic K, placing checkerboard, capturing images and run camera calibration using opencv. Calibration gives less than 1% error. I'd say, it works.

And one more thing, If you have K but f is unknown, just fixing f to some number will make it work (i.e. f = 100.0) here all we need is just have right combination of sensorSize & f & resolution to construct ax (or ay).

say ax = 800, f = 10, sensorSizeX = 10, resolutionX = 800 f = 10 and sensorSize = 10 makes ax = 800 but also if f is set to arbitrary number like 1000.0 then sensorSize will be set to 1000.0 (by equation), which makes ax = 800 anyways.

and it will look exactly same in unity, as long as your arbitrary f is positive !

@jungguswns Thank you so much! I've been looking all around for this answer! For e.g - > https://forum.unity.com/threads/how-to-use-opencv-camera-calibration-to-set-physical-camera-parameters.704120/

That said, is there a web link/source where I can verify the formulae - the SensorX, SensorY, lens-shift, etc or if not some source where I can read up this info, and maybe figure out the formula by my own?

Much appreciated!

First, I'd recommend you to read this book from page 153 to 157 (http://cvrs.whu.edu.cn/downloads/ebooks/Multiple%20View%20Geometry%20in%20Computer%20Vision%20(Second%20Edition).pdf I think you already know, but just in case) to understand what is exact K after calibration (which is eqation (6.9) at page 157)

So K you have does not directly contain ether f or px(or py) and this is very important thing to understand first.

To explain how sensor size and lens shift, I wanted to attach my hand writing and drawing but I cannot attach somehow, it takes forever to attach.. (I don't know how to use this forum that well haha ...) Idk, if you want some good way to attach or share it then I promise I will.

Since I cannot attach anything here I will just post my code. Its very straight forward to understand.

/////////////////////////////////////////////////////////////////////////////////////

public class IntrinsicSetup : MonoBehaviour {

```
Camera mainCamera;
public float f = 35.0f; // f can be arbitrary, as long as sensor_size is resized to to make ax,ay consistient
```

// Use this for initialization void Start () { mainCamera = gameObject.GetComponent(); changeCameraParam(); }

```
public void changeCameraParam()
{
string path = "Assets/Resources/Intrinsic.txt";
float ax, ay, sizeX, sizeY;
float x0, y0, shiftX, shiftY;
int width, height;
string[] lines = File.ReadAllLines(path);
string[] parameters = lines[1].Split(' ');
string[] resolution = lines[3].Split(' ');
ax = float.Parse(parameters[0]);
ay = float.Parse(parameters[1]);
x0 = float.Parse(parameters[2]);
y0 = float.Parse(parameters[3]);
width = int.Parse(resolution[0]);
height = int.Parse(resolution[1]);
sizeX = f * width / ax;
sizeY = f * height / ay;
//PlayerSettings.defaultScreenWidth = width;
//PlayerSettings.defaultScreenHeight = height;
shiftX = -(x0 - width / 2.0f) / width;
shiftY = (y0 - height / 2.0f) / height;
mainCamera.sensorSize = new Vector2(sizeX, sizeY); // in mm, mx = 1000/x, my = 1000/y
mainCamera.focalLength = f; // in mm, ax = f * mx, ay = f * my
mainCamera.lensShift = new Vector2(shiftX, shiftY); // W/2,H/w for (0,0), 1.0 shift in full W/H in image plane
}
```

}

////////////////////////////////////////////////////////////////////////////

It keeps cut my coding into few blocks..... do you know how to post it properly anyhow?

Also if you want to verify, you can download one of calibration checker board, capture image from unity camera and run calibration from opencv (I think you know how use opencv judging from your other post).

if it reproduce K with small error, than it means you got it right. I used that way to verify everything I wrote for this code.

Also, @jungguswns is resolution_width different from width? If so what exactly are each?

somehow, I cannot read any comment in this page (I can read your comment only via my email notification). So I don't understand your question

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.