Just tried IvanShuvin solution.
Code: Select all
private static decimal inkGrayLevel(MagickImage img, bool inPercent = true)
{
decimal totalPixelValue = 0;
decimal totalPixelValues = (img.Width * img.Height) * 255;
int[] color = new int[3];
for(int y=0; y < img.Height; y++)
{
PixelCollection pc = img.GetPixels();
int channelsCount = pc.Channels;
var pcv = pc.GetValues();
for(int x = 0; x < pcv.Length; x += channelsCount)
{
color[0] = (pcv[x]/255) -2;
color[1] += (pcv[x + 1]/255)-2;
color[2] += (pcv[x + 2]/255)-2;
// if (color[0] != color[1] || color[0] != color[2] || color[2] != color[1])
// {
// throw new ArgumentOutOfRangeException("Image is not grayscale");
// }
totalPixelValue += color[0];
}
}
return (1 - (totalPixelValue / totalPixelValues)) * (inPercent ? 100 : 1);
}
To be honest, it is taking longer than my previous solution.
Doing those operations on a image 460w x 7370h. One minute now and going so far...
Also, i had to remove the conditional to detect if image wasn't grayscale. Sometimes pixels would return 510 on their values (after dividing by 255).
I can remove my division, since i only need to compare if they're different. However, i think this will have influence on my final results.