如何在Java中序列化/反序列化OpenCv Mat?

时间:2018-11-16 08:04:00

标签: java android opencv

我想序列化OpenCv Mat(https://docs.opencv.org/3.4.3/javadoc/index.html)以保存在Preference(https://developer.android.com/reference/android/preference/Preference)中,然后反序列化。

在这里我如何序列化它。

        Mat cameraMatrix = {put some Mat value here};
        Gson gson = new Gson();
        String json;

        // serialize
        json = gson.toJson(Double.toString(cameraMatrix));

        // deserialize          
        cameraMatrix = gson.fromJson(json, Mat.class);

不幸的是,它不起作用,因为Java Mat存储nativeObject而不是实际Mat值。

如何在Java中序列化和反序列化Mat?

2 个答案:

答案 0 :(得分:0)

Mat无法直接序列化,因为像素是本地存储的。您可以执行以下操作:

  • 将Mat转换为位图。
  • 从位图获取像素数组或将位图压缩为字节数组。
  • 根据需要将像素数组或字节数组序列化为JSON或Base64。

反序列化反序列化。

答案 1 :(得分:0)

我已将其用于我的用例。这是序列化/反序列化类。我在这里使用该表作为参考。 http://ninghang.blogspot.com/2012/11/list-of-mat-type-in-opencv.html

import com.google.gson.Gson;

import org.opencv.core.Mat;

class CommonUtility
{
//  Serialization/deserialization utility
    public static String SerializeFromMat(Mat mat)
    {
        SerializedMat serializedMat = new SerializedMat();
        serializedMat.setType(mat.type());
        serializedMat.setRows(mat.rows());
        serializedMat.setCols(mat.cols());

        if (serializedMat.getType()==0||
                serializedMat.getType()==8||
                serializedMat.getType()==16||
                serializedMat.getType()==24)
        {
            serializedMat.setBytes(new byte[(int)(mat.total()*mat.elemSize())]);
            mat.get(0,0,serializedMat.bytes);
        }
        else if (serializedMat.getType()==1||
            serializedMat.getType()==9||
            serializedMat.getType()==17||
            serializedMat.getType()==25)
        {
            serializedMat.setBytes(new byte[(int)(mat.total()*mat.elemSize())]);
            mat.get(0,0,serializedMat.bytes);
        }
        else if (serializedMat.getType()==2||
                serializedMat.getType()==10||
                serializedMat.getType()==18||
                serializedMat.getType()==26)
        {
            serializedMat.setShorts(new short[(int)(mat.total()*mat.elemSize())]);
            mat.get(0,0,serializedMat.shorts);
        }
        else if (serializedMat.getType()==3||
                serializedMat.getType()==11||
                serializedMat.getType()==19||
                serializedMat.getType()==27)
        {
            serializedMat.setShorts(new short[(int)(mat.total()*mat.elemSize())]);
            mat.get(0,0,serializedMat.shorts);
        }
        else if (serializedMat.getType()==4||
                serializedMat.getType()==12||
                serializedMat.getType()==20||
                serializedMat.getType()==28)
        {
            serializedMat.setInts(new int[(int)(mat.total()*mat.elemSize())]);
            mat.get(0,0,serializedMat.ints);
        }
        else if (serializedMat.getType()==5||
                serializedMat.getType()==13||
                serializedMat.getType()==21||
                serializedMat.getType()==29)
        {
            serializedMat.setFloats(new float[(int)(mat.total()*mat.elemSize())]);
            mat.get(0,0,serializedMat.floats);
        }
        else if (serializedMat.getType()==6||
                serializedMat.getType()==14||
                serializedMat.getType()==22||
                serializedMat.getType()==30)
        {
            serializedMat.setDoubles(new double[(int)(mat.total()*mat.elemSize())]);
            mat.get(0,0,serializedMat.doubles);
        }

        Gson gson = new Gson();
        return gson.toJson(serializedMat);
    }

    public static Mat DeserializeToMat(String json)
    {
        Gson gson = new Gson();
        SerializedMat serializedMat = gson.fromJson(json, SerializedMat.class);
        Mat mat = new Mat(serializedMat.getRows(),serializedMat.getCols(),serializedMat.getType());

        if (serializedMat.getType()==0||
                serializedMat.getType()==8||
                serializedMat.getType()==16||
                serializedMat.getType()==24)
        {
            mat.put(0,0,serializedMat.getBytes());
        }
        else if (serializedMat.getType()==1||
                serializedMat.getType()==9||
                serializedMat.getType()==17||
                serializedMat.getType()==25)
        {
            mat.put(0,0,serializedMat.getBytes());
        }
        else if (serializedMat.getType()==2||
                serializedMat.getType()==10||
                serializedMat.getType()==18||
                serializedMat.getType()==26)
        {
            mat.put(0,0,serializedMat.getShorts());
        }
        else if (serializedMat.getType()==3||
                serializedMat.getType()==11||
                serializedMat.getType()==19||
                serializedMat.getType()==27)
        {
            mat.put(0,0,serializedMat.getShorts());
        }
        else if (serializedMat.getType()==4||
                serializedMat.getType()==12||
                serializedMat.getType()==20||
                serializedMat.getType()==28)
        {
            mat.put(0,0,serializedMat.getInts());
        }
        else if (serializedMat.getType()==5||
                serializedMat.getType()==13||
                serializedMat.getType()==21||
                serializedMat.getType()==29)
        {
            mat.put(0,0,serializedMat.getFloats());
        }
        else if (serializedMat.getType()==6||
                serializedMat.getType()==14||
                serializedMat.getType()==22||
                serializedMat.getType()==30)
        {
            mat.put(0,0,serializedMat.getDoubles());
        }

        return mat;
    }

    private static class SerializedMat
    {
        byte[] bytes;
        short[] shorts;
        int[] ints;
        float[] floats;
        double[] doubles;

        int type;
        int rows;
        int cols;

        byte[] getBytes()
        {
            return bytes;
        }

        void setBytes(byte[] bytes)
        {
            this.bytes = bytes;
        }

        short[] getShorts()
        {
            return shorts;
        }

        void setShorts(short[] shorts)
        {
            this.shorts = shorts;
        }

        int[] getInts()
        {
            return ints;
        }

        void setInts(int[] ints)
        {
            this.ints = ints;
        }

        float[] getFloats()
        {
            return floats;
        }

        void setFloats(float[] floats)
        {
            this.floats = floats;
        }

        double[] getDoubles()
        {
            return doubles;
        }

        void setDoubles(double[] doubles)
        {
            this.doubles = doubles;
        }

        int getType()
        {
            return type;
        }

        void setType(int type)
        {
            this.type = type;
        }

        int getRows()
        {
            return rows;
        }

        void setRows(int rows)
        {
            this.rows = rows;
        }

        int getCols()
        {
            return cols;
        }

        void setCols(int cols)
        {
            this.cols = cols;
        }

        SerializedMat()
        {
        }
    }
}