透视变形矩形的比例

时间:2009-07-28 14:09:08

标签: image-processing geometry computer-vision reverseprojection projective-geometry

给出了由透视图扭曲的矩形的2d图片:

enter image description here

我知道这个形状最初是一个矩形,但我不知道它的原始大小。

如果我知道这张照片中角落的像素坐标,我该如何计算原始比例,即矩形的商(宽度/高度)?

(背景:目标是自动取消矩形文档的照片,边缘检测可能会用hough变换完成)

更新:

已经讨论过是否有可能根据给出的信息确定宽度:高度比。我天真的想法是它必须是可能的,因为我认为没有办法将例如1:4的矩形投射到上面描绘的四边形上。该比率显然接近1:1,因此应该有一种方法可以用数学方法确定它。然而,除了我的直觉猜测,我没有证据证明这一点。

我还没有完全理解下面提出的论点,但我认为必须有一些隐含的假设,即我们在这里缺失并且解释不同。

然而,经过几个小时的搜索,我终于找到了一些与问题相关的论文。 我很难理解那里使用的数学,到目前为止还没有成功。特别是第一篇论文似乎准确地讨论了我想要做的事情,遗憾的是没有代码示例和非常密集的数学。

  • 张正友,何立伟,“白板扫描和图像增强” http://research.microsoft.com/en-us/um/people/zhang/papers/tr03-39.pdf第11页

      

    “由于透视失真,矩形的图像看起来是四边形。但是,由于我们知道它是空间中的矩形,我们能够估计相机的焦距和矩形的纵横比。 “

  • ROBERT M. HARALICK“从矩形的透视投影中确定相机参数” http://portal.acm.org/citation.cfm?id=87146

      

    “我们将展示如何使用3D空间中未知大小和位置的矩形的2D透视投影来确定相对于矩形平面图的相机视角参数。”

9 个答案:

答案 0 :(得分:28)

这是我在阅读论文后试图回答我的问题

我在SAGE中操纵了方程式一段时间,并以c风格提出了这个伪代码:


// in case it matters: licensed under GPLv2 or later
// legend:
// sqr(x)  = x*x
// sqrt(x) = square root of x

// let m1x,m1y ... m4x,m4y be the (x,y) pixel coordinates
// of the 4 corners of the detected quadrangle
// i.e. (m1x, m1y) are the cordinates of the first corner, 
// (m2x, m2y) of the second corner and so on.
// let u0, v0 be the 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

// first, transform the image so the principal point is at (0,0)
// this makes the following equations much easier
m1x = m1x - u0;
m1y = m1y - v0;
m2x = m2x - u0;
m2y = m2y - v0;
m3x = m3x - u0;
m3y = m3y - v0;
m4x = m4x - u0;
m4y = m4y - v0;


// temporary variables k2, k3
double k2 = ((m1y - m4y)*m3x - (m1x - m4x)*m3y + m1x*m4y - m1y*m4x) /
            ((m2y - m4y)*m3x - (m2x - m4x)*m3y + m2x*m4y - m2y*m4x) ;

double k3 = ((m1y - m4y)*m2x - (m1x - m4x)*m2y + m1x*m4y - m1y*m4x) / 
            ((m3y - m4y)*m2x - (m3x - m4x)*m2y + m3x*m4y - m3y*m4x) ;

// f_squared is the focal length of the camera, squared
// if k2==1 OR k3==1 then this equation is not solvable
// if the focal length is known, then this equation is not needed
// in that case assign f_squared= sqr(focal_length)
double f_squared = 
    -((k3*m3y - m1y)*(k2*m2y - m1y) + (k3*m3x - m1x)*(k2*m2x - m1x)) / 
                      ((k3 - 1)*(k2 - 1)) ;

//The width/height ratio of the original rectangle
double whRatio = sqrt( 
    (sqr(k2 - 1) + sqr(k2*m2y - m1y)/f_squared + sqr(k2*m2x - m1x)/f_squared) /
    (sqr(k3 - 1) + sqr(k3*m3y - m1y)/f_squared + sqr(k3*m3x - m1x)/f_squared) 
) ;

