一种图象二值化的算法(C#)
来源:互联网 发布:搞怪软件下载 编辑:程序博客网 时间:2024/05/17 07:54
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.
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图象算法
- 图象细化算法大全
- 图象细化算法大全
- fstream文件流和vc调试
- 前台javascript 调用 c# 后台的Codebehind函数
- 如何刷新其他页面(非当前页面)的框架页
- Linux下Broadcom 43xx系列无线网卡适配方法
- 用php写的最简单的网站访问记数器
- 一种图象二值化的算法(C#)
- JAVA内存泄漏分析(一)
- 图像二值化 (C#)
- 地图频道接口(MapOne API)
- 确立09年奋斗目标
- InstallShield: xzIISConfig 用于配置 IIS 的 InstallScript 脚本
- InstallShield内部库函数全集一
- 段错误bug的调试
- JAVA内存泄漏分析(二)