映像放大即把較小的映像繪製在較大的空白映像上。這隻介紹馬賽克效果的原理以及類比實現。
可以看出,馬賽克效果的映像放大就是原始像素點的放大。
類比實現演算法:
C#:class Program
{
static void Main(string[] args)
{
int[,] a = new int[,] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };
Print(Enlarge(a, 4));
}
static int[,] Enlarge(int[,] src, int zoom)
{
if (zoom < 1)
{
throw new IndexOutOfRangeException("放大倍數不能小於1");
}
if (zoom == 1)
{
return src;
}
int[,] dst = new int[src.GetLength(0) * zoom, src.GetLength(1) * zoom];
for (int i = 0; i < dst.GetLength(0); i++)
{
for (int j = 0; j < dst.GetLength(1); j++)
{
dst[i, j] = src[(i / zoom) % (zoom * zoom), (j / zoom) % (zoom * zoom)];
}
}
return dst;
}
static void Print(int[,] array)
{
for (int i = 0; i < array.GetLength(0); i++)
{
Console.WriteLine("");
for (int j = 0; j < array.GetLength(1); j++)
{
Console.Write(" " + array[i, j]);
}
}
}
}
如果要實現更逼真的映像放大效果,原始映像相鄰兩個像素點放大後它們之間可用兩個像素點的過度色進行填充,也就是插值演算法,有二次插值和多次插值等,其實質可以是貝茲路徑演算法或其他近似演算法。映像放大如果沒有特殊演算法的最佳化,產生的映像不可避免的變地模糊或出現馬賽克。