gpt4 book ai didi

c++ - 使用高度图扭曲图像?

转载 作者:太空狗 更新时间:2023-10-29 21:55:46 26 4
gpt4 key购买 nike

我有一张图像的高度图,它告诉我每个像素在 Z 方向上的偏移量。我的目标是仅使用它的高度图来展平扭曲的图像。

我该怎么做呢?我知道相机的位置,如果有帮助的话。


为此,我考虑假设每个像素都是平面上的一个点,然后根据我从高度图中获得的 Z 值垂直平移每个点,并从该平移(想象一下您正在从上面看这些点;移动会导致该点从您的角度移动)。

从该投影偏移中,我可以提取每个像素的 X 和 Y 偏移,我可以将其输入 cv.Remap() .

但我不知道如何使用 OpenCV 获得点的投影 3D 偏移,更不用说从中构建偏移图了。


这是我正在做的事情的引用图片:

Calibration Image Warped Image

我知道激光的角度(45 度),根据校准图像,我可以很容易地计算出书的高度:

h(x) = sin(theta) * abs(calibration(x) - actual(x))

我对两条线都这样做,并使用这种方法对两条线进行线性插值以生成一个表面(Python 代码。它在一个循环内):

height_grid[x][y] = heights_top[x] * (cv.GetSize(image)[1] - y) + heights_bottom[x] * y

我希望这对您有所帮助;)


现在,这就是我必须对图像进行去扭曲的方法。中间所有奇怪的东西都会将 3D 坐标转换到相机平面上,给定它的位置(以及相机的位置、旋转等):

class Point:
def __init__(self, x = 0, y = 0, z = 0):
self.x = x
self.y = y
self.z = z

mapX = cv.CreateMat(cv.GetSize(image)[1], cv.GetSize(image)[0], cv.CV_32FC1)
mapY = cv.CreateMat(cv.GetSize(image)[1], cv.GetSize(image)[0], cv.CV_32FC1)

c = Point(CAMERA_POSITION[0], CAMERA_POSITION[1], CAMERA_POSITION[2])
theta = Point(CAMERA_ROTATION[0], CAMERA_ROTATION[1], CAMERA_ROTATION[2])
d = Point()
e = Point(0, 0, CAMERA_POSITION[2] + SENSOR_OFFSET)

costx = cos(theta.x)
costy = cos(theta.y)
costz = cos(theta.z)

sintx = sin(theta.x)
sinty = sin(theta.y)
sintz = sin(theta.z)


for x in xrange(cv.GetSize(image)[0]):
for y in xrange(cv.GetSize(image)[1]):

a = Point(x, y, heights_top[x / 2] * (cv.GetSize(image)[1] - y) + heights_bottom[x / 2] * y)
b = Point()

d.x = costy * (sintz * (a.y - c.y) + costz * (a.x - c.x)) - sinty * (a.z - c.z)
d.y = sintx * (costy * (a.z - c.z) + sinty * (sintz * (a.y - c.y) + costz * (a.x - c.x))) + costx * (costz * (a.y - c.y) - sintz * (a.x - c.x))
d.z = costx * (costy * (a.z - c.z) + sinty * (sintz * (a.y - c.y) + costz * (a.x - c.x))) - sintx * (costz * (a.y - c.y) - sintz * (a.x - c.x))

mapX[y, x] = x + (d.x - e.x) * (e.z / d.z)
mapY[y, x] = y + (d.y - e.y) * (e.z / d.z)


print
print 'Remapping original image using map...'

remapped = cv.CreateImage(cv.GetSize(image), 8, 3)
cv.Remap(image, remapped, mapX, mapY, cv.CV_INTER_LINEAR)

这现在变成了一个巨大的图像和代码线程......无论如何,这个代码块需要我 7 分钟才能在 18MP 相机图像上运行;这方式太长了,最后,这种方法对图像没有任何作用(每个像素的偏移量是 << 1 )。

有什么想法吗?

最佳答案

我最终实现了自己的解决方案:

for x in xrange(cv.GetSize(image)[0]):
for y in xrange(cv.GetSize(image)[1]):

a = Point(x, y, heights_top[x / 2] * (cv.GetSize(image)[1] - y) + heights_bottom[x / 2] * y)
b = Point()

d.x = costy * (sintz * (a.y - c.y) + costz * (a.x - c.x)) - sinty * (a.z - c.z)
d.y = sintx * (costy * (a.z - c.z) + sinty * (sintz * (a.y - c.y) + costz * (a.x - c.x))) + costx * (costz * (a.y - c.y) - sintz * (a.x - c.x))
d.z = costx * (costy * (a.z - c.z) + sinty * (sintz * (a.y - c.y) + costz * (a.x - c.x))) - sintx * (costz * (a.y - c.y) - sintz * (a.x - c.x))

mapX[y, x] = x + 100.0 * (d.x - e.x) * (e.z / d.z)
mapY[y, x] = y + 100.0 * (d.y - e.y) * (e.z / d.z)


print
print 'Remapping original image using map...'

remapped = cv.CreateImage(cv.GetSize(image), 8, 3)
cv.Remap(image, remapped, mapX, mapY, cv.CV_INTER_LINEAR)

这(缓慢地)使用 cv.Remap 函数重新映射每个像素,这似乎有点工作...

关于c++ - 使用高度图扭曲图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5171787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com