I’m calibrating a camera in opencv. I was able to perform both calibration and pose estimation and now I should find the board_corners of the chessboard in python. To find board corners, I need board center, l and w (dimention of the chessboard) and the orientation of the board in the plane. I know the board lays on x-y plane in world coordinates, but it’s not parallel to x or y axis.
Right now I have the board_center in world coordinates but I’m not quite sure how to find the corners of the board:
board_center = np.mean(objp, axis=0)
How can I do this in python?
Also I’m not sure how to pass those points in the camera frame with the rotation and the tralsation vector obtainded through solvePnP.
This is my code for now for pose estimation, but I’m not quite sure how to find the orientation of the plane
import numpy as np
import cv2 as cv
import glob
# Load previously saved data
with np.load('CameraParmas.npz') as file:
mtx, dist, _, _ = [file[i] for i in ('cameraMatrix','dist','rvecs','tvecs')]
def draw(img, corners, imgpts):
def tupleOfInts(arr):
return tuple(int(x) for x in arr)
corner = tupleOfInts(corners[0].ravel())
cv.line(img, (corner), tupleOfInts(imgpts[0].ravel()), (255,0,0), 5)
cv.line(img, (corner), tupleOfInts(imgpts[1].ravel()), (0,255,0), 5)
cv.line(img, (corner), tupleOfInts(imgpts[2].ravel()), (0,0,255), 5)
return img
def drawBoxes(img, corners, imgpts):
imgpts = np.int32(imgpts).reshape(-1,2)
# draw ground floor in green
img = cv.drawContours(img, [imgpts[:4]],-1,(0,255,0),-3)
# draw pillars in blue color
for i,j in zip(range(4),range(4,8)):
img = cv.line(img, tuple(imgpts[i]), tuple(imgpts[j]),(255),3)
# draw top layer in red color
img = cv.drawContours(img, [imgpts[4:]],-1,(0,0,255),3)
return img
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((8*6,3), np.float32)
objp[:,:2] = np.mgrid[0:8,0:6].T.reshape(-1,2)
size_of_chessboard_squares_mm = 30
objp = objp * size_of_chessboard_squares_mm
axis = np.float32([[50,0,0], [0,50,0], [0,0,-50]]).reshape(-1,3)
axisBoxes = np.float32([[0,0,0], [0,50,0], [50,50,0], [50,0,0],
[0,0,-50],[0,50,-50],[50,50,-50],[50,0,-50] ])
images = glob.glob('right*.png')
for image in images:
img = cv.imread(image)
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
ret, corners = cv.findChessboardCorners(gray, (8,6),None)
if ret == True:
corners2 = cv.cornerSubPix(gray,corners,(11,11),(-1,-1), criteria)
# Find the rotation and translation vectors.
ret, rvecs, tvecs = cv.solvePnP(objp, corners2, mtx, None)
rotM, _ = cv.Rodrigues(rvecs)
# Project 3D points to image plane
imgpts, jac = cv.projectPoints(axis, rvecs, tvecs, mtx, None)
img = draw(img,corners2,imgpts)
cv.imshow('img',img)
k = cv.waitKey(1000)
cv.imwrite('pose'+image, img)
#Board Center
board_center = np.mean(objp, axis=0)
print("nThe Board Center is:n",board_center)
print("nobjptsn", objp)
#print("nobjpn",objp)
effedi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.