What could make undistortion code not work for several chessboard images having the same dimensions?

柔情痞子 提交于 2021-01-29 14:01:42

问题


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 chessboards have the same dimension.

The code i am talking about is as below

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)

Could someone enlighten me please. Thanks


回答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)


来源:https://stackoverflow.com/questions/65128995/what-could-make-undistortion-code-not-work-for-several-chessboard-images-having

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!