内存法通过把图像储存在内存中进行处理,效率大大高于GetPixel方法,安全性高于指针法。

笔者当初写图像处理的时候发现网上多是用GetPixel方法实现,提到内存法的时候也没有具体实现,所以笔者在这里具体实现一下- -,望指正。

首先讲一下用到的一些方法。

1.LockBits和UnlockBits:使用 LockBits 方法,可在系统内存中锁定现有的位图,以便通过编程方式进行更改,每调用LockBits之后都应该调用一次UnlockBits。

2.Scan0:图像的第一个字节地址。

3.Stride:步幅,扫描宽度,形象的说就是一行的长度。

4.PixelFormat:数据的实际像素格式。

给出原图:

C# 内存法图像处理-LMLPHP

一、灰度

对每个像素点进行加权平均,(方法不唯一)。

C# 内存法图像处理-LMLPHP

        /// <summary>
/// 灰化实现方法
/// </summary>
void Image_Ashing()
{
if (pbshowbox.Image != null)
{
int Height = this.pbshowbox.Image.Height;
int Width = this.pbshowbox.Image.Width;
Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
Bitmap MyBitmap = (Bitmap)this.pbshowbox.Image; BitmapData oldData = MyBitmap.LockBits(new Rectangle(, , Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData newData = bitmap.LockBits(new Rectangle(, , Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* pin = (byte*)(oldData.Scan0.ToPointer());
byte* pout = (byte*)(newData.Scan0.ToPointer());
for (int y = ; y < oldData.Height; y++)
{
for (int x = ; x < oldData.Width; x++)
{
byte Result = (byte)(pin[] * 0.1 + pin[] * 0.2 + pin[] * 0.7);//加权平均实现灰化
pout[] = (byte)(Result);
pout[] = (byte)(Result);
pout[] = (byte)(Result);
pin = pin + ;
pout = pout + ;
}
pin += oldData.Stride - oldData.Width * ;
pout += newData.Stride - newData.Width * ;
} bitmap.UnlockBits(newData);
MyBitmap.UnlockBits(oldData);
this.pbshowbox.Image = bitmap; } }
else
{
MessageBox.Show("请先打开一张图片!");
} }

二、柔化

像素点与周围像素点差别较大时取平均值。

C# 内存法图像处理-LMLPHP

        /// <summary>
/// 柔化实现方法
/// </summary>
void Image_Soften()
{
if (pbshowbox.Image != null)
{
int Height = this.pbshowbox.Image.Height;
int Width = this.pbshowbox.Image.Width;
Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppRgb);
Bitmap MyBitmap = (Bitmap)this.pbshowbox.Image; BitmapData oldData = MyBitmap.LockBits(new Rectangle(, , Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
BitmapData newData = bitmap.LockBits(new Rectangle(, , Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
unsafe
{
byte* pin = (byte*)(oldData.Scan0.ToPointer());
byte* pout = (byte*)(newData.Scan0.ToPointer());
//高斯模板
int[] Gauss = { , , , , , , , , };
for (int i = ; i < Width - ; i++)
{
for (int j = ; j < Height - ; j++)
{
int r = , g = , b = ;
int Index = ; for (int col = -; col <= ; col++)
{
for (int row = -; row <= ; row++)
{
int off = ((j + row) * (Width) + (i + col)) * ;
r += pin[off + ] * Gauss[Index];
g += pin[off + ] * Gauss[Index];
b += pin[off + ] * Gauss[Index];
Index++;
}
}
r /= ;
g /= ;
b /= ;
//处理颜色值溢出
if (r < ) r = ;
if (r > ) r = ;
if (g < ) g = ;
if (g > ) g = ;
if (b < ) b = ;
if (b > ) b = ;
int off2 = (j * Width + i) * ;
pout[off2 + ] = (byte)r;
pout[off2 + ] = (byte)g;
pout[off2 + ] = (byte)b;
}
}
bitmap.UnlockBits(newData);
MyBitmap.UnlockBits(oldData);
this.pbshowbox.Image = bitmap;
} }
else
{
MessageBox.Show("请先打开一张图片!");
} }

三、锐化

突出显示颜色值大的像素点。

C# 内存法图像处理-LMLPHP

        /// <summary>
/// 锐化实现方法,显示数值最大像素点
/// </summary>
void Image_Sharpen()
{
if (this.pbshowbox.Image != null)
{
int Height = this.pbshowbox.Image.Height;
int Width = this.pbshowbox.Image.Width;
Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppRgb);
Bitmap MyBitmap = (Bitmap)this.pbshowbox.Image; BitmapData oldData = MyBitmap.LockBits(new Rectangle(, , Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
BitmapData newData = bitmap.LockBits(new Rectangle(, , Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
unsafe
{
byte* pin = (byte*)(oldData.Scan0.ToPointer());
byte* pout = (byte*)(newData.Scan0.ToPointer());
//拉普拉斯模板
int[] Laplacian = { -, -, -, -, , -, -, -, - };
for (int i = ; i < Width - ; i++)
{
for (int j = ; j < Height - ; j++)
{
int r = , g = , b = ;
int Index = ; for (int col = -; col <= ; col++)
{
for (int row = -; row <= ; row++)
{
int off = ((j + row) * (Width) + (i + col)) * ;
r += pin[off + ] * Laplacian[Index];
g += pin[off + ] * Laplacian[Index];
b += pin[off + ] * Laplacian[Index];
Index++;
}
} if (r < ) r = ;
if (r > ) r = ;
if (g < ) g = ;
if (g > ) g = ;
if (b < ) b = ;
if (b > ) b = ;
int off2 = (j * Width + i) * ;
pout[off2 + ] = (byte)r;
pout[off2 + ] = (byte)g;
pout[off2 + ] = (byte)b;
}
}
bitmap.UnlockBits(newData);
MyBitmap.UnlockBits(oldData);
this.pbshowbox.Image = bitmap;
} }
else
{
MessageBox.Show("请先打开一张图片!");
}
}

四、浮雕

对图像像素点的像素值分别与相邻像素点的像素值相减后加上128, 然后将其作为新的像素点的值。

C# 内存法图像处理-LMLPHP

        /// <summary>
/// 浮雕实现方法
/// </summary>
void Image_Relief()
{
if (this.pbshowbox.Image != null)
{ int Height = this.pbshowbox.Image.Height;
int Width = this.pbshowbox.Image.Width;
Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
Bitmap MyBitmap = (Bitmap)this.pbshowbox.Image;
BitmapData oldData = MyBitmap.LockBits(new Rectangle(, , Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData newData = bitmap.LockBits(new Rectangle(, , Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* pin_1 = (byte*)(oldData.Scan0.ToPointer());
byte* pin_2 = pin_1 + (oldData.Stride);
byte* pout = (byte*)(newData.Scan0.ToPointer());
for (int y = ; y < oldData.Height - ; y++)
{
for (int x = ; x < oldData.Width; x++)
{
int b = (int)pin_1[] - (int)pin_2[] + ;
int g = (int)pin_1[] - (int)pin_2[] + ;
int r = (int)pin_1[] - (int)pin_2[] + ; if (r < ) r = ;
if (r > ) r = ;
if (g < ) g = ;
if (g > ) g = ;
if (b < ) b = ;
if (b > ) b = ;
pout[] = (byte)(b);
pout[] = (byte)(g);
pout[] = (byte)(r);
pin_1 = pin_1 + ;
pin_2 = pin_2 + ;
pout = pout + ;
}
pin_1 += oldData.Stride - oldData.Width * ;
pin_2 += oldData.Stride - oldData.Width * ;
pout += newData.Stride - newData.Width * ;
}
bitmap.UnlockBits(newData);
MyBitmap.UnlockBits(oldData);
this.pbshowbox.Image = bitmap;
} }
else
{
MessageBox.Show("请先打开一张图片!");
}
}

五、底片

颜色值取反。

C# 内存法图像处理-LMLPHP

        /// <summary>
/// 底片实现方法
/// </summary>
void Image_Negative()
{
if (pbshowbox.Image != null)
{
int Height = this.pbshowbox.Image.Height;
int Width = this.pbshowbox.Image.Width;
Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
Bitmap MyBitmap = (Bitmap)this.pbshowbox.Image;
BitmapData oldData = MyBitmap.LockBits(new Rectangle(, , Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData newData = bitmap.LockBits(new Rectangle(, , Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* pin = (byte*)(oldData.Scan0.ToPointer());
byte* pout = (byte*)(newData.Scan0.ToPointer());
for (int y = ; y < oldData.Height; y++)
{
for (int x = ; x < oldData.Width; x++)
{
pout[] = (byte)( - pin[]);
pout[] = (byte)( - pin[]);
pout[] = (byte)( - pin[]);
pin = pin + ;
pout = pout + ;
}
pin += oldData.Stride - oldData.Width * ;
pout += newData.Stride - newData.Width * ;
}
bitmap.UnlockBits(newData);
MyBitmap.UnlockBits(oldData);
this.pbshowbox.Image = bitmap;
}
}
else
{
MessageBox.Show("请先打开一张图片!");
}
}

六、积木

低像素置0,高像素置255。

C# 内存法图像处理-LMLPHP

        /// <summary>
/// 积木实现方法
/// </summary>
private void Image_Block()
{
if (this.pbshowbox.Image != null)
{
int Height = this.pbshowbox.Image.Height;
int Width = this.pbshowbox.Image.Width;
Bitmap bitmap = new Bitmap(Width, Height);
Bitmap Mybitmap = (Bitmap)this.pbshowbox.Image;
BitmapData oldData = Mybitmap.LockBits(new Rectangle(, , Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData newData = bitmap.LockBits(new Rectangle(, , Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* pin = (byte*)(oldData.Scan0.ToPointer());
byte* pout = (byte*)(newData.Scan0.ToPointer());
for (int y = ; y < oldData.Height; y++)
{
for (int x = ; x < oldData.Width; x++)
{
int avg = (pin[] + pin[] + pin[]) / ;
if (avg > )
{
pout[] = ;
pout[] = ;
pout[] = ;
}
else
{
pout[] = ;
pout[] = ;
pout[] = ;
}
pin = pin + ;
pout = pout + ;
}
pin = pin + oldData.Stride - oldData.Width * ;
pout = pout + newData.Stride - newData.Width * ;
}
bitmap.UnlockBits(newData);
Mybitmap.UnlockBits(oldData);
this.pbshowbox.Image = bitmap; }
}
else
{
MessageBox.Show("请先打开一张图片!");
} }

有些图片效果看起来不明显是因为笔者把图缩小了,其实效果挺明显的- -。

04-24 11:10
查看更多