I want to draw 3D coordinate axis (X, Y, Z axes) on a chessboard's first corner using Opencv and python.
But when I run this following code,
import cv2
import numpy as np
import glob
def draw(img, corners, imgpts):
corner = tuple(corners[0].ravel())
img = cv2.line(img, corner, tuple(imgpts[0].ravel()), (255,0,0), 5)
img = cv2.line(img, corner, tuple(imgpts[1].ravel()), (0,255,0), 5)
img = cv2.line(img, corner, tuple(imgpts[2].ravel()), (0,0,255), 5)
return img
# Load previously saved data
with np.load('B.npz') as X:
mtx, dist, _, _ = [X[i] for i in ('mtx','dist','rvecs','tvecs')]
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((6*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)
axis = np.float32([[3,0,0], [0,3,0], [0,0,-3]]).reshape(-1,3)
for fname in glob.glob('left*.jpg'):
img = cv2.imread(fname)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (7,6),None)
if ret == True:
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
# Find the rotation and translation vectors.
rvecs, tvecs, inliers = cv2.solvePnPRansac(objp, corners2, mtx, dist)
# project 3D points to image plane
imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, mtx, dist)
img = draw(img,corners2,imgpts)
cv2.imshow('img',img)
k = cv2.waitKey(0) & 0xff
if k == 's':
cv2.imwrite(fname[:6]+'.png', img)
cv2.destroyAllWindows()
I am getting this error:
Traceback (most recent call last):
File "D:\3D Image Processing Dev Soft\Code\pose_estimation.py", line 13, in <module>
with np.load('B.npz') as X:
File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 422, in load
fid = open(os_fspath(file), "rb")
IOError: [Errno 2] No such file or directory: 'B.npz'
Help me to overcome this problem. Thank you.
In order to generate the B.npz
file, you should add np.savez()
with some parameters ( mtx
, dist
, rvecs
and tvecs
) after cv.calibrateCamera()
in the previous section , like this:
for fname in images:
img = cv.imread(fname)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# Find the chess board corners
ret, corners = cv.findChessboardCorners(gray, (7,6), None)
# If found, add object points, image points (after refining them)
if ret == True:
objpoints.append(objp)
corners2 = cv.cornerSubPix(gray,corners, (11,11), (-1,-1), criteria)
imgpoints.append(corners)
# Draw and display the corners
cv.drawChessboardCorners(img, (7,6), corners2, ret)
cv.imshow('img', img)
cv.waitKey(500)
ret, mtx, dist, rvecs, tvecs = cv.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
np.savez('B.npz', mtx=mtx, dist=dist, rvecs=rvecs, tvecs=tvecs)
cv.destroyAllWindows()
Incidentally, np.savez saves several arrays into a single file in uncompressed .npz
format.
with np.load('B.npz') as X: In this line you are trying to load B.npz. And it;s unable to find that file in the location specified.Make sure to place the fie in specified location.
I am also struggling with this. The difference is that, i am using another image, not chessboard, but i have camera intrinsic and extrinsic parameters. I want to use openCV python to draw 3D axis on my image. May i know any reference or code? Thanks
您必须使用 NumPy(np.savez、np.savetxt 等)保存上一个项目( https://docs.opencv.org/4.3.0/dc/dbb/tutorial_py_calibration.html )中的相机矩阵和失真系数.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.