【问题标题】:How to perform a linear homography of an image in tensorflow如何在张量流中执行图像的线性单应性
【发布时间】:2020-01-27 14:51:04
【问题描述】:

我希望能够复制 opencv 函数 warpPerspective 的行为,该函数将图像和单应矩阵作为输入,并根据单应矩阵投影图像(更多详细信息:https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html)。

似乎tf.contrib.image.sparse_image_warp 应该完成这项工作,但我无法复制warpPerspective 的行为。尽管使用了参数interpolation_order=1,但我得到的输出以非线性方式失真。

经过进一步研究,我怀疑这是因为tf.contrib.image.interpolate_spline 即使在其阶数为 1 时也不执行线性插值,而是使用了一些 RBF 内核。

除了使用dense_image_warp 对其进行编码之外,我看不到任何解决方法,但这似乎有点矫枉过正,而且可能代价高昂。有人有其他解决方案吗?

【问题讨论】:

    标签: opencv tensorflow image-processing


    【解决方案1】:

    经过一番研究,这里有一个解决方案。它使用tf.contrib.image.dense_image_warp 函数,不是很漂亮,但它仍然有效:

    第一个函数计算执行单应性所需的光流:

        def homography_matrix_to_flow(tf_homography_matrix, im_shape1, im_shape2):
            Y, X = np.meshgrid(range(im_shape1), range(im_shape2))
            Z = np.ones_like(X)
            XYZ = np.stack((X, Y, Z), axis=-1)
            tf_XYZ = tf.constant(XYZ.astype("float64"))
            tf_XYZ = tf_XYZ[tf.newaxis,:,:, :, tf.newaxis]
    
            tf_homography_matrix = tf.tile(tf_homography_matrix[tf.newaxis, tf.newaxis], (1, im_shape2, im_shape1, 1, 1))
            tf_unnormalized_transformed_XYZ = tf.matmul(tf_homography_matrix, tf_XYZ, transpose_b=False)
            tf_transformed_XYZ = tf_unnormalized_transformed_XYZ / tf_unnormalized_transformed_XYZ[:,:,:, -1][:,:,:, tf.newaxis]
            flow = -tf.squeeze(tf_transformed_XYZ-tf_XYZ)[..., :2]
    
            return flow
    

    然后,它用于将原始图像扭曲为扭曲的图像。

    有一个技巧:由于tf.contrib.image.dense_image_warp 函数的工作原理,您需要通过单应矩阵的逆来找到要使用的正确光流。

            homography_matrix = np.array([[-4.86219067e-01, -2.20871298e+00,  4.08214879e+02],
            [-1.02940133e-01, -5.60378659e+00,  3.87573763e+02],
            [-1.35051362e-04, -6.59600583e-03,  2.91244998e-01]])
    
            inv_homography_matrix = np.linalg.inv(homography_matrix)
    
            tf_inv_homography_matrix = tf.constant(inv_homography_matrix)[tf.newaxis]
            flow = homography_matrix_to_flow(tf_inv_homography_matrix, img.shape[1], img.shape[2])[tf.newaxis]
            flow =tf.tile(flow, (self.bs, 1,1,1))
            image_warped = tf.contrib.image.dense_image_warp(tf.transpose(img, (0,2,1,3)), flow)
            image_warped = tf.transpose(image_warped, (0,2,1,3))
    
    

    我仍然希望找到一个更好的答案(不必计算整个流量张量的答案),因此,我暂时不回答这个问题。

    【讨论】:

      猜你喜欢
      • 2017-02-02
      • 1970-01-01
      • 2023-03-15
      • 2021-07-05
      • 2017-11-15
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多