C#双线性插值

news/2024/7/21 5:39:19 标签: c#, 图像处理

根据维基百科的定义:
双线性插值,又称为双线性内插。在数学上,双线性插值是对线性插值在二维直角网格上的扩展,用于对双变量函数(例如 x 和 y)进行插值。其核心思想是在两个方向分别进行一次线性插值。

定义一个函数方法ReClass

 //使用双线性插值法进行重采样
        private Bitmap ReClass(Bitmap bitmap)
        {
            Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            BitmapData bmpData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, bitmap.PixelFormat);
            IntPtr ptr = bmpData.Scan0;
            //得到横坐标和纵坐标的缩放量
            double x =2;
            double y =2.5;
            //图像的几何中心
            int halfHeight = (int)(bitmap.Height / 2);
            int halfWidth = (int)(bitmap.Width / 2);
            int xz = 0;
            int yz = 0;
            int tempWidth = 0;
            int tempHeight = 0;
            int bytes = bitmap.Width * bitmap.Height;
            byte[] grayValues = new byte[bytes];
            byte[] tempArray = new byte[bytes];
            double tempX, tempY, p, q;
            for (int i = 0; i < bitmap.Height; i++)
            {
                for (int j = 0; j < bitmap.Width; j++)
                {
                    //以图像的几何中心为坐标进行坐标变换
                    //按逆向映射法得到输入图像的坐标
                    tempHeight = i - halfHeight;
                    tempWidth = j - halfWidth;
                    tempX = tempWidth / x;
                    tempY = tempHeight / y;
                    

                    //在不同象限内进行取整处理
                    if (tempWidth > 0)
                    {
                        xz = (int)tempX;

                    }
                    else
                    {
                        xz = (int)(tempX - 1);
                    }
                    if (tempHeight > 0)
                    {
                        yz = (int)tempY;
                    }
                    else
                    {
                        yz = (int)(tempY - 1);
                    }
                    //得到灰度插值法的公式中的变量p和q
                    p = tempX - xz;
                    q = tempY - yz;

                    //坐标逆变换
                    tempWidth = xz + halfWidth;
                    tempHeight = yz + halfHeight;

                    if (tempWidth < 0 || (tempWidth + 1) >= bitmap.Width || tempHeight < 0 || (tempHeight + 1) >= bitmap.Height)
                    {
                        //缩放后留下空白的空间用白色像素代替
                        tempArray[i * bitmap.Width + j] = 255;
                    }
                    else
                    {
                        tempArray[i * bitmap.Width + j] = (byte)((1.0 - q) * (1.0 - p) * grayValues[tempHeight * bitmap.Width + tempWidth] + p*grayValues[tempWidth*bitmap.Width+tempWidth+1]+q*(1.0-p)*grayValues[(tempHeight+1)*bitmap.Width+tempWidth]+p*grayValues[(tempHeight+1)*bitmap.Width+1+tempWidth]);
                    }        
                    
                }
            }
            grayValues = (byte[])tempArray.Clone();
            System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
            bitmap.UnlockBits(bmpData);
            return bitmap;
        }

然后在主函数中对这个方法进行应用

picturebox1.Image = ReClass(curBitmap);//curBitmap代表但前的二进制图像

http://www.niftyadmin.cn/n/1783388.html

相关文章

递增运算符重载

1.作用&#xff1a;通过重载递增运算符&#xff0c;实现自己的整型数据 2.前置递增返回引用&#xff0c;后置递增返回值 #include<iostream> using namespace std; #include<string>//重载递增运算符 //自定义整型 class MyInteger {friend ostream &operato…

Java那些事之正则表达式

今天说说正则表达式。这可是写程序经常遇到的&#xff0c;也是一个程序员必须掌握的技术。其实不只是java&#xff0c;任何的技术任何的语言都离不开正则表达式&#xff0c;而且他们得形式都大同小异&#xff0c;基本上是一样的。 下面先说说正则表达式&#xff0c;这里推荐一篇…

如何卸载Windows 7中的IE10并还原到IE9

今日微软终于发布了 IE10 For Windows 7 版本的下载&#xff0c;由于该版本为预览版&#xff08;Release Preview&#xff09;&#xff0c;虽然安装以后可以使用到 IE10 的新功能&#xff0c;包括增强的安全性和性能&#xff0c;但与使用早期的 IE 浏览器版本相比&#xff0c;可…

C#截取picturebox的一部分图片

//m_Bitmap是当前图片//创建新图位图Bitmap bitmap new Bitmap(m_Bitmap.Width , m_Bitmap.Height / 2);//创建作图区域Graphics graphic Graphics.FromImage(bitmap);//截取原图相应区域写入作图区graphic.DrawImage(m_Bitmap, 0, 0, new Rectangle(m_Bitmap.Width / 5, m_B…

C#自动阈值选择:迭代法

迭代法的基本思想是&#xff1a; 开始选择一个阈值作为初始估计值&#xff0c;然后按某种策略通过迭代不断地改变这一估计值&#xff0c;直到满足给定的准则为止。 步骤&#xff1a; 1、在一幅灰度范围为[0,L-1]的图像中&#xff0c;选择灰度图像的中值作为初始阈值T0,其中图像…

C++十种方法Hello World

2019独角兽企业重金招聘Python工程师标准>>> 初学编程&#xff0c;无论是VB&#xff0c;C/C&#xff0c;Java&#xff0c;C#大多都是从Hellow World这个程序开 始的&#xff0c;也是最常见的入门方法。C/C本身有很多特性和用发&#xff0c;这里就用十种方法 实现Hel…

2011/7/18,知识点

1、BGP选路原则&#xff1a;&#xff08;红色为可以人为修改&#xff09;Prefer highest weight (local to router).Prefer highest local preference (global within AS).Prefer route originated by the local router (next hop 0.0.0.0).Prefer shortest AS path.Prefer lo…

【赠书活动 - 第1期】- 测试工程师Python开发实战(异步图书出品)| 文末送书

⭐️ 赠书 - 测试工程师Python开发实战&#xff08;异步图书出品&#xff09; 当初就是因为开发做不好&#xff0c;才去做测试了…… 这句玩笑话在过去可以说是测试人员的真实写照。 常规测试工作给人的印象&#xff0c;就是弄清楚软件功能&#xff0c;编写测试用例&#xff0…