【发布时间】:2022-01-15 13:13:30
【问题描述】:
给定一个输入图像和单应矩阵,我想在转换后得到一个输出图像。
这是ndimage中的内置函数:
im = np.array(Image.open('lena.jpg').convert('L'))
H = np.array([[1.4,0.05,-100],[0.05,1.5,-100],[0,0,1]])
im2 = ndimage.affine_transform(im, H[:2,:2], (H[0,2], H[1, 2]))
imshow(im)
imshow(im2)
对于原始图像,我看到了这个:
对于 ndimage 转换后的 im2,我看到了这个:
现在我想编写一个只使用 python 和 numpy 库的代码来自己做这个单应性。这是我写的代码:
left, up = 0, 0
right, down = im.shape[1], im.shape[0]
# define the homography operation
def get_point_coor(x, y, H):
input = np.array(([x], [y], [1]))
output = np.dot(H, input)
return int(output[0]), int(output[1])
# after transformation the image size might be different from the original one,
# we need to find the new size
height_max = max(get_point_coor(left, up, H)[0], get_point_coor(left, down, H)[0], get_point_coor(right, up, H)[0], get_point_coor(right, up, H)[0])
width_max = max(get_point_coor(left, up, H)[1], get_point_coor(left, down, H)[1], get_point_coor(right, up, H)[1], get_point_coor(right, up, H)[1])
height_min = min(get_point_coor(left, up, H)[0], get_point_coor(left, down, H)[0], get_point_coor(right, up, H)[0], get_point_coor(right, up, H)[0])
width_min = min(get_point_coor(left, up, H)[1], get_point_coor(left, down, H)[1], get_point_coor(right, up, H)[1], get_point_coor(right, up, H)[1])
# can ignore this 50 now. The new_height without 50 should be able to be the new boundary
# , but somehow it is not, so I add a random big number (50) for ploting.
new_height = abs(height_max) + abs(height_min)+50
new_width = abs(width_max) + abs(width_min)+50
new_image = np.zeros((new_height, new_width))
# start the main
for row in range(im.shape[0]):
for col in range(im.shape[1]):
new_row, new_col = get_point_coor(row, col, H)
new_col += abs(width_min)
new_row += abs(height_min)
new_image[new_row, new_col] = im[row][col]
imshow(new_image)
我得到的结果是这样的:
方向、颜色和大小看起来都与 ndimage 非常不同。实现这个单应性的正确方法是什么?
【问题讨论】:
-
反转单应性。然后,对于每个结果像素位置,将其粘贴在倒置矩阵中(如果它是真正的单应性而不仅仅是仿射,您还需要除以“w”坐标,以便得到
(?, ?, 1)) ,然后您将获得需要采样的 source 中的像素位置。 ——你做错了。你不能 push 像素。这会留下间隙和其他令人讨厌的伪影。 -
您在图像中看到的是
scipy.ndimage.affine_transform不 反转矩阵。它假设矩阵已经反转(即向后)。始终阅读您正在使用的文档,并努力理解所有内容:docs.scipy.org/doc/scipy/reference/generated/… -
另外,您的最小/最大内容会删除结果中矩阵的翻译分量,因此您看不到它的效果...
标签: image image-processing scikit-image homography