java中的OpenCV Mat对象序列化 [英] OpenCV Mat object serialization in java
本文介绍了java中的OpenCV Mat对象序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图序列化对的映射并获得如下异常:
java.io.NotSerializableException:或者是.bn $。 解决方案我从这里。经过测试和工作
SerializationUtils类这里
public static String matToJson(Mat mat){
JsonObject obj = new JsonObject();
if(mat.isContinuous()){
int cols = mat.cols();
int rows = mat.rows();
int elemSize =(int)mat.elemSize();
int type = mat.type();
obj.addProperty(rows,rows);
obj.addProperty(cols,cols);
obj.addProperty(type,type);
//我们无法将二进制数据设置为json对象,因此:
//将数据字节数组编码为Base64。
String dataString;
if(type == CvType.CV_32S || type == CvType.CV_32SC2 || type == CvType.CV_32SC3 || type == CvType.CV_16S){
int [] data = new int [cols * rows * elemSize];
mat.get(0,0,data);
dataString = new String(Base64.encodeBase64(SerializationUtils.toByteArray(data)));
}
else if(type == CvType.CV_32F || type == CvType.CV_32FC2){
float [] data = new float [cols * rows * elemSize];
mat.get(0,0,data);
dataString = new String(Base64.encodeBase64(SerializationUtils.toByteArray(data)));
}
else if(type == CvType.CV_64F || type == CvType.CV_64FC2){
double [] data = new double [cols * rows * elemSize];
mat.get(0,0,data);
dataString = new String(Base64.encodeBase64(SerializationUtils.toByteArray(data)));
}
else if(type == CvType.CV_8U){
byte [] data = new byte [cols * rows * elemSize];
mat.get(0,0,data);
dataString = new String(Base64.encodeBase64(data));
}
else {
抛出新的UnsupportedOperationException(未知类型);
}
obj.addProperty(data,dataString);
Gson gson = new Gson();
String json = gson.toJson(obj);
返回json;
} else {
System.out.println(Mat not continuous。);
}
返回{};
}
public static Mat matFromJson(String json){
JsonParser parser = new JsonParser();
JsonObject JsonObject = parser.parse(json).getAsJsonObject();
int rows = JsonObject.get(rows)。getAsInt();
int cols = JsonObject.get(cols)。getAsInt();
int type = JsonObject.get(type)。getAsInt();
Mat mat = new Mat(rows,cols,type);
String dataString = JsonObject.get(data)。getAsString();
if(type == CvType.CV_32S || type == CvType.CV_32SC2 || type == CvType.CV_32SC3 || type == CvType.CV_16S){
int [] data = SerializationUtils.toIntArray (Base64.decodeBase64(dataString.getBytes()));
mat.put(0,0,data);
}
else if(type == CvType.CV_32F || type == CvType.CV_32FC2){
float [] data = SerializationUtils.toFloatArray(Base64.decodeBase64(dataString.getBytes() ));
mat.put(0,0,data);
}
else if(type == CvType.CV_64F || type == CvType.CV_64FC2){
double [] data = SerializationUtils.toDoubleArray(Base64.decodeBase64(dataString.getBytes() ));
mat.put(0,0,data);
}
else if(type == CvType.CV_8U){
byte [] data = Base64.decodeBase64(dataString.getBytes());
mat.put(0,0,data);
}
else {
抛出新的UnsupportedOperationException(未知类型);
}
返回垫;
}
I've tried to serialize a map of pairs and obtained an exception as below:
java.io.NotSerializableException: org.opencv.core.Mat
Is there some kind of way to serialize this?
解决方案 I made some improvments from here. Tested and working
SerializationUtils class here
public static String matToJson(Mat mat){
JsonObject obj = new JsonObject();
if(mat.isContinuous()){
int cols = mat.cols();
int rows = mat.rows();
int elemSize = (int) mat.elemSize();
int type = mat.type();
obj.addProperty("rows", rows);
obj.addProperty("cols", cols);
obj.addProperty("type", type);
// We cannot set binary data to a json object, so:
// Encoding data byte array to Base64.
String dataString;
if( type == CvType.CV_32S || type == CvType.CV_32SC2 || type == CvType.CV_32SC3 || type == CvType.CV_16S) {
int[] data = new int[cols * rows * elemSize];
mat.get(0, 0, data);
dataString = new String(Base64.encodeBase64(SerializationUtils.toByteArray(data)));
}
else if( type == CvType.CV_32F || type == CvType.CV_32FC2) {
float[] data = new float[cols * rows * elemSize];
mat.get(0, 0, data);
dataString = new String(Base64.encodeBase64(SerializationUtils.toByteArray(data)));
}
else if( type == CvType.CV_64F || type == CvType.CV_64FC2) {
double[] data = new double[cols * rows * elemSize];
mat.get(0, 0, data);
dataString = new String(Base64.encodeBase64(SerializationUtils.toByteArray(data)));
}
else if( type == CvType.CV_8U ) {
byte[] data = new byte[cols * rows * elemSize];
mat.get(0, 0, data);
dataString = new String(Base64.encodeBase64(data));
}
else {
throw new UnsupportedOperationException("unknown type");
}
obj.addProperty("data", dataString);
Gson gson = new Gson();
String json = gson.toJson(obj);
return json;
} else {
System.out.println("Mat not continuous.");
}
return "{}";
}
public static Mat matFromJson(String json){
JsonParser parser = new JsonParser();
JsonObject JsonObject = parser.parse(json).getAsJsonObject();
int rows = JsonObject.get("rows").getAsInt();
int cols = JsonObject.get("cols").getAsInt();
int type = JsonObject.get("type").getAsInt();
Mat mat = new Mat(rows, cols, type);
String dataString = JsonObject.get("data").getAsString();
if( type == CvType.CV_32S || type == CvType.CV_32SC2 || type == CvType.CV_32SC3 || type == CvType.CV_16S) {
int[] data = SerializationUtils.toIntArray(Base64.decodeBase64(dataString.getBytes()));
mat.put(0, 0, data);
}
else if( type == CvType.CV_32F || type == CvType.CV_32FC2) {
float[] data = SerializationUtils.toFloatArray(Base64.decodeBase64(dataString.getBytes()));
mat.put(0, 0, data);
}
else if( type == CvType.CV_64F || type == CvType.CV_64FC2) {
double[] data = SerializationUtils.toDoubleArray(Base64.decodeBase64(dataString.getBytes()));
mat.put(0, 0, data);
}
else if( type == CvType.CV_8U ) {
byte[] data = Base64.decodeBase64(dataString.getBytes());
mat.put(0, 0, data);
}
else {
throw new UnsupportedOperationException("unknown type");
}
return mat;
}
这篇关于java中的OpenCV Mat对象序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文