Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from copy import deepcopy
  5.  
  6. from image import *
  7.  
  8. CAMERA_CALIBRATION_FRAMES = 50
  9.  
  10. class Camera():
  11.     def __init__(self, frames):
  12.         self._mat = None
  13.         self.calibrate(frames)
  14.        
  15.     def getIntrinsicMatrix(self):
  16.         return self._mat
  17.        
  18.     def calibrate(self, frames):
  19.         """Fonte: https://docs.opencv.org/3.4.1/dc/dbb/tutorial_py_calibration.html"""
  20.        
  21.         criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
  22.        
  23.         # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
  24.         objp = np.zeros((8*6,3), np.float32)
  25.         objp[:,:2] = np.mgrid[0:8,0:6].T.reshape(-1,2)
  26.        
  27.         # Arrays to store object points and image points from all the images.
  28.         objpoints = [] # 3d point in real world space
  29.         imgpoints = [] # 2d points in image plane.
  30.        
  31.         i = 0
  32.         print("Calibration Started!")
  33.         while i < CAMERA_CALIBRATION_FRAMES:
  34.             gray = frames[i].getGrayScale()
  35.            
  36.             corners = gray.getChessBoardCorners(8,6)
  37.             #cv2.findChessboardCorners(gray, (8, 6), None)
  38.             #print("hmm")
  39.             if corners != None:
  40.                 i += 1
  41.                 print(i,"/", CAMERA_CALIBRATION_FRAMES)
  42.                
  43.                 objpoints.append(objp)
  44.                 corners2 = cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
  45.                 imgpoints.append(corners)
  46.        
  47.         ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
  48.        
  49.         self._mat = mtx
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement