【发布时间】:2017-02-01 21:22:01
【问题描述】:
我正在尝试读取我的图像有多少像素。它工作正常,但是它返回了错误的像素数。图像总共有 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 像素的完整图像吗?
【问题讨论】:
-
究竟是在哪里打印这些数字?
-
在控制台中,我使用的是 debug.log
-
我的意思是在你的代码中。根据您发布的内容,您打印的所有内容都是“我是白人”或“我是黑人”,而且您没有进行任何计数。那么你的数字是从哪里来的呢?
-
很确定你数错了,或者图像不是你认为的分辨率。对此代码的快速测试表明它循环遍历图像的每个像素,没有问题并且计数正确。
-
调试时
image.width和image.height是什么?