LEADTOOLS使用教程:图像数据格式的更改
来源:互联网 发布:淘宝金牌卖家条件 编辑:程序博客网 时间:2024/05/21 06:55
在LeadTools中,您可以使用许多高级、低级的方法和类改变颜色分辨率(每像素的位数)。大多数图像处理方法在内存中操作图像,因此在文件中保存图像时这些更改是永久性的。一些低级的方法会操作您管理的缓冲区,例如在加载图像时处理数据。
下面我们将介绍LeadTools中改变颜色分辨率的类和方法,主要包括以下几类:
1. 自动颜色还原
Dithering method
获取和设置默认的抖动方法,被一些内部的LEADTOOLS方法引用。
2. 主要颜色分辨率
此类可以将一个图像转换为每像素任意位数。
可以设置输出路径、设定调色板。您可以直接转换原图或创建一个新的转换后图像。
在指定调色板时,您可以使用简单的调色板选项或设定一个符合您要求的自定义调色板。自定义调色板可设置为任意大小,它可以包括特定的颜色、保留的空白项和开放项。如果您想多次使用一些自定义调色板,可以使用RasterUserMatchTable创建一个表格优化性能。
以下为使用ColorResolutionCommand类的代码片段,将图像的分辨率转为每像素8位:
注:附件sampleColorResolutionCommand.rar
1: RasterCodecs codecs = new RasterCodecs(); 2: string srcFileName = Path.Combine(ImagesPath.Path, "Image1.cmp"); 3: string destFileName = Path.Combine(ImagesPath.Path, "Image1_colorres8.bmp"); 4: 5: // 从磁盘加载源图像 6: RasterImage image = codecs.Load(srcFileName); 7: 8: //将颜色分辨率转为每像素8位,在单独的图像中使用Netscape调色板 9: ColorResolutionCommand cmd = new ColorResolutionCommand(); 10: cmd.Mode = ColorResolutionCommandMode.CreateDestinationImage; 11: cmd.BitsPerPixel = 8; 12: cmd.Order = RasterByteOrder.Rgb; 13: cmd.DitheringMethod = RasterDitheringMethod.None; 14: cmd.PaletteFlags = ColorResolutionCommandPaletteFlags.UsePalette; 15: cmd.SetPalette(RasterPalette.Netscape()); 16: cmd.Run(image); 17: RasterImage destImage = cmd.DestinationImage; 18: Assert.IsTrue(destImage.BitsPerPixel == 8); 19: 20: // 保存至磁盘 21: codecs.Save(destImage, destFileName, RasterImageFormat.Bmp, 8); 22: destImage.Dispose(); 23: image.Dispose(); 24: codecs.Dispose();
3. 支持自定义调色板的抖动
RasterUserMatchTable 类
使用RasterUserMatchTable类可创建一个预定义的表格,加快在ColorResolutionCommand中使用用户调色板此类情况的转换速度。
在使用时我们应该遵循以下的顺序:
- 创建一个RasterUserMatchTable类的新实例;
- 调用RasterUserMatchTable.Create方法创建表格;
- 调用RasterUserMatchTable.Use方法使表格可用。(将此方法设为一个单独的方法允许您可以在不重新创建此表格的情况下在文件中保存表格、获取您需要的一个表格)
- 将ColorResolutionCommand.PaletteFlags 属性设置为ColorResolutionCommandPaletteFlags.UsePalette 或ColorResolutionCommandPaletteFlags.FastMatch,运行ColorResolutionCommand。
- 若不再需要使用,调用Unuse方法。
以下为RasterUserMatchTable类使用的代码片段(注:附件sampleRasterUserMatchTable.rar)
1: RasterCodecs codecs = new RasterCodecs(); 2: string srcFileName = Path.Combine(ImagesPath.Path, "Image1.cmp"); 3: string destFileName = Path.Combine(ImagesPath.Path, "RasterUserMatchTable.bmp"); 4: 5: // 从磁盘加载图像 6: RasterImage image = codecs.Load(srcFileName); 7: 8: //64色彩虹调色板 9: RasterColor[] colors = 10: { 11: new RasterColor(0, 0, 0), new RasterColor(0, 0, 85), new RasterColor(0, 0, 170), new RasterColor(0, 0, 255), 12: new RasterColor(85, 0, 0), new RasterColor(85, 0, 85), new RasterColor(85, 0, 170), new RasterColor(85, 0, 255), 13: new RasterColor(170, 0, 0), new RasterColor(170, 0, 85), new RasterColor(170, 0, 170), new RasterColor(170, 0, 255), 14: new RasterColor(255, 0, 0), new RasterColor(255, 0, 85), new RasterColor(255, 0, 170), new RasterColor(255, 0, 255), 15: new RasterColor(0, 85, 0), new RasterColor(0, 85, 85), new RasterColor(0, 85, 170), new RasterColor(0, 85, 255), 16: new RasterColor(85, 85, 0), new RasterColor(85, 85, 85), new RasterColor(85, 85, 170), new RasterColor(85, 85, 255), 17: new RasterColor(170, 85, 0), new RasterColor(170, 85, 85), new RasterColor(170, 85, 170), new RasterColor(170, 85, 255), 18: new RasterColor(255, 85, 0), new RasterColor(255, 85, 85), new RasterColor(255, 85, 170), new RasterColor(255, 85, 255), 19: new RasterColor(0, 170, 0), new RasterColor(0, 170, 85), new RasterColor(0, 170, 170), new RasterColor(0, 170, 255), 20: new RasterColor(85, 170, 0), new RasterColor(85, 170, 85), new RasterColor(85, 170, 170), new RasterColor(85, 170, 255), 21: new RasterColor(170, 170, 0), new RasterColor(170, 170, 85), new RasterColor(170, 170, 170), new RasterColor(170, 170, 255), 22: new RasterColor(255, 170, 0), new RasterColor(255, 170, 85), new RasterColor(255, 170, 170), new RasterColor(255, 170, 255), 23: new RasterColor(0, 255, 0), new RasterColor(0, 255, 85), new RasterColor(0, 255, 170), new RasterColor(0, 255, 255), 24: new RasterColor(85, 255, 0), new RasterColor(85, 255, 85), new RasterColor(85, 255, 170), new RasterColor(85, 255, 255), 25: new RasterColor(170, 255, 0), new RasterColor(170, 255, 85), new RasterColor(170, 255, 170), new RasterColor(170, 255, 255), 26: new RasterColor(255, 255, 0), new RasterColor(255, 255, 85), new RasterColor(255, 255, 170), new RasterColor(255, 255, 255) 27: }; 28: 29: // 创建和设置user match table 30: RasterUserMatchTable userMatchTable = new RasterUserMatchTable(); 31: userMatchTable.Create(colors); 32: userMatchTable.Use(); 33: 34: // 使用新调色板更改颜色分辨率。注意若您多次使用,user match table 使您的代码速度更快。它的//代码在这里出现是因为我们想要向您展示它是怎么使用的。 35: 36: ColorResolutionCommand command = new ColorResolutionCommand( 37: ColorResolutionCommandMode.InPlace, 38: 8, 39: RasterByteOrder.Rgb, 40: RasterDitheringMethod.FloydStein, 41: ColorResolutionCommandPaletteFlags.UsePalette | ColorResolutionCommandPaletteFlags.FastMatch, 42: colors); 43: command.Run(image); 44: 45: //若您不再需要使用user match table,释放它 46: userMatchTable.Unuse(); 47: 48: // 将图像保存回磁盘 49: codecs.Save(image, destFileName, RasterImageFormat.Bmp, 8); 50: 51: // 清理 52: image.Dispose(); 53: codecs.Dispose();
4. 专门的分辨率方法
类
说明
AutoBinarizeCommand 类
此方法自动在位图中使用二值分割。注意,此类只在Document/Medical工具包中可用。此方法能改进识别的结果(包括OCR、条码、OMR、ICR)。
但在某些图像的使用上有一些限制,详情请点击AutoBinarizeCommand查看帮助文档。
AutoBinaryCommand 类
基于直方图统计分析的两峰值方法,可将二值分割应用于使用了一个自动计算阈值的图像。注意,此类也只在Document/Medical工具包中可用。
更多信息请点击AutoBinaryCommand查看帮助文档。
ChannelMixerCommand 类
使用ChannelMixerCommandFactor类,可以重新分配一个特定图像的RGB值。用于颜色的调整和修正。
更多信息请点击ChannelMixerCommand查看帮助文档。
ConvertToColoredGrayCommand类
将图像转为灰度图像,并基于此类的属性扩展色彩成分。注意,此类只在Document工具包中可用。
更多信息请点击ConvertToColoredGrayCommand查看帮助文档。
DesaturateCommand 类
通过将每个颜色的饱和度减少至0,将图像转换为灰度级。这个转换不会改变图像的颜色辨率。
更多信息请点击DesaturateCommand查看帮助文档。
DynamicBinaryCommand 类
通过使用每个像素的本地阈值,在不改变每像素位数的情况下将图像转为黑白图像。这个方法可用于条码识别的预处理,能够改进识别的结果。
更多信息请点击DynamicBinaryCommand查看帮助文档。
GrayscaleCommand 类
将一个1位、4位、8位、16位、24位或32位的图像转化为8位、12位或16位的灰度图像。在Document/Medical Imaging版本中仅支持12位和16位的灰度图像。
更多信息请点击GrayscaleCommand查看帮助文档。
GrayScaleToDuotoneCommand 类
通过将像素的原始值和新的颜色混合,或用新的颜色替换像素的原始值的方法,将灰度图像转换为彩色。此类在Raster Pro及以上的工具包可用。此方法的设计对象为灰度图像,如果图像不是灰度的,只能影响图像中红=绿=蓝的像素或区域。
更多信息请点击GrayScaleToDuotoneCommand查看帮助文档。
GrayScaleToMultitoneCommand类
通过将像素的原始值和一种或多种新颜色混合,或用一种或多种新颜色替换像素原始值的方法,将灰度图像转换为彩色。此类在Raster Pro及以上的工具包可用。此方法的设计对象为灰度图像,如果图像不是灰度的,只能影响图像中红=绿=蓝的像素或区域。
更多信息请点击GrayScaleToMultitoneCommand查看帮助文档。
HalfToneCommand 类
使用特定的循环模式,将1位、4位、8位、16位、24位或32位图像转换为半色调图像。半色调图像是1位的抖动图像,用于黑白打印或显示。
更多信息请点击HalfToneCommand查看帮助文档。
SampleTargetCommand 类
通过将样本颜色转换为目标颜色来校正颜色值。这个命令在Raster Pro及以上工具包中可用。
更多信息请点击SampleTargetCommand查看帮助文档。
SegmentCommand 类
分割图像,分割后的每段像素具有大致相同的颜色(颜色均匀)。这个命令在Raster Pro及以上工具包中可用。
更多信息请点击SegmentCommand查看帮助文档。
WindowLevelCommand 类
将一个12位或16位的灰度图像转换为8位灰度或24-位RGB图像。此类仅在Medical工具包中可用。
更多信息请点击WindowLevelCommand查看帮助文档。
5. 低级颜色分辨率的方法和类
方法/类
说明
Convert 方法
在指定的缓冲区中将数据转换为规定的每像素位数和颜色顺序。
DitherLine 方法
采用循环的方式,在输入缓冲区中以行的方式抖动,并将其写入一个输出缓冲区。
StartDithering 方法
初始化一个图像的缓冲抖动。
StopDithering 方法
清除StartDithering 和 DitherLine(Byte[],Int32,Byte[],Int32)方法中的所有数据变量和缓冲区。
6. 颜色空间的转换
Windows通常使用RGB颜色空间模型,在加载和保存文件时,如有需要,LEADTOOLS会将图像数据在RGB和其他颜色空间之间转换。此外,LEADTOOLS还为其他颜色空间模型提供了方法。您可以使用高级ColorSeparateCommand和ColorMergeCommand类创建和合并使用多种颜色空间模型的分色,包括RGB、CMYK、CMY、HSV和HLS。您也可以使用低级RasterColorSpace类在缓冲区中将原数据在不同的颜色空间之间转换,包括RGB、YUV、CMYK、CMY、YIQ、HSV、和 HLS。
注意:LEADTOOLS还支持TIFF CMYK文件的加载,且不需将数据转换为BGR。在使用了LoadCmykPlanes的图像中,我们将每一个单独的页面作为CMYK平面加载。若您想要将平面保存为TIFF CMYK,可以调用SaveCmykPlanes。
转载来自于http://blog.gcpowertools.com.cn/post/2014/09/11/image-processing-change-data-format-by-leadtools.aspx
- LEADTOOLS使用教程:图像数据格式的更改
- LEADTOOLS使用教程:图像的几何变换
- LEADTOOLS使用教程:图像的倾斜校正处理
- LEADTOOLS JavaScript SDK使用教程:HTML5图像
- LEADTOOLS HTML5 SDK使用教程:DICOM图像注释
- LEADTOOLS使用教程:LEADTOOLS Barcode用法
- LEADTOOLS使用教程:DICOM注释
- LEADTOOLS使用教程:表单自动对齐
- LEADTOOLS使用教程:创建OCR引擎实例
- LEADTOOLS使用教程及资源汇总
- LEADTOOLS使用教程:如何使用文件扫描检测空白页
- LEADTOOLS使用教程:读写、编辑PDF文件和元数据
- LEADTOOLS使用教程:表单识别和处理流程概述
- 图像颜色的数据格式 - PixelFormat
- PixelFormat 图像颜色的数据格式
- LEADTOOLS JavaScript 客户端图像处理
- json数据格式的使用
- LEADTOOLS中如何使用文档清理命令接口来清理文档图像
- 编译arm版本的QT遇到的问题
- iOS 8最佳适配——被点赞的百度输入法
- phonegap开发--基于iscroll4实现下拉刷新等问题解析
- 阿里开放数据处理服务(ODPS)-余波
- memcpy和strcpy
- LEADTOOLS使用教程:图像数据格式的更改
- 时间函数整理
- Android Intent Flag的介绍
- Ubuntu上挂载ntfs硬盘,并开机启动svn服务器
- LEADTOOLS使用教程:图像的几何变换
- Python学习笔记5:函数参数详解
- SQL中where 1=1的用法
- The Last Time For Yourself!
- java HMAC_SHA1加密