【问题标题】:OpenCVSharp ImRead() function can`t load Image in Unity on macOSOpenCVSharp ImRead() 函数无法在 macOS 上的 Unity 中加载图像
【发布时间】:2022-03-15 14:06:58
【问题描述】:

我正在尝试使用Cv2.ImRead() 函数从磁盘加载我的图像。我已调试该矩阵为空。我尝试使用另一个 c# 函数或统一的 texture2d 函数加载该图像,并且成功。那么问题出在哪里呢?为什么那个矩阵是空的?我知道该文件没有成功加载,但为什么其他功能可以做到这一点。

文件确实存在并且没问题 - 另一个不是 openCVSharp 的函数将加载该图像。例如 Unity 函数 Texture2D.LoadImage(在我将该图片加载到字节后)或 c# Image.FromFile

非常感谢您的任何想法。

Mat img = Cv2.ImRead("object.png"); // also jpg is not working, also tried absolute path - same result
UnityEngine.Debug.Log(img.Empty()); // returns true
UnityEngine.Debug.Log(img.Width); // returns 0

【问题讨论】:

  • Related: stackoverflow.com/q/54697425/7111561 你确定这样的路径是正确的吗?
  • 是的,当我写File.Exists("object.png"); 时,响应是正确的,我也尝试像统一纹理一样加载它,它完全没问题。我也尝试了相对路径和绝对路径,同样的结果 img 是空的。我已经阅读了关于 stackoverflow 的几乎所有这些答案,我知道几乎每个Cv2.ImRead() 问题都在路径中,但是为什么其他方法可以成功加载它呢?我真的不知道该怎么办。
  • File.Exists,尤其是 Unity 加载方法可能使用相对数据路径,而 CV2 可能总是需要绝对路径
  • 是的,但我尝试过使用Cv2.ImRead() 的相对和绝对路径,也尝试过File.Exists() 的绝对和相对路径,但结果就像File.Exists() 让我都知道了,但是`Cv2. ImRead()' 两者都为空:/

标签: c# unity3d opencvsharp


【解决方案1】:

我有同样的问题。已成功将图像字节加载到 Texture2D 中,然后使用该 Texture2D 中的数据填充 Mat 的字节。您可以手动执行此操作,或者资产商店有一个package that does TextureToMat

    private static Mat matImageFile(string filePath)
    {
        Mat matResult = null;
        if (File.Exists(filePath))
        {
            // load into Mat type. Hack: workaround for Cv2.ImRead() being broke.
            byte[] fileData = File.ReadAllBytes(filePath);
            var tex = new Texture2D(2, 2);
            tex.LoadImage(fileData); // this will auto-resize the 2,2 texture dimensions.
            matResult = OpenCvSharp.Unity.TextureToMat(tex);
            Cv2.CvtColor(matResult, matResult, ColorConversionCodes.BGR2GRAY);
        }
        return matResult;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 2019-01-23
    • 2021-05-31
    • 1970-01-01
    • 2022-09-27
    相关资源
    最近更新 更多