yenchenlin / nerf-pytorch

A PyTorch implementation of NeRF (Neural Radiance Fields) that reproduces the results.
MIT License
5.33k stars 1.04k forks source link

Question about ray direction #99

Closed zhshi0816 closed 1 year ago

zhshi0816 commented 1 year ago

Hi,

Thanks for making a great contribution to our PyTorch community. I have a question about the ray direction. The camera projection formulation is as follows: $$u = \frac{x}{z}f + \frac{W}{2}, v = -\frac{y}{z}f + \frac{H}{2}$$

I just reverse the formulation to derive the ray direction as follows: $$[ \frac{u-\frac{W}{2}}{f}z, \hspace{6mm} -\frac{v-\frac{H}{2}}{f}z, \hspace{6mm} z]$$.

Then I set z=-1, which is different from your result. Can anyone show me where I was wrong? How do you calculate the direction?

JunjieLl commented 1 year ago

so have you ever figure out why? I have the same question as yours, would you please give me some response, thanks.

JunjieLl commented 1 year ago

world coordinates $P=(X,Y,Z)$, imaging plane coordinates $P^{'}=(X^{'},Y^{'},Z^{'})$, pixel coordinates $(u,v)$

$$ \frac{Z}{f}=-\frac{X}{X^{'}}=-\frac{Y}{Y^{'}} $$

$$ X^{'}=-f\frac{X}{Z},Y^{'}=-f\frac{Y}{Z} $$

$$ u=X^{'}+c_x,v=Y^{'}+c_y $$

$$ u=-f\frac{X}{Z}+c_x,v=-f\frac{Y}{Z}+c_y $$

$$ X=\frac{c_x-u}{f}Z,Y=\frac{c_y-v}{f}Z $$

$$ P=(\frac{c_x-u}{f}Z,\frac{c_y-v}{f}Z,Z) $$

$$ P=(\frac{u-c_x}{f},\frac{v-c_y}{f},-1),\ where\ Z=-1 $$

Finally, I resolve this issue from here, the axis y is reversed and then the equation matches the code.

chzhan commented 11 months ago

世界坐标磷=(X,和,和),成像平面坐标磷′=(X′,和′,和′),像素坐标(在,在)

和F=−XX′=−和和′

X′=−FX和,和′=−F和和

在=X′+CX,在=和′+C和

在=−FX和+CX,在=−F和和+C和

X=CX−在F和,和=C和−在F和

磷=(CX−在F和,C和−在F和,和)

磷=(在−CXF,在−C和F,−1), 在H这是r这是 和=−1

最后,我从这里解决了这个问题,y轴反转,然后方程与代码匹配。

遇到了同样的问题,感觉你的推理是对的,但是坐标轴是对不上的,我查了下常用的坐标系,像素坐标系x轴和相机坐标系x轴是同向的,y轴是反方向的