Reputation: 361
Im trying to read how many pixels my image has. It's working fine however it's returning a wrong pixel number. The image has 400 pixels in total and im only getting 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");
}
}
}
}
it is printing 148 for white and 108 for black.. 148 + 108 = 256. So there are alot of pixels missing. Any idea why its not reading the full image which is 400 pixels?
Upvotes: 2
Views: 12570
Reputation: 9244
Try this instead:
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));
Pretty sure your output lines are just being truncated.
BTW, "GetPixel" is notoriously slow, but that's another story.
Upvotes: 2
Reputation: 424
I think the issue is that the rest of the pixels are not black or white, just a small tint that the eye can not see. Try debugging image.width * image.height to get the pixel count of the image.
EDIT: You should probably take a look at the max size inside the inspector of the image. It may be locked on 256.
~ Menno
Upvotes: 2