def check_projection(cam_opts,distorted=True): cam = _build_test_camera(**cam_opts) R = cam.get_rect() if not np.allclose(R, np.eye(3)): # opencv's ProjectPoints2 does not take into account # rectifciation matrix, thus we skip this test. from nose.plugins.skip import SkipTest raise SkipTest("Test %s is skipped: %s" %( check_projection.__name__, 'cannot check if rectification matrix is not unity')) pts3D = _build_points_3d() n_pts = len(pts3D) src = numpy2opencv_image(pts3D) dst = numpy2opencv_pointmat(np.empty( (n_pts, 2) )) t = np.array(cam.get_translation(),copy=True) t.shape = 3,1 R = cam.get_rotation() rvec = numpy2opencv_image(np.empty( (1,3) )) cv.Rodrigues2(numpy2opencv_image(R), rvec) if distorted: K = cam.get_K() cv_distortion = numpy2opencv_image(cam.get_D()) else: K = cam.get_P()[:3,:3] cv_distortion = numpy2opencv_image(np.zeros((5,1))) cv.ProjectPoints2(src, rvec, numpy2opencv_image(t), numpy2opencv_image(K), cv_distortion, dst) result_cv = opencv_pointmat2numpy(dst) result_np = cam.project_3d_to_pixel( pts3D, distorted=distorted ) assert result_cv.shape == result_np.shape if cam.is_opencv_compatible(): try: assert np.allclose(result_cv, result_np) except: debug() debug('result_cv') debug(result_cv) debug('result_np') debug(result_np) debug() raise else: from nose.plugins.skip import SkipTest raise SkipTest("Test %s is skipped: %s" %( check_projection.__name__, 'camera model is not OpenCV compatible, skipping test'))
def test_simple_projection(): # get some 3D points pts_3d = _build_points_3d() if DRAW: fig = plt.figure(figsize=(8, 12)) ax1 = fig.add_subplot(3, 1, 1, projection='3d') ax1.scatter(pts_3d[:, 0], pts_3d[:, 1], pts_3d[:, 2]) ax1.set_xlabel('X') ax1.set_ylabel('Y') ax1.set_zlabel('Z') # build a camera calibration matrix focal_length = 1200 width, height = 640, 480 R = np.eye(3) # look at +Z c = np.array((9.99, 19.99, 20)) M = make_M(focal_length, width, height, R, c)['M'] # now, project these 3D points into our image plane pts_3d_H = np.vstack((pts_3d.T, np.ones((1, len(pts_3d))))) # make homog. undist_rst_simple = np.dot(M, pts_3d_H) # multiply undist_simple = undist_rst_simple[:2, :] / undist_rst_simple[ 2, :] # project if DRAW: ax2 = fig.add_subplot(3, 1, 2) ax2.plot(undist_simple[0, :], undist_simple[1, :], 'b.') ax2.set_xlim(0, width) ax2.set_ylim(height, 0) ax2.set_title('matrix multiply') # build a camera model from our M and project onto image plane cam = CameraModel.load_camera_from_M(M, width=width, height=height) undist_full = cam.project_3d_to_pixel(pts_3d).T if DRAW: plot_camera(ax1, cam, scale=10, axes_size=5.0) sz = 20 x = 5 y = 8 z = 19 ax1.auto_scale_xyz([x, x + sz], [y, y + sz], [z, z + sz]) ax3 = fig.add_subplot(3, 1, 3) ax3.plot(undist_full[0, :], undist_full[1, :], 'b.') ax3.set_xlim(0, width) ax3.set_ylim(height, 0) ax3.set_title('pymvg') if DRAW: plt.show() assert np.allclose(undist_full, undist_simple)
def test_simple_projection(): # get some 3D points pts_3d = _build_points_3d() if DRAW: fig = plt.figure(figsize=(8,12)) ax1 = fig.add_subplot(3,1,1, projection='3d') ax1.scatter( pts_3d[:,0], pts_3d[:,1], pts_3d[:,2]) ax1.set_xlabel('X') ax1.set_ylabel('Y') ax1.set_zlabel('Z') # build a camera calibration matrix focal_length = 1200 width, height = 640,480 R = np.eye(3) # look at +Z c = np.array( (9.99, 19.99, 20) ) M = make_M( focal_length, width, height, R, c)['M'] # now, project these 3D points into our image plane pts_3d_H = np.vstack( (pts_3d.T, np.ones( (1,len(pts_3d))))) # make homog. undist_rst_simple = np.dot(M, pts_3d_H) # multiply undist_simple = undist_rst_simple[:2,:]/undist_rst_simple[2,:] # project if DRAW: ax2 = fig.add_subplot(3,1,2) ax2.plot( undist_simple[0,:], undist_simple[1,:], 'b.') ax2.set_xlim(0,width) ax2.set_ylim(height,0) ax2.set_title('matrix multiply') # build a camera model from our M and project onto image plane cam = CameraModel.load_camera_from_M( M, width=width, height=height ) undist_full = cam.project_3d_to_pixel(pts_3d).T if DRAW: plot_camera( ax1, cam, scale=10, axes_size=5.0 ) sz = 20 x = 5 y = 8 z = 19 ax1.auto_scale_xyz( [x,x+sz], [y,y+sz], [z,z+sz] ) ax3 = fig.add_subplot(3,1,3) ax3.plot( undist_full[0,:], undist_full[1,:], 'b.') ax3.set_xlim(0,width) ax3.set_ylim(height,0) ax3.set_title('pymvg') if DRAW: plt.show() assert np.allclose( undist_full, undist_simple )
def check_projection(cam_opts, distorted=True): cam = _build_test_camera(**cam_opts) R = cam.get_rect() if not np.allclose(R, np.eye(3)): # opencv's ProjectPoints2 does not take into account # rectifciation matrix, thus we skip this test. from nose.plugins.skip import SkipTest raise SkipTest("Test %s is skipped: %s" % (check_projection.__name__, 'cannot check if rectification matrix is not unity')) pts3D = _build_points_3d() n_pts = len(pts3D) src = numpy2opencv_image(pts3D) dst = numpy2opencv_pointmat(np.empty((n_pts, 2))) t = np.array(cam.get_translation(), copy=True) t.shape = 3, 1 R = cam.get_rotation() rvec = numpy2opencv_image(np.empty((1, 3))) cv.Rodrigues2(numpy2opencv_image(R), rvec) if distorted: K = cam.get_K() cv_distortion = numpy2opencv_image(cam.get_D()) else: K = cam.get_P()[:3, :3] cv_distortion = numpy2opencv_image(np.zeros((5, 1))) cv.ProjectPoints2(src, rvec, numpy2opencv_image(t), numpy2opencv_image(K), cv_distortion, dst) result_cv = opencv_pointmat2numpy(dst) result_np = cam.project_3d_to_pixel(pts3D, distorted=distorted) assert result_cv.shape == result_np.shape if cam.is_opencv_compatible(): try: assert np.allclose(result_cv, result_np) except: debug() debug('result_cv') debug(result_cv) debug('result_np') debug(result_np) debug() raise else: from nose.plugins.skip import SkipTest raise SkipTest( "Test %s is skipped: %s" % (check_projection.__name__, 'camera model is not OpenCV compatible, skipping test'))