压缩TIF文件
我正在尝试在C#中将多页彩色tiff文件转换为cCompressionCCITT3 tiff。我意识到我需要确保所有像素均为1位。我没有在网上找到有用的例子。
解决方案
回答
检出:http://bobpowell.net/onebit.htm
我们需要进行此转换,因为CCITT3和CCITT4不支持颜色(如果我没记错的话)。
回答
我看到了上面的代码,看起来它正在使用手动逻辑转换每个像素。
这对我们有用吗?
导入System.Drawing.Imaging
'获取颜色tif文件
将bmpColorTIF设为新的位图(" C:\ color.tif")
'选择tif的区域(将抓取所有帧)
将rectColorTIF设置为新的Rectangle(0,0,bmpColorTIF.Width,bmpColorTIF.Height)
'将矩形克隆为1位彩色tif
Dim bmpBlackWhiteTIF As Bitmap = bmpColorTIF.Clone(rectColorTIF,PixelFormat.Format1bppIndexed)
'使用新的位图(保存等)执行我们想要的操作
...
注意:有很多像素格式可供选择。
回答
皮条客免责声明:我为Atalasoft工作,该公司生产.NET图像处理软件。
使用dotImage,此任务将变为以下内容:
FileSystemImageSource source = new FileSystemImageSource("path-to-your-file.tif", true); // true = loop over all frames // tiff encoder will auto-select an appropriate compression - CCITT4 for 1 bit. TiffEncoder encoder = new TiffEncoder(); encoder.Append = true; // DynamicThresholdCommand is very good for documents. For pictures, use DitherCommand DynamicThresholdCommand threshold = new DynamicThresholdCommand(); using (FileStream outstm = new FileStream("path-to-output.tif", FileMode.Create)) { while (source.HasMoreImages()) { AtalaImage image = source.AcquireNext(); AtalaImage finalImage = image; // convert when needed. if (image.PixelFormat != PixelFormat.Pixel1bppIndexed) { finalImage = threshold.Apply().Image; } encoder.Save(outstm, finalImage, null); if (finalImage != image) { finalImage.Dispose(); } source.Release(image); } }
鲍勃·鲍威尔(Bob Powell)的例子就目前而言是一个很好的例子,但是它有很多问题,其中最重要的是它使用了一个简单的阈值,如果我们想要速度并且实际上并不关心输出是什么,那就太好了看起来或者输入域是这样的,实际上它实际上只是用颜色表示的黑色和白色。二值化是一个棘手的问题。当任务是将可用信息减少1/24时,如何保留正确的信息并丢弃其余信息是一个挑战。 DotImage具有六个不同的工具(IIRC)用于二值化。从我的角度来看,SimpleThreshold是最基本的。
回答
我建议先尝试使用tiff和image实用工具尝试所需的结果,然后再研究编码。我发现VIPS是一种方便的工具。下一个选择是研究LibTIFF可以做什么。使用c的免费LibTiff.NET取得了不错的效果(另请参见stackoverflow)。尽管里程可能会有所不同,但我对GDI tiff功能感到非常失望(我需要缺少的16位灰度)。
我们也可以使用LibTiff实用程序(即,参见http://www.libtiff.org/man/tiffcp.1.html)