org.opencv.calib3d.Calib3d Java Examples

The following examples show how to use org.opencv.calib3d.Calib3d. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AutoCalibrationManager.java    From ShootOFF with GNU General Public License v3.0 6 votes vote down vote up
public Optional<MatOfPoint2f> findChessboard(Mat mat) {

		final MatOfPoint2f imageCorners = new MatOfPoint2f();

		final boolean found = Calib3d.findChessboardCorners(mat, boardSize, imageCorners,
				Calib3d.CALIB_CB_ADAPTIVE_THRESH | Calib3d.CALIB_CB_NORMALIZE_IMAGE);

		if (logger.isTraceEnabled()) logger.trace("found chessboard corners {}", found);

		if (found) {
			// optimization
			Imgproc.cornerSubPix(mat, imageCorners, new Size(1, 1), new Size(-1, -1), term);

			return Optional.of(imageCorners);
		}
		return Optional.empty();
	}
 
Example #2
Source File: CameraCalibrator.java    From OpenCV-AndroidSamples with MIT License 6 votes vote down vote up
public void calibrate() {
    ArrayList<Mat> rvecs = new ArrayList<Mat>();
    ArrayList<Mat> tvecs = new ArrayList<Mat>();
    Mat reprojectionErrors = new Mat();
    ArrayList<Mat> objectPoints = new ArrayList<Mat>();
    objectPoints.add(Mat.zeros(mCornersSize, 1, CvType.CV_32FC3));
    calcBoardCornerPositions(objectPoints.get(0));
    for (int i = 1; i < mCornersBuffer.size(); i++) {
        objectPoints.add(objectPoints.get(0));
    }

    Calib3d.calibrateCamera(objectPoints, mCornersBuffer, mImageSize,
            mCameraMatrix, mDistortionCoefficients, rvecs, tvecs, mFlags);

    mIsCalibrated = Core.checkRange(mCameraMatrix)
            && Core.checkRange(mDistortionCoefficients);

    mRms = computeReprojectionErrors(objectPoints, rvecs, tvecs, reprojectionErrors);
    Log.i(TAG, String.format("Average re-projection error: %f", mRms));
    Log.i(TAG, "Camera matrix: " + mCameraMatrix.dump());
    Log.i(TAG, "Distortion coefficients: " + mDistortionCoefficients.dump());
}
 
Example #3
Source File: CameraCalibrator.java    From OpenCV-AndroidSamples with MIT License 6 votes vote down vote up
private double computeReprojectionErrors(List<Mat> objectPoints,
                                         List<Mat> rvecs, List<Mat> tvecs, Mat perViewErrors) {
    MatOfPoint2f cornersProjected = new MatOfPoint2f();
    double totalError = 0;
    double error;
    float viewErrors[] = new float[objectPoints.size()];

    MatOfDouble distortionCoefficients = new MatOfDouble(mDistortionCoefficients);
    int totalPoints = 0;
    for (int i = 0; i < objectPoints.size(); i++) {
        MatOfPoint3f points = new MatOfPoint3f(objectPoints.get(i));
        Calib3d.projectPoints(points, rvecs.get(i), tvecs.get(i),
                mCameraMatrix, distortionCoefficients, cornersProjected);
        error = Core.norm(mCornersBuffer.get(i), cornersProjected, Core.NORM_L2);

        int n = objectPoints.get(i).rows();
        viewErrors[i] = (float) Math.sqrt(error * error / n);
        totalError  += error * error;
        totalPoints += n;
    }
    perViewErrors.create(objectPoints.size(), 1, CvType.CV_32FC1);
    perViewErrors.put(0, 0, viewErrors);

    return Math.sqrt(totalError / totalPoints);
}
 
Example #4
Source File: CameraCalibrator.java    From OpenCV-AndroidSamples with MIT License 5 votes vote down vote up
public CameraCalibrator(int width, int height) {
    mImageSize = new Size(width, height);
    mFlags = Calib3d.CALIB_FIX_PRINCIPAL_POINT +
            Calib3d.CALIB_ZERO_TANGENT_DIST +
            Calib3d.CALIB_FIX_ASPECT_RATIO +
            Calib3d.CALIB_FIX_K4 +
            Calib3d.CALIB_FIX_K5;
    Mat.eye(3, 3, CvType.CV_64FC1).copyTo(mCameraMatrix);
    mCameraMatrix.put(0, 0, 1.0);
    Mat.zeros(5, 1, CvType.CV_64FC1).copyTo(mDistortionCoefficients);
    Log.i(TAG, "Instantiated new " + this.getClass());
}
 
Example #5
Source File: CameraCalibrator.java    From OpenCV-AndroidSamples with MIT License 4 votes vote down vote up
private void findPattern(Mat grayFrame) {
    mPatternWasFound = Calib3d.findCirclesGrid(grayFrame, mPatternSize,
            mCorners, Calib3d.CALIB_CB_ASYMMETRIC_GRID);
}
 
Example #6
Source File: CameraCalibrator.java    From OpenCV-AndroidSamples with MIT License 4 votes vote down vote up
private void drawPoints(Mat rgbaFrame) {
    Calib3d.drawChessboardCorners(rgbaFrame, mPatternSize, mCorners, mPatternWasFound);
}