我有一张图像的高度图,它告诉我每个像素在 Z 方向上的偏移量。我的目标是仅使用它的高度图来展平扭曲的图像。
我该怎么做呢?我知道相机的位置,如果有帮助的话。
为此,我正在考虑假设每个像素是平面上的一个点,然后根据我从高度图和从该平移中获得的 Z 值垂直平移每个点(假设您正在寻找在上面的点上;移动会导致点从你的角度移动)。
从那个预计的偏移中,我可以提取每个像素的 X 和 Y 偏移,我可以将其输入到cv.Remap()
.
但我不知道如何使用 OpenCV 获得一个点的投影 3D 偏移,更不用说从中构建偏移图了。
这是我正在做的参考图片:
我知道激光的角度(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
。
有任何想法吗?