当前位置:网站首页>Camera coordinate system, world coordinate system, pixel coordinate system conversion, and Fov conversion of OPENGLDEFocal Length and Opengl
Camera coordinate system, world coordinate system, pixel coordinate system conversion, and Fov conversion of OPENGLDEFocal Length and Opengl
2022-07-30 07:50:00 【sunset stained ramp】
目的:Understand how cameras work,Rendered and camera-taken depth maps
Recent studies on camera parameters and openglParameter relationship for the rendered image.It will be encountered in the process of going from the camera coordinate system to the pixel coordinate systemproject 矩阵.
Theoretical camera transformation
Camera-based learning of theoretical knowledge.Normally we build with camera parametersprojectGenerally need to go through the following two processes:
相机坐标系->图像坐标系->像素坐标系
Suppose we get a vertex in the camera coordinate system p ( x c , y c , z c ) p(x_c,y_c,z_c) p(xc,yc,zc),Convert to vertices on the image p ( x i , y i ) p(x_i,y_i) p(xi,yi).It's converted to a matrix as M p r o j M_{proj} Mproj
Below we will describe a few steps:
- 相机坐标系到图像坐标系 M p 2 c M_{p2c} Mp2c
相同的位置,Different cameras will get different pictures.It can be seen that this process is generally related to the parameters of the camera.Some parameters of the camera are generally in the camera's manual,Instructions for the corresponding styles can also be found online,Some even inside the configuration file(For example, the image camera camera parameters in the scanner are all available).In order to understand the function of these parameters,We need to understand the principle of camera imaging(小孔成像).
The coordinate system in the image is ( O ′ x , O ′ y ) (O'x, O'y) (O′x,O′y), 相机坐标系为 ( O c x c , O c y c , O c z c ) (O_cx_c, O_cy_c , O_cz_c) (Ocxc,Ocyc,Oczc) , 方便理解 ,Transform the viewable view.因为 ( O ′ x , O ′ y ) (O'x, O'y) (O′x,O′y) 和 ( O c x c , O c y c , O c z c ) (O_cx_c, O_cy_c , O_cz_c) (Ocxc,Ocyc,Oczc) 在 ( O c x c , O c y c ) (O_cx_c, O_cy_c) (Ocxc,Ocyc) 平行.So according to geometrical symmetry,Move the image coordinate system to the mirrored position,如下图.其中 ∠ O c A B \angle{O_cAB} ∠OcAB为直角.The pictures are not very accurate.

