矩阵卷积算法
一、题目说明
1.1背景
在遥感图像处理中,经常要进行图像增强、图像滤波、边缘提前等操作,要将原始影像与算子模板进行卷积计算。本题采用文本数据,相关思想可应用于图像处理之中。
1.2实现步骤
1.数据文件读取2.算法实现3.保存计算报告
二、代码实现
2.1基本元素定义
1public double[,] M;2public double[,] N;3
4public Algo(double[,] M, double[,]N)5{6this.M = M;7this.N = N;8}
其中M和N是文件读取以及计算需要的两个矩阵
M是33 N为1010
2.2算法
1/// <summary>2/// 判断Mij的条件3/// </summary>4/// <param name="i"></param>5/// <param name="j"></param>6/// <param name="I"></param>7/// <param name="J"></param>8/// <returns></returns>9private double JudgeMij(int i,int j,int I,int J)10{11double result=0;12int num = 10;13if(I-i-1<0||J-j-1<0||I-i-1>num-1||J-j-1>num-1)14{15result = 0;87 collapsed lines
16}17else18{19result = M[i,j];20}21return result;22}23/// <summary>24/// 算法一计算V25/// </summary>26/// <param name="I"></param>27/// <param name="J"></param>28/// <returns></returns>29public double V1(int I,int J)30{31int m = 3;32int i, j;33
34double up = 0, down = 0;;35double mij = 0;36
37for ( i = 0; i < m; i++)38{39for ( j = 0; j < m; j++)40{41mij=JudgeMij(i,j,I,J);42double eps = 1e-10;43if (Math.Abs(mij) > eps)44{45up += mij * N[I - i - 1, J - j - 1];46down += mij;47}48}49}50return up / down;51}52
53public double[,]Algo1()54{55int n = 10;56double[,] V = new double[n, n];57for ( int I = 0;I < n;I++)58{59for(int J = 0;J< n;J++)60{61V[I, J] = V1(I, J);62}63}64return V;65}66public double V2(int I, int J)67{68int m = 3;69int i, j;70
71double up = 0, down = 0;72double mij = 0;73
74for (i = 0; i < m; i++)75{76for (j = 0; j < m; j++)77{78mij = JudgeMij(i, j, I, J);79double eps = 1e-10;80if (Math.Abs(mij) > eps)81{82up += mij * N[9-(I - i - 1),9-( J - j - 1)];83down += mij;84}85}86}87return up / down;88}89//算法二90public double[,] Algo2()91{92int n = 10;93double[,] V = new double[n, n];94for (int I = 0; I < n; I++)95{96for (int J = 0; J < n; J++)97{98V[I, J] = V2(I, J);99}100}101return V;102}
2.3读文件
2.3.1读取M矩阵文件
1private void 导入M矩阵ToolStripMenuItem_Click(object sender, EventArgs e)2{3try4{5
6OpenFileDialog opf1 = new OpenFileDialog();7opf1.Filter = "文本文件|*.txt";8opf1.Title = "请导入M数据文件";9if (opf1.ShowDialog() == DialogResult.OK)10{11StreamReader sr1 = new StreamReader(opf1.FileName);12string line;13string[] info;14int i = 0;15M = new double[3, 3];27 collapsed lines
16while ((line = sr1.ReadLine()) != null)17{18
19info = line.Split('\t');20M[i, 0] = double.Parse(info[0]);21M[i, 1] = double.Parse(info[1]);22M[i, 2] = double.Parse(info[2]);23i++;24}25for (int m = 0; m < 3; m++)26{27for (int n = 0; n < 3; n++)28{29Report += M[m, n] + "\t";30}31Report += "\n";32}33
34richTextBox1.Text = Report;35Report = null;36}37}38catch (Exception ex)39{40throw;41}42}
2.3.2读取N矩阵文件
1private void 导入N矩阵ToolStripMenuItem_Click(object sender, EventArgs e)2{3
4try5{6OpenFileDialog opf2 = new OpenFileDialog();7opf2.Filter = "文本文件|*.txt";8opf2.Title = "请导入N数据文件";9if (opf2.ShowDialog() == DialogResult.OK)10{11StreamReader sr2 = new StreamReader(opf2.FileName);12string line;13string[] info;14int i = 0;15N = new double[10, 10];29 collapsed lines
16while ((line = sr2.ReadLine()) != null)17{18
19info = line.Split('\t');20for (int j = 0; j < info.Length; j++)21{22N[i, j] = double.Parse(info[j]);23}24i++;25}26for (int m = 0; m < 10; m++)27{28for (int n = 0; n < 10; n++)29{30Report += N[m, n] + "\t";31}32Report += "\n";33}34
35richTextBox1.Text = Report;36Report = null;37}38}39
40catch (Exception ex)41{42throw;43}44}
2.4保存计算结果
1private void 保存计算报告ToolStripMenuItem_Click(object sender, EventArgs e)2{3if(V1 != null&&V2!=null)4{5SaveFileDialog sav=new SaveFileDialog();6sav.Filter = "文本文件|*.txt";7if(sav.ShowDialog() == DialogResult.OK)8{9StreamWriter sw=new StreamWriter(sav.FileName);10sw.Write(Report);11sw.Flush();12}13}14else MessageBox.Show("请先进行计算");15}
三、数据文件
3.1读取数据文件
M文件
0.20 0.30 0.20
0.25 0.50 0.35
0.10 0.30 0.20
N文件
10.00 13.50 14.00 13.80 13.90 15.60 13.30 14.50 13.70 14.40
13.50 13.30 15.10 16.40 15.40 14.90 11.30 13.50 17.70 13.30
15.70 14.00 16.30 18.60 16.80 16.60 12.50 15.50 16.70 14.80
16.50 15.90 15.20 17.40 17.60 17.70 14.30 14.50 18.50 15.60
12.60 13.30 14.40 16.50 18.40 18.40 17.30 16.50 19.70 17.40
14.10 17.70 16.00 15.40 14.50 19.60 15.20 18.50 14.70 18.30
18.50 14.50 14.70 13.10 15.40 14.30 12.30 17.50 12.40 13.20
22.30 15.20 15.80 18.00 17.20 13.50 13.70 16.50 14.70 15.30
17.50 16.30 16.30 13.60 18.40 15.70 16.30 15.50 15.70 16.40
13.20 17.30 15.00 12.80 19.10 16.60 17.60 16.50 13.30 17.30
3.2结果文件
--------------------------------------------------------算法1------------------------------------------------------------
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN 10.00 11.40 12.64 13.80 13.89 14.36 14.46 14.30 13.93
NaN 11.56 12.07 13.03 14.25 14.61 14.76 14.33 13.78 13.99
NaN 13.66 13.29 13.83 15.00 15.78 15.72 14.71 13.74 14.17
NaN 15.59 15.07 14.95 15.73 16.86 16.98 15.70 14.33 14.70
NaN 14.94 15.02 14.95 15.56 16.85 17.60 17.01 15.73 15.85
NaN 13.85 14.51 14.82 15.37 16.10 17.27 17.52 16.95 16.59
NaN 15.43 15.17 15.30 15.25 15.23 15.96 16.43 16.52 16.17
NaN 19.08 17.34 16.38 15.40 15.34 15.30 15.06 14.99 15.35
NaN 19.86 18.46 16.89 15.50 15.94 15.97 15.19 14.64 15.30
---------------------------------------------------------算法2-----------------------------------------------------------
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN 17.30 15.70 15.36 15.90 17.00 17.60 16.59 15.23 15.03
NaN 16.90 16.03 15.51 15.75 16.59 17.16 16.63 15.48 15.10
NaN 16.16 15.86 15.56 15.55 15.77 16.16 16.62 16.03 15.48
NaN 14.74 14.70 15.10 15.18 14.84 14.79 15.81 15.96 15.49
NaN 15.44 14.66 15.04 15.40 15.33 14.85 15.41 15.31 15.35
NaN 17.05 16.48 16.32 16.46 16.57 16.54 16.35 15.32 15.24
NaN 16.91 17.55 17.31 16.83 16.66 17.46 17.45 16.33 15.49
NaN 15.64 16.60 16.85 16.00 15.48 16.63 17.61 17.05 15.84
NaN 14.40 15.54 15.91 15.02 14.11 15.26 16.79 16.89 15.94