• 【c++&GDAL】均值滤波+中值滤波


    【c++&GDAL】均值滤波+中值滤波

    实现3*3的均值滤波和中值滤波

    1. 均值滤波

    使用一个M×N的窗口(又称滤波窗口、模板、掩膜),根据一定准则计算窗口内所有像元均值,然后赋予窗口中心像元,作为窗口中心像元滤波后的值。
    再逐行或逐列移动窗口,并按上述方法计算,直至每个像元计算完毕。

    void MeanFilter(unsigned char* in, unsigned char* out, int W, int H)
    {
    	/*
    	memcpy(void *dest,void *src,unsigned int count
    	由src所指内存区赋值count个字节到dest所指内存区
    	*/
    	int sum = W * H * sizeof(unsigned char);   //图像所占容量
    	memcpy((unsigned char *)out, (unsigned char*)in, sum);
    
    	for (int j = 1; j < H - 1; j++)
    	{
    		for (int i = 1; i < W - 1; i++)
    		{
    			out[j*W + i] = (in[(j - 1)*W + (i - 1)] + in[(j - 1)*W + i] + in[(j - 1)*W + (i + 1)] +
    				in[(j)*W + (i - 1)] + in[(j)*W + i] + in[(j)*W + (i + 1)] +
    				in[(j + 1)*W + (i - 1)] + in[(j + 1)*W + i] + in[(j + 1)*W + (i + 1)]) / 9;
    
    		}
    	}
    	cout << "使用<均值滤波>完成图像平滑" << endl;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    因为窗口中涉及j-1,j+1,i-1,i+1,所以i和j的范围是从[1,W/H-1).

    2. 中值滤波:

    基本原理与均值滤波相同,不同的是使用窗口内各点的中值代替窗口中心点的值(一般为奇数窗口)。

    代码:

    void MedianFilter(unsigned char* in, unsigned char* out, int W, int H)
    {
    	int sum = W * H * sizeof(unsigned char);   //图像所占容量
    	memcpy((unsigned char *)out, (unsigned char*)in, sum);
    	for (int j = 1; j < H - 1; j++)
    	{
    		for (int i = 1; i < W - 1; i++)
    		{
    			int k = 0;
    			unsigned char win[9];
    			for (int jj = j - 1; jj < j + 2; jj++)
    			{
    				for (int ii = i - 1; ii < i + 2; ii++)
    				{
    					win[k++] = in[jj*W + ii];    //将窗口的九个元素记录到一个数组中
    				}
    				//排序函数,第5个为中值
    				for (int m = 0; m < 5; m++)
    				{
    					int min = m;
    					for (int n = m + 1; n < 9; n++)
    					{
    						if (win[n] < win[min])
    							min = n;
    					}
    					unsigned char temp = win[m];
    					win[m] = win[min];
    					win[min] = temp;
    				}
    				out[j*W + i] = win[4];
    			}
    		}
    	}
    	cout << "使用<中值滤波>完成图像平滑" << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    3. 完整程序

    #include
    #include
    #include "gdal_priv.h"
    #include "gdalwarper.h"
    using namespace std;
    
    
    void MeanFilter(unsigned char* in, unsigned char* out, int W, int H)
    {
    	/*
    	memcpy(void *dest,void *src,unsigned int count
    	由src所指内存区赋值count个字节到dest所指内存区
    	*/
    	int sum = W * H * sizeof(unsigned char);   //图像所占容量
    	memcpy((unsigned char *)out, (unsigned char*)in, sum);
    
    	for (int j = 1; j < H - 1; j++)
    	{
    		for (int i = 1; i < W - 1; i++)
    		{
    			out[j*W + i] = (in[(j - 1)*W + (i - 1)] + in[(j - 1)*W + i] + in[(j - 1)*W + (i + 1)] +
    				in[(j)*W + (i - 1)] + in[(j)*W + i] + in[(j)*W + (i + 1)] +
    				in[(j + 1)*W + (i - 1)] + in[(j + 1)*W + i] + in[(j + 1)*W + (i + 1)]) / 9;
    
    		}
    	}
    	cout << "使用<均值滤波>完成图像平滑" << endl;
    }
    
    
    void MedianFilter(unsigned char* in, unsigned char* out, int W, int H)
    {
    	int sum = W * H * sizeof(unsigned char);   //图像所占容量
    	memcpy((unsigned char *)out, (unsigned char*)in, sum);
    	for (int j = 1; j < H - 1; j++)
    	{
    		for (int i = 1; i < W - 1; i++)
    		{
    			int k = 0;
    			unsigned char win[9];
    			for (int jj = j - 1; jj < j + 2; jj++)
    			{
    				for (int ii = i - 1; ii < i + 2; ii++)
    				{
    					win[k++] = in[jj*W + ii];    //将窗口的九个元素记录到一个数组中
    				}
    				//排序函数,第5个为中值
    				for (int m = 0; m < 5; m++)
    				{
    					int min = m;
    					for (int n = m + 1; n < 9; n++)
    					{
    						if (win[n] < win[min])
    							min = n;
    					}
    					unsigned char temp = win[m];
    					win[m] = win[min];
    					win[min] = temp;
    				}
    				out[j*W + i] = win[4];
    			}
    		}
    	}
    	cout << "使用<中值滤波>完成图像平滑" << endl;
    }
    
    void main()
    {
    //注册所有的格式驱动
    	GDALAllRegister();
    	//支持中文路径
    	CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
    
    	//打开图像
    	const char* filepath = "Img.tif";
    	GDALDataset* image = (GDALDataset *)GDALOpen(filepath, GA_ReadOnly);
    	if (image == NULL)
    	{
    		//AfxMessageBox("读取图像失败");
    		cout << "读取图像失败";
    	}
    	else
    		cout << "读取成功" << endl;
    	//定义图像的长宽
    	int W = image->GetRasterXSize();
    	int H = image->GetRasterYSize();
    	int C = image->GetRasterCount();
    	GDALDataType ImgType = image->GetRasterBand(1)->GetRasterDataType();   //数字图像类型
    
    	GDALRasterBand *band1 = image->GetRasterBand(1);    //第一个波段的影像读入到数据之中
    	unsigned char* bdata = new unsigned char[W*H];  //创建存放数据的内存
    	band1->RasterIO(GF_Read, 0, 0, W, H, bdata, W, H, ImgType, 0, 0);   //GDALRasterBand
    
    	//创建保存影像数据集
    	GDALDriver* imgDriver = GetGDALDriverManager()->GetDriverByName("GTiff");   //获取驱动
    	const char* outFilename1 = "MeanFilter.tif";
    	const char* outFilename2 = "MedianFilter.tif";
    	GDALDataset* outIMG1 = imgDriver->Create(outFilename1, W, H, 1, ImgType, NULL);
    	GDALDataset* outIMG2 = imgDriver->Create(outFilename2, W, H, 1, ImgType, NULL);
    	unsigned char* newdata = new unsigned char[W*H];
    
    	MeanFilter(bdata,newdata, W, H);
    	outIMG1->GetRasterBand(1)->RasterIO(GF_Write, 0, 0, W, H, newdata, W, H, ImgType, 0, 0);
    	
    	MedianFilter(bdata, newdata, W, H);
    	outIMG2->GetRasterBand(1)->RasterIO(GF_Write, 0, 0, W, H, newdata, W, H, ImgType, 0, 0);
    	//保存影像
    
    	GDALClose(image);
    	GDALClose(outIMG1);
    	GDALClose(outIMG2);
    	delete bdata;
    	delete newdata;
    	
    	system("pause");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
  • 相关阅读:
    Kubernetes kafka系列 | k8s部署kafka+zookeepe集群
    硬件描述语言 Chisel 入门教程
    influxdb得导出与导入
    MobPush Android 快速集成
    航空数据链协议解析与仿真测试
    【英语语法】 yet
    Autosar CAN硬件配置-Tc27x基于Davinci Cfg
    代码签名证书到期了怎么续费?
    webpack5基于React+Antd搭建开发和生产环境
    MyBatis
  • 原文地址:https://blog.csdn.net/gsgs1234/article/details/132927906