图像锐化是补偿图像的轮廓,增强图像的边缘及灰度跳变的部分,使图像变得清晰。图像平滑往往使图像中的边界、轮廓模糊,为了减少这类不利影响,利用图像锐化技术可以使图像的边缘清晰。图像锐化处理的目的是使图像的边缘、轮廓线及图像的细节变得清晰。经过平滑的图像变得模糊的根本原因是对图像进行了平均或积分运算,因此对其进行逆运算。
1。梯度锐化
Bitmap desc = new Bitmap(source.Width, source.Height);
BitmapData sourcedata = source.LockBits(new Rectangle(0, 0, source.Width, source.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData descdata = desc.LockBits(new Rectangle(0, 0, desc.Width, desc.Height),
ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
unsafe
{
byte* sourceptr = (byte*)sourcedata.Scan0;
byte* descptr = (byte*)descdata.Scan0;
int step = source.Width * 3;
double value;
for (int x = 0; x < source.Height; x++)
{
for (int y = 0; y < source.Width; y++)
{
value = Math.Sqrt((*sourceptr - *(sourceptr + step + 3)) * (*sourceptr - *(sourceptr + step + 3)) +
(*(sourceptr + 3) - *(sourceptr + step)) * (*(sourceptr + 3) - *(sourceptr + step)));
*(descptr++) = value > 255 ? (byte)255 : (byte)(value);
sourceptr++;
}
sourceptr += sourcedata.Stride - source.Width * 3;
descptr += descdata.Stride - desc.Width * 3;
}
}
source.UnlockBits(sourcedata);
desc.UnlockBits(descdata);
BitmapData sourcedata = source.LockBits(new Rectangle(0, 0, source.Width, source.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData descdata = desc.LockBits(new Rectangle(0, 0, desc.Width, desc.Height),
ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
unsafe
{
byte* sourceptr = (byte*)sourcedata.Scan0;
byte* descptr = (byte*)descdata.Scan0;
int step = source.Width * 3;
double value;
for (int x = 0; x < source.Height; x++)
{
for (int y = 0; y < source.Width; y++)
{
value = Math.Sqrt((*sourceptr - *(sourceptr + step + 3)) * (*sourceptr - *(sourceptr + step + 3)) +
(*(sourceptr + 3) - *(sourceptr + step)) * (*(sourceptr + 3) - *(sourceptr + step)));
*(descptr++) = value > 255 ? (byte)255 : (byte)(value);
sourceptr++;
}
sourceptr += sourcedata.Stride - source.Width * 3;
descptr += descdata.Stride - desc.Width * 3;
}
}
source.UnlockBits(sourcedata);
desc.UnlockBits(descdata);
2。拉普拉斯掩膜锐化
Bitmap desc = new Bitmap(source.Width, source.Height);
BitmapData sourcedata = source.LockBits(new Rectangle(0, 0, source.Width, source.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData descdata = desc.LockBits(new Rectangle(0, 0, desc.Width, desc.Height),
ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
unsafe
{
byte* sourceptr = (byte*)sourcedata.Scan0;
byte* descptr = (byte*)descdata.Scan0;
int step = source.Width * 3;
double value;
for (int x = 0; x < source.Height; x++)
{
for (int y = 0; y < source.Width; y++)
{
value = Math.Abs(*(sourceptr - step) + *(sourceptr - 3) + *(sourceptr + 3) +
*(sourceptr + step) - *(sourceptr) * 4);
*(descptr++) = value > 255 ? (byte)255 : (byte)(value);
sourceptr++;
}
sourceptr += sourcedata.Stride - source.Width * 3;
descptr += descdata.Stride - desc.Width * 3;
}
}
source.UnlockBits(sourcedata);
desc.UnlockBits(descdata);
BitmapData sourcedata = source.LockBits(new Rectangle(0, 0, source.Width, source.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData descdata = desc.LockBits(new Rectangle(0, 0, desc.Width, desc.Height),
ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
unsafe
{
byte* sourceptr = (byte*)sourcedata.Scan0;
byte* descptr = (byte*)descdata.Scan0;
int step = source.Width * 3;
double value;
for (int x = 0; x < source.Height; x++)
{
for (int y = 0; y < source.Width; y++)
{
value = Math.Abs(*(sourceptr - step) + *(sourceptr - 3) + *(sourceptr + 3) +
*(sourceptr + step) - *(sourceptr) * 4);
*(descptr++) = value > 255 ? (byte)255 : (byte)(value);
sourceptr++;
}
sourceptr += sourcedata.Stride - source.Width * 3;
descptr += descdata.Stride - desc.Width * 3;
}
}
source.UnlockBits(sourcedata);
desc.UnlockBits(descdata);