简体   繁体   English

是否有比LibTIFF更快的方法从32位tiff中提取浮点数?

[英]Is there a quicker way to extract floats from a 32 bit tiff than LibTIFF?

I am currently using this to extract floats: 我目前正在使用它来提取浮点数:

TIFF* tiff = TIFFOpen(tiffs[i].c_str(), "r");
if (tiff) {
    uint32 width, height;
    tsize_t scanlength; 
    if (TIFFGetField(tiff,TIFFTAG_IMAGEWIDTH, &width) != 1) {}
    if (TIFFGetField(tiff,TIFFTAG_IMAGELENGTH, &height) != 1) {}
    fwidth = width;
    fheight = height;
    vector<float> data;
    scanlength = TIFFScanlineSize(tiff);
    float image[height][width];
    for (uint32 y = 0; y < height; y++) {
        TIFFReadScanline(tiff,image[y],y);
    }
}

This is taking over 0.02 seconds per TIFF and I need it to be much quicker. 每个TIFF花费超过0.02秒,我需要更快。 I know that other libraries can kind of handle this, but I have only found one other that can handle 32 bit tiffs, and it was CImg, which took way longer. 我知道,其他库可以种处理这个问题,但我只找到一个其他的可以处理32位TIFF格式,它是CIMG,历时较长。 Even if this is as simple as using system() to do a comand line thing or call a really fast script, I would love to know if there is a faster way. 即使这就像使用system()来执行命令行或调用一个真正快速的脚本那样简单,我也很想知道是否有一种更快的方法。

Thank you! 谢谢!

https://www.dropbox.com/s/5zb8spaz7cma1gx/pic.tif?dl=0 https://www.dropbox.com/s/5zb8spaz7cma1gx/pic.tif?dl=0

This is an example tif. 这是一个例子。

Mmmm, not as complete an answer as I was hoping to be able to provide, but I do have some thoughts to share. 嗯,答案还不尽如我所愿,但我确实有一些想法要分享。 Maybe they will trigger some further thoughts from me or other folks... 也许他们会引起我或其他人的进一步思考。


Firstly, your images are lacking TIFF tag 262 ( "Photometric Interpretation" ) which is upsetting several tools you might otherwise use. 首先,您的图像缺少TIFF标签262( “光度解释” ),这会打乱您可能会使用的几种工具。 What program generated the images - as they are not strictly compliant? 图像由什么程序生成-由于它们不严格兼容? Can you correct/improve the program that generated the images? 您可以更正/改善生成图像的程序吗?

I managed to set the "Photometric Interpretation" tag to "min-is-black" with: 我设法通过以下方法将“光度学解释”标签设置为“ min-is-black”

tiffset -s 262 0 YourImage.tif

Once that is set, I managed to use vips (from here) - which is exceedingly fast, and memory-efficient, to convert your file to JPEG. 设置好之后,我设法使用了vips (从这里开始) -非常快,而且内存高效,可以将文件转换为JPEG。 It has Ruby and Python bindings if you prefer those languages. 如果您喜欢这些语言,它具有Ruby和Python绑定。

So, the command-line in Terminal to convert your file to JPEG is: 因此,终端中用于将文件转换为JPEG的命令行为:

vips im_vips2jpeg YourFile.tif result.jpg

在此处输入图片说明

I am not convinced that works correctly though, so maybe John @user894763 (the author of vips ) would take a look. 我不相信,虽然可以正常工作,所以也许约翰@ user894763(笔者vips )将看一看。


Another thought, using vips is that the following command will save a raw RGB file of 3 floats per pixel which you can read straight into your own program without any decoding at all: 使用vips另一种想法是,以下命令将保存每个像素3个浮点的RGB原始文件,您可以将其直接读取到自己的程序中,而无需任何解码:

vips rawsave YourFile.tif image.raw

-rw-r--r--   1 mark  staff  3145728 20 Jun 16:59 image.raw

You'll note that the file size (3145728) corresponds to: 您会注意到文件大小(3145728)对应于:

512 pixels * 512 pixels * 3 RGB values * 4 bytes of float each

I also used ImageMagick to convert your image to JPEG, with 我还使用ImageMagick将图像转换为JPEG

convert YourImage.tif result.jpg

and got this result: 并得到以下结果:

在此处输入图片说明


A further thought that occurred to me was that you could pre-warm your buffer cache before running your own TIFF extract program, by running cat on each of your files to cause them to be fetched from the NFS server: 我想到的另一种想法是,您可以在运行自己的TIFF提取程序之前预热缓冲区缓存,方法是在每个文件上运行cat以使它们从NFS服务器中获取:

cat *.tif > /dev/null

or maybe run parallel streams of that to reduce latency. 或运行并行流以减少延迟。


Another thought was that you could pre-fetch the files to a RAM-backed filesystem so that your files can be read with minimal latency. 另一个想法是,您可以将文件预取到RAM支持的文件系统中,以便可以以最小的延迟读取文件。 At 186kB per file, you could get 5,000 in a 1GB RAMdisk for much faster processing: 每个文件186kB,您可以在1GB RAM磁盘中获得5,000,以进行更快的处理:

mkdir /tmp/RAM
sudo mount -t temps -o size=1G temps /tmp/RAM

You could also put intermediate files that I suggest in my thoughts above into the RAM filesystem. 您也可以将我在上面的想法中建议的中间文件放入RAM文件系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM