【发布时间】:2021-06-02 18:10:09
【问题描述】:
我想将图像从球形投影到立方体贴图。根据我学习数学的理解,我需要为每个像素创建一个 theta、phi 分布,然后将其转换为笛卡尔系统以获得归一化的像素图。
我使用下面的代码来做到这一点
theta = 0
phi = np.pi/2
squareLength = 2048
# theta phi distribution for X-positive face
t = np.linspace(theta + np.pi/4, theta - np.pi/4, squareLength)
p = np.linspace(phi + np.pi/4, phi - np.pi/4, squareLength)
x, y = np.meshgrid(t, p)
# converting into cartesion sytem for X-positive face (where r is the distance from sphere center to cube plane and X is constantly 0.5 in cartesian system)
X = np.zeros_like(y)
X[:,:] = 0.5
r = X / (np.cos(x) * np.sin(y))
Y = r * np.sin(x) * np.sin(y)
Z = r * np.cos(y)
XYZ = np.stack((X, Y, Z), axis=2)
# shifting pixels from the negative side
XYZ = XYZ + [0, 0.5, 0.5]
# since i want to project on X-positive face my map should be
x_map = -XYZ[:, :, 1] * squareLength
y_map = XYZ[:,:, 2] * squareLength
上面创建的地图应该会给我我想要的cv2.remap() 的结果,但事实并非如此。然后我尝试循环遍历像素并实现我自己的重映射而不进行插值或外推。经过一番尝试和尝试,我推导出了以下公式,它给了我正确的结果
for i in range(2048):
for j in range(2048):
try:
image[int(y_map[i,j]), int(x_map[i,j])] = im[i, j]
except:
pass
这与实际的cv2 remapping 相反,它说dst(x,y)=src(mapx(x,y),mapy(x,y))
我不明白数学是否全错了,或者有没有办法将x_map 和y_map 转换为更正表格,以便cv2.remap() 给出所需的结果。
【问题讨论】:
标签: python opencv projection remap