// if k2==1 AND k3==1, then the focal length equation is not solvable 
// but the focal length is not needed to calculate the ratio.
// I am still trying to figure out under which circumstances k2 and k3 become 1
// but it seems to be when the rectangle is not distorted by perspective, 
// i.e. viewed straight on. Then the equation is obvious:
if (k2==1 && k3==1) whRatio = sqrt( 
    (sqr(m2y-m1y) + sqr(m2x-m1x)) / 
    (sqr(m3y-m1y) + sqr(m3x-m1x))


// After testing, I found that the above equations 
// actually give the height/width ratio of the rectangle, 
// not the width/height ratio. 
// If someone can find the error that caused this, 
// I would be most grateful.
// until then:
whRatio = 1/whRatio;

更新:这里是如何确定这些方程:

以下是SAGE中的代码。 它可以在http://www.sagenb.org/home/pub/704/在线访问。 (Sage在求解方程式时非常有用,并且可以在任何浏览器中使用,请查看)

# 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
m1x = var('m1x')
m1y = var('m1y')
m2x = var('m2x')
m2y = var('m2y')
m3x = var('m3x')
m3y = var('m3y')
m4x = var('m4x')
m4y = var('m4y')

# 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 = var('u0')
v0 = var('v0')

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

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


# the following equations are later used in calculating the the focal length 
# and the rectangle's aspect ratio.
# temporary variables: k2, k3, n2, n3

# see [zhang-single] Equation 11, 12
k2_ = m1.cross_product(m4).dot_product(m3) / m2.cross_product(m4).dot_product(m3)
k3_ = m1.cross_product(m4).dot_product(m2) / m3.cross_product(m4).dot_product(m2)
k2 = var('k2')
k3 = var('k3')

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


# the focal length of the camera.
f = var('f')
# see [zhang-single] Equation 21
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
          ) 
         ) 
        )


# standard pinhole camera matrix
# see [zhang-single] Equation 1
A = matrix([[f,0,u0],[0,s*f,v0],[0,0,1]])


#the width/height ratio of the original rectangle
# see [zhang-single] Equation 20
whRatio = sqrt (
               (n2*A.transpose()^(-1) * A^(-1)*n2.transpose()) / 
               (n3*A.transpose()^(-1) * A^(-1)*n3.transpose())
              ) 

c代码中的简化方程式由

确定
print "simplified equations, assuming u0=0, v0=0, s=1"
print "k2 := ", k2_
print "k3 := ", k3_
print "f  := ", f_(u0=0,v0=0,s=1)
print "whRatio := ", whRatio(u0=0,v0=0,s=1)

    simplified equations, assuming u0=0, v0=0, s=1
    k2 :=  ((m1y - m4y)*m3x - (m1x - m4x)*m3y + m1x*m4y - m1y*m4x)/((m2y
    - m4y)*m3x - (m2x - m4x)*m3y + m2x*m4y - m2y*m4x)
    k3 :=  ((m1y - m4y)*m2x - (m1x - m4x)*m2y + m1x*m4y - m1y*m4x)/((m3y
    - m4y)*m2x - (m3x - m4x)*m2y + m3x*m4y - m3y*m4x)
    f  :=  sqrt(-((k3*m3y - m1y)*(k2*m2y - m1y) + (k3*m3x - m1x)*(k2*m2x
    - m1x))/((k3 - 1)*(k2 - 1)))
    whRatio :=  sqrt(((k2 - 1)^2 + (k2*m2y - m1y)^2/f^2 + (k2*m2x -
    m1x)^2/f^2)/((k3 - 1)^2 + (k3*m3y - m1y)^2/f^2 + (k3*m3x -
    m1x)^2/f^2))

