Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2023, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package boofcv.io.calibration;
import boofcv.BoofVersion;
import boofcv.alg.geo.PerspectiveOps;
import boofcv.alg.geo.calibration.CalibrationObservation;
import boofcv.io.UtilIO;
import boofcv.misc.BoofMiscOps;
import boofcv.struct.calib.*;
import boofcv.struct.geo.PointIndex2D_F64;
import georegression.struct.se.Se3_F64;
import org.apache.commons.io.IOUtils;
import org.ejml.data.DMatrixRMaj;
import org.jetbrains.annotations.Nullable;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.representer.Representer;
import java.io.*;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import static boofcv.misc.BoofMiscOps.getOrThrow;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* Functions for loading and saving camera calibration related data structures from/to disk
*
* @author Peter Abeles
*/
@SuppressWarnings("ALL")
public class CalibrationIO {
public static String MODEL_PINHOLE = "pinhole";
public static String MODEL_BROWN = "pinhole_radial_tangential";
public static String MODEL_OMNIDIRECTIONAL_UNIVERSAL = "omnidirectional_universal";
public static String MODEL_KANNALA_BRANDT = "kannala_brandt";
public static String MODEL_STEREO = "stereo_camera";
public static String MODEL_MULT_CAMERA = "mult_camera";
public static String MODEL_RIGID_BODY = "rigid_body";
public static String MODEL_VISUAL_DEPTH = "visual_depth";
public static String MODEL_MONO_PLANE = "monocular_plane";
public static String VERSION = "version";
/**
* Saves intrinsic camera model to disk
*
* @param parameters Camera parameters
* @param outputWriter Path to where it should be saved
*/
public static void save( T parameters, Writer outputWriter ) {
PrintWriter out = new PrintWriter(outputWriter);
Yaml yaml = createYmlObject();
Map data = new HashMap<>();
if (parameters instanceof CameraPinholeBrown) {
out.println("# Pinhole camera model with radial and tangential distortion");
out.println("# (fx,fy) = focal length, (cx,cy) = principle point, (width,height) = image shape");
out.println("# radial = radial distortion, (t1,t2) = tangential distortion");
out.println();
putModelBrown((CameraPinholeBrown)parameters, data);
} else if (parameters instanceof CameraUniversalOmni) {
out.println("# Omnidirectional camera model with radial and tangential distortion");
out.println("# C. Mei, and P. Rives. \"Single view point omnidirectional camera calibration" +
" from planar grids.\" ICRA 2007");
out.println("# (fx,fy) = focal length, (cx,cy) = principle point, (width,height) = image shape");
out.println("# mirror_offset = offset mirror along z-axis in unit circle");
out.println("# radial = radial distortion, (t1,t2) = tangential distortion");
out.println();
putModelUniversalOmni((CameraUniversalOmni)parameters, data);
} else if (parameters instanceof CameraKannalaBrandt) {
out.println("# A camera model for pinhole, wide angle, and fisheye cameras.");
out.println("# Kannala, J., and Brandt, S. S. \"A generic camera model and calibration method for conventional,");
out.println("# wide-angle, and fish-eye lenses.\" IEEE transactions on pattern analysis and machine intelligence, 2006");
out.println("# (fx,fy) = focal length, (cx,cy) = principle point, (width,height) = image shape");
out.println("# Everything else is coefficients for different types of distortion");
out.println();
putKannalaBrandt((CameraKannalaBrandt)parameters, data);
} else {
out.println("# Pinhole camera model");
out.println("# (fx,fy) = focal length, (cx,cy) = principle point, (width,height) = image shape");
out.println();
putModelPinhole((CameraPinhole)parameters, data);
}
yaml.dump(data, out);
out.flush();
}
public static void save( T parameters, String filePath ) {
try (var stream = new FileOutputStream(filePath)) {
save(parameters, new OutputStreamWriter(stream, UTF_8));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static void save( T parameters, File filePath ) {
save(parameters, filePath.getPath());
}
public static Yaml createYmlObject() {
var dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
// Configure to get around point limit (as of SnakeYaml 1.32) causing a crash in a regression test
var loaderOptions = new LoaderOptions();
loaderOptions.setCodePointLimit(30_145_728);
return new Yaml(new SafeConstructor(new LoaderOptions()), new Representer(dumperOptions),
dumperOptions, loaderOptions);
}
/**
* Saves stereo camera model to disk
*
* @param parameters Camera parameters
* @param outputWriter Stream to save the parameters to
*/
public static void save( StereoParameters parameters, Writer outputWriter ) {
Map map = new HashMap<>();
map.put("model", MODEL_STEREO);
map.put(VERSION, 0);
map.put("left", putModelBrown(parameters.left, null));
map.put("right", putModelBrown(parameters.right, null));
map.put("rightToLeft", putSe3(parameters.right_to_left));
PrintWriter out = new PrintWriter(outputWriter);
out.println("# Intrinsic and extrinsic parameters for a stereo camera pair");
Yaml yaml = createYmlObject();
yaml.dump(map, out);
out.flush();
}
public static void save( StereoParameters parameters, String outputPath ) {
try (var stream = new FileOutputStream(outputPath)) {
save(parameters, new OutputStreamWriter(stream, UTF_8));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* Saves {@link MultiCameraCalibParams} to disk
*
* @param parameters Camera system parameters
* @param outputWriter Stream to save to
*/
public static void save( MultiCameraCalibParams parameters, Writer outputWriter ) {
List