class CalibrationIface(PlayerInterface):
    """
    Implements the PlayerInterface class with an ImageListVideoStream
    It uses the CameraCalibration class to compute the camera matrix from a set of images containing a
    chessboard pattern.
    """
    def __init__(self, app, context, parent, params, displayName, providerName, timerSpeed=200):
        PlayerInterface.__init__(self, app, context, parent, params, displayName, providerName, timerSpeed)
        
        self.nColumns = 9
        self.nRows = 6
        self.matrixType = 'normal'
        
        self._setDisplay()
        self._setDisplay()

    @pyqtSlot()
    def calibrate(self):
        """
        Compute the camera matrix 
        """
        self.calib = CameraCalibration(self.nColumns, self.nRows)
        self.calib.calibrate(self.srcFolder)
        self.params.calib = self.calib
        
        self.nFrames = len(self.calib.srcImgs)
        
        self.stream = ImageListVideoStream(self.calib.srcImgs)
        self._setDisplay()
        self._setDisplayMax()
        
        self._updateImgProvider()

    @pyqtSlot(result=QVariant)
    def getNRows(self):
        """
        Get the number of inner rows in the chessboard pattern
        """
        return self.nRows

    @pyqtSlot(QVariant)
    def setNRows(self, nRows):
        """
        Set the number of inner rows in the chessboard pattern
        """
        self.nRows = int(nRows)

    @pyqtSlot(result=QVariant)
    def getNColumns(self):
        """
        Get the number of inner columns in the chessboard pattern
        """
        return self.nColumns

    @pyqtSlot(QVariant)
    def setNColumns(self, nColumns):
        """
        Set the number of inner rows in the chessboard pattern
        """
        self.nColumns = int(nColumns)

    @pyqtSlot(result=QVariant)
    def getFolderPath(self):
        """
        Get the path to the folder where the images with the pattern are stored
        """
        diag = QFileDialog()
        srcFolder = diag.getExistingDirectory(parent=diag, caption="Chose directory",
                                            directory=os.getenv('HOME'))
        self.srcFolder = srcFolder
        return srcFolder

    @pyqtSlot(QVariant)
    def setMatrixType(self, matrixType):
        """
        Set the matrix type to be saved. Resolution independant (normal) or dependant (optimized)
        
        :param string matrixType: The type of matrix to be saved. One of ['normal', 'optimized']
        """
        matrixType = matrixType.lower()
        if matrixType not in ['normal', 'optimized']:
            raise KeyError("Expected one of ['normal', 'optimized'], got {}".format(matrixType))
        else:
            self.matrixType = matrixType

    @pyqtSlot()
    def saveCameraMatrix(self):
        """
        Save the camera matrix selected as self.matrixType
        """
        diag = QFileDialog()
        destPath = diag.getSaveFileName(parent=diag,
                                    caption='Save matrix',
                                    directory=os.getenv('HOME'), 
                                    filter='Numpy (.npy)')
        destPath = destPath[0]
        if destPath:
            if self.matrixType == 'normal':
                np.save(destPath, self.cameraMatrix)
            elif self.matrixType == 'optimized':
                np.save(destPath, self.optimalCameraMatrix)

    @pyqtSlot(QVariant)
    def setFrameType(self, frameType):
        """
        Selects the type of frame to be displayed. (Before, during or after distortion correction)
        
        :param string frameType: The selected frame type. One of ['source', 'detected', 'corrected']
        """
        frameType = frameType.lower()
        currentIndex = self.stream.currentFrameIdx
        if frameType == "source":
            imgs = self.calib.srcImgs
        elif frameType == "detected":
            imgs = self.calib.detectedImgs
        elif frameType == "corrected":
            imgs = self.calib.correctedImgs
        else:
            raise KeyError("Expected one of ['source', 'detected', 'corrected'], got {}".format(frameType))
        self.stream = ImageListVideoStream(imgs)
        self._updateImgProvider()
        self.stream.currentFrameIdx = self._validateFrameIdx(currentIndex -1) # reset to previous position
        self.getImg()
