What Could Make Undistortion Code Not Work For Several Chessboard Images Having The Same Dimensions?
I am beginner in OpenCV-Python. I would like to know what could make my undistortion code work for this chessboard picture and not work for this one knowing that the too chessboa
Solution 1:
Your checkboard dimensions must be (no of corners per row, no of corners per col) ->(9,7). And, you should threshold your image before passing into the find corners function as there is shades in four corners of the problematic image. Try this, it works.
import cv2
import numpy as np
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
cbrow = 7
cbcol = 9
objp = np.zeros((cbrow * cbcol, 3), np.float32)
objp[:, :2] = np.mgrid[0:cbcol, 0:cbrow].T.reshape(-1,2)
objpoints = []
imgpoints = []
CHECKERBOARD = (9,7)
img = cv2.imread("ChessUOledVGA.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_, out = cv2.threshold(gray, 40, 255, cv2.THRESH_BINARY_INV)
ret, corners = cv2.findChessboardCorners(out, CHECKERBOARD, cv2.CALIB_CB_ADAPTIVE_THRESH+cv2.CALIB_CB_FAST_CHECK+cv2.CALIB_CB_NORMALIZE_IMAGE)
print(corners, ret)
if ret == True:
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
imgpoints.append(corners2)
## Draw and display the corners
img = cv2.drawChessboardCorners(img, (cbcol, cbrow), corners2, ret)
cv2.imshow('img', img)
cv2.waitKey(0)
if 0:
ret, mtx, dist , rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
print('mtx', mtx)
print('dist',dist)
img = cv2.imread('ChessUOledVGA.jpg', 1)
h, w = img.shape[:2] # get the size and the shape of the image h,w
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, (w, h))
print('newcameramtx', newcameramtx)
dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
cv2.imwrite('result.jpg', dst)
Post a Comment for "What Could Make Undistortion Code Not Work For Several Chessboard Images Having The Same Dimensions?"