I'm trying to load / save OpenCV calibration data in YAML format using the official OpenCV Java bindings. I am aware OpenCV (c++ version at least) can serialize to XML and JSON but I would like to support older YAML calibration files.
A calibration file looks like this:
%YAML:1.0
cameraMatrix: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 6.6278599887122368e+02, 0., 3.1244256016006659e+02, 0.,
6.6129276875199082e+02, 2.2747179767124251e+02, 0., 0., 1. ]
imageSize_width: 640
imageSize_height: 480
sensorSize_width: 0
sensorSize_height: 0
distCoeffs: !!opencv-matrix
rows: 5
cols: 1
dt: d
data: [ -1.8848338341464690e-01, 1.0721890419183855e+00,
-3.5244467228016116e-03, -7.0195032848241403e-04,
-2.0412827999027101e+00 ]
reprojectionError: 2.1723265945911407e-01
I had a look at a few answer already here and here, however I'm looking for an elegant solution as I haven't quite understood to best map java classes to YAML and back. I've tried a few libraries like jyaml, yamlbeans (both 1.0 from SourceForge and 1.13 via Maven Central) and SnakeYAML.
My current attempt at deserialising sort of works but feels quite hacky:
CalibrationParseTest.java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.opencv.core.Core;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
public class CalibrationParseTest {
public static void main(String[] args) {
// load OpenCV native
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String yamlPath = "./data/calibration.yml";
try{
String yamlString = new String(Files.readAllBytes(Paths.get(yamlPath)), StandardCharsets.UTF_8);
// remove %YAML:1.0 to avoid scan directive error
yamlString = yamlString.replaceAll("%YAML:1.0", "");
// map custom class
yamlString = yamlString.replaceAll("opencv-matrix", "MatYAML");
System.out.println("<loaded>");
System.out.println(yamlString);
System.out.println("</loaded>");
Yaml yaml = new Yaml(new Constructor(CalibrationData.class));
CalibrationData data = yaml.load(yamlString);
// currently manually parsing data from the HashMap: can this be better ?
data.populateCV();
// double check data
System.out.println("<deserialized>");
System.out.println(data);
System.out.println("</deserialized>");
}catch (IOException e) {
e.printStackTrace();
}
}
}
CalibrationData.java
import java.util.HashMap;
import org.opencv.core.Mat;
import org.opencv.core.Size;
public class CalibrationData extends HashMap{
public Mat cameraMatrix;
public Size imageSize;
public Size sensorSize;
public Mat distCoeffs;
public float reprojectionError;
public CalibrationData(){}
public void populateCV(){
cameraMatrix = ((MatYAML)get("cameraMatrix")).toMat();
imageSize = new Size((int)get("imageSize_width"),(int)get("imageSize_height"));
sensorSize = new Size((int)get("sensorSize_width"),(int)get("sensorSize_height"));
distCoeffs = ((MatYAML)get("distCoeffs")).toMat();
reprojectionError = (float)((double)get("reprojectionError"));
}
public String toString(){
if(cameraMatrix == null){
return String.format("[CalibrationData (not parsed to CV-> call populateCV()\n\tdata: %s\n]",super.toString());
}
return String.format("[CalibrationData\n" +
"\tcalibrationMatrix: %s\n" +
"\timageSize: %s\n" +
"\tsensorSize: %s\n" +
"\tdistCoeffs: %s\n" +
"\treprojectionError: %f\n]", cameraMatrix.dump(), imageSize.toString(), sensorSize.toString(), distCoeffs.dump(), reprojectionError);
}
}
MatYAML.java
import java.util.List;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
public class MatYAML{
public int rows;
public int cols;
public String dt;
public List<Double> data;
Mat toMat(){
Mat out = new Mat(rows, cols, dt.equals("d") ? CvType.CV_64F : CvType.CV_32F);
int index = 0;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
out.put(row, col, data.get(index++));
}
}
return out;
}
}
This outputs the expected result:
<loaded>
cameraMatrix: !!MatYAML
rows: 3
cols: 3
dt: d
data: [ 6.6278599887122368e+02, 0., 3.1244256016006659e+02, 0.,
6.6129276875199082e+02, 2.2747179767124251e+02, 0., 0., 1. ]
imageSize_width: 640
imageSize_height: 480
sensorSize_width: 0
sensorSize_height: 0
distCoeffs: !!MatYAML
rows: 5
cols: 1
dt: d
data: [ -1.8848338341464690e-01, 1.0721890419183855e+00,
-3.5244467228016116e-03, -7.0195032848241403e-04,
-2.0412827999027101e+00 ]
reprojectionError: 2.1723265945911407e-01
</loaded>
<deserialized>
[CalibrationData
calibrationMatrix: [662.7859988712237, 0, 312.4425601600666;
0, 661.2927687519908, 227.4717976712425;
0, 0, 1]
imageSize: 640x480
sensorSize: 0x0
distCoeffs: [-0.1884833834146469; 1.072189041918385; -0.003524446722801612; -0.000701950328482414; -2.04128279990271]
reprojectionError: 0.217233
]
</deserialized>
Is there a more elegant way of serializing/deserializing between Java OpenCV classes and YAML without these hacks ?
By hacks I mean:
- manually removing yaml version directive
- swapping opencv-matrix with MatYAML string
- manually casting HashMap values
- potentially avoiding to manually populate OpenCV
Mat
data ? (if possible ?)
Update 2
amanin's answer is cleaner and makes it possible to avoid hackily replacing "!!opencv-matrix", however it doesn't serializing/deserializing Mat
:
OpenCVConfig{imageSize_width=640, imageSize_height=480, sensorSize_width=0, sensorSize_height=0, camerMatrix=Matrix{rows=3, cols=3, dt=d, data=[662.7859988712237, 0.0, 312.4425601600666, 0.0, 661.2927687519908, 227.4717976712425, 0.0, 0.0, 1.0]}, distCoeffs=Matrix{rows=5, cols=1, dt=d, data=[-0.1884833834146469, 1.0721890419183855, -0.0035244467228016116, -7.01950328482414E-4, -2.04128279990271]}}
---
imageSize_width: 640
imageSize_height: 480
sensorSize_width: 0
sensorSize_height: 0
reprojectionError: 0.21723265945911407
cameraMatrix:
rows: 3
cols: 3
dt: "d"
data:
- 662.7859988712237
- 0.0
- 312.4425601600666
- 0.0
- 661.2927687519908
- 227.4717976712425
- 0.0
- 0.0
- 1.0
distCoeffs:
rows: 5
cols: 1
dt: "d"
data:
- -0.1884833834146469
- 1.0721890419183855
- -0.0035244467228016116
- -7.01950328482414E-4
- -2.04128279990271
Please advise on integrating a solution with org.opencv.core.Mat