Exemplo n.º 2
0
class CalibrationIface(PlayerInterface):
    """
    Implements the PlayerInterface class with an ImageListVideoStream
    It uses the CameraCalibration class to compute the camera matrix from a set of images containing a
    chessboard pattern.
    """
    def __init__(self,
                 app,
                 context,
                 parent,
                 params,
                 displayName,
                 providerName,
                 timerSpeed=200):
        PlayerInterface.__init__(self, app, context, parent, params,
                                 displayName, providerName, timerSpeed)

        self.nColumns = 9
        self.nRows = 6
        self.matrixType = 'normal'

        self._setDisplay()
        self._setDisplay()

    @pyqtSlot()
    def calibrate(self):
        """
        Compute the camera matrix 
        """
        self.calib = CameraCalibration(self.nColumns, self.nRows)
        self.calib.calibrate(self.srcFolder)
        self.params.calib = self.calib

        self.nFrames = len(self.calib.srcImgs)

        self.stream = ImageListVideoStream(self.calib.srcImgs)
        self._setDisplay()
        self._setDisplayMax()

        self._updateImgProvider()

    @pyqtSlot(result=QVariant)
    def getNRows(self):
        """
        Get the number of inner rows in the chessboard pattern
        """
        return self.nRows

    @pyqtSlot(QVariant)
    def setNRows(self, nRows):
        """
        Set the number of inner rows in the chessboard pattern
        """
        self.nRows = int(nRows)

    @pyqtSlot(result=QVariant)
    def getNColumns(self):
        """
        Get the number of inner columns in the chessboard pattern
        """
        return self.nColumns

    @pyqtSlot(QVariant)
    def setNColumns(self, nColumns):
        """
        Set the number of inner rows in the chessboard pattern
        """
        self.nColumns = int(nColumns)

    @pyqtSlot(result=QVariant)
    def getFolderPath(self):
        """
        Get the path to the folder where the images with the pattern are stored
        """
        diag = QFileDialog()
        srcFolder = diag.getExistingDirectory(parent=diag,
                                              caption="Chose directory",
                                              directory=os.getenv('HOME'))
        self.srcFolder = srcFolder
        return srcFolder

    @pyqtSlot(QVariant)
    def setMatrixType(self, matrixType):
        """
        Set the matrix type to be saved. Resolution independant (normal) or dependant (optimized)
        
        :param string matrixType: The type of matrix to be saved. One of ['normal', 'optimized']
        """
        matrixType = matrixType.lower()
        if matrixType not in ['normal', 'optimized']:
            raise KeyError(
                "Expected one of ['normal', 'optimized'], got {}".format(
                    matrixType))
        else:
            self.matrixType = matrixType

    @pyqtSlot()
    def saveCameraMatrix(self):
        """
        Save the camera matrix selected as self.matrixType
        """
        diag = QFileDialog()
        destPath = diag.getSaveFileName(parent=diag,
                                        caption='Save matrix',
                                        directory=os.getenv('HOME'),
                                        filter='Numpy (.npy)')
        destPath = destPath[0]
        if destPath:
            if self.matrixType == 'normal':
                np.save(destPath, self.cameraMatrix)
            elif self.matrixType == 'optimized':
                np.save(destPath, self.optimalCameraMatrix)

    @pyqtSlot(QVariant)
    def setFrameType(self, frameType):
        """
        Selects the type of frame to be displayed. (Before, during or after distortion correction)
        
        :param string frameType: The selected frame type. One of ['source', 'detected', 'corrected']
        """
        frameType = frameType.lower()
        currentIndex = self.stream.currentFrameIdx
        if frameType == "source":
            imgs = self.calib.srcImgs
        elif frameType == "detected":
            imgs = self.calib.detectedImgs
        elif frameType == "corrected":
            imgs = self.calib.correctedImgs
        else:
            raise KeyError(
                "Expected one of ['source', 'detected', 'corrected'], got {}".
                format(frameType))
        self.stream = ImageListVideoStream(imgs)
        self._updateImgProvider()
        self.stream.currentFrameIdx = self._validateFrameIdx(
            currentIndex - 1)  # reset to previous position
        self.getImg()
import cv2
from camera_calibration import CameraCalibration
import image_processing as imgproc
import numpy as np
from line import Line

scaled_size = 1
image_size = (int(1280 * scaled_size), int(720 * scaled_size))
offset = image_size[1] * 0.3

file_name = './output_images/undist_straight_lines1.jpg'

calibration = CameraCalibration('camera_cal/')
calibration.calibrate()
intrinsic_mat = calibration.get_intrinsic()
dist_paras = calibration.get_distortion_paras()

perspective_src_points = scaled_size * np.float32(
    [[233, 694], [595, 450], [686, 450], [1073, 694]])  # These points are manually selected
perspective_dst_points = np.float32([[offset, image_size[1]], [offset, 0],
                                     [image_size[0] - offset, 0], [image_size[0] - offset, image_size[1]]])

#######################
img = cv2.imread(file_name)
undist = calibration.distort_correction(img)
undist = cv2.resize(undist, (0, 0), fx=scaled_size, fy=scaled_size)

hls = cv2.cvtColor(undist, cv2.COLOR_BGR2HSV)
h = hls[:, :, 0]
s = hls[:, :, 1]
v = hls[:, :, 2]