java保存mat对象_java中的OpenCV Mat对象序列化

这篇博客介绍了如何使用Java将OpenCV的Mat对象进行序列化和反序列化。通过实现SerializationUtils类的matToJson和matFromJson方法,将Mat对象转换为JSON字符串并能恢复原始数据。主要涉及了对不同数据类型(如int, float, double, byte)的处理,并使用Base64编码来确保数据安全。" 124074269,11439703,TCP连接详解:三次握手与流量控制,"['网络协议', 'TCP']
摘要由CSDN通过智能技术生成

我从here开始做了一些改进.经过测试和工作

SerializationUtils类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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值