c# - 团结: read image pixels

标签 c# unity-game-engine

我正在尝试读取我的图像有多少像素。它工作正常,但返回错误的像素数。该图像总共有 400 个像素,我只得到 256 个。

private void Pixelreader()
{
    // Load image
    Texture2D image = (Texture2D)Resources.Load(texture);
    Debug.Log(image);

    // Iterate through it's pixels
    for (int i = 0; i < image.width; i++)
    {
        for (int j = 0; j < image.height; j++)
        { 
            Color pixel = image.GetPixel(i, j);

            // if it's a white color then just debug...
            if (pixel == Color.white)
            {
                Debug.Log("Im white");
            }
            else 
            {
                Debug.Log("Im black");
            }
        }
    }
}

白色打印 148,黑色打印 108。148 + 108 = 256。所以有很多像素丢失。知道为什么它不读取 400 像素的完整图像吗?

最佳答案

试试这个:

var whitePixels = 0;
var blackPixels = 0;
for (int i = 0; i < image.width; i++)
    for (int j = 0; j < image.height; j++)
    { 
        Color pixel = image.GetPixel(i, j);

        // if it's a white color then just debug...
        if (pixel == Color.white)
          whitePixels++;
        else 
          blackPixels++;
    }
Debug.Log(string.Format("White pixels {0}, black pixels {1}", whitePixels, blackPixels));

非常确定您的输出行刚刚被截断。

顺便说一句,“GetPixel”是出了名的慢,但那是另一个故事了。

关于c# - 团结: read image pixels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41979055/

相关文章:

C# Unity 下拉菜单,在更改调用函数时,函数查询列表返回匹配的列表项

c# - Entity Framework 代码优先 : CASCADE DELETE for same table many-to-many relationship

c# - Windows Phone 8 中缺少 ButtonBase.Click

api - Unity 表示 API 级别太低

unity-game-engine - 如果宽度超过 Unity2D 容器大小,则水平缩小文本

react-native - Unity AR 与 React Native (iOS) 的集成

c# - ASP.NET Core - 覆盖默认的 ControllerFactory

c# - PC XNA 游戏转换为 Xbox 360 - 巨大的性能问题

c# - 即使类被多次调用,如何使构造函数只被调用一次?

android - IOS 和 Android (Unity) 开发的差异