print "Everything in one equation:"
print "whRatio := ", whRatio(f=f_)(k2=k2_,k3=k3_)(u0=0,v0=0,s=1)

    Everything in one equation:
    whRatio :=  sqrt(((((m1y - m4y)*m2x - (m1x - m4x)*m2y + m1x*m4y -
    m1y*m4x)/((m3y - m4y)*m2x - (m3x - m4x)*m2y + m3x*m4y - m3y*m4x) -
    1)*(((m1y - m4y)*m3x - (m1x - m4x)*m3y + m1x*m4y - m1y*m4x)/((m2y -
    m4y)*m3x - (m2x - m4x)*m3y + m2x*m4y - m2y*m4x) - 1)*(((m1y -
    m4y)*m3x - (m1x - m4x)*m3y + m1x*m4y - m1y*m4x)*m2y/((m2y - m4y)*m3x
    - (m2x - m4x)*m3y + m2x*m4y - m2y*m4x) - m1y)^2/((((m1y - m4y)*m2x -
    (m1x - m4x)*m2y + m1x*m4y - m1y*m4x)*m3y/((m3y - m4y)*m2x - (m3x -
    m4x)*m2y + m3x*m4y - m3y*m4x) - m1y)*(((m1y - m4y)*m3x - (m1x -
    m4x)*m3y + m1x*m4y - m1y*m4x)*m2y/((m2y - m4y)*m3x - (m2x - m4x)*m3y
    + m2x*m4y - m2y*m4x) - m1y) + (((m1y - m4y)*m2x - (m1x - m4x)*m2y +
    m1x*m4y - m1y*m4x)*m3x/((m3y - m4y)*m2x - (m3x - m4x)*m2y + m3x*m4y
    - m3y*m4x) - m1x)*(((m1y - m4y)*m3x - (m1x - m4x)*m3y + m1x*m4y -
    m1y*m4x)*m2x/((m2y - m4y)*m3x - (m2x - m4x)*m3y + m2x*m4y - m2y*m4x)
    - m1x)) + (((m1y - m4y)*m2x - (m1x - m4x)*m2y + m1x*m4y -
    m1y*m4x)/((m3y - m4y)*m2x - (m3x - m4x)*m2y + m3x*m4y - m3y*m4x) -
    1)*(((m1y - m4y)*m3x - (m1x - m4x)*m3y + m1x*m4y - m1y*m4x)/((m2y -
    m4y)*m3x - (m2x - m4x)*m3y + m2x*m4y - m2y*m4x) - 1)*(((m1y -
    m4y)*m3x - (m1x - m4x)*m3y + m1x*m4y - m1y*m4x)*m2x/((m2y - m4y)*m3x
    - (m2x - m4x)*m3y + m2x*m4y - m2y*m4x) - m1x)^2/((((m1y - m4y)*m2x -
    (m1x - m4x)*m2y + m1x*m4y - m1y*m4x)*m3y/((m3y - m4y)*m2x - (m3x -
    m4x)*m2y + m3x*m4y - m3y*m4x) - m1y)*(((m1y - m4y)*m3x - (m1x -
    m4x)*m3y + m1x*m4y - m1y*m4x)*m2y/((m2y - m4y)*m3x - (m2x - m4x)*m3y
    + m2x*m4y - m2y*m4x) - m1y) + (((m1y - m4y)*m2x - (m1x - m4x)*m2y +
    m1x*m4y - m1y*m4x)*m3x/((m3y - m4y)*m2x - (m3x - m4x)*m2y + m3x*m4y
    - m3y*m4x) - m1x)*(((m1y - m4y)*m3x - (m1x - m4x)*m3y + m1x*m4y -
    m1y*m4x)*m2x/((m2y - m4y)*m3x - (m2x - m4x)*m3y + m2x*m4y - m2y*m4x)
    - m1x)) - (((m1y - m4y)*m3x - (m1x - m4x)*m3y + m1x*m4y -
    m1y*m4x)/((m2y - m4y)*m3x - (m2x - m4x)*m3y + m2x*m4y - m2y*m4x) -
    1)^2)/((((m1y - m4y)*m2x - (m1x - m4x)*m2y + m1x*m4y -
    m1y*m4x)/((m3y - m4y)*m2x - (m3x - m4x)*m2y + m3x*m4y - m3y*m4x) -
    1)*(((m1y - m4y)*m3x - (m1x - m4x)*m3y + m1x*m4y - m1y*m4x)/((m2y -
    m4y)*m3x - (m2x - m4x)*m3y + m2x*m4y - m2y*m4x) - 1)*(((m1y -
    m4y)*m2x - (m1x - m4x)*m2y + m1x*m4y - m1y*m4x)*m3y/((m3y - m4y)*m2x
    - (m3x - m4x)*m2y + m3x*m4y - m3y*m4x) - m1y)^2/((((m1y - m4y)*m2x -
    (m1x - m4x)*m2y + m1x*m4y - m1y*m4x)*m3y/((m3y - m4y)*m2x - (m3x -
    m4x)*m2y + m3x*m4y - m3y*m4x) - m1y)*(((m1y - m4y)*m3x - (m1x -
    m4x)*m3y + m1x*m4y - m1y*m4x)*m2y/((m2y - m4y)*m3x - (m2x - m4x)*m3y
    + m2x*m4y - m2y*m4x) - m1y) + (((m1y - m4y)*m2x - (m1x - m4x)*m2y +
    m1x*m4y - m1y*m4x)*m3x/((m3y - m4y)*m2x - (m3x - m4x)*m2y + m3x*m4y
    - m3y*m4x) - m1x)*(((m1y - m4y)*m3x - (m1x - m4x)*m3y + m1x*m4y -
    m1y*m4x)*m2x/((m2y - m4y)*m3x - (m2x - m4x)*m3y + m2x*m4y - m2y*m4x)
    - m1x)) + (((m1y - m4y)*m2x - (m1x - m4x)*m2y + m1x*m4y -
    m1y*m4x)/((m3y - m4y)*m2x - (m3x - m4x)*m2y + m3x*m4y - m3y*m4x) -
    1)*(((m1y - m4y)*m3x - (m1x - m4x)*m3y + m1x*m4y - m1y*m4x)/((m2y -
    m4y)*m3x - (m2x - m4x)*m3y + m2x*m4y - m2y*m4x) - 1)*(((m1y -
    m4y)*m2x - (m1x - m4x)*m2y + m1x*m4y - m1y*m4x)*m3x/((m3y - m4y)*m2x
    - (m3x - m4x)*m2y + m3x*m4y - m3y*m4x) - m1x)^2/((((m1y - m4y)*m2x -
    (m1x - m4x)*m2y + m1x*m4y - m1y*m4x)*m3y/((m3y - m4y)*m2x - (m3x -
    m4x)*m2y + m3x*m4y - m3y*m4x) - m1y)*(((m1y - m4y)*m3x - (m1x -
    m4x)*m3y + m1x*m4y - m1y*m4x)*m2y/((m2y - m4y)*m3x - (m2x - m4x)*m3y
    + m2x*m4y - m2y*m4x) - m1y) + (((m1y - m4y)*m2x - (m1x - m4x)*m2y +
    m1x*m4y - m1y*m4x)*m3x/((m3y - m4y)*m2x - (m3x - m4x)*m2y + m3x*m4y
    - m3y*m4x) - m1x)*(((m1y - m4y)*m3x - (m1x - m4x)*m3y + m1x*m4y -
    m1y*m4x)*m2x/((m2y - m4y)*m3x - (m2x - m4x)*m3y + m2x*m4y - m2y*m4x)
    - m1x)) - (((m1y - m4y)*m2x - (m1x - m4x)*m2y + m1x*m4y -
    m1y*m4x)/((m3y - m4y)*m2x - (m3x - m4x)*m2y + m3x*m4y - m3y*m4x) -
    1)^2))


# some testing:
# - choose a random rectangle, 
# - project it onto a random plane,
# - insert the corners in the above equations,
# - check if the aspect ratio is correct.

from sage.plot.plot3d.transform import rotate_arbitrary

#redundandly random rotation matrix
rand_rotMatrix = \
           rotate_arbitrary((uniform(-5,5),uniform(-5,5),uniform(-5,5)),uniform(-5,5)) *\
           rotate_arbitrary((uniform(-5,5),uniform(-5,5),uniform(-5,5)),uniform(-5,5)) *\
           rotate_arbitrary((uniform(-5,5),uniform(-5,5),uniform(-5,5)),uniform(-5,5))

#random translation vector
rand_transVector = vector((uniform(-10,10),uniform(-10,10),uniform(-10,10))).transpose()

#random rectangle parameters
rand_width =uniform(0.1,10)
rand_height=uniform(0.1,10)
rand_left  =uniform(-10,10)
rand_top   =uniform(-10,10)

#random focal length and principal point
rand_f  = uniform(0.1,100)
rand_u0 = uniform(-100,100)
rand_v0 = uniform(-100,100)

# homogenous standard pinhole projection, see [zhang-single] Equation 1
hom_projection = A * rand_rotMatrix.augment(rand_transVector)

# construct a random rectangle in the plane z=0, then project it randomly 
rand_m1hom = hom_projection*vector((rand_left           ,rand_top            ,0,1)).transpose()
rand_m2hom = hom_projection*vector((rand_left           ,rand_top+rand_height,0,1)).transpose()
rand_m3hom = hom_projection*vector((rand_left+rand_width,rand_top            ,0,1)).transpose()
rand_m4hom = hom_projection*vector((rand_left+rand_width,rand_top+rand_height,0,1)).transpose()

