图像缩放函数,算法:双线型内插值算法

来源:互联网 发布:数据库管理系统dbms是 编辑:程序博客网 时间:2024/05/16 01:36
  /// <summary>
       /// 图像缩放函数,算法:双线型内插值算法
       /// </summary>
       /// <param name="imgSrc">要处理的图像Image<Bgr, Byte></param>
       /// <param name="width">目标宽度</param>
       /// <param name="height">目标高度</param>
       /// <returns>返回结果</returns>
       private Image<Bgr, Byte> convert(Image<Bgr, Byte> imgSrc, int width, int height)
       {
           Bitmap oldBmp = imgSrc.Bitmap;
           Bitmap newBmp = new Bitmap(width, height);
           float kc = ((float)imgSrc.Width) / width;
           float kr = ((float)imgSrc.Height) / height;
           Image<Bgr, Byte> newImg = null;
           for (int x = 0; x < width; x++)
           {
               for (int y = 0; y < height; y++)
               {
                   //双线型内插值算法
                   int x_des = (int)Math.Floor(x * kc);
                   int y_des = (int)Math.Floor(y * kr);

                   float x_weight = x * kc - x_des;
                   float y_weight = y * kr - y_des;
                   Color color11 = oldBmp.GetPixel(x_des, y_des);
                   Color color12;
                   Color color21;
                   Color color22;
                   Color color;
                   if (x_des + 1 < oldBmp.Width && y_des + 1 < oldBmp.Height)
                   {
                       color12 = oldBmp.GetPixel(x_des + 1, y_des);
                       color21 = oldBmp.GetPixel(x_des, y_des + 1);
                       color22 = oldBmp.GetPixel(x_des + 1, y_des + 1);
                       color = Color.FromArgb(
                           (int)Math.Round((1 - y_weight) * ((1 - x_weight) * color11.A + x_weight * color12.A) + y_weight * ((1 - x_weight) * color21.A + x_weight * color22.A)),
                           (int)Math.Round((1 - y_weight) * ((1 - x_weight) * color11.R + x_weight * color12.R) + y_weight * ((1 - x_weight) * color21.R + x_weight * color22.R)),
                           (int)Math.Round((1 - y_weight) * ((1 - x_weight) * color11.G + x_weight * color12.G) + y_weight * ((1 - x_weight) * color21.G + x_weight * color22.G)),
                           (int)Math.Round((1 - y_weight) * ((1 - x_weight) * color11.B + x_weight * color12.B) + y_weight * ((1 - x_weight) * color21.B + x_weight * color22.B))
                           );
                   }
                   else if (x_des + 1 < oldBmp.Width)
                   {
                       color12 = oldBmp.GetPixel(x_des + 1, y_des);
                       color = Color.FromArgb(
                           (int)Math.Round(((1 - x_weight) * color11.A + x_weight * color12.A)),
                           (int)Math.Round(((1 - x_weight) * color11.R + x_weight * color12.R)),
                           (int)Math.Round(((1 - x_weight) * color11.G + x_weight * color12.G)),
                           (int)Math.Round(((1 - x_weight) * color11.B + x_weight * color12.B))
                           );
                   }
                   else if (y_des + 1 < oldBmp.Height)
                   {
                       color21 = oldBmp.GetPixel(x_des, y_des + 1);
                       color = Color.FromArgb(
                           (int)Math.Round((1 - y_weight) * color11.A  + y_weight * color21.A),
                           (int)Math.Round((1 - y_weight) * color11.R  + y_weight * color21.R),
                           (int)Math.Round((1 - y_weight) * color11.G  + y_weight * color21.G),
                           (int)Math.Round((1 - y_weight) * color11.B  + y_weight * color21.B)
                           );
                   }
                   else
                   {
                       color = Color.FromArgb(
                           color11.A,
                           color11.R,
                           color11.G,
                           color11.B
                           );
                   }
                   newBmp.SetPixel(x, y, color);
               }
           }
           newImg = new Image<Bgr, Byte>(newBmp);
           return newImg;
       }