是什么使得不失真代码对具有相同尺寸的多个棋盘图像不起作用?
What could make undistortion code not work for several chessboard images having the same dimensions?
我是 OpenCV 的初学者-Python。我想知道什么可以使我的无失真代码适用于这张棋盘图片
不适合这个
知道棋盘的尺寸是一样的
我说的代码如下
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 = (7,9)
img = cv2.imread("ChessUOledVGA.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, cv2.CALIB_CB_ADAPTIVE_THRESH+cv2.CALIB_CB_FAST_CHECK+cv2.CALIB_CB_NORMALIZE_IMAGE)
print(corners)
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)
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)
谁能赐教。谢谢
您的棋盘尺寸必须为(每行角数,每列角数)->(9,7)。而且,你应该在传递到查找角函数之前对你的图像进行阈值处理,因为有问题的图像的四个角都有阴影。试试这个,它有效。
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)
我是 OpenCV 的初学者-Python。我想知道什么可以使我的无失真代码适用于这张棋盘图片
不适合这个
知道棋盘的尺寸是一样的
我说的代码如下
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 = (7,9)
img = cv2.imread("ChessUOledVGA.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, cv2.CALIB_CB_ADAPTIVE_THRESH+cv2.CALIB_CB_FAST_CHECK+cv2.CALIB_CB_NORMALIZE_IMAGE)
print(corners)
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)
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)
谁能赐教。谢谢
您的棋盘尺寸必须为(每行角数,每列角数)->(9,7)。而且,你应该在传递到查找角函数之前对你的图像进行阈值处理,因为有问题的图像的四个角都有阴影。试试这个,它有效。
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)