-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathOpenCVFeatures2dSerialization.java
95 lines (83 loc) · 4.77 KB
/
OpenCVFeatures2dSerialization.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_features2d.AKAZE;
import static org.bytedeco.javacpp.opencv_core.read;
import static org.bytedeco.javacpp.opencv_core.write;
/**
* (De)serialize OpenCV structures using XML to files and memory
* <p>
* Created by Maurice Betzel on 24.11.2017.
*/
public class OpenCVFeatures2dSerialization {
public static void main(String[] args) throws IOException {
String imageFile = (args.length > 0) ? args[0] : "Blob3.jpg";
BufferedImage bufferedImage = ImageIO.read(new File(imageFile));
try (Mat matrix = new OpenCVFrameConverter.ToMat().convert(new Java2DFrameConverter().convert(bufferedImage))
) {
String fileName = "serialized.xml";
serializeFile(matrix, fileName);
deserializeFile(fileName);
String serialized = serializeMemory(matrix);
System.out.println(serialized);
deserializeMemory(serialized);
}
}
private static void serializeFile(Mat matrix, String fileName) throws UnsupportedEncodingException {
try (KeyPointVector keyPointVectorSerialize = new KeyPointVector(); Mat objectDescriptorsSerialize = new Mat(); AKAZE akaze = AKAZE.create();
FileStorage fileStorage = new FileStorage(fileName, FileStorage.WRITE, StandardCharsets.UTF_8.name())
) {
akaze.detectAndCompute(matrix, new Mat(), keyPointVectorSerialize, objectDescriptorsSerialize, false);
System.out.println("Vector size: " + keyPointVectorSerialize.size());
System.out.println("Descriptor size: " + objectDescriptorsSerialize.cols());
write(fileStorage, "keyPoints", keyPointVectorSerialize);
write(fileStorage, "descriptors", objectDescriptorsSerialize);
fileStorage.release();
}
}
private static void deserializeFile(String file) {
try (KeyPointVector keyPointVectorDeserialize = new KeyPointVector(); Mat objectDescriptorsDeserialize = new Mat();
FileStorage fileStorage = new FileStorage(file, FileStorage.READ, StandardCharsets.UTF_8.name());
FileNode keyPointsFileNode = fileStorage.get("keyPoints"); FileNode descriptorsFileNode = fileStorage.get("descriptors")
) {
read(keyPointsFileNode, keyPointVectorDeserialize);
read(descriptorsFileNode, objectDescriptorsDeserialize);
System.out.println("Vector size: " + keyPointVectorDeserialize.size());
System.out.println("Descriptor size: " + objectDescriptorsDeserialize.cols());
fileStorage.release();
}
}
private static String serializeMemory(Mat matrix) throws UnsupportedEncodingException {
try (KeyPointVector keyPointVectorSerialize = new KeyPointVector(); Mat objectDescriptorsSerialize = new Mat(); AKAZE akaze = AKAZE.create();
FileStorage fileStorage = new FileStorage(".xml", FileStorage.WRITE | FileStorage.MEMORY, StandardCharsets.UTF_8.name())
) {
akaze.detectAndCompute(matrix, new Mat(), keyPointVectorSerialize, objectDescriptorsSerialize, false);
System.out.println("Vector size: " + keyPointVectorSerialize.size());
System.out.println("Descriptor size: " + objectDescriptorsSerialize.cols());
write(fileStorage, "keyPoints", keyPointVectorSerialize);
write(fileStorage, "descriptors", objectDescriptorsSerialize);
BytePointer bytePointer = fileStorage.releaseAndGetString();
return bytePointer.getString(StandardCharsets.UTF_8.name());
}
}
private static void deserializeMemory(String serialized) {
try (KeyPointVector keyPointVectorDeserialize = new KeyPointVector(); Mat objectDescriptorsDeserialize = new Mat();
FileStorage fileStorage = new FileStorage(serialized, FileStorage.READ | FileStorage.MEMORY, StandardCharsets.UTF_8.name());
FileNode keyPointsFileNode = fileStorage.get("keyPoints"); FileNode descriptorsFileNode = fileStorage.get("descriptors")
) {
read(keyPointsFileNode, keyPointVectorDeserialize);
read(descriptorsFileNode, objectDescriptorsDeserialize);
System.out.println("Vector size: " + keyPointVectorDeserialize.size());
System.out.println("Descriptor size: " + objectDescriptorsDeserialize.cols());
fileStorage.release();
}
}
}