C#双三次插值缩放图像

        图像缩放操作中,输出图像像素点坐标有可能对应于输入图像上几个像素点之间的位置,需要通过灰度插值处理来计算出该输出点的灰度值。不同的插值算法有不同的精度,直接影响着图像的失真程度。最常用的插值算法有三种:最近邻插值、双线性插值、双三次插值,其中双三次插值的效果是最佳的。

        由于图像像素的灰度值是离散的, 因此一般的处理方法是对原来在整数点坐标上的像素值进行插值生成连续的曲面, 然后在插值曲面上重新采样以获得缩放图像像素的灰度值。缩放处理从输出图像出发,采用逆向映射方法,即在输出图像中找到与之对应的输入图像中的某个或某几个像素,采用这种方法能够保证输出图像中的每个像素都有一个确定值,否则,如果从输入图像出发来推算输出图像,输出图像的像素点可能出现无灰度值的情况。因为,对图像进行缩放处理时输出图像像素和输入图像之间可能不再存在着一一对应关系。

        双三次插值不仅考虑到周围四个直接相邻像素点灰度值的影响,还考虑到它们灰度值变化率的影响。在这种方法中,函数 f 在点(x, y) 的值可以通过矩形网格中最近的十六个采样点的加权平均得到,在这里需要使用两个多项式插值三次函数,每个方向使用一个。通过双三次插值可以得到一个连续的插值函数,它的一阶偏导数连续,并且交叉导数处处连续。

        双三次插值核如下:

        这里参考AForge.NET实现了双三次插值算法,并将实验结果与GDI+及EmguCV自带的双三次插值算法效率进行比较(缩放同一幅图像到相同尺寸,计算平均耗时,同时参考缩放效果)。

        (1)双三次插值算法实现代码:

[csharp]  view plain  copy
  1. /// <summary>  
  2. /// 双三次插值缩放灰度图像。  
  3. /// </summary>  
  4. /// <param name="image">源图像。</param>  
  5. /// <param name="newWidth">新宽度。</param>  
  6. /// <param name="newHeight">新高度。</param>  
  7. /// <returns>缩放后的图像。</returns>  
  8. public static Bitmap ResizeGrayscaleImage(Bitmap image, int newWidth, int newHeight)  
  9. {  
  10.     // 检查源图像格式  
  11.     CheckSourceFormat(image);  
  12.   
  13.     // 锁定源图像内存  
  14.     BitmapData srcData = image.LockBits(  
  15.         new Rectangle(0, 0, image.Width, image.Height),  
  16.         ImageLockMode.ReadOnly, image.PixelFormat);  
  17.   
  18.     // 新建目标图像  
  19.     Bitmap dstImage = CreateGrayscaleImage(newWidth, newHeight);  
  20.     // 锁定目标图像内存  
  21.     BitmapData dstData = dstImage.LockBits(  
  22.         new Rectangle(0, 0, newWidth, newHeight),  
  23.         ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);  
  24.   
  25.     try  
  26.     {  
  27.         Resize(srcData, ref dstData);  
  28.     }  
  29.     finally  
  30.     {  
  31.         // 解锁图像内存  
  32.         dstImage.UnlockBits(dstData);  
  33.         image.UnlockBits(srcData);  
  34.     }  
  35.   
  36.     return dstImage;  
  37. }  
  38.   
  39. /// <summary>  
  40. /// 双三次插值处理缩放。  
  41. /// </summary>  
  42. /// <param name="srcData">源图像数据。</param>  
  43. /// <param name="dstData">目标图像数据。</param>  
  44. private static void ResizeProcess(BitmapData srcData, ref BitmapData dstData)  
  45. {  
  46.     // 获取源图像数据  
  47.     int srcWidth  = srcData.Width;  
  48.     int srcHeight = srcData.Height;  
  49.     int srcStride = srcData.Stride;  
  50.     IntPtr srcPtr = srcData.Scan0;  
  51.   
  52.     // 获取目标图像数据  
  53.     int dstWidth  = dstData.Width;  
  54.     int dstHeight = dstData.Height;  
  55.     int dstStride = dstData.Stride;  
  56.     int dstOffset = dstStride - dstWidth;  
  57.     IntPtr dstPtr = dstData.Scan0;  
  58.   
  59.     // 计算比例系数  
  60.     double xFactor = (double)srcWidth / dstWidth;  
  61.     double yFactor = (double)srcHeight / dstHeight;  
  62.   
  63.     // 将源图像数据复制到托管内存中  
  64.     int srcBytes = srcStride * srcHeight;  
  65.     byte[] srcGrayData = new byte[srcBytes];  
  66.     Marshal.Copy(srcPtr, srcGrayData, 0, srcBytes);  
  67.     // 保存目标图像数据  
  68.     int dstBytes = dstStride * dstHeight;  
  69.     byte[] dstGrayData = new byte[dstBytes];  
  70.     int dst = 0;    // 下标  
  71.   
  72.     // 源图像坐标点及系数  
  73.     double ox, oy, dx, dy, k1, k2;  
  74.     int ox1, oy1, ox2, oy2;  
  75.     // 目标图像像素值  
  76.     double grayValue;  
  77.     // 边界  
  78.     int ymax = srcHeight - 1;  
  79.     int xmax = srcWidth - 1;  
  80.  
  81.     #region 插值  
  82.     for (int y = 0; y < dstHeight; y++)  
  83.     {  
  84.         // Y坐标  
  85.         oy = (double)y * yFactor - 0.5;  
  86.         oy1 = (int)oy;  
  87.         dy = oy - (double)oy1;  
  88.   
  89.         for (int x = 0; x < dstWidth; x++, dst++)  
  90.         {  
  91.             // X坐标  
  92.             ox = (double)x * xFactor - 0.5f;  
  93.             ox1 = (int)ox;  
  94.             dx = ox - (double)ox1;  
  95.   
  96.             // 像素值归零  
  97.             grayValue = 0;  
  98.   
  99.             for (int n = -1; n < 3; n++)  
  100.             {  
  101.                 // Y系数  
  102.                 k1 = BiCubicInterpolator(dy - (double)n);  
  103.   
  104.                 oy2 = oy1 + n;  
  105.                 if (oy2 < 0)  
  106.                     oy2 = 0;  
  107.                 if (oy2 > ymax)  
  108.                     oy2 = ymax;  
  109.   
  110.                 for (int m = -1; m < 3; m++)  
  111.                 {  
  112.                     // X系数  
  113.                     k2 = k1 * BiCubicInterpolator((double)m - dx);  
  114.   
  115.                     ox2 = ox1 + m;  
  116.                     if (ox2 < 0)  
  117.                         ox2 = 0;  
  118.                     if (ox2 > xmax)  
  119.                         ox2 = xmax;  
  120.   
  121.                     grayValue += k2 * srcGrayData[oy2 * srcStride + ox2];  
  122.                 }  
  123.             }  
  124.             dstGrayData[dst] = (byte)Math.Max(0, Math.Min(255, grayValue));  
  125.         }  
  126.         dst += dstOffset;  
  127.     }  
  128.     Marshal.Copy(dstGrayData, 0, dstPtr, dstBytes);  
  129.     #endregion  
  130. }  
  131.   
  132. /// <summary>  
  133. /// 双三次插值器。  
  134. /// coefficient is set to -0.5.  
  135. /// </summary>  
  136. /// <param name="x">X Value.</param>  
  137. /// <returns>Bicubic cooefficient.</returns>  
  138. private static double BiCubicInterpolator(double x)  
  139. {  
  140.     if (x < 0)  
  141.     {  
  142.         x = -x;  
  143.     }  
  144.   
  145.     double biCoef = 0;  
  146.   
  147.     if (x <= 1)  
  148.     {  
  149.         biCoef = (1.5 * x - 2.5) * x * x + 1;  
  150.     }  
  151.     else if (x < 2)  
  152.     {  
  153.         biCoef = ((-0.5 * x + 2.5) * x - 4) * x + 2;  
  154.     }  
  155.   
  156.     return biCoef;  
  157. }  
  158.   
  159. /// <summary>  
  160. /// 检查格式。  
  161. /// </summary>  
  162. /// <param name="original">图像。</param>  
  163. private static void CheckSourceFormat(Bitmap original)  
  164. {  
  165.     if ((original.PixelFormat != PixelFormat.Format8bppIndexed) ||  
  166.         (IsGrayscale(original) == false))  
  167.     {  
  168.         throw new Exception("Source pixel format is not supported.");  
  169.     }  
  170. }  
  171.   
  172. /// <summary>  
  173. /// 判断位图是不是8位灰度。  
  174. /// </summary>  
  175. /// <param name="original">位图。</param>  
  176. /// <returns>判断结果。</returns>  
  177. public static bool IsGrayscale(Bitmap original)  
  178. {  
  179.     bool ret = false;  
  180.   
  181.     // 检查像素格式  
  182.     if (original.PixelFormat == PixelFormat.Format8bppIndexed)  
  183.     {  
  184.         ret = true;  
  185.         // 检查调色板  
  186.         ColorPalette palette = original.Palette;  
  187.         Color color;  
  188.           
  189.         for (int i = 0; i < 256; i++)  
  190.         {  
  191.             color = palette.Entries[i];  
  192.             if ((color.R != i) || (color.G != i) || (color.B != i))  
  193.             {  
  194.                 ret = false;  
  195.                 break;  
  196.             }  
  197.         }  
  198.     }  
  199.     return ret;  
  200. }  
  201.   
  202. /// <summary>  
  203. /// 新建8位灰度位图。  
  204. /// </summary>  
  205. /// <param name="width">长。</param>  
  206. /// <param name="height">宽。</param>  
  207. /// <returns>新建8位灰度位图。</returns>  
  208. public static Bitmap CreateGrayscaleImage(int width, int height)  
  209. {  
  210.     // 新建图像  
  211.     Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);  
  212.     // 设置灰度图像的调色板  
  213.     SetGrayscalePalette(bitmap);  
  214.       
  215.     return bitmap;  
  216. }  
  217.   
  218. /// <summary>  
  219. /// 设置灰度位图调色板。  
  220. /// </summary>  
  221. /// <param name="original">灰度位图。</param>  
  222. public static void SetGrayscalePalette(Bitmap original)  
  223. {  
  224.     // 检查像素格式  
  225.     if (original.PixelFormat != PixelFormat.Format8bppIndexed)  
  226.         throw new Exception("Source image is not 8 bpp image.");  
  227.   
  228.     // 获取调色板  
  229.     ColorPalette palette = original.Palette;  
  230.     // init palette  
  231.     for (int i = 0; i < 256; i++)  
  232.     {  
  233.         palette.Entries[i] = Color.FromArgb(i, i, i);  
  234.     }  
  235.     // 设置调色板  
  236.     original.Palette = palette;  
  237. }  

        算法效率:


