如何解决从TIFF文件中删除空白或几乎空白页面的方法
|| 我有大约4000万个TIFF文档,全都是1位单页双工。在大约40%的情况下,这些TIFF的背面图像为“空白”,我想在对CMS进行加载以减少空间需求之前将其删除。 有没有一种简单的方法可以查看每页的数据内容,并在其低于预设阈值(例如2%“黑色”)时将其删除? 我对此技术不了解,但是C#解决方案可能是最容易支持的。问题是,我没有图像处理经验,所以真的不知道从哪里开始。 编辑添加:图像是旧扫描,\ dirty \也是如此,因此这并不是一门精确的科学。需要设置阈值以避免误报的机会。解决方法
您可能应该:
打开每个图像
遍历页面(使用
Bitmap.GetFrameCount
/Bitmap.SelectActiveFrame
方法)
每页的访问位(使用“ 2”方法)
分析每个页面的内容(简单循环)
如果内容值得,则将数据复制到另一个图像(Bitmap.LockBits
和一个循环)
这个任务不是特别复杂,但是需要编写一些代码。该站点包含一些示例,您可以使用方法名称作为关键字进行搜索。
附言我假设所有图像都可以成功加载到System.Drawing.Bitmap
中。,您可以使用DotImage做类似的事情(免责声明,我为Atalasoft工作,并且编写了您将使用的大多数基础类)。这样做的代码如下所示:
public void RemoveBlankPages(Stream source stm)
{
List<int> blanks = new List<int>();
if (GetBlankPages(stm,blanks)) {
// all pages blank - delete file? Skip? Your choice.
}
else {
// memory stream is convenient - maybe a temp file instead?
using (MemoryStream ostm = new MemoryStream()) {
// pulls out all the blanks and writes to the temp stream
stm.Seek(0,SeekOrigin.Begin);
RemoveBlanks(blanks,stm,ostm);
CopyStream(ostm,stm); // copies first stm to second,truncating at end
}
}
}
private bool GetBlankPages(Stream stm,List<int> blanks)
{
TiffDecoder decoder = new TiffDecoder();
ImageInfo info = decoder.GetImageInfo(stm);
for (int i=0; i < info.FrameCount; i++) {
try {
stm.Seek(0,SeekOrigin.Begin);
using (AtalaImage image = decoder.Read(stm,i,null)) {
if (IsBlankPage(image)) blanks.Add(i);
}
}
catch {
// bad file - skip? could also try to remove the bad page:
blanks.Add(i);
}
}
return blanks.Count == info.FrameCount;
}
private bool IsBlankPage(AtalaImage image)
{
// you might want to configure the command to do noise removal and black border
// removal (or not) first.
BlankPageDetectionCommand command = new BlankPageDetectionCommand();
BlankPageDetectionResults results = command.Apply(image) as BlankPageDetectionResults;
return results.IsImageBlank;
}
private void RemoveBlanks(List<int> blanks,Stream source,Stream dest)
{
// blanks needs to be sorted low to high,which it will be if generated from
// above
TiffDocument doc = new TiffDocument(source);
int totalRemoved = 0;
foreach (int page in blanks) {
doc.Pages.RemoveAt(page - totalRemoved);
totalRemoved++;
}
doc.Save(dest);
}
您应该注意,空白页的检测并不像“所有像素都是白色(-ish)?”那样简单,因为扫描会引入各种有趣的伪像。要获取BlankPageDetectionCommand,您将需要Document Imaging包。,您是否对缩小文件感兴趣,或者只是想避免人们浪费时间查看空白页?您可以通过仅将第二个IFD修补为0x00000000来对文件进行快速而肮脏的编辑,以摆脱已知的空白页。这就是我的意思-如果您仅浏览页面,TIFF文件的布局将很简单:
TIFF标头(4个字节)
第一个IFD偏移量(4个字节-通常指向0x00000008)
IFD:
标签数量(2字节)
{个人TIFF标签}(每个12个字节)
下一个IFD偏移量(4个字节)
只需将\“下一个IFD偏移\”的值修补为0x00000000,以将\“下一个IFD偏移\”页扩展到当前页之外。版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。