使用C#实现双线性插值、最近邻法、双三次卷积法插值,目标是矩阵而不是图像,锻炼编写程序的能力。数据来源采用读取txt的方式。
【同班同学不要来抄我的。】
1、最近邻法插值:
目标矩阵:
[1, 1
2 ,2]
实现代码:
using System;
namespace 最近邻像元法
{
class 最近邻像元法
{
static void Main(string[] args)
{
Console.WriteLine("最邻近像元法");
float[,] ZB;
ZB = new float[,] { { 10, 20 }, { 30, 40 } };
Console.WriteLine("四个像素的值为:");
int i, j;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
Console.Write(ZB[i, j] + " ");
}
Console.WriteLine();
}
float x = 0.4F;
float y = 0.3F;
Console.WriteLine("采样点的坐标为:" + "(" + x + "," + y + ")");
float result;
int Xn = (int)(x + 0.5);
int Yn = (int)(y + 0.5);
result = ZB[Xn, Yn];
Console.WriteLine("采样结果为:" + result);//输出结果
Console.ReadLine();
}
}
}
2、双线性内插
目标2x2矩阵
1 1
2 2
代码:
采用读取txt的方式,比较复杂。
using System;
using System.IO;
namespace 内插1
{
class 双线性内插
{
static void Main(string[] args)
{
// float[,] ZB;
// ZB = new float[,] { { 10, 10 }, { 20, 20 } };
Console.WriteLine("相对路径下的请输入文件名:(例如test.txt)");
string Path = @"test.txt";
Path = Convert.ToString(Console.ReadLine());
float x = 0.5F;
float y = 0.5F;
Console.WriteLine("请输入采样点的x坐标:");
x = (float)Convert.ToDoubl