#change type from 1x3 matrix to vector
rand_m1hom = rand_m1hom.column(0)
rand_m2hom = rand_m2hom.column(0)
rand_m3hom = rand_m3hom.column(0)
rand_m4hom = rand_m4hom.column(0)

#normalize
rand_m1hom = rand_m1hom/rand_m1hom[2]
rand_m2hom = rand_m2hom/rand_m2hom[2]
rand_m3hom = rand_m3hom/rand_m3hom[2]
rand_m4hom = rand_m4hom/rand_m4hom[2]

#substitute random values for f, u0, v0
rand_m1hom = rand_m1hom(f=rand_f,s=1,u0=rand_u0,v0=rand_v0)
rand_m2hom = rand_m2hom(f=rand_f,s=1,u0=rand_u0,v0=rand_v0)
rand_m3hom = rand_m3hom(f=rand_f,s=1,u0=rand_u0,v0=rand_v0)
rand_m4hom = rand_m4hom(f=rand_f,s=1,u0=rand_u0,v0=rand_v0)

# printing the randomly choosen values
print "ground truth: f=", rand_f, "; ratio=", rand_width/rand_height

# substitute all the variables in the equations:
print "calculated: f= ",\
f_(k2=k2_,k3=k3_)(s=1,u0=rand_u0,v0=rand_v0)(
  m1x=rand_m1hom[0],m1y=rand_m1hom[1],
  m2x=rand_m2hom[0],m2y=rand_m2hom[1],
  m3x=rand_m3hom[0],m3y=rand_m3hom[1],
  m4x=rand_m4hom[0],m4y=rand_m4hom[1],
),"; 1/ratio=", \
1/whRatio(f=f_)(k2=k2_,k3=k3_)(s=1,u0=rand_u0,v0=rand_v0)(
  m1x=rand_m1hom[0],m1y=rand_m1hom[1],
  m2x=rand_m2hom[0],m2y=rand_m2hom[1],
  m3x=rand_m3hom[0],m3y=rand_m3hom[1],
  m4x=rand_m4hom[0],m4y=rand_m4hom[1],
)

print "k2 = ", k2_(
  m1x=rand_m1hom[0],m1y=rand_m1hom[1],
  m2x=rand_m2hom[0],m2y=rand_m2hom[1],
  m3x=rand_m3hom[0],m3y=rand_m3hom[1],
  m4x=rand_m4hom[0],m4y=rand_m4hom[1],
), "; k3 = ", k3_(
  m1x=rand_m1hom[0],m1y=rand_m1hom[1],
  m2x=rand_m2hom[0],m2y=rand_m2hom[1],
  m3x=rand_m3hom[0],m3y=rand_m3hom[1],
  m4x=rand_m4hom[0],m4y=rand_m4hom[1],
)

# ATTENTION: testing revealed, that the whRatio 
# is actually the height/width ratio, 
# not the width/height ratio
# This contradicts [zhang-single]
# if anyone can find the error that caused this, I'd be grateful

    ground truth: f= 72.1045134124554 ; ratio= 3.46538779959142
    calculated: f=  72.1045134125 ; 1/ratio= 3.46538779959
    k2 =  0.99114614987 ; k3 =  1.57376280159

答案 1 :(得分:7)

<强>更新

