This is my first post here on stackoverflow. Sorry for my English and my programming knowledge if it is somehow bothering.
Well, I'm trying to calibrate the camera using opencv 2.4.9 on the Windows 8.1 operating system (the ubuntu operating system does not solve the problem.)
Problem: I use the code below to calibrate my camera, but it seems that if the number of my sample images (with a control card template) is more than 2, then roi of newcameramtx, roi = cv2.getOptimalNewCameraMatrix (mtx, dist, (w, h) , 1, (w, h)) lead to [0,0,0,0]. How many samples are associated with this result? (previously, before making some changes to this code, the maximum number of samples was 12).
Speaking of the maximum number of samples, I mean the images obtained from my camera with a checkerboard pattern, whether roi does not give a good result if the number exceeds the maximum number.
Angular detection works very well. You can find my sample images here .
"""
Created on Fri May 16 15:23:00 2014
@author: kakarot
"""
import numpy as np
import cv2
from matplotlib import pyplot as plt
LeftorRight = 'L'
numer = 12
chx = 6
chy = 9
chd = 25
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, numer, 0.001)
objp = np.zeros((chy*chx,3), np.float32)
objp[:,:2] = np.mgrid[0:chy,0:chx].T.reshape(-1,2)
objpoints = []
imgpoints = []
enum = 1
while(enum<=numer):
img=cv2.imread('1280x720p/BestAsPerMatlab/calib_'+str(LeftorRight)+str(enum)+'.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (chy,chx),None)
if ret == True and enum <= numer:
objpoints.append(objp*chd)
cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
imgpoints.append(corners)
cv2.drawChessboardCorners(img, (chy,chx),corners,ret)
cv2.imshow('Calibration',img)
cv2.imwrite('1280x720p/Chessboard/calibrated_L{0}.jpg'.format(enum),img)
print enum
if enum == numer:
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
img = cv2.imread('1280x720p/BestAsPerMatlab/calib_'+str(LeftorRight)+'7.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
h, w = img.shape[:2]
newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))
if (np.size(roi) == 4 and np.mean(roi) != 0):
mapx,mapy = cv2.initUndistortRectifyMap(mtx,dist,None,newcameramtx,(w,h),5)
dst = cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
x,y,w,h = roi
dst = dst[y:y+h, x:x+w]
dst = cv2.cvtColor(dst,cv2.COLOR_RGB2BGR)
plt.imshow(dst)
else:
np.disp('Something Went Wrong')
enum += 1
'''
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
'''
cv2.destroyAllWindows()
EDIT: I use two cheap USB cameras. I realized that the sample set of one of the cameras is fine, and I can use more than 19 samples without problems. But when using calibration samples of another camera, the maximum number of images of samples is 2. (if I make a different set of samples, the number will change). In conclusion, it seems that something is happening with the calibration matrices that produce. But this is strange.
, fisheye, , ... , , !