在python中导入opencv mat(用c#压缩)

Import opencv mat in python (compressed with c#)

我遇到了 decoding/unzipping python 中最初使用 C# 压缩的字符串的问题 使用“GZipStream”。

“opencv mat”已转换为 json,其中:

public override void WriteJson(JsonWriter writer, Mat value, JsonSerializer serializer)
{
    byte[] matBytes = null;
    Cv2.ImEncode(".png", value, out matBytes);

    // Convert byte[] to Base64 String
    string matBase64 = Convert.ToBase64String(matBytes);

    var resCompressed = Utils.StringCompressor.CompressString(matBase64);
    writer.WriteValue(resCompressed);
}

要解压缩的示例字符串“jsontoken”是:

uAoAAB+LCAAAAAAABADtls3SsrgWhS+IASogOOiB/GgS/gkqMlMUgkFEEGK8+o7v1931DU7VqTqzU9UMfDS1srPY2dmx3pthwmbutnqsxRPgHXF2lfj28xNU1vrniceHr34HPrPGifeJur4q5nqHdocErlnx0YH8eA2fZwhrsz4q2epTEWRSyjESqDjO0VogzpFPK/yDWgD69I5jJLDFKYLOL1ABGyFKLYGfIOa/sf6XWJGbAaDW9kzmUryc6FDcpQMGmbYGaKIGka8J4ZmGYFnC9co1ahpOs+JKpXRhE6O6zQg/qtdIzLuNp9YGmgWgfr8wTcImDzVaBAF3wkTxU5KP2MSRflTPZ1rsbtInBAttu4HRqLPTNX5jW78bfkudHZEWEphoOKYyOtsKTzKk3GU2N9o5z7Xe8OGYeih8L+4c7JEy7hi2NM8gS3DIUELbQv0gv6f9x1ZjHZq7RtKzdakBSCflpG4U+KaNsVTWkrYZ7hMIl24GW7phmpLUmmP0Ys0c9NT7vAogU79OpvDFbmPco4PQ4KdmGo+vBnlCQwpPplaNp3DF5oudzMchWFF5J5bvi3DVhMazF9qoQa6heZal4732OTjmguqfeoBgQG27OcZRnpYuZ2zJ72PSkPecBkXr2XSCeQczcDmJBMVT+GSknjXXS0DsVfcTEOrWXmMHJ13sJl4N/nPoztQ8Xva5pSCLLXW+OeH2vTjQ04xGn2GA8xcq2/Mx6U5YDjG71ayRioaAVYOM15vAy3/gILj6RbQXfEZ22rtYW19Bd4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cd/H/ey//G+q+x5KpcbVeiPaTR+Gbfv6AmSnaa01NUVdUff/wJwhGNxbgKAAA=

加载此数据的相应 C# 代码是:

private static void CreateDataFromJsonZip(ref ThresholdMask mask, JObject jsonObj, string maskName)
{
    // get JSON result objects into a list
    JToken jsonToken = jsonObj["Selection"]; # jsonToken

    var res = Utils.StringCompressor.DecompressString(jsonToken.ToString());
    byte[] data = Convert.FromBase64String(res);
    mask.Selection = Cv2.ImDecode(data, ImreadModes.Unchanged);

我尝试读取 python 中的数据是:

jsonToken = r'uAoAAB+LCAAAAAAABADtls3SsrgWhS+IASogOOiB/GgS/gkqMlMUgkFEEGK8+o7v1931DU7VqTqzU9UMfDS1srPY2dmx3pthwmbutnqsxRPgHXF2lfj28xNU1vrniceHr34HPrPGifeJur4q5nqHdocErlnx0YH8eA2fZwhrsz4q2epTEWRSyjESqDjO0VogzpFPK/yDWgD69I5jJLDFKYLOL1ABGyFKLYGfIOa/sf6XWJGbAaDW9kzmUryc6FDcpQMGmbYGaKIGka8J4ZmGYFnC9co1ahpOs+JKpXRhE6O6zQg/qtdIzLuNp9YGmgWgfr8wTcImDzVaBAF3wkTxU5KP2MSRflTPZ1rsbtInBAttu4HRqLPTNX5jW78bfkudHZEWEphoOKYyOtsKTzKk3GU2N9o5z7Xe8OGYeih8L+4c7JEy7hi2NM8gS3DIUELbQv0gv6f9x1ZjHZq7RtKzdakBSCflpG4U+KaNsVTWkrYZ7hMIl24GW7phmpLUmmP0Ys0c9NT7vAogU79OpvDFbmPco4PQ4KdmGo+vBnlCQwpPplaNp3DF5oudzMchWFF5J5bvi3DVhMazF9qoQa6heZal4732OTjmguqfeoBgQG27OcZRnpYuZ2zJ72PSkPecBkXr2XSCeQczcDmJBMVT+GSknjXXS0DsVfcTEOrWXmMHJ13sJl4N/nPoztQ8Xva5pSCLLXW+OeH2vTjQ04xGn2GA8xcq2/Mx6U5YDjG71ayRioaAVYOM15vAy3/gILj6RbQXfEZ22rtYW19Bd4gbHhvhwWi2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q+x5KpcbVeiPaTR+Gbfv6AmSnaa01NUVdUff/wJwhGNxbgKAAA='

string = jsonToken
original = base64.b64decode(string)
convert_to_np = np.frombuffer(original, dtype=np.uint8)
print(convert_to_np)
len(convert_to_np)
img = cv2.imdecode(convert_to_np, cv2.IMREAD_UNCHANGED)

不幸的是,最后一步 imdecode 不起作用,结果是 None。 有什么提示吗?

这将完全按照您的要求执行,遵循 this SO answer 中的 StringCompressor.DecompressString 代码(解压缩的长度存储在前 4 个字节中):

original = base64.b64decode(gzip.decompress(base64.b64decode(jsonToken)[4:]))

但是,我建议您简化 C# 实现。从原始 OpenCV 垫开始,您:

  1. 将其编码为 PNG 图片
  2. 将该二进制数据转换为 base64 字符串
  3. 用 gzip 压缩文本字符串
  4. 再次转换为 base64
  5. 将其存储到 json 文件

步骤 2 完全没有必要,步骤 3 是多余的,因为 PNG 编码过程已经压缩了图像数据。添加另一层压缩不会有太大帮助,甚至可能会使情况变得更糟。

我只是将其转换为 PNG,使用 base64 进行编码,然后将其写入 json 文件。然后您的原始 Python 代码将按预期工作。