【问题标题】:Python Opencv SolvePnP yields wrong translation vectorPython Opencv SolvePnP 产生错误的翻译向量
【发布时间】:2013-01-09 00:38:48
【问题描述】:

我正在尝试使用单应性在 Blender 3d 中校准和查找单个虚拟相机的位置和旋转。我正在使用 Blender,以便在进入更困难的现实世界之前仔细检查我的结果。

我在我的固定相机的视野中渲染了十张棋盘在不同位置和旋转的图片。在 OpenCV 的 Python 中,我使用 cv2.calibrateCamera 从十幅图像中检测到的棋盘角中找到内在矩阵,然后在 cv2.solvePnP 中使用它来找到外在参数(平移和旋转)。

然而,虽然估计的参数接近实际参数,但还是有一些可疑之处。我对翻译的初步估计是(-0.11205481,-0.0490256,8.13892491)。实际位置是(0,0,8.07105)。很接近吧?

但是,当我稍微移动和旋转相机并重新渲染图像时,估计的平移变得更远了。估计:(-0.15933154,0.13367286,9.34058867)。实际:(-1.7918,-1.51073,9.76597)。 Z 值接近,但 X 和 Y 不接近。

我完全糊涂了。如果有人能帮我解决这个问题,我将不胜感激。这是代码(它基于 OpenCV 提供的 Python2 校准示例):

#imports left out
USAGE = '''
USAGE: calib.py [--save <filename>] [--debug <output path>] [--square_size] [<image mask>]
'''   

args, img_mask = getopt.getopt(sys.argv[1:], '', ['save=', 'debug=', 'square_size='])
args = dict(args)
try: img_mask = img_mask[0]
except: img_mask = '../cpp/0*.png'
img_names = glob(img_mask)
debug_dir = args.get('--debug')
square_size = float(args.get('--square_size', 1.0))

pattern_size = (5, 8)
pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 )
pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
pattern_points *= square_size

obj_points = []
img_points = []
h, w = 0, 0
count = 0
for fn in img_names:
    print 'processing %s...' % fn,
    img = cv2.imread(fn, 0)
    h, w = img.shape[:2]
    found, corners = cv2.findChessboardCorners(img, pattern_size)        

    if found:
        if count == 0:
            #corners first is a list of the image points for just the first image.
            #This is the image I know the object points for and use in solvePnP
            corners_first =  []
            for val in corners:
                corners_first.append(val[0])                
            np_corners_first = np.asarray(corners_first,np.float64)                
        count+=1
        term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 )
        cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
    if debug_dir:
        vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
        cv2.drawChessboardCorners(vis, pattern_size, corners, found)
        path, name, ext = splitfn(fn)
        cv2.imwrite('%s/%s_chess.bmp' % (debug_dir, name), vis)
    if not found:
        print 'chessboard not found'
        continue
    img_points.append(corners.reshape(-1, 2))
    obj_points.append(pattern_points)        

    print 'ok'

rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h))
print "RMS:", rms
print "camera matrix:\n", camera_matrix
print "distortion coefficients: ", dist_coefs.ravel()    
cv2.destroyAllWindows()    

np_xyz = np.array(xyz,np.float64).T #xyz list is from file. Not shown here for brevity
camera_matrix2 = np.asarray(camera_matrix,np.float64)
np_dist_coefs = np.asarray(dist_coefs[:,:],np.float64)    

found,rvecs_new,tvecs_new = cv2.solvePnP(np_xyz, np_corners_first,camera_matrix2,np_dist_coefs)

np_rodrigues = np.asarray(rvecs_new[:,:],np.float64)
print np_rodrigues.shape
rot_matrix = cv2.Rodrigues(np_rodrigues)[0]

def rot_matrix_to_euler(R):
    y_rot = asin(R[2][0]) 
    x_rot = acos(R[2][2]/cos(y_rot))    
    z_rot = acos(R[0][0]/cos(y_rot))
    y_rot_angle = y_rot *(180/pi)
    x_rot_angle = x_rot *(180/pi)
    z_rot_angle = z_rot *(180/pi)        
    return x_rot_angle,y_rot_angle,z_rot_angle

print "Euler_rotation = ",rot_matrix_to_euler(rot_matrix)
print "Translation_Matrix = ", tvecs_new

【问题讨论】:

  • 我也有同样的问题,不过我用的是不同的方法。我希望这里有人知道答案。 stackoverflow.com/questions/14444433/…
  • 你不需要自己的 rot_matrix_to_euler,罗德里格斯会为你做的
  • 好吧,b_m,看来我们是在同一条船上哈哈。希望有人能够帮助我们。如何直接从 Rodrigues 获得欧拉角?对我来说,3x1 旋转矢量与欧拉角不同。另外,我对solvePnP的输入是否正确?这就是您使用 calibratecamera 输出的方式吗?
  • @user1713402 抱歉,我完全糊涂了,你说得对,solvePnP 返回的 3x1 不是欧拉角,抱歉。我的建议是,您可以为自己找到一个 solvePnP 给出意外结果的特定示例。列出进入点的 3d 和 2d 坐标,以及您的相机矩阵,以及您认为 solvePnP 的输出错误的原因。
  • 是的,请发布您的数据 :)

标签: python opencv camera-calibration


【解决方案1】:

我想您可能会将tvecs_new 视为相机位置。有点令人困惑,事实并非如此!事实上,它是世界原点在相机坐标中的位置。要在对象/世界坐标中获得相机姿势,我相信你需要

-np.matrix(rotation_matrix).T * np.matrix(tvecs_new)

您可以使用cv2.decomposeProjectionMatrix(P)[-1] 获得欧拉角,其中P[r|t] 3 x 4 外在矩阵。

我发现this 是一篇关于内在函数和外在函数的非常好的文章...

【讨论】:

  • 哇!奇迹般有效!太感谢了。我还没有看到这篇文章(我打算),但这肯定会修复结果。我有点困惑你所说的“它在相机坐标中的世界原点位置”是什么意思。你有没有机会简单地详细说明一下?什么是相机坐标?此外,通过分解获得的欧拉角与我计算的欧拉角相对应(尽管我将坚持使用 opencvs 代码)
  • @user1713402 - 酷:)。 camera coordinate system是相机笛卡尔坐标系,它随着相机移动,相机始终在原点,z轴是相机视图的方向,y在图像上,x在右边。而世界坐标系统是静态的,而相机在其中移动。 Heres 另一个解释得更多的问题。
  • 太棒了!这就说得通了!再次感谢
  • 这很好用,但 x 和 y 中的欧拉值不适合搅拌机坐标系
猜你喜欢
  • 2018-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-27
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
相关资源
最近更新 更多