遥感图像滤波处理(原始数据为文本格式)

来源:互联网 发布:excel2010数据按钮 编辑:程序博客网 时间:2024/06/06 01:57

       前面几篇文章都是基于影像进行的一些处理,从影像上处理,不便于理解,这篇文章就文本类型数据进行滤波算法介绍:

处理过程分为以下几步:

1、自己有一个文本文件,存放一些需要进行处理的数值

2、使用代码读取文本文件

3、读取完毕后进行滤波处理

4、将处理的结果重新存储到另一个文本文件中


通过以上4步即可得到一个经过滤波处理的文件:

       分步进行介绍:

1、在这次滤波处理中,我首先自己创造一个文本文件,为了读取方便,我们可以将文本数据的行列数放在第一行。我创建的文本如下(7行9列):


2、读取文本文件:

//------------------------------------------------------读取文本文件-------------------------------------------------ifstream readfile("data.txt");int row, col;readfile >> row >> col;cout << "the number of the rows is: " << row << "\n";cout << "the number of the cols is: " << col << "\n";//动态建立二维数组float **dataArray;dataArray = new float*[row];for (int i = 0; i < row; i++) {dataArray[i] = new float[col];}//将文本文件中的数据读入到数组中for (int i = 0; i < row; i++) {for (int j = 0; j < col; j++) {readfile >> dataArray[i][j];}}//将读取的文本文件内容进行输出for (int i = 0; i < row; i++) {for (int j = 0; j < col; j++) {cout << dataArray[i][j] << "\t";}cout << "\n";}//动态删除二维数组for (int i = 0; i < row; i++) {delete[] dataArray[i];}delete[] dataArray;//--------------------------------------------------------------------------------------------------------------------
3、进行各种滤波处理

均值滤波:

//均值滤波的函数定义,3_3的窗口大小/*row:行数col:列数array:需要进行处理的数据数组*/void MeanLvbo(int row, int col, float**array){float Sum;//用来记录一个窗口中的所有数值之和for (int i = 1; i < row - 1; i++) {for (int j = 1; j < col - 1; j++) {Sum = 0.0;Sum += array[i - 1][j - 1] + array[i - 1][j] + array[i - 1][j + 1] +array[i][j - 1] + array[i][j] + array[i][j + 1] +array[i + 1][j - 1] + array[i + 1][j] + array[i + 1][j + 1];array[i][j] = Sum / 9.0;}}//将滤波处理的数据进行输出ofstream outfile("meanlvbo.txt");//将进行滤波后的数据的行列数输入到文本文件中(由于该滤波是没有对边缘进行计算,故行列数均减2)outfile << row - 2 << " " << col - 2 << "\n";for (int i = 1; i < row - 1; i++) {for (int j = 1; j < col - 1; j++) {outfile << array[i][j] << "\t";}outfile << "\n";}}
中值滤波:
//中值滤波/*row:行数col:列数array:需要进行处理的数据数组*/void MiddleLvbo(int row, int col, float**array){for (int i = 1; i < row - 1; i++) {for (int j = 1; j < col - 1; j++) {float a[9];//用9个float类型的值来进行冒泡排序a[0] = array[i - 1][j - 1];a[1] = array[i - 1][j];a[2] = array[i - 1][j + 1];a[3] = array[i][j - 1];a[4] = array[i][j];a[5] = array[i][j + 1];a[6] = array[i + 1][j - 1];a[7] = array[i + 1][j];a[8] = array[i + 1][j + 1];//使用冒泡排序方法对上面9个数进行排序for (int i0 = 0; i0 < 9; i0++) {for (int j0 = 0; j0 < 9 - i0 - 1; j0++) {if (a[j0] > a[j0 + 1]) {float temp = a[j0];a[j0] = a[j0 + 1];a[j0 + 1] = temp;}}}array[i][j] = a[4];//将中值结果赋值给记录数组}}//将滤波处理的数据进行输出ofstream outfile("Middlelvbo.txt");//将进行滤波后的数据的行列数输入到文本文件中(由于该滤波是没有对边缘进行计算,故行列数均减2)outfile << row - 2 << " " << col - 2 << "\n";for (int i = 1; i < row - 1; i++) {for (int j = 1; j < col - 1; j++) {outfile << array[i][j] << "\t";}outfile << "\n";}}
索伯尔滤波:
//索伯尔滤波/*row:行数col:列数array:需要进行处理的数据数组*/void Sobel(int row, int col, float**array){for (int i = 1; i < row - 1; i++) {for (int j = 1; j < col - 1; j++) {float dx, dy;dx = -array[i - 1][j - 1] + array[i - 1][j + 1] - (2 * array[i][j - 1]) + (2 * array[i][j + 1]) - array[i + 1][j - 1] + array[i + 1][j + 1];dy = array[i - 1][j - 1] + array[i - 1][j] + array[i - 1][j + 1] - array[i + 1][j - 1] - array[i + 1][j] - array[i + 1][j + 1];float outresult = sqrt(dx*dx + dy*dy);array[i][j] = outresult;//将中值结果赋值给记录数组}}//将滤波处理的数据进行输出ofstream outfile("Sobel.txt");//将进行滤波后的数据的行列数输入到文本文件中(由于该滤波是没有对边缘进行计算,故行列数均减2)outfile << row - 2 << " " << col - 2 << "\n";for (int i = 1; i < row - 1; i++) {for (int j = 1; j < col - 1; j++) {outfile << array[i][j] << "\t";}outfile << "\n";}}
4、在每个进行滤波处理的函数中均进行了结果输出


以上就是滤波的全部实现过程,源码分块给出,下载整合代码、data数据及处理结果请访问:代码下载进行下载,积分不足的可以邮件跟我要源码。


1 1
原创粉丝点击