java - java中的OpenCV Mat对象序列化

标签 java opencv serialization

我尝试序列化成对的映射并获得如下异常:

 java.io.NotSerializableException: org.opencv.core.Mat

有某种方法可以序列化它吗?

最佳答案

我对 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;
}

关于java - java中的OpenCV Mat对象序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27065062/

相关文章:

java - GSON 抛出 "Expected BEGIN_OBJECT but was BEGIN_ARRAY"?

java - 上传文件时报错 "Unable to process parts as no multi-part configuration has been provided"

python - Unpickle 有时会生成空白对象

java - 尽管行数超过 1,但 JTable 仅显示 1 行

android - 提供的数据元素编号 (0) 应该是 Mat channel 数 (1) Android OpenCV 的倍数

python - 要与StereoBM一起使用,是否必须校准摄像机?

python - HoughLine 修改后的代码会检测到简单的图像,但不会检测到复杂的图像

c# - 什么是 [Serializable] 以及我应该何时使用它?

python - json.dumps() : escaping forward slashes

java - 字符串比较没有跳出 while 循环