本文整理汇总了Python中cv2.solvePnP方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.solvePnP方法的具体用法?Python cv2.solvePnP怎么用?Python cv2.solvePnP使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv2
的用法示例。
在下文中一共展示了cv2.solvePnP方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pnp
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import solvePnP [as 别名]
def pnp(points_3D, points_2D, cameraMatrix):
try:
distCoeffs = pnp.distCoeffs
except:
distCoeffs = np.zeros((8, 1), dtype='float32')
assert points_2D.shape[0] == points_2D.shape[0], 'points 3D and points 2D must have same number of vertices'
_, R_exp, t = cv2.solvePnP(points_3D,
# points_2D,
np.ascontiguousarray(points_2D[:,:2]).reshape((-1,1,2)),
cameraMatrix,
distCoeffs)
# , None, None, False, cv2.SOLVEPNP_UPNP)
R, _ = cv2.Rodrigues(R_exp)
return R, t
示例2: solve_head_pose
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import solvePnP [as 别名]
def solve_head_pose(self, face_landmarks):
indices = [17, 21, 22, 26, 36, 39, 42, 45, 31, 35]
image_pts = np.zeros((len(indices), 2))
for i in range(len(indices)):
part = face_landmarks.part(indices[i])
image_pts[i, 0] = part.x
image_pts[i, 1] = part.y
_, rotation_vec, translation_vec = cv2.solvePnP(self.face_model_points,
image_pts,
self.camera_matrix,
self.distortion_coeffs)
projected_head_pose_box_points, _ = cv2.projectPoints(self.head_pose_box_points,
rotation_vec,
translation_vec,
self.camera_matrix,
self.distortion_coeffs)
projected_head_pose_box_points = tuple(map(tuple, projected_head_pose_box_points.reshape(8, 2)))
# Calculate euler angle
rotation_mat, _ = cv2.Rodrigues(rotation_vec)
pose_mat = cv2.hconcat((rotation_mat, translation_vec))
_, _, _, _, _, _, euler_angles = cv2.decomposeProjectionMatrix(pose_mat)
return projected_head_pose_box_points, euler_angles
示例3: solve_pose
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import solvePnP [as 别名]
def solve_pose(self, image_points):
"""
Solve pose from image points
Return (rotation_vector, translation_vector) as pose.
"""
assert image_points.shape[0] == self.model_points_68.shape[0], "3D points and 2D points should be of same number."
(_, rotation_vector, translation_vector) = cv2.solvePnP(
self.model_points, image_points, self.camera_matrix, self.dist_coefs)
# (success, rotation_vector, translation_vector) = cv2.solvePnP(
# self.model_points,
# image_points,
# self.camera_matrix,
# self.dist_coefs,
# rvec=self.r_vec,
# tvec=self.t_vec,
# useExtrinsicGuess=True)
return (rotation_vector, translation_vector)
示例4: solve_pose
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import solvePnP [as 别名]
def solve_pose(self, image_points):
"""
Solve pose from image points
Return (rotation_vector, translation_vector) as pose.
"""
assert image_points.shape[0] == self.model_points_68.shape[0], "3D points and 2D points should be of same number."
(_, rotation_vector, translation_vector) = cv2.solvePnP(
self.model_points, image_points, self.camera_matrix, self.dist_coeefs)
# (success, rotation_vector, translation_vector) = cv2.solvePnP(
# self.model_points,
# image_points,
# self.camera_matrix,
# self.dist_coeefs,
# rvec=self.r_vec,
# tvec=self.t_vec,
# useExtrinsicGuess=True)
return (rotation_vector, translation_vector)
示例5: estimate_head_pose
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import solvePnP [as 别名]
def estimate_head_pose(self, face: Face, camera: Camera) -> None:
"""Estimate the head pose by fitting 3D template model."""
# If the number of the template points is small, cv2.solvePnP
# becomes unstable, so set the default value for rvec and tvec
# and set useExtrinsicGuess to True.
# The default values of rvec and tvec below mean that the
# initial estimate of the head pose is not rotated and the
# face is in front of the camera.
rvec = np.zeros(3, dtype=np.float)
tvec = np.array([0, 0, 1], dtype=np.float)
_, rvec, tvec = cv2.solvePnP(self.LANDMARKS,
face.landmarks,
camera.camera_matrix,
camera.dist_coefficients,
rvec,
tvec,
useExtrinsicGuess=True,
flags=cv2.SOLVEPNP_ITERATIVE)
rot = Rotation.from_rotvec(rvec)
face.head_pose_rot = rot
face.head_position = tvec
face.reye.head_pose_rot = rot
face.leye.head_pose_rot = rot
示例6: solve_pose_by_68_points
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import solvePnP [as 别名]
def solve_pose_by_68_points(self, image_points):
"""
Solve pose from all the 68 image points
Return (rotation_vector, translation_vector) as pose.
"""
if self.r_vec is None:
(_, rotation_vector, translation_vector) = cv2.solvePnP(
self.model_points_68, image_points, self.camera_matrix, self.dist_coeefs)
self.r_vec = rotation_vector
self.t_vec = translation_vector
(_, rotation_vector, translation_vector) = cv2.solvePnP(
self.model_points_68,
image_points,
self.camera_matrix,
self.dist_coeefs,
rvec=self.r_vec,
tvec=self.t_vec,
useExtrinsicGuess=True)
return (rotation_vector, translation_vector)
示例7: calculateExtrinsics
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import solvePnP [as 别名]
def calculateExtrinsics(self, cameraParameters):
'''
Inputs:
cameraParameters is CameraParameters object
Calculate: rotate vector and transform vector
>>> marker.calculateExtrinsics(camera_matrix, dist_coeff)
>>> print(marker.rvec, marker.tvec)
'''
object_points = np.zeros((4,3), dtype=np.float32)
object_points[:,:2] = np.mgrid[0:2,0:2].T.reshape(-1,2)
# Test Code.
# object_points[:] -= 0.5
marker_points = self.corners
if marker_points is None: raise TypeError('The marker.corners is None')
camera_matrix = cameraParameters.camera_matrix
dist_coeff = cameraParameters.dist_coeff
ret, rvec, tvec = cv2.solvePnP(object_points, marker_points,
camera_matrix, dist_coeff)
if ret: self.rvec, self.tvec = rvec, tvec
return ret
示例8: _return_landmarks
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import solvePnP [as 别名]
def _return_landmarks(self, inputImg, roiX, roiY, roiW, roiH, points_to_return=range(0,68)):
""" Return the the roll pitch and yaw angles associated with the input image.
@param image It is a colour image. It must be >= 64 pixel.
@param radians When True it returns the angle in radians, otherwise in degrees.
"""
#Creating a dlib rectangle and finding the landmarks
dlib_rectangle = dlib.rectangle(left=int(roiX), top=int(roiY), right=int(roiW), bottom=int(roiH))
dlib_landmarks = self._shape_predictor(inputImg, dlib_rectangle)
#It selects only the landmarks that
#have been indicated in the input parameter "points_to_return".
#It can be used in solvePnP() to estimate the 3D pose.
landmarks = np.zeros((len(points_to_return),2), dtype=np.float32)
counter = 0
for point in points_to_return:
landmarks[counter] = [dlib_landmarks.parts()[point].x, dlib_landmarks.parts()[point].y]
counter += 1
return landmarks
示例9: forward
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import solvePnP [as 别名]
def forward(ctx, pts2d, pts3d, K, ini_pose=None):
bs = pts2d.size(0)
n = pts2d.size(1)
device = pts2d.device
pts3d_np = np.array(pts3d.detach().cpu())
K_np = np.array(K.detach().cpu())
P_6d = torch.zeros(bs,6,device=device)
for i in range(bs):
pts2d_i_np = np.ascontiguousarray(pts2d[i].detach().cpu()).reshape((n,1,2))
if ini_pose is None:
_, rvec0, T0, _ = cv.solvePnPRansac(objectPoints=pts3d_np, imagePoints=pts2d_i_np, cameraMatrix=K_np, distCoeffs=None, flags=cv.SOLVEPNP_ITERATIVE, confidence=0.9999 ,reprojectionError=3)
else:
rvec0 = np.array(ini_pose[i, 0:3].cpu().view(3, 1))
T0 = np.array(ini_pose[i, 3:6].cpu().view(3, 1))
_, rvec, T = cv.solvePnP(objectPoints=pts3d_np, imagePoints=pts2d_i_np, cameraMatrix=K_np, distCoeffs=None, flags=cv.SOLVEPNP_ITERATIVE, useExtrinsicGuess=True, rvec=rvec0, tvec=T0)
angle_axis = torch.tensor(rvec,device=device,dtype=torch.float).view(1, 3)
T = torch.tensor(T,device=device,dtype=torch.float).view(1, 3)
P_6d[i,:] = torch.cat((angle_axis,T),dim=-1)
ctx.save_for_backward(pts2d,P_6d,pts3d,K)
return P_6d
示例10: solve_pose_by_68_points
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import solvePnP [as 别名]
def solve_pose_by_68_points(self, image_points):
"""
Solve pose from all the 68 image points
Return (rotation_vector, translation_vector) as pose.
"""
if self.r_vec is None:
(_, rotation_vector, translation_vector) = cv2.solvePnP(
self.model_points_68, image_points, self.camera_matrix, self.dist_coefs)
self.r_vec = rotation_vector
self.t_vec = translation_vector
(_, rotation_vector, translation_vector) = cv2.solvePnP(
self.model_points_68,
image_points,
self.camera_matrix,
self.dist_coefs,
rvec=self.r_vec,
tvec=self.t_vec,
useExtrinsicGuess=True)
R, _ = cv2.Rodrigues(rotation_vector)
points_3d = R.dot(self.model_points_68.T) + translation_vector # 3x68
reproject_image_points = self.camera_matrix.dot(points_3d).T # 68x2
reproject_image_points /= reproject_image_points[:, 2:3]
reproject_image_points = reproject_image_points[:, :2]
reprojection_error = np.mean((image_points - reproject_image_points)**2)
return reprojection_error, rotation_vector, translation_vector
示例11: draw_overlay
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import solvePnP [as 别名]
def draw_overlay(self, vis, tracked):
x0, y0, x1, y1 = tracked.target.rect
quad_3d = np.float32([[x0, y0, 0], [x1, y0, 0], [x1, y1, 0], [x0, y1, 0]])
fx = 0.5 + cv2.getTrackbarPos('focal', 'plane') / 50.0
h, w = vis.shape[:2]
K = np.float64([[fx*w, 0, 0.5*(w-1)],
[0, fx*w, 0.5*(h-1)],
[0.0,0.0, 1.0]])
dist_coef = np.zeros(4)
ret, rvec, tvec = cv2.solvePnP(quad_3d, tracked.quad, K, dist_coef)
verts = ar_verts * [(x1-x0), (y1-y0), -(x1-x0)*0.3] + (x0, y0, 0)
verts = cv2.projectPoints(verts, rvec, tvec, K, dist_coef)[0].reshape(-1, 2)
for i, j in ar_edges:
(x0, y0), (x1, y1) = verts[i], verts[j]
cv2.line(vis, (int(x0), int(y0)), (int(x1), int(y1)), (255, 255, 0), 2)
示例12: get_head_pose
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import solvePnP [as 别名]
def get_head_pose(shape,img):
h,w,_=img.shape
K = [w, 0.0, w//2,
0.0, w, h//2,
0.0, 0.0, 1.0]
# Assuming no lens distortion
D = [0, 0, 0.0, 0.0, 0]
cam_matrix = np.array(K).reshape(3, 3).astype(np.float32)
dist_coeffs = np.array(D).reshape(5, 1).astype(np.float32)
# image_pts = np.float32([shape[17], shape[21], shape[22], shape[26], shape[36],
# shape[39], shape[42], shape[45], shape[31], shape[35],
# shape[48], shape[54], shape[57], shape[8]])
image_pts = np.float32([shape[17], shape[21], shape[22], shape[26], shape[36],
shape[39], shape[42], shape[45], shape[31], shape[35]])
_, rotation_vec, translation_vec = cv2.solvePnP(object_pts, image_pts, cam_matrix, dist_coeffs)
reprojectdst, _ = cv2.projectPoints(reprojectsrc, rotation_vec, translation_vec, cam_matrix,
dist_coeffs)
reprojectdst = tuple(map(tuple, reprojectdst.reshape(8, 2)))
# calc euler angle
rotation_mat, _ = cv2.Rodrigues(rotation_vec)
pose_mat = cv2.hconcat((rotation_mat, translation_vec))
_, _, _, _, _, _, euler_angle = cv2.decomposeProjectionMatrix(pose_mat)
return reprojectdst, euler_angle
示例13: overlay_graphics
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import solvePnP [as 别名]
def overlay_graphics(self, img, tracked):
x_start, y_start, x_end, y_end = tracked.target.rect
quad_3d = np.float32([[x_start, y_start, 0], [x_end, y_start, 0], \
[x_end, y_end, 0], [x_start, y_end, 0]])
h, w = img.shape[:2]
K = np.float64([[w, 0, 0.5*(w-1)],
[0, w, 0.5*(h-1)],
[0, 0, 1.0]])
dist_coef = np.zeros(4)
ret, rvec, tvec = cv2.solvePnP(quad_3d, tracked.quad, K, dist_coef)
self.time_counter += 1
if not self.time_counter % 20:
self.graphics_counter = (self.graphics_counter + 1) % 8
self.overlay_vertices = np.float32([[0, 0, 0], [0, 1, 0],\
[1, 1, 0], [1, 0, 0], [0.5, 0.5, self.graphics_counter]])
verts = self.overlay_vertices * [(x_end-x_start), (y_end-y_start),\
-(x_end-x_start)*0.3] + (x_start, y_start, 0)
verts = cv2.projectPoints(verts, rvec, tvec, K, dist_coef)[0].reshape(-1, 2)
verts_floor = np.int32(verts).reshape(-1,2)
cv2.drawContours(img, [verts_floor[:4]], -1, self.color_base, -3)
cv2.drawContours(img, [np.vstack((verts_floor[:2], \
verts_floor[4:5]))], -1, (0,255,0), -3)
cv2.drawContours(img, [np.vstack((verts_floor[1:3], \
verts_floor[4:5]))], -1, (255,0,0), -3)
cv2.drawContours(img, [np.vstack((verts_floor[2:4], \
verts_floor[4:5]))], -1, (0,0,150), -3)
cv2.drawContours(img, [np.vstack((verts_floor[3:4], \
verts_floor[0:1], verts_floor[4:5]))], -1, (255,255,0), -3)
for i, j in self.overlay_edges:
(x_start, y_start), (x_end, y_end) = verts[i], verts[j]
cv2.line(img, (int(x_start), int(y_start)), (int(x_end), int(y_end)), self.color_lines, 2)
开发者ID:PacktPublishing,项目名称:OpenCV-3-x-with-Python-By-Example,代码行数:38,代码来源:augmented_reality_piramide_v2.py
示例14: overlay_graphics
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import solvePnP [as 别名]
def overlay_graphics(self, img, tracked):
x_start, y_start, x_end, y_end = tracked.target.rect
quad_3d = np.float32([[x_start, y_start, 0], [x_end,
y_start, 0],
[x_end, y_end, 0], [x_start, y_end, 0]])
h, w = img.shape[:2]
K = np.float64([[w, 0, 0.5*(w-1)],
[0, w, 0.5*(h-1)],
[0, 0, 1.0]])
dist_coef = np.zeros(4)
ret, rvec, tvec = cv2.solvePnP(objectPoints=quad_3d, imagePoints=tracked.quad,
cameraMatrix=K, distCoeffs=dist_coef)
verts = self.overlay_vertices * \
[(x_end-x_start), (y_end-y_start), -(x_end-x_start)*0.3] + (x_start, y_start, 0)
verts = cv2.projectPoints(verts, rvec, tvec, cameraMatrix=K,
distCoeffs=dist_coef)[0].reshape(-1, 2)
verts_floor = np.int32(verts).reshape(-1,2)
cv2.drawContours(img, contours=[verts_floor[:4]],
contourIdx=-1, color=self.color_base, thickness=-3)
cv2.drawContours(img, contours=[np.vstack((verts_floor[:2],
verts_floor[4:5]))], contourIdx=-1, color=(0,255,0), thickness=-3)
cv2.drawContours(img, contours=[np.vstack((verts_floor[1:3],
verts_floor[4:5]))], contourIdx=-1, color=(255,0,0), thickness=-3)
cv2.drawContours(img, contours=[np.vstack((verts_floor[2:4],
verts_floor[4:5]))], contourIdx=-1, color=(0,0,150), thickness=-3)
cv2.drawContours(img, contours=[np.vstack((verts_floor[3:4],
verts_floor[0:1], verts_floor[4:5]))], contourIdx=-1, color=(255,255,0), thickness=-3)
for i, j in self.overlay_edges:
(x_start, y_start), (x_end, y_end) = verts[i], verts[j]
cv2.line(img, (int(x_start), int(y_start)), (int(x_end), int(y_end)), self.color_lines, 2)
开发者ID:PacktPublishing,项目名称:OpenCV-3-x-with-Python-By-Example,代码行数:34,代码来源:augmented_reality_piramide.py
示例15: get_default_params
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import solvePnP [as 别名]
def get_default_params(corners, ycoords, xcoords):
# page width and height
page_width = np.linalg.norm(corners[1] - corners[0])
page_height = np.linalg.norm(corners[-1] - corners[0])
rough_dims = (page_width, page_height)
# our initial guess for the cubic has no slope
cubic_slopes = [0.0, 0.0]
# object points of flat page in 3D coordinates
corners_object3d = np.array([
[0, 0, 0],
[page_width, 0, 0],
[page_width, page_height, 0],
[0, page_height, 0]])
# estimate rotation and translation from four 2D-to-3D point
# correspondences
_, rvec, tvec = cv2.solvePnP(corners_object3d,
corners, K, np.zeros(5))
span_counts = [len(xc) for xc in xcoords]
params = np.hstack((np.array(rvec).flatten(),
np.array(tvec).flatten(),
np.array(cubic_slopes).flatten(),
ycoords.flatten()) +
tuple(xcoords))
return rough_dims, span_counts, params