def __calibrate(self): ''' Calibrates camera (run in background) ''' # check for correct image if self._img != None: self.__calibrating = True self._gui.status("Calibrating ...") # get values x = int(str( self._gui.getObj("txtPatternX").text() )) y = int(str( self._gui.getObj("txtPatternY").text() )) n = int(str( self._gui.getObj("txtCalibrationFrames").text() )) square_size = float(str( self._gui.getObj("txtCalibrationSquareSize").text() )) # calculate distortion pattern_size = (x, y) ret = self.__calibrateCorners(pattern_size, n, square_size) if ret != None: camera_matrix, dist_coefs = ret self.__camera_matrix, self.__dist_coefs = camera_matrix, dist_coefs # undistort image img = self.undistortImage(self._img) self.__imgScene = img self._gui.status("Searching for boders to crop image ...") # get corners if ret != None: found = False frames = 0; tries = 0; while not found and frames < n and tries < 10: img = self.undistortImage(self._img) found, corners = getChessBoardCorners( img, pattern_size ) if found: frames += 1 tries += 1 if found: self.__corners = getImageSizeFromCorners(corners) # crop image self.__imgScene = self.cropImage(img) self.__calibrated = True # set calibration flag self._gui.status("Calibration finished.") self.__calibrating = False
def __calibrateCorners(self, pattern_size, n, square_size): ''' Calibrates corners of chessboard @return: camera_matrix, dist_coefs ''' pattern_points = zeros( (prod(pattern_size), 3), float32 ) pattern_points[:,:2] = indices(pattern_size).T.reshape(-1, 2) pattern_points *= square_size obj_points = [] img_points = [] frames = 0; tries = 0 lastCount = self._imgCounter; # detect pattern while frames < n and tries < 10: img = self._img img = cvtColor(img, COLOR_RGB2GRAY) img = threshold(img, 40, 255, THRESH_BINARY)[1] img = cvtColor(img, COLOR_GRAY2RGB) self.__imgScene = img from time import sleep sleep(1) if lastCount != self._imgCounter: # get corners print "getchessboardcorners" found, corners = getChessBoardCorners( img, pattern_size ) print "found", found # check if corners found if found: frames += 1 # get more detailed points and add them to list img_points.append( corners.reshape(-1, 2) ) obj_points.append( pattern_points ) # draw corners drawChessboardCorners( img, pattern_size, corners, found ) self.__imgScene = img tries = 0 tries += 1 # get cmera values if img != None and len(obj_points) > 0 and len(img_points) > 0: return getCalibrationData(img, obj_points, img_points )