使用Python进行白板纠正会引发错误

时间:2015-04-10 10:53:22

标签: python computer-vision

我一直在尝试制作张"矩形的单视图几何 应用于白板图像校正"在Python工作。

Abstract and formula source from Microsoft

这来自堆栈上的另一个问题,但代码是用Sage编写的,不再运行在最新的Sage版本上。大部分话题都是关于是否有可能的辩论。它是,但在Python中工作一直是个问题。

问题:用于计算焦点的公式会引发数学域错误,因为它会尝试获取负数的平方根。我不确定我是否正确地将张的公式转换为Python。没有正确的焦点,在确定矩形的长度与宽度的比率时,它不是非常准确。正确的焦点似乎工作得很好。

修改:这是将在新Sage网站上运行的版本。它没有为帧提出错误,但是它提出了一个焦点和w / h比率,但是:https://cloud.sagemath.com/projects/a8af3fd5-1390-4e80-a12a-13f18f77fdc0/files/Sage Whiteboard rectification.sagews

非常感谢任何帮助。我花了很多时间。

以下代码来自Python 2.7

Source image for the coords in the program

import numpy as np
from numpy import matrix
from numpy import *
from math import sqrt
from math import pow


def FindAspect():

    # CALCULATING THE ASPECT RATIO OF A RECTANGLE DISTORTED BY PERSPECTIVE

    #
    # BIBLIOGRAPHY:
    # [zhang-single]: "Single-View Geometry of A Rectangle
    #  With Application to Whiteboard Image Rectification"
    #  by Zhenggyou Zhang
    #  http://research.microsoft.com/users/zhang/Papers/WhiteboardRectification.pdf

    # pixel coordinates of the 4 corners of the quadrangle (m1, m2, m3, m4)
    # see [zhang-single] figure 1


    # The shape in the image is a perfect square filmed with a focal of 960.

    w = 1920
    h = 1080
    u = 960
    v = 540


    # These coords work, and give a ratio of 1.1499 using the focal formula
    # If you input the focal as 960 then the second part correctly identifies
    # the ratio very accurately. 1.0025

    ##    m1x = 690.196
    ##    m1y = 998.728
    ##
    ##    m2x = 1112.58
    ##    m2y = 798.383
    ##
    ##    m3x = 690.201
    ##    m3y = 81.2587
    ##
    ##    m4x = 1112.6
    ##    m4y = 281.54


    #These coords throw an error due to focal formula sqrt of negative number.

    m1x = 849.029
    m1y = 792.363

    m2x = 1170.147
    m2y = 1019.212

    m3x = 849.033
    m3y = 287.516

    m4x = 1170.154
    m4y = 60.757


    # pixel coordinates of the principal point of the image
    # for a normal camera this will be the center of the image,
    # i.e. u0=IMAGEWIDTH/2; v0 =IMAGEHEIGHT/2
    # This assumption does not hold if the image has been cropped asymmetrically
    u0 = u
    v0 = v

    # pixel aspect ratio; for a normal camera pixels are square, so s=1
    s = 1

    # homogenous coordinates of the quadrangle
    m1 = array([m1x,m1y,1])
    m2 = array([m2x,m2y,1])
    m3 = array([m3x,m3y,1])
    m4 = array([m4x,m4y,1])


    # The following equations are later used in calculating the the focal length
    # and the rectangle's aspect ratio.

    # see [zhang-single] Equation 11, 12
    k2 = np.dot(np.cross(m1, m4), m3) / np.dot(np.cross(m2, m4), m3)
    k3 = np.dot(np.cross(m1, m4), m2) / np.dot(np.cross(m3, m4), m2)


    # see [zhang-single] Equation 14,16
    n2 = k2 * m2 - m1
    n3 = k3 * m3 - m1


    # the focal length of the camera.



    f = sqrt(
             -1 / (
              n2[2]*n3[2]*s**2
             ) * (
              (
               n2[0]*n3[0] - (n2[0]*n3[2]+n2[2]*n3[0])*u0 + n2[2]*n3[2] * u0**2
              )*s**2 + (
               n2[1]*n3[1] - (n2[1]*n3[2]+n2[2]*n3[1])*v0 + n2[2]*n3[2] * v0**2
              )
             )
            )

    print 'Focal: ', f

    ##    f = 960


    # standard pinhole camera matrix
    # see [zhang-single] Equation 1

    K = np.matrix([[f,0,u0],[0,f,v0],[0,0,1]])


    #the width/height ratio of the original rectangle
    # see [zhang-single] Equation 20

    whRatio = sqrt(
                   (np.dot(np.asarray(n2 * (K.T**-1)).reshape(-1), np.asarray((K**-1).dot(n2)).reshape(-1))) /
                   (np.dot(np.asarray(n3 * (K.T**-1)).reshape(-1), np.asarray((K**-1).dot(n3)).reshape(-1)))
                  )

    print whRatio



FindAspect()

0 个答案:

没有答案