阅读完更新后,查看第一个参考(白板扫描和图像增强),我看到缺失点在哪里。

问题的输入数据是投影图像的中心O的四倍(A,B,C,D), AND 。在文章中,它对应于假设u0 = v0 = 0。添加这一点,问题变得足以获得矩形的宽高比。

然后重新解决问题如下:给定Z = 0平面中的四倍(A,B,C,D),找到眼睛位置E(0,0,h),h> 0和3D平面P使得(A,B,C,D)在P上的投影是矩形。

注意P由E:确定以得到平行四边形,P必须包含与(EU)和(EV)的平行,其中U =(AB)x(CD)和V =(AD)x(BC)。

实验上,似乎这个问题通常有一个独特的解决方案,对应于矩形的w / h比的唯一值。

alt text alt text

上一篇文章

不,您无法从投影中确定矩形比率。

在一般情况下,Z = 0平面的四个非共线点的四倍(A,B,C,D)是无限多个矩形的投影,具有无限多的宽/高比。

考虑两个消失点U,(AB)和(CD)和V的交点,(AD)和(BC)的交点,以及点I,两个对角线(AC)和(BD)的交点。要投影为ABCD,中心I的平行四边形必须位于包含平行于(UV)到点I的线的平面上。在一个这样的平面上,您可以找到许多投影到ABCD的矩形,所有矩形都具有不同的w / h比率。

查看使用Cabri 3D完成的这两张图片。在这两种情况下,ABCD不变(在灰色Z = 0平面上),并且包含矩形的蓝色平面也不改变。部分隐藏的绿线是(UV)线,可见的绿线与它平行并包含I。

alt text alt text

答案 2 :(得分:1)

尺寸不是真的需要,也不是比例。考虑到他正在使用文件的照片/扫描,并知道哪一方面是不相关的。我怀疑他会扫描它们的背面。

“角落交叉点”是纠正透视的方法。这可能有所帮助:

How to draw a Perspective-Correct Grid in 2D

答案 3 :(得分:1)

关于为什么结果给出h / w而不是w / h的问题: 我想知道上面的公式20的表达式是否正确。 发表是:

       whRatio = sqrt (
            (n2*A.transpose()^(-1) * A^(-1)*n2.transpose()) / 
            (n3*A.transpose()^(-1) * A^(-1)*n3.transpose())
           ) 

当我尝试使用OpenCV执行该操作时,我得到一个例外。但是当我使用下面的等式时,一切都正常,这对我看起来更像是等式20: 但基于公式20,它看起来应该是:

        whRatio = sqrt (
            (n2.transpose()*A.transpose()^(-1) * A^(-1)*n2) /
            (n3.transpose()*A.transpose()^(-1) * A^(-1)*n3)
           )

答案 4 :(得分:1)

您可以通过此回答Calculating rectangle 3D coordinate with coordinate its shadow?确定宽度/高度。假设您的矩形在交叉点对角线上旋转,计算它的宽度和高度。但是当你改变假设阴影平面到真​​实阴影平面之间的距离时,矩形的比例与计算出的宽度/高度是一样的!

答案 5 :(得分:0)

如果不知道“相机”的距离,就不可能知道这个矩形的宽度。

从5厘米处看到的一个小矩形与从几米远处看到的巨大矩形相同

答案 6 :(得分:0)

您需要更多信息,转换的数字可能来自任意视角的任何平行四边形。

所以我猜你需要先做一些校准。

编辑:对于那些说我错了的人,这里有数学证据证明有无限组合的矩形/相机可以产生同样的投影:

为了简化问题(因为我们只需要边的比例),我们假设我们的矩形由以下几点定义:R=[(0,0),(1,0),(1,r),(0,r)](这种简化与将任何问题转换为等价物相同一个在仿射空间里。)

变换后的多边形定义为:T=[(tx0,ty0),(tx1,ty1),(tx2,ty2),(tx3,ty3)]

存在满足M = [[m00,m01,m02],[m10,m11,m12],[m20,m21,m22]]

的转换矩阵(Rxi,Ryi,1)*M=wi(txi,tyi,1)'

如果我们扩展上面的等式,

我们得到R_0

