是什么在C#.NET一个很好的像素化的算法?很好、算法、像素、NET

2023-09-03 21:06:19 作者:成王败寇 ζ

什么是好的算法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.