数字图像处理——图像平滑(中值滤波)

void CWvltDoc::OnFilterSmoothmid() //中值滤波
{
	LPBITMAPFILEHEADER lpBitmapFileHeader = (LPBITMAPFILEHEADER)m_pBitmap;
	LPBITMAPINFOHEADER lpBitmapInfoHeader = (LPBITMAPINFOHEADER)(m_pBitmap + 14);
	unsigned char *lpData = m_pBitmap + lpBitmapFileHeader->bfOffBits;
	unsigned long biHeight = lpBitmapInfoHeader->biHeight;
	unsigned long biWidth = lpBitmapInfoHeader->biWidth;
	unsigned long biAlign = (biWidth * 3 + 3) / 4 * 4;
	unsigned long bmSize = biHeight * biAlign;
	if (m_pTransfered == NULL)
		m_pTransfered = (unsigned char*)malloc(bmSize);
	if (m_pTransfered == NULL)
		return;
	//Add the processing code here, which reverses the color of each pixel.
	int x, y, cur;
	for (y = 1; y < (int)biHeight - 1; y++)
	{
		for (x = 1; x < (int)biWidth - 1; x++)
		{
			cur = y*biAlign + 3 * x;
			int i, j,m,n,temp;
			int data[9];
			for (i = 0; i < 3; i++)
			{
				for (j = 0; j < 9; j++)
				{
					data[j] = lpData[(cur + i) - 3 * (4 - j)];
				}
				//排序
				for (m = 0; m < 8; m++)
				{
					for (n = m + 1; n < 9; n++)
					{
						if (data[m]>data[n])
						{
							temp = data[m];
							data[m] = data[n];
							data[n] = temp;
						}
					}
				}
				m_pTransfered[cur + i] = data[5];
			}
		}
	}
	UpdateAllViews(NULL);//文档被修改后可调用此函数,把文档被修改的信息通知给每个视图.
}
效果图
处理前
<img src="https://img-blog.csdn.net/20161106112931339?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
处理后
<img src="https://img-blog.csdn.net/20161106113017234?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

猜你喜欢

转载自blog.csdn.net/sn_g_e_n_i_u_s_/article/details/53053523
今日推荐