图像放大即把较小的图像绘制在较大的空白图像上。这只介绍马赛克效果的原理以及模拟实现。
上图可以看出,马赛克效果的图像放大就是原始像素点的放大。
模拟实现算法:
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]);
}
}
}
}
{
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]);
}
}
}
}
如果要实现更逼真的图像放大效果,原始图像相邻两个像素点放大后它们之间可用两个像素点的过度色进行填充,也就是插值算法,有二次插值和多次插值等,其实质可以是贝塞尔曲线算法或其他近似算法。图像放大如果没有特殊算法的优化,生成的图像不可避免的变地模糊或出现马赛克。