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

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

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

文章目录

    • 【c++&GDAL】均值滤波+中值滤波
      • 1. 均值滤波
      • 2. 中值滤波:
      • 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;
}

因为窗口中涉及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;
}

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");
}

你可能感兴趣的:(基于GDAL的图像处理编程,c++,均值算法,图像处理)