通过压缩“CCITT T.6”分离多页tiff非常慢
我需要分离多帧 tiff 文件,并使用以下方法:
public static Image[] GetFrames(Image sourceImage)
{
Guid objGuid = sourceImage.FrameDimensionsList[0];
FrameDimension objDimension = new FrameDimension(objGuid);
int frameCount = sourceImage.GetFrameCount(objDimension);
Image[] images = new Image[frameCount];
for (int i = 0; i < frameCount; i++)
{
MemoryStream ms = new MemoryStream();
sourceImage.SelectActiveFrame(objDimension, i);
sourceImage.Save(ms, ImageFormat.Tiff);
images[i] = Image.FromStream(ms);
}
return images;
}
它工作正常,但如果源图像使用 CCITT T.6 压缩进行编码,则分离 20 帧文件最多需要 15 秒
当使用标准压缩 (LZW) 将图像保存到单个文件时,LZW 文件的分离时间低于 1第二。
使用 CCITT 压缩保存也需要很长时间。
有没有办法加快这个过程?
编辑:
我测量了执行时间:
sourceImage.SelectActiveFrame(objDimension, i);
sourceImage.Save(ms, ImageFormat.Tiff);
这两个调用各占总处理时间的 50% 左右。 使用一个初始容量足以容纳所有图像的 MemoryStream 不会导致可测量的速度增益。 Image.FromStream 方法几乎不需要任何处理时间。
我需要单帧,因为我需要处理它们(纠偏、旋转等)。
如果有与我完全不同的方法,我会很高兴听到。
I need to separate multiframe tiff files, and use the following method:
public static Image[] GetFrames(Image sourceImage)
{
Guid objGuid = sourceImage.FrameDimensionsList[0];
FrameDimension objDimension = new FrameDimension(objGuid);
int frameCount = sourceImage.GetFrameCount(objDimension);
Image[] images = new Image[frameCount];
for (int i = 0; i < frameCount; i++)
{
MemoryStream ms = new MemoryStream();
sourceImage.SelectActiveFrame(objDimension, i);
sourceImage.Save(ms, ImageFormat.Tiff);
images[i] = Image.FromStream(ms);
}
return images;
}
It works fine, but if the source image was encoded using the CCITT T.6 compression, separating a 20-frame-file takes up to 15 seconds on my 2,5ghz CPU.(One core is at 100% during the process)
When saving the images afterwards to a single file using standard compression (LZW), the separation time of the LZW-file is under 1 second.
Saving with CCITT compression also takes very long.
Is there a way to speed up the process?
edit:
I have measured the execution times:
sourceImage.SelectActiveFrame(objDimension, i);
sourceImage.Save(ms, ImageFormat.Tiff);
These two calls each account for around 50% of the total processing time.
Using one MemoryStream with an initial capacity big enough for all images results in no measurable speed gain.
The Image.FromStream method takes barely any processing time.
I need the single frames because I need to process them(deskew, rotate, etc.).
If there is a completely different method than mine, I would be happy to hear it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的情况,要做的第一件事就是衡量。
在我们弄清楚如何使其更快之前,当然,在我们通过优化使其变得更加复杂之前,我们需要知道哪些部分是缓慢的。幸运的是,您有一段非常短的代码,因此很容易添加您自己的计时代码,然后我们可以进行更明智的查看。
话虽如此,这里有一些不知情的建议:
The first thing to do in your situation would be to measure.
Before we can figure out how to make it faster, and certainly before we make it far more complicated with optimizations, we need to know what are the slow parts. luckily you have a very short piece of code so it would be pretty easy to just throw in your own timing code and then we can take a more informed look.
That being said here are a few uninformed pieces of advice:
这似乎是 Windows 7 上 GDI+ 的问题。
我在运行 Windows XP 的速度慢得多的计算机上运行了一个示例程序,并且在压缩图像上获得了比 Windows 7 更好的性能(大约快 2-3 倍)
It seems to be a problem with GDI+ on Windows 7.
I ran a sample program on a much slower machine with Windows XP, and got much better performance on compressed images than I got with Windows 7(around 2-3 times faster)