一种图象二值化的算法(C#)
来源:互联网 发布:自学编程有多难 编辑:程序博客网 时间:2024/06/07 00:34
源代码:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace Nobugz {
static class Util {
public static Bitmap BitmapTo1Bpp(Bitmap img) {
int w = img.Width;
int h = img.Height;
Bitmap bmp = new Bitmap(w, h, PixelFormat.Format1bppIndexed);
BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
for (int y = 0; y < h; y++) {
byte[] scan = new byte[(w + 7) / 8];
for (int x = 0; x < w; x++) {
Color c = img.GetPixel(x, y);
if (c.GetBrightness() >= 0.5) scan[x / 8] |= (byte)(0x80 >> (x % 8));
}
Marshal.Copy(scan, 0, (IntPtr)((int)data.Scan0 + data.Stride * y), scan.Length);
}
return bmp;
}
}
}
调用方法:
Bitmap bmp = Nobugz.Util.BitmapTo1Bpp(new Bitmap(@"c:/temp/test1.bmp"));
bmp.Save(@"c:/temp/test.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
说明:
A few remarks with this code. Sadly, I couldn't avoid the pointer arithmetic in the Marshal.Copy() call, the code is not going to work in 64-bit apps. And it is not going to be terribly fast, Bitmap.GetPixel() is slow. Hope it works for you.
- 一种图象二值化的算法(C#)
- 一种图象二值化的算法(C#)
- 一种结合混沌密码理论的彩色图象水印算法
- 图象的骨架提取算法
- <<一种基于δ函数的图象边缘检测算法>>一文算法的实现。
- 关于《一种鱼眼图象到透视投影图象的变换模型》
- VC下显示JPG,GIF图象的一种简便方法
- VC下显示JPG,GIF图象的一种简便方法
- VC下显示JPG,GIF图象的一种简便方法
- 一种对图象进行局部放大的有效方法
- 工作用到的主要图象算法
- MD5算法的两种应用(c#)不是加密 只是一种数据转换
- C语言一种乘法算法
- 银行家算法的一种实现——使用C语言
- 求n阶幻方的一种算法实现C/C++
- 3D图象算法
- 图象细化算法大全
- 图象细化算法大全
- 上广电换帅背后:液晶产业盈利难题
- 让matlab程序脱离matlab环境运行
- poj 1321 棋盘问题 (DFS )
- 关于数组堆的总结
- 验证码的破解思路!
- 一种图象二值化的算法(C#)
- android audio (针对 alsa) 架构图
- 让你吃惊的60个绝对得看的常识
- 祝福!
- java 常用时间格式
- js 设置cookie
- Notifier
- PKU 1091 跳蚤 数论 容斥原理
- C#基础——静态构造器(static constructor)