5

我有一张从某个角度拍摄的棋盘图像。现在我想扭曲透视图,使棋盘图像再次看起来好像是直接从上方拍摄的。

我知道我可以尝试在匹配点之间使用“findHomography”,但我想避免它并使用例如来自移动传感器的旋转数据来自己构建单应矩阵。我校准了我的相机以获得内在参数。然后假设以下图像是围绕 x 轴以约 60 度角拍摄的。我认为我所要做的就是将相机矩阵与旋转矩阵相乘以获得单应矩阵。我尝试使用以下代码,但看起来我没有正确理解某些内容,因为它没有按预期工作(结果图像完全是黑色或白色。

在此处输入图像描述

import cv2
import numpy as np
import math 



camera_matrix = np.array([[ 5.7415988502105745e+02, 0., 2.3986181527877352e+02],
                           [0., 5.7473682183375217e+02, 3.1723734404756237e+02], 
                           [0., 0., 1.]])

distortion_coefficients = np.array([ 1.8662919398453856e-01, -7.9649812697463640e-01,
   1.8178068172317731e-03, -2.4296638847737923e-03,
   7.0519002388825025e-01 ])

theta = math.radians(60)

rotx = np.array([[1, 0, 0],
               [0, math.cos(theta), -math.sin(theta)],
               [0, math.sin(theta), math.cos(theta)]])   



homography = np.dot(camera_matrix, rotx)


im = cv2.imread('data/chess1.jpg')
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

im_warped = cv2.warpPerspective(gray, homography, (480, 640), flags=cv2.WARP_INVERSE_MAP)
cv2.imshow('image', im_warped)
cv2.waitKey()
pass

校准后我也有失真系数。如何将这些合并到代码中以改善结果?

4

3 回答 3

2

This answer is awfully late by several years, but here it is ...

(Disclaimer: my use of terminology in this answer may be imprecise or incorrect. Please do look up on this topic from other more credible sources.)


Remember:

  • Because you only have one image (view), you can only compute 2D homography (perspective correspondence between one 2D view and another 2D view), not the full 3D homography.
  • Because of that, the nice intuitive understanding of the 3D homography (rotation matrix, translation matrix, focal distance, etc.) are not available to you.
  • What we say is that with 2D homography you cannot factorize the 3x3 matrix into those nice intuitive components like 3D homography does.
  • You have one matrix - (which is the product of several matrices unknown to you) - and that is it.

However,

OpenCV provides a getPerspectiveTransform function which solves the 3x3 perspective matrix (using homogenous coordinate system) for a 2D homography between two planar quadrilaterals.

Link to documentation

To use this function,

  • Find the four corners of the chessboard on the image. These will be your source coordinates.
  • Supply four rectangle corners of your choice. These will be your destination coordinates.
  • Pass the source coordinates and destination coordinates into the getPerspectiveTransform to generate a 3x3 matrix that is able to dewarp your chessboard to an upright rectangle.

Notes to remember:

  • Mind the ordering of the four corners.

    • If the source coordinates are picked in clockwise order, the destination also needs to be picked in clockwise order.
    • Likewise, if counter-clockwise order is used, do it consistently.
    • Likewise, if z-order (top left, top right, bottom left, bottom right) is used, do it consistently.
    • Failure to order the corners consistently will generate a matrix that executes the point-to-point correspondence exactly (mathematically speaking), but will not generate a usable output image.
  • The aspect ratio of the destination rectangle can be chosen arbitrarily. In fact, it is not possible to deduce the "original aspect ratio" of the object in world coordinates, because "this is 2D homography, not 3D".

于 2014-11-26T17:08:35.063 回答
1

一个问题是,要乘以相机矩阵,您需要一些 az 坐标的概念。在考虑畸变系数之前,您应该首先在给定欧拉角的情况下获得基本的图像变形。查看这个答案以获得更详细的解释,并尝试复制我的结果。将图像沿 z 轴移动然后用相机矩阵投影的想法可能会令人困惑,如果其中任何部分没有意义,请告诉我。

于 2012-10-23T22:41:09.377 回答
0

您不需要校准相机或估计相机方向(但是,后者在这种情况下非常容易:只需找到那些正交线束的消失点,然后取它们的叉积来找到飞机,详见 Hartley & Zisserman 的圣经)。

您唯一需要做的就是估计将棋子映射到正方形的单应性,然后将其应用于图像。

于 2012-10-25T13:43:07.520 回答