Sameer J
Sameer J

Reputation: 85

Camera calibration Open CV-Python

I'm trying to do camera calibration, I have taken the code from open cv documentation. Here is my code -

import numpy as np
import cv2
import glob
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)
objpoints = []
imgpoints = []
images = glob.glob('/usr/local/share/OpenCV/samples/cpp/chess*.jpg')
img = cv2.imread("2.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret = False
ret, corners = cv2.findChessboardCorners(gray, (7, 6))
print (ret)

if ret == True:
    objpoints.append(objp)
    cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
    imgpoints.append(corners)

    # Draw and display the corners
    cv2.drawChessboardCorners(img, (7,6), corners, ret)
    cv2.imshow('img',img)
    cv2.imwrite('Corners_detected.jpg', img, None)
    cv2.waitKey(0)
cv2.destroyAllWindows()

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, 
gray.shape[::-1],None,None)

img = cv2.imread('2.jpg')
h,  w = img.shape[:2]

newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))

# undistort
dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
cv2.imwrite('calibration_result.png',dst)

In this code image 2.jpg is taken for calibration,

This is the image considered for understanding of calibration

My code is detecting corners for only this image. It is not working fine with other checker board image.It is not able to detect corners. Why is it so ?

Upvotes: 2

Views: 1663

Answers (2)

Joey Zhao
Joey Zhao

Reputation: 59

The thing about the Camera Calibration method is that it sometimes will not recognize a Checkerboard grid that isn't the maximum size. You could most likely get away with 8,6 or 9,5 as the size. However, with 6,7 there is too much of a difference and so the method won't recognize it.

I don't have any research sources but I've tested this myself before.

Upvotes: 0

Aiden Ray
Aiden Ray

Reputation: 379

Unfortunately, I do not have enough reputation to comment and clarify some points. However, I will try to answer anyway. Given you have added the print(ret) I assume this is where your problem lies.

It looks like you are using the wrong checkerboard size in cv2.findChessboardCorners(gray, (7, 6)). I have found this function returns False given the wrong input dimension values.
This is als the case for the objp object.

Given the image, you are showing this should be n-1 and m-1 (where n and m are the checkboard dimensions).

For your given image, this should be cv2.findChessboardCorners(gray, (9, 6))

Notice on the opencv calibration example the checkerboard is an 8x7, hence the given 7x6 input value.

Upvotes: 2

Related Questions