m02-tx0*w0 = m12-ty0*w0 = m22-w0 = 0

我们得到R_1

m00-tx1*w1 = m10-ty1*w1 = m20+m22-w1 = 0

我们得到R_2

m00+r*m01-tx2*w2 = m10+r*m11-ty2*w2 = m20+r*m21+m22-w2 = 0

R_3我们得到:m00+r*m01-tx3*w3 = m10+r*m11-ty3*w3 = m20 + r*m21 + m22 -w3 = 0

到目前为止,我们有12个方程,14个未知变量(9个来自矩阵,4个来自wi,1个来自比率r),其余是已知值({{1} }和txi被给出。

即使系统没有被指定,一些未知数在它们之间成倍增加(tyir产品)使系统非线性(你可以将它转换为线性系统分配一个每个产品的新名称,但你仍然会有13个未知数,其中3个被扩展为无限解。)

如果您在推理或数学方面有任何缺陷,请告诉我。

答案 7 :(得分:0)

绘制一个右等腰三角形,这两个消失点和地平线下方的第三个点(即与矩形相同的地平线侧)。第三点将是我们的起源,消失点的两条线将是我们的轴。调用从原点到消失点pi / 2的距离。现在将矩形的边从消失点延伸到轴,并标记它们与轴相交的位置。选择一个轴,测量从两个标记到原点的距离,转换这些距离:x-> tan(x),差值将是该边的“真实”长度。对另一个轴执行相同操作。取这两个长度的比例,你就完成了。

答案 8 :(得分:0)

Dropbox在他们的科技博客上有一篇广泛的文章,他们描述了他们如何解决扫描仪应用程序的问题。

https://blogs.dropbox.com/tech/2016/08/fast-document-rectification-and-enhancement/

  

整理文件

     

我们假设输入文档在物理世界中是矩形的,但如果它不完全面向相机,则图像中产生的角将是一般的凸四边形。因此,为了满足我们的第一个目标,我们必须撤消捕获过程所应用的几何变换。除了相机的焦距(内在参数)之外,这种变换还取决于相机相对于文档的视点(这些是所谓的外部参数)。以下是捕获方案的图表:

     

为了撤消几何变换,我们必须首先确定所述参数。如果我们假设一个很好的对称相机(没有散光,没有歪斜,等等),这个模型中的未知数是:

     
      
  • 相机相对于文档的3D位置(3个自由度),
  •   
  • 相机相对于文档的3D方向(3个自由度),
  •   
  • 文档的尺寸(2个自由度)和
  •   
  • 相机的焦距(1自由度)。
  •   
     

另一方面,四个检测到的文档角的x坐标和y坐标有效地给出了八个约束条件。虽然看似更多的未知数(9)而不是约束条件(8),但未知数并非完全自由变量 - 人们可以想象物理地缩放文档并将其放置在远离相机的位置,以获得相同的照片。这种关系会产生额外的约束,因此我们需要一个完全约束的系统来解决。 (我们解决的实际方程组涉及其他一些考虑因素;相关的维基百科文章给出了一个很好的总结:https://en.wikipedia.org/wiki/Camera_resectioning

     

参数恢复后,我们可以撤消捕获过程应用的几何变换,以获得漂亮的矩形图像。然而,这可能是一个耗时的过程:对于每个输出像素,可以查找源图像中相应输入像素的值。当然,GPU专门设计用于这样的任务:在虚拟空间中渲染纹理。存在视图变换 - 恰好是我们刚刚解决的相机变换的反转! - 用于渲染完整的输入图像并获得经过校正的文档。 (一种简单的方法是注意,一旦在手机屏幕上显示完整的输入图像,您就可以倾斜和翻译手机,使屏幕上文档区域的投影看起来是直线的。)< / p>      

最后,请回想一下,在规模方面存在模糊性:我们无法判断该文件是信纸大小的纸张(8.5“x 11”)还是海报板(17“x 22”),实例。输出图像的尺寸应该是多少?为解决这种不确定性,我们计算输入图像中四边形内的像素数,并将输出分辨率设置为与此像素数相匹配。我们的想法是,我们不想过多地对图像进行上采样或下采样。<​​/ p>