什么是好的算法pixelating图像在C#.NET?
What is a good algorithm for pixelating an image in C# .NET?
一个简单的,但效率不高的解决方案是调整到一个更小的尺寸,然后调整回用像素复制。
A simple, yet unefficient solution would be to resize to a smaller size, then resize back using pixel duplication.
有一个更好的解决办法(伪code): (时间为O(n),额外的空间(除了可变源图像):O(1))
A better solution would be (pseudo-code): (Time O(n), Additional space (besides mutable source image): O(1))
// Pixelize in x axis (choose a whole k s.t. 1 <= k <= Width)
var sum = Pixel[0, 0];
for (y = 0; y < Height; y++)
{
for (x = 0; x < Width + 1; x++)
{
if (x % k == 0)
{
sum /= k;
for (xl = Max(0, x-k); xl < x; xl++)
Pixel[y, xl] = sum;
sum = 0;
}
if (x == Width)
break;
sum += Pixel[y, x];
}
}
// Now do the same in the y axis
// (make sure to keep y the outer loop - for better performance)
// If your image has more than one channel, then then Pixel should be a struct.