求助一个C#图像处理 本帖最后由 kkpy125 于 2013-05-21 04:15:00 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Bitmap image = new Bitmap("c:\\images\\image.gif"); //获取图像的BitmapData对像 BitmapData data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); //循环处理 unsafe { byte* ptr = (byte*)(data.Scan0); for (int i = 0; i < data.Height; i++) { for (int j = 0; j < data.Width; j++) { if (ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0)//可以适当放宽条件 { ptr[0] = 255; ptr[1] = 255; ptr[2] = 255; } else { ptr[0] = 0; ptr[1] = 0; ptr[2] = 0; } ptr += 3; } ptr += data.Stride - data.Width * 3; } } image.UnlockBits(data);处理24位位图 额,写反了if (ptr[0] == 255 && ptr[1] == 255 && ptr[2] == 255)//可以适当放宽条件{ ptr[0] = 0; ptr[1] = 0; ptr[2] = 0;}else{ ptr[0] = 255; ptr[1] = 255; ptr[2] = 255;} /**//// <summary> /// 变成黑白图 /// </summary> /// <param name="bmp">原始图</param> /// <param name="mode">模式。0:加权平均 1:算数平均</param> /// <returns></returns> private Bitmap ToGray(Bitmap bmp,int mode) { if (bmp == null) { return null; } int w = bmp.Width; int h = bmp.Height; try { byte newColor = 0; BitmapData srcData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); unsafe { byte* p = (byte*)srcData.Scan0.ToPointer(); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if (mode == 0) // 加权平均 { newColor = (byte)((float)p[0] * 0.114f + (float)p[1] * 0.587f + (float)p[2] * 0.299f); } else // 算数平均 { newColor = (byte)((float)(p[0] + p[1] + p[2]) / 3.0f); } p[0] = newColor; p[1] = newColor; p[2] = newColor; p += 3; } p += srcData.Stride - w * 3; } bmp.UnlockBits(srcData); return bmp; } } catch { return null; } }LZ试下 补充一个问题AFORGE.NET使用的时候为什么一个大矩形总是被识别成三角形? 二值化,白返黑。黑返白,然后过滤文字部份加到200分就给你搞个DEMO public class ReImage { public static Bitmap ToGray(Bitmap bmp) { for (int i = 0; i < bmp.Width; i++) { for (int j = 0; j < bmp.Height; j++) { //获取该点的像素的RGB的颜色 Color color = bmp.GetPixel(i, j); //利用公式计算灰度值 int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11); Color newColor = Color.FromArgb(gray, gray, gray); bmp.SetPixel(i, j, newColor); } } return bmp; } /// <summary> /// 图像灰度反转 /// </summary> /// <param name="bmp"></param> /// <returns></returns> public static Bitmap GrayReverse(Bitmap bmp) { for (int i = 0; i < bmp.Width; i++) { for (int j = 0; j < bmp.Height; j++) { //获取该点的像素的RGB的颜色 Color color = bmp.GetPixel(i, j); Color newColor = Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B); bmp.SetPixel(i, j, newColor); } } return bmp; } public static Bitmap ConvertToBpp(Bitmap bmp) { for (int i = 0; i < bmp.Width; i++) { for (int j = 0; j < bmp.Height; j++) { //获取该点的像素的RGB的颜色 Color color = bmp.GetPixel(i, j); Color newColor = ((color.R + color.G + color.B) / 3) > 10? Color.White:Color.Black; bmp.SetPixel(i, j, newColor); } } return bmp; }}调用列子 pictureBox1.Image = ReImage.ConvertToBpp(ReImage.GrayReverse(ReImage.ToGray(BMP图片)));给分 public class ReImage { public static Bitmap ToGray(Bitmap bmp) { for (int i = 0; i < bmp.Width; i++) { for (int j = 0; j < bmp.Height; j++) { //获取该点的像素的RGB的颜色 Color color = bmp.GetPixel(i, j); //利用公式计算灰度值 int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11); Color newColor = Color.FromArgb(gray, gray, gray); bmp.SetPixel(i, j, newColor); } } return bmp; } /// <summary> /// 图像灰度反转 /// </summary> /// <param name="bmp"></param> /// <returns></returns> public static Bitmap GrayReverse(Bitmap bmp) { for (int i = 0; i < bmp.Width; i++) { for (int j = 0; j < bmp.Height; j++) { //获取该点的像素的RGB的颜色 Color color = bmp.GetPixel(i, j); Color newColor = Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B); bmp.SetPixel(i, j, newColor); } } return bmp; } public static Bitmap ConvertToBpp(Bitmap bmp) { for (int i = 0; i < bmp.Width; i++) { for (int j = 0; j < bmp.Height; j++) { //获取该点的像素的RGB的颜色 Color color = bmp.GetPixel(i, j); Color newColor = ((color.R + color.G + color.B) / 3) > 10? Color.White:Color.Black; bmp.SetPixel(i, j, newColor); } } return bmp; }}调用列子 pictureBox1.Image = ReImage.ConvertToBpp(ReImage.GrayReverse(ReImage.ToGray(BMP图片)));给分分给你,我开个贴,最后的反转少一次,现在是白底黑格,我要的是黑底白格!剩下100分在那个贴里面过来拿! 等级决定最多只能给100,你搞出来了能用,我再开个贴,再给你100!我给你做好了,你留下邮箱,我把程序发给你,效果你看了绝对满意!邮箱[email protected] 等级决定最多只能给100,你搞出来了能用,我再开个贴,再给你100!我给你做好了,你留下邮箱,我把程序发给你,效果你看了绝对满意!邮箱[email protected]已经发送了! 等级决定最多只能给100,你搞出来了能用,我再开个贴,再给你100!我给你做好了,你留下邮箱,我把程序发给你,效果你看了绝对满意!邮箱[email protected]你的邮箱有点问题,一直发送失败,我贴源码给你,源码效率绝对比楼上的高!不信你可以测试一下!namespace BinaryForm{ public partial class Form1 : Form { public Form1() { InitializeComponent(); curBitmap = new Bitmap("1.png"); pictureBox1.Image = (Image)curBitmap; } //定义图像变量 private Bitmap curBitmap; //图像处理 private void button2_Click(object sender, EventArgs e) { if (curBitmap != null) { curBitmap = PFilterCharacter(curBitmap); curBitmap = PBinary(curBitmap, 240); } pictureBox1.Image = (Image)curBitmap; pictureBox1.Width = curBitmap.Width; pictureBox1.Height = curBitmap.Height; } //关闭 private void button3_Click(object sender, EventArgs e) { this.Close(); } //定义二值化函数(T为设定的阈值) private Bitmap PBinary(Bitmap a, int T) { int w = a.Width; int h = a.Height; Bitmap dstBitmap = new Bitmap(a.Width, a.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); System.Drawing.Imaging.BitmapData srcData = a.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); unsafe { byte* pIn = (byte*)srcData.Scan0.ToPointer(); byte* pOut = (byte*)dstData.Scan0.ToPointer(); byte* p; int stride = srcData.Stride; int r, g, b; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { p = pIn; r = p[2]; g = p[1]; b = p[0]; pOut[0] = pOut[1] = pOut[2] = (byte)(((byte)(0.2125 * r + 0.7154 * g + 0.0721 * b) >= T) ? 0 : 255); pIn += 3; pOut += 3; } pIn += srcData.Stride - w * 3; pOut += srcData.Stride - w * 3; } a.UnlockBits(srcData); dstBitmap.UnlockBits(dstData); return dstBitmap; } } //去除字符函数 private Bitmap PFilterCharacter(Bitmap a) { int w = a.Width; int h = a.Height; Bitmap dstBitmap = new Bitmap(a.Width, a.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); System.Drawing.Imaging.BitmapData srcData = a.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); unsafe { byte* pIn = (byte*)srcData.Scan0.ToPointer(); byte* pOut = (byte*)dstData.Scan0.ToPointer(); byte* p; int stride = srcData.Stride; int r, g, b; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { p = pIn; r = p[2]; g = p[1]; b = p[0]; if (r < 200 && g < 200 && b < 200) { pOut[0] = (byte)237; pOut[1] = (byte)239; pOut[2] = (byte)240; } else { pOut[0] = (byte)b; pOut[1] = (byte)g; pOut[2] = (byte)r; } pIn += 3; pOut += 3; } pIn += srcData.Stride - w * 3; pOut += srcData.Stride - w * 3; } a.UnlockBits(srcData); dstBitmap.UnlockBits(dstData); return dstBitmap; } } }} 关于MDI子窗体的Transparentkey属性... insert 问题 生成的类库的注释问题,高手帮帮忙 请教,怎样把dataGrid的数据赋值给label 急...高分求教__日志写入的问题!!! 我用GDI画个图形,怎么我选不中它.我想画后选中? 工控机的!同时接收6个串口发送过来的数据,每次收到之后便写入数据库 水晶报表ABC。 请问怎样让我在控件里画的图形随控件大小改变而改变。 关于命名空间之间的关系 树结构可实现保存吗? WPF,路由事件的困惑
//获取图像的BitmapData对像
BitmapData data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
//循环处理
unsafe
{
byte* ptr = (byte*)(data.Scan0);
for (int i = 0; i < data.Height; i++)
{
for (int j = 0; j < data.Width; j++)
{
if (ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0)//可以适当放宽条件
{
ptr[0] = 255;
ptr[1] = 255;
ptr[2] = 255;
}
else
{
ptr[0] = 0;
ptr[1] = 0;
ptr[2] = 0;
}
ptr += 3;
}
ptr += data.Stride - data.Width * 3;
}
}
image.UnlockBits(data);
处理24位位图
{
ptr[0] = 0;
ptr[1] = 0;
ptr[2] = 0;
}
else
{
ptr[0] = 255;
ptr[1] = 255;
ptr[2] = 255;
}
/// 变成黑白图
/// </summary>
/// <param name="bmp">原始图</param>
/// <param name="mode">模式。0:加权平均 1:算数平均</param>
/// <returns></returns>
private Bitmap ToGray(Bitmap bmp,int mode)
{
if (bmp == null)
{
return null;
} int w = bmp.Width;
int h = bmp.Height;
try
{
byte newColor = 0;
BitmapData srcData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* p = (byte*)srcData.Scan0.ToPointer();
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{ if (mode == 0) // 加权平均
{
newColor = (byte)((float)p[0] * 0.114f + (float)p[1] * 0.587f + (float)p[2] * 0.299f);
}
else // 算数平均
{
newColor = (byte)((float)(p[0] + p[1] + p[2]) / 3.0f);
}
p[0] = newColor;
p[1] = newColor;
p[2] = newColor; p += 3;
}
p += srcData.Stride - w * 3;
}
bmp.UnlockBits(srcData);
return bmp;
}
}
catch
{
return null;
} }LZ试下
二值化,白返黑。黑返白,然后过滤文字部份
加到200分就给你搞个DEMO
public class ReImage
{
public static Bitmap ToGray(Bitmap bmp)
{
for (int i = 0; i < bmp.Width; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
//获取该点的像素的RGB的颜色
Color color = bmp.GetPixel(i, j);
//利用公式计算灰度值
int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
Color newColor = Color.FromArgb(gray, gray, gray);
bmp.SetPixel(i, j, newColor);
}
}
return bmp;
} /// <summary>
/// 图像灰度反转
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public static Bitmap GrayReverse(Bitmap bmp)
{
for (int i = 0; i < bmp.Width; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
//获取该点的像素的RGB的颜色
Color color = bmp.GetPixel(i, j);
Color newColor = Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B);
bmp.SetPixel(i, j, newColor);
}
}
return bmp;
} public static Bitmap ConvertToBpp(Bitmap bmp)
{
for (int i = 0; i < bmp.Width; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
//获取该点的像素的RGB的颜色
Color color = bmp.GetPixel(i, j); Color newColor = ((color.R + color.G + color.B) / 3) > 10? Color.White:Color.Black;
bmp.SetPixel(i, j, newColor);
}
}
return bmp;
}
}
调用列子 pictureBox1.Image = ReImage.ConvertToBpp(ReImage.GrayReverse(ReImage.ToGray(BMP图片)));给分
public class ReImage
{
public static Bitmap ToGray(Bitmap bmp)
{
for (int i = 0; i < bmp.Width; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
//获取该点的像素的RGB的颜色
Color color = bmp.GetPixel(i, j);
//利用公式计算灰度值
int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
Color newColor = Color.FromArgb(gray, gray, gray);
bmp.SetPixel(i, j, newColor);
}
}
return bmp;
} /// <summary>
/// 图像灰度反转
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public static Bitmap GrayReverse(Bitmap bmp)
{
for (int i = 0; i < bmp.Width; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
//获取该点的像素的RGB的颜色
Color color = bmp.GetPixel(i, j);
Color newColor = Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B);
bmp.SetPixel(i, j, newColor);
}
}
return bmp;
} public static Bitmap ConvertToBpp(Bitmap bmp)
{
for (int i = 0; i < bmp.Width; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
//获取该点的像素的RGB的颜色
Color color = bmp.GetPixel(i, j); Color newColor = ((color.R + color.G + color.B) / 3) > 10? Color.White:Color.Black;
bmp.SetPixel(i, j, newColor);
}
}
return bmp;
}
}
调用列子 pictureBox1.Image = ReImage.ConvertToBpp(ReImage.GrayReverse(ReImage.ToGray(BMP图片)));给分
分给你,我开个贴,最后的反转少一次,现在是白底黑格,我要的是黑底白格!
剩下100分在那个贴里面过来拿!
邮箱[email protected]
邮箱[email protected]已经发送了!
邮箱[email protected]你的邮箱有点问题,一直发送失败,我贴源码给你,源码效率绝对比楼上的高!不信你可以测试一下!
namespace BinaryForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
curBitmap = new Bitmap("1.png");
pictureBox1.Image = (Image)curBitmap; }
//定义图像变量
private Bitmap curBitmap;
//图像处理
private void button2_Click(object sender, EventArgs e)
{
if (curBitmap != null)
{
curBitmap = PFilterCharacter(curBitmap);
curBitmap = PBinary(curBitmap, 240);
}
pictureBox1.Image = (Image)curBitmap;
pictureBox1.Width = curBitmap.Width;
pictureBox1.Height = curBitmap.Height;
}
//关闭
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
//定义二值化函数(T为设定的阈值)
private Bitmap PBinary(Bitmap a, int T)
{
int w = a.Width;
int h = a.Height;
Bitmap dstBitmap = new Bitmap(a.Width, a.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Imaging.BitmapData srcData = a.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
unsafe
{
byte* pIn = (byte*)srcData.Scan0.ToPointer();
byte* pOut = (byte*)dstData.Scan0.ToPointer();
byte* p;
int stride = srcData.Stride;
int r, g, b;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
p = pIn;
r = p[2];
g = p[1];
b = p[0];
pOut[0] = pOut[1] = pOut[2] = (byte)(((byte)(0.2125 * r + 0.7154 * g + 0.0721 * b) >= T)
? 0 : 255);
pIn += 3;
pOut += 3;
}
pIn += srcData.Stride - w * 3;
pOut += srcData.Stride - w * 3;
}
a.UnlockBits(srcData);
dstBitmap.UnlockBits(dstData);
return dstBitmap;
}
} //去除字符函数
private Bitmap PFilterCharacter(Bitmap a)
{
int w = a.Width;
int h = a.Height;
Bitmap dstBitmap = new Bitmap(a.Width, a.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Imaging.BitmapData srcData = a.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
unsafe
{
byte* pIn = (byte*)srcData.Scan0.ToPointer();
byte* pOut = (byte*)dstData.Scan0.ToPointer();
byte* p;
int stride = srcData.Stride;
int r, g, b;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
p = pIn;
r = p[2];
g = p[1];
b = p[0];
if (r < 200 && g < 200 && b < 200)
{
pOut[0] = (byte)237;
pOut[1] = (byte)239;
pOut[2] = (byte)240;
}
else
{
pOut[0] = (byte)b;
pOut[1] = (byte)g;
pOut[2] = (byte)r;
}
pIn += 3;
pOut += 3;
}
pIn += srcData.Stride - w * 3;
pOut += srcData.Stride - w * 3;
}
a.UnlockBits(srcData);
dstBitmap.UnlockBits(dstData);
return dstBitmap;
}
} }
}