public void TestMatToFileStorage() { //create a matrix m with random values Mat m = new Mat(120, 240, DepthType.Cv8U, 1); using (ScalarArray low = new ScalarArray(0)) using (ScalarArray high = new ScalarArray(255)) CvInvoke.Randu(m, low, high); //Convert the random matrix m to yml format, good for matrix that contains values such as calibration, homography etc. String mStr; using (FileStorage fs = new FileStorage(".yml", FileStorage.Mode.Write | FileStorage.Mode.Memory)) { fs.Write(m, "m"); mStr = fs.ReleaseAndGetString(); } //Treat the Mat as image data and convert it to png format. using (VectorOfByte bytes = new VectorOfByte()) { CvInvoke.Imencode(".png", m, bytes); byte[] rawData = bytes.ToArray(); } }
public void TestFileStorage2() { Mat m = new Mat(40, 30, DepthType.Cv8U, 3); using (ScalarArray lower = new ScalarArray(new MCvScalar(0, 0, 0))) using (ScalarArray higher = new ScalarArray(new MCvScalar(255, 255, 255))) CvInvoke.Randu(m, lower, higher ); int intValue = 10; float floatValue = 213.993f; double doubleValue = 32.314; using (FileStorage fs = new FileStorage(".xml", FileStorage.Mode.Write | FileStorage.Mode.Memory)) { fs.Write(m, "m"); fs.Write(intValue, "int"); fs.Write(floatValue, "float"); fs.Write(doubleValue, "double"); string s = fs.ReleaseAndGetString(); using (FileStorage fs2 = new FileStorage(s, FileStorage.Mode.Read | FileStorage.Mode.Memory)) { using (FileNode node = fs2.GetFirstTopLevelNode()) { Mat m2 = new Mat(); node.ReadMat(m2); EmguAssert.IsTrue(m.Equals(m2)); } using (FileNode node = fs2.GetNode("m")) { Mat m2 = new Mat(); node.ReadMat(m2); EmguAssert.IsTrue(m.Equals(m2)); } using (FileNode node = fs2.GetNode("int")) { EmguAssert.IsTrue(intValue.Equals(node.ReadInt())); } using (FileNode node = fs2.GetNode("float")) { EmguAssert.IsTrue(floatValue.Equals(node.ReadFloat())); } using (FileNode node = fs2.GetNode("double")) { EmguAssert.IsTrue(doubleValue.Equals(node.ReadDouble())); } } } }