图1.源图像


图2.ResizeGrayscaleImage方法耗时

        (2)用GDI+的双三次插值算法实现代码:

[csharp]  view plain  copy
  1. /// <summary>  
  2. /// 使用GDI+缩放图像。  
  3. /// </summary>  
  4. /// <param name="original">要缩放的图像。</param>  
  5. /// <param name="newWidth">新宽度。</param>  
  6. /// <param name="newHeight">新高度。</param>  
  7. /// <returns>缩放后的图像。</returns>  
  8. public static Bitmap ResizeUsingGDIPlus(Bitmap original, int newWidth, int newHeight)  
  9. {  
  10.     try  
  11.     {  
  12.         Bitmap bitmap = new Bitmap(newWidth, newHeight);  
  13.         Graphics graphics = Graphics.FromImage(bitmap);  
  14.   
  15.         // 插值算法的质量  
  16.         graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;  
  17.         graphics.SmoothingMode = SmoothingMode.HighQuality;  
  18.         graphics.DrawImage(original, new Rectangle(0, 0, newWidth, newHeight),   
  19.             new Rectangle(0, 0, original.Width, original.Height), GraphicsUnit.Pixel);  
  20.         graphics.Dispose();  
  21.   
  22.         return bitmap;  
  23.     }  
  24.     catch  
  25.     {  
  26.         return null;  
  27.     }  
  28. }  

        算法效率:


图3.GDI+方法耗时

        (3)用EmguCV的双三次插值算法实现代码:

[csharp]  view plain  copy
  1. /// <summary>  
  2. /// 使用EmguCV缩放图像。  
  3. /// </summary>  
  4. /// <param name="original">要缩放的图像。</param>  
  5. /// <param name="newWidth">新宽度。</param>  
  6. /// <param name="newHeight">新高度。</param>  
  7. /// <returns>缩放后的图像。</returns>  
  8. public static Bitmap ResizeUsingEmguCV(Bitmap original, int newWidth, int newHeight)  
  9. {  
  10.     try  
  11.     {  
  12.         Emgu.CV.Image<Emgu.CV.Structure.Gray, byte> image =  
  13.             new Emgu.CV.Image<Emgu.CV.Structure.Gray, byte>(original);  
  14.         Emgu.CV.Image<Emgu.CV.Structure.Gray, byte> newImage = image.Resize(  
  15.             newWidth, newHeight, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);  
  16.         return newImage.Bitmap;  
  17.     }  
  18.     catch  
  19.     {  
  20.         return null;  
  21.     }  
  22. }  

        算法效率:

(以上代码为封装在静态类中,直接使用EmguCV要快的多,eg.这个实验耗时为封装后的三分之一)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值