# Path: imgProcessor/imgIO.py
# def imread(img, color=None, dtype=None):
#     '''
#     dtype = 'noUint', uint8, float, 'float', ...
#     '''
#     COLOR2CV = {'gray': cv2.IMREAD_GRAYSCALE,
#                 'all': cv2.IMREAD_COLOR,
#                 None: cv2.IMREAD_ANYCOLOR
#                 }
#     c = COLOR2CV[color]
#     if callable(img):
#         img = img()
#     elif isinstance(img, string_types):
#         #         from_file = True
#         #         try:
#         #             ftype = img[img.find('.'):]
#         #             img = READERS[ftype](img)[0]
#         #         except KeyError:
#         # open with openCV
#         # grey - 8 bit
#         if dtype in (None, "noUint") or np.dtype(dtype) != np.uint8:
#             c |= cv2.IMREAD_ANYDEPTH
#         img2 = cv2.imread(img, c)
#         if img2 is None:
#             raise IOError("image '%s' is not existing" % img)
#         img = img2
# 
#     elif color == 'gray' and img.ndim == 3:  # multi channel img like rgb
#         # cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #cannot handle float64
#         img = toGray(img)
#     # transform array to uint8 array due to openCV restriction
#     if dtype is not None:
#         if isinstance(img, np.ndarray):
#             img = _changeArrayDType(img, dtype, cutHigh=False)
# 
#     return img
# 
# Path: imgProcessor/exceptions.py
# class NothingFound(Exception):
#     pass
# 
# class EnoughImages(Exception):
#     pass


import numpy as np
import cv2
import imgProcessor
    import sys
from collections import OrderedDict
from imgProcessor.imgIO import imread
from imgProcessor.exceptions import NothingFound, EnoughImages
    from fancytools.os.PathStr import PathStr


    def _findSymmetricCircles(self, flags=cv2.CALIB_CB_SYMMETRIC_GRID):
        (didFindCorners, corners) = cv2.findCirclesGrid(
            self.img, self.opts['size'],
            flags=flags | cv2.CALIB_CB_CLUSTERING)
        return didFindCorners, corners

    def getCoeffStr(self):
        '''
        get the distortion coeffs in a formated string
        '''
        txt = ''
        for key, val in self.coeffs.items():
            txt += '%s = %s\n' % (key, val)
        return txt

    def drawChessboard(self, img=None):
        '''
        draw a grid fitting to the last added image
        on this one or an extra image
        img == None
            ==False -> draw chessbord on empty image
            ==img
        '''
        assert self.findCount > 0, 'cannot draw chessboard if nothing found'
        if img is None:
            img = self.img
        elif isinstance(img, bool) and not img:
            img = np.zeros(shape=(self.img.shape), dtype=self.img.dtype)
        else:

# Path: imgProcessor/imgIO.py
# def imread(img, color=None, dtype=None):
#     '''
#     dtype = 'noUint', uint8, float, 'float', ...
#     '''
#     COLOR2CV = {'gray': cv2.IMREAD_GRAYSCALE,
#                 'all': cv2.IMREAD_COLOR,
#                 None: cv2.IMREAD_ANYCOLOR
#                 }
#     c = COLOR2CV[color]
#     if callable(img):
#         img = img()
#     elif isinstance(img, string_types):
#         #         from_file = True
#         #         try:
#         #             ftype = img[img.find('.'):]
#         #             img = READERS[ftype](img)[0]
#         #         except KeyError:
#         # open with openCV
#         # grey - 8 bit
#         if dtype in (None, "noUint") or np.dtype(dtype) != np.uint8:
#             c |= cv2.IMREAD_ANYDEPTH
#         img2 = cv2.imread(img, c)
#         if img2 is None:
#             raise IOError("image '%s' is not existing" % img)
#         img = img2
# 
#     elif color == 'gray' and img.ndim == 3:  # multi channel img like rgb
#         # cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #cannot handle float64
#         img = toGray(img)
#     # transform array to uint8 array due to openCV restriction
#     if dtype is not None:
#         if isinstance(img, np.ndarray):
#             img = _changeArrayDType(img, dtype, cutHigh=False)
# 
#     return img
# 
# Path: imgProcessor/exceptions.py
# class NothingFound(Exception):
#     pass
# 
# class EnoughImages(Exception):
#     pass


# Path: imgProcessor/camera/LensDistortion.py
import numpy as np
import cv2
import imgProcessor
    import sys
from collections import OrderedDict
from imgProcessor.imgIO import imread
from imgProcessor.exceptions import NothingFound, EnoughImages
    from fancytools.os.PathStr import PathStr


    def _findSymmetricCircles(self, flags=cv2.CALIB_CB_SYMMETRIC_GRID):
        (didFindCorners, corners) = cv2.findCirclesGrid(
            self.img, self.opts['size'],
            flags=flags | cv2.CALIB_CB_CLUSTERING)
        return didFindCorners, corners

    def getCoeffStr(self):
        '''
        get the distortion coeffs in a formated string
        '''
        txt = ''
        for key, val in self.coeffs.items():
            txt += '%s = %s\n' % (key, val)
        return txt

    def drawChessboard(self, img=None):
        '''
        draw a grid fitting to the last added image
        on this one or an extra image
        img == None
            ==False -> draw chessbord on empty image
            ==img
        '''
        assert self.findCount > 0, 'cannot draw chessboard if nothing found'
        if img is None:
            img = self.img
        elif isinstance(img, bool) and not img:
            img = np.zeros(shape=(self.img.shape), dtype=self.img.dtype)
        else:

            img = imread(img, dtype='uint8')