因为相似三角形(1):
△ A B O c ∼ △ o C O c \bigtriangleup ABO_c \sim \bigtriangleup oCO_c △ABOc∼△oCOc
得到下面公式:
O c o O c Z c = o C A B = O c C O c B ( 1 ) \frac{O_co}{O_cZ_c} = \frac{oC}{AB} = \frac{O_cC}{O_cB} \space \space \space (1) OcZcOco=ABoC=OcBOcC (1)
Another way of expressing(因为 O c O_c Oc 为原点,changed symbols)为下面:
O c o O c Z c = f Z c ( 2 ) \frac{O_co}{O_cZ_c} = \frac{f}{Z_c} \space \space \space (2) OcZcOco=Zcf (2)
o C A B = x X c ( 3 ) \frac{oC}{AB} = \frac{x}{X_c} \space \space \space (3) ABoC=Xcx (3)
因为相似三角形(2):
△ A B O c ∼ △ o C O c \bigtriangleup ABO_c \sim \bigtriangleup oCO_c △ABOc∼△oCOc
得到下面公式:
O c C O c B = C p B P ( 4 ) \frac{O_cC}{O_cB} = \frac{Cp}{BP} \space \space \space (4) OcBOcC=BPCp (4)
Another way of expressing(因为 O c O_c Oc 为原点,changed symbols)为下面:
C p B P = y Y c ( 5 ) \frac{Cp}{BP} = \frac{y}{Y_c} \space \space \space(5) BPCp=Ycy (5)
因为 ( 1 ) ( 2 ) ( 3 ) ( 4 ) ( 5 ) (1)(2)(3)(4)(5) (1)(2)(3)(4)(5)公式得到:
f Z c = x X c = y Y c \frac{f}{Z_c} = \frac{x}{X_c} = \frac{y}{Y_c} Zcf=Xcx=Ycy
It is further transformed into the formula as follows:
x = f ∗ X c Z c ( 6 ) x=\frac{f*X_c}{Z_c} \space \space \space(6) x=Zcf∗Xc (6)
y = f ∗ Y c Z c ( 7 ) y=\frac{f*Y_c}{Z_c} \space \space \space(7) y=Zcf∗Yc (7)
把其中 ( 6 ) , ( 7 ) (6),(7) (6),(7)The formula written in matrix form is obtained as follows:
[ x y z ] = [ f 0 0 0 0 f 0 0 0 0 1 0 ] [ X c Y c Z c 1 ] \begin{bmatrix} x\\ y \\ z \end{bmatrix} = \begin{bmatrix} f & 0 & 0 & 0 \\ 0 & f & 0 & 0 \\ 0 & 0 & 1 & 0 \end{bmatrix} \begin{bmatrix} X_c\\ Y_c \\ Z_c \\ 1 \end{bmatrix} ⎣⎡xyz⎦⎤=⎣⎡f000f0001000⎦⎤⎣⎢⎢⎡XcYcZc1⎦⎥⎥⎤
The resulting matrix equation M p 2 c M_{p2c} Mp2c为下面:
M p 2 c = [ f 0 0 0 0 f 0 0 0 0 1 0 ] M_{p2c}= \begin{bmatrix} f & 0 & 0 & 0 \\ 0 & f & 0 & 0 \\ 0 & 0 & 1 & 0 \end{bmatrix} Mp2c=⎣⎡f000f0001000⎦⎤
A shorthand formula can be obtained:
[ x y z ] = M p 2 c [ X c Y c Z c 1 ] \begin{bmatrix} x\\ y \\ z \end{bmatrix} = M_{p2c}\begin{bmatrix} X_c\\ Y_c \\ Z_c \\ 1 \end{bmatrix} ⎣⎡xyz⎦⎤=Mp2c⎣⎢⎢⎡XcYcZc1⎦⎥⎥⎤
这时候获取的 [ x y z ] \begin{bmatrix} x\\ y \\ z \end{bmatrix} ⎣⎡xyz⎦⎤for physical units.不是像素为单位,Therefore, it needs to be converted into pixels,It requires the specific physical length of each pixel.Camera is requiredsensor参数.The camera coordinates to pixel coordinates will be introduced below.
- The image coordinate system is converted to the pixel coordinate system M i 2 p M_{i2p} Mi2p
The camera coordinate system needs to be converted to a pixel coordinate system,It involves two issues,
1)Two coordinate system problems.in the image coordinate system,Usually the origin is at the center point.And our traditional pixel display coordinate system origin is the upper left corner of the image,This vector needs to be translated.
2)Unit conversion problem:Need to know how many physical units each pixel has(一般是mm),The physical unit is millimeters.conversion for both,需要知道 d x , d y d_x,d_y dx,dyIndicate how much each column and each row represent, respectivelymm,表示一列的宽度 1 p i x e l = d x m m 1 \space pixel = d_x \space mm 1 pixel=dx mm;一行的宽度 1 p i x e l = d y m m 1 \space pixel = d_y \space mm 1 pixel=dy mm,一般情况下 d x = d y d_x=d_y dx=dy.因此 x x xThe pixels represented by the coordinates are x d x \frac{x}{dx} dxx,相应的 y y yThe pixels represented by the coordinates are y d y \frac{y}{dy} dyy
The two coordinate systems are shown as follows:
The following formula is obtained by transforming the translation coordinate system:
u = x d x + u 0 ( 8 ) u= \frac{x}{d_x}+u_0 \space \space \space (8) u=dxx+u0 (8)
v = y d y + v 0 ( 9 ) v= \frac{y}{d_y}+v_0 \space \space \space (9) v=dyy+v0 (9)
把其中 ( 8 ) , ( 9 ) (8),(9) (8),(9)The formula written in matrix form is obtained as follows:
[ u v 1 ] = [ 1 d x 0 u 0 1 d y 0 v 0 0 0 1 ] [ u v 1 ] \begin{bmatrix} u\\ v \\1 \end{bmatrix}= \begin{bmatrix} \frac{1}{d_x}&0&u_0 \\ \frac{1}{d_y}&0&v_0 \\0&0&1 \end{bmatrix}\begin{bmatrix} u\\ v \\1 \end{bmatrix} ⎣⎡uv1⎦⎤=⎣⎡dx1dy10000u0v01⎦⎤⎣⎡uv1⎦⎤
The matrix formula is obtained as :
M i 2 p = [ 1 d x 0 u 0 1 d y 0 v 0 0 0 1 ] M_{i2p}= \begin{bmatrix} \frac{1}{d_x}&0&u_0 \\ \frac{1}{d_y}&0&v_0 \\0&0&1 \end{bmatrix} Mi2p=⎣⎡dx1dy10000u0v01⎦⎤
Summarize the above two transformations:
M p r o j = [ 1 d x 0 u 0 1 d y 0 v 0 0 0 1 ] [ f 0 0 0 0 f 0 0 0 0 1 0 ] M_{proj}= \begin{bmatrix} \frac{1}{d_x}&0&u_0 \\ \frac{1}{d_y}&0&v_0 \\0&0&1 \end{bmatrix} \begin{bmatrix} f & 0 & 0 & 0 \\ 0 & f & 0 & 0 \\ 0 & 0 & 1 & 0 \end{bmatrix} Mproj=⎣⎡dx1dy10000u0v01⎦⎤⎣⎡f000f0001000⎦⎤
Finally, the formula projection matrix is obtained as :
M p r o j = [ f d x 0 u 0 0 0 f d y v 0 0 0 0 1 0 ] = [ f x 0 u 0 0 0 f x v 0 0 0 0 1 0 ] M_{proj}= \begin{bmatrix} \frac{f}{d_x}& 0 & u_0 & 0 \\ 0 & \frac{f}{d_y} & v_0 & 0 \\ 0 & 0 & 1 & 0 \end{bmatrix} = \begin{bmatrix} f_x & 0 & u_0 & 0 \\ 0 & f_x & v_0 & 0 \\ 0 & 0 & 1 & 0 \end{bmatrix} Mproj=⎣⎢⎡dxf000dyf0u0v01000⎦⎥⎤=⎣⎡fx000fx0u0v01000⎦⎤
Understand relationships through formulas,得到 f x = f d x f_x=\frac{f}{d_x} fx=dxf,它表示focal lengthHow many pixels are there(This is the pixel unit),同理 f y = f d y f_y=\frac{f}{d_y} fy=dyfAlso indicates how many pixels there are,一般情况下 d x = d y d_x=d_y dx=dy,且focal length为一个,上图中的 f f f.
The summary is as follows:
M p r o j = [ f x 0 u 0 0 0 f x v 0 0 0 0 1 0 ] M_{proj}= \begin{bmatrix} f_x & 0 & u_0 & 0 \\ 0 & f_x & v_0 & 0 \\ 0 & 0 & 1 & 0 \end{bmatrix} Mproj=⎣⎡fx000fx0u0v01000⎦⎤
OpenglCoordinate rendering theory
基于相机的OPENGL理论知识.Normally we build with camera parametersproject需要fov,aspect ratio,near,far比较多.Among them we found that there are4parameters in the specific rendering,The parameters needed to be able to render the space accurately,and they are easy to understand,And cut a space,Used for normalization operations,Calculations that are easy to render.For details, please BaidulearnopenglAbout camera settings.It sets about the above4个参数.它的project有两种,Below I will describe how they work:
如果不转化为fov,aspect ratio,near,far,可以直接project矩阵,通过glFrustum完成.但是在学习opengl渲染中,通常使用的gluPerspective,The parameters it uses are fov,aspect ratio,near,far.我们可以直接在shader里面设置project Matrix to get renderedproject矩阵.It is also imitationgluPerspective生成project矩阵, 同时,It can also be done by converting to the above4个,由opengl的gluPerspective帮你生成project矩阵.这两种方法都可以.
1)The parameters of the camera are converted to fov,aspect ratio,near,far这四个参数.
First understand these four parameters:
1)Field of view (FOV), aspect ratio
Aspect ratio is the x/y ratio of the final displayed image
FOV:Indicates the angle of the open field of view,见下图.
2)near,far Indicates the distance of the far and near tangent planes from the origin,See the two parallel planes in Fig.
The relationship can be seen in the figure above:
If you need to render a picture consistent with the image, the picture is represented asimg,We set by imageaspect ratio和FOV
根据定义,ratio is the x/y ratio of the final displayed image,
The calculation formula is as follows:
a s p e c t = i m g . c o l s i m g . r o w s aspect=\frac{img.cols}{img.rows} aspect=img.rowsimg.cols
Bring in the above formula,topcorresponds to halfy轴,That is, in the pixel coordinate systempixelwidth,为
t o p = p i x e l w i d t h 2.0 top = \frac{pixelwidth}{2.0} top=2.0pixelwidth
其中near对应于focal length(像素坐标系)It's in pixel coordinates.单位统一.
The latter is converted into the formula as follows:
F O V = 2.0 ∗ a t a n f ( p i x e l w i d t h 2.0 ∗ f o c a l L e n g t h ) ∗ π 180.0 FOV=2.0*atanf({\frac{pixelwidth}{2.0*focalLength}})*\frac{\pi}{180.0} FOV=2.0∗atanf(2.0∗focalLengthpixelwidth)∗180.0π
对于near 和far The distance can be set as close and as far as possible.
Its matrix is as follows:
设置后,The rendering is the same as the image captured by the camera.
The derivation process is added later.
边栏推荐
- Event Delivery and Responder Chains
- How to create a shortcut without the "shortcut" suffix?
- Test Development Engineer Growth Diary 007 - Bug Priority Definition and Filling Specifications
- GCD的定时器
- 远程连接服务器的MySql
- DHCP principle and configuration
- prometheus监控nacos
- 向量三重积的等式推导证明
- 【Untitled】
- Linx常见目录&文件管理命令&VI编辑器使用 介绍
猜你喜欢

05-Theos

多线程进阶(CountDownLatch,死锁,线程安全集合类)

使用 Grafana 的 Redis Data Source 插件监控 Redis

04-packing and unpacking

Shortcut keys commonly used in the use of Word

Rapidly develop GraphScope graph analysis applications

Interactively compose graphs in GraphScope based on the JupyterLab plugin

The Force Plan Microservices | Centralized Configuration Center Config Asymmetric Encryption and Security Management

CTO说不建议我使用SELECT * ,这是为什么?

how to use xilinx's FFT ip
随机推荐
Multithreading basics (concept, create, interrupt)
引导过程与服务控制
LVM和磁盘配额
Test Development Engineer Growth Diary 003 - Interface Automation Framework Construction
Selenium02
I can't hide it, I want to expose the bad things about cloud native
Mastering JESD204B (1) – Debugging of AD6676
软件测试开发:发送第一封测试报告邮件
MongoDB - Introduction, Data Types, Basic Statements
Interactively compose graphs in GraphScope based on the JupyterLab plugin
Test the basics 02
测试开发工程师成长日记002 - 从0开始做接口自动化
Test Development Engineer Growth Diary 008 - Talking About Some Bugs/Use Case Management Platform/Collaboration Platform
prometheus-tls加密
05-Theos
向量叉乘的几何意义及其模的计算
从安装到编译: 10分钟教你在本地使用和开发GraphScope
B站崩了,如果是你是那晚负责的开发人员你会怎么做?
02-Use of Cycript
Network Protocol 03 - Routing and NAT