测绘之路

矩阵卷积算法

2023-11-10
测绘程序设计
Csharp
algo
最后更新:2024-09-19
6分钟
1124字

矩阵卷积算法

一、题目说明

1.1背景

在遥感图像处理中,经常要进行图像增强、图像滤波、边缘提前等操作,要将原始影像与算子模板进行卷积计算。本题采用文本数据,相关思想可应用于图像处理之中。

1.2实现步骤

1.数据文件读取2.算法实现3.保存计算报告

二、代码实现

2.1基本元素定义

1
public double[,] M;
2
public double[,] N;
3
4
public Algo(double[,] M, double[,]N)
5
{
6
this.M = M;
7
this.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>
9
private double JudgeMij(int i,int j,int I,int J)
10
{
11
double result=0;
12
int num = 10;
13
if(I-i-1<0||J-j-1<0||I-i-1>num-1||J-j-1>num-1)
14
{
15
result = 0;
87 collapsed lines
16
}
17
else
18
{
19
result = M[i,j];
20
}
21
return result;
22
}
23
/// <summary>
24
/// 算法一计算V
25
/// </summary>
26
/// <param name="I"></param>
27
/// <param name="J"></param>
28
/// <returns></returns>
29
public double V1(int I,int J)
30
{
31
int m = 3;
32
int i, j;
33
34
double up = 0, down = 0;;
35
double mij = 0;
36
37
for ( i = 0; i < m; i++)
38
{
39
for ( j = 0; j < m; j++)
40
{
41
mij=JudgeMij(i,j,I,J);
42
double eps = 1e-10;
43
if (Math.Abs(mij) > eps)
44
{
45
up += mij * N[I - i - 1, J - j - 1];
46
down += mij;
47
}
48
}
49
}
50
return up / down;
51
}
52
53
public double[,]Algo1()
54
{
55
int n = 10;
56
double[,] V = new double[n, n];
57
for ( int I = 0;I < n;I++)
58
{
59
for(int J = 0;J< n;J++)
60
{
61
V[I, J] = V1(I, J);
62
}
63
}
64
return V;
65
}
66
public double V2(int I, int J)
67
{
68
int m = 3;
69
int i, j;
70
71
double up = 0, down = 0;
72
double mij = 0;
73
74
for (i = 0; i < m; i++)
75
{
76
for (j = 0; j < m; j++)
77
{
78
mij = JudgeMij(i, j, I, J);
79
double eps = 1e-10;
80
if (Math.Abs(mij) > eps)
81
{
82
up += mij * N[9-(I - i - 1),9-( J - j - 1)];
83
down += mij;
84
}
85
}
86
}
87
return up / down;
88
}
89
//算法二
90
public double[,] Algo2()
91
{
92
int n = 10;
93
double[,] V = new double[n, n];
94
for (int I = 0; I < n; I++)
95
{
96
for (int J = 0; J < n; J++)
97
{
98
V[I, J] = V2(I, J);
99
}
100
}
101
return V;
102
}

2.3读文件

2.3.1读取M矩阵文件
1
private void 导入M矩阵ToolStripMenuItem_Click(object sender, EventArgs e)
2
{
3
try
4
{
5
6
OpenFileDialog opf1 = new OpenFileDialog();
7
opf1.Filter = "文本文件|*.txt";
8
opf1.Title = "请导入M数据文件";
9
if (opf1.ShowDialog() == DialogResult.OK)
10
{
11
StreamReader sr1 = new StreamReader(opf1.FileName);
12
string line;
13
string[] info;
14
int i = 0;
15
M = new double[3, 3];
27 collapsed lines
16
while ((line = sr1.ReadLine()) != null)
17
{
18
19
info = line.Split('\t');
20
M[i, 0] = double.Parse(info[0]);
21
M[i, 1] = double.Parse(info[1]);
22
M[i, 2] = double.Parse(info[2]);
23
i++;
24
}
25
for (int m = 0; m < 3; m++)
26
{
27
for (int n = 0; n < 3; n++)
28
{
29
Report += M[m, n] + "\t";
30
}
31
Report += "\n";
32
}
33
34
richTextBox1.Text = Report;
35
Report = null;
36
}
37
}
38
catch (Exception ex)
39
{
40
throw;
41
}
42
}
2.3.2读取N矩阵文件
1
private void 导入N矩阵ToolStripMenuItem_Click(object sender, EventArgs e)
2
{
3
4
try
5
{
6
OpenFileDialog opf2 = new OpenFileDialog();
7
opf2.Filter = "文本文件|*.txt";
8
opf2.Title = "请导入N数据文件";
9
if (opf2.ShowDialog() == DialogResult.OK)
10
{
11
StreamReader sr2 = new StreamReader(opf2.FileName);
12
string line;
13
string[] info;
14
int i = 0;
15
N = new double[10, 10];
29 collapsed lines
16
while ((line = sr2.ReadLine()) != null)
17
{
18
19
info = line.Split('\t');
20
for (int j = 0; j < info.Length; j++)
21
{
22
N[i, j] = double.Parse(info[j]);
23
}
24
i++;
25
}
26
for (int m = 0; m < 10; m++)
27
{
28
for (int n = 0; n < 10; n++)
29
{
30
Report += N[m, n] + "\t";
31
}
32
Report += "\n";
33
}
34
35
richTextBox1.Text = Report;
36
Report = null;
37
}
38
}
39
40
catch (Exception ex)
41
{
42
throw;
43
}
44
}

2.4保存计算结果

1
private void 保存计算报告ToolStripMenuItem_Click(object sender, EventArgs e)
2
{
3
if(V1 != null&&V2!=null)
4
{
5
SaveFileDialog sav=new SaveFileDialog();
6
sav.Filter = "文本文件|*.txt";
7
if(sav.ShowDialog() == DialogResult.OK)
8
{
9
StreamWriter sw=new StreamWriter(sav.FileName);
10
sw.Write(Report);
11
sw.Flush();
12
}
13
}
14
else 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

本文标题:矩阵卷积算法
文章作者:测绘王同学
发布时间:2023-11-10