从图像文件中提取感兴趣的区域而不读取整个图像 [英] Extracting a region of interest from an image file without reading the entire image

查看:310
本文介绍了从图像文件中提取感兴趣的区域而不读取整个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索能够读取图像文件区域(任何格式)的库(使用任何语言),而无需最初读取整个图像文件。

I am searching for a library (in any language) that is capable of reading a region of an image file (any format) without having to initially read that entire image file.

我遇到了一些选项,比如vips,它确实没有将整个图像保留在内存中,但似乎仍然需要完全读取它才能开始。

I have come across a few options such as vips, which does indeed not keep the entire image in memory, but still seems to need to read it entirely to begin with.

我意识到这可能不适用于压缩格式,例如jpeg,但理论上听起来像bmps或tiff应该允许这种类型的读取。

I realize this may not be available for compressed formats such as jpegs, but in theory it sounds like bmps or tiffs should allow for this type of reading.

推荐答案

libvips 将只读取您需要的部分,当它能够。例如,如果您从大型PNG的左上角裁剪100x100像素,则速度很快:

libvips will read just the part you need, when it can. For example, if you crop 100x100 pixels from the top-left of a large PNG, it's fast:

$ time vips crop wtc.png x.jpg 0 0 100 100
real    0m0.063s
user    0m0.041s
sys 0m0.023s

(四个数字是从 wtc.png 裁剪的区域的左,顶部,宽度,高度并写入 x.jpg

(the four numbers are left, top, width, height of the area to be cropped from wtc.png and written to x.jpg)

但是从底部附近的100x100像素区域相当慢,因为它必须读取并在想要到达文件中正确位置的像素之前解压缩像素:

But a 100x100 pixel region from near the bottom is rather slow, since it has to read and decompress the pixels before the pixels you want to get to the right point in the file:

$ time vips crop wtc.png x.jpg 0 9000 100 100
real    0m3.063s
user    0m2.884s
sys 0m0.181s

JPG和剥离TIFF以相同的方式工作,虽然它不那么明显,因为它们的格式要快得多。

JPG and strip TIFF work in the same way, though it's less obvious since they are much faster formats.

某些格式支持true随机访问读取。例如,平铺的TIFF在任何地方都很快,因为libvips可以使用libtiff只读取它需要的tile:

Some formats support true random-access read. For example, tiled TIFF is fast everywhere, since libvips can use libtiff to read only the tiles it needs:

$ vips copy wtc.png wtc.tif[tile]
$ time vips crop wtc.tif x.jpg 0 0 100 100
real    0m0.033s
user    0m0.013s
sys 0m0.021s
$ time vips crop wtc.tif x.jpg 0 9000 100 100
real    0m0.037s
user    0m0.021s
sys 0m0.017s

OpenSlide,vips,平铺OpenEXR,FITS,二进制PPM / PGM / PBM,HDR,RAW,Analyze,Matlab以及其他一些支持像这样真正的随机访问。

OpenSlide, vips, tiled OpenEXR, FITS, binary PPM/PGM/PBM, HDR, RAW, Analyze, Matlab and probably some others all support true random access like this.

如果您对更多细节感兴趣,那么API文档中有一章描述了libvips如何打开文件:

If you're interested in more detail, there's a chapter in the API docs describing how libvips opens a file:

http ://jcupitt.github.io/libvips/API/current/How-it-opens-files.md.html

这里的裁剪加上保存Python使用 pyvips

Here's crop plus save in Python using pyvips:

import pyvips

image = pyvips.Image.new_from_file(input_filename, access='sequential')
tile = image.crop(left, top, width, height)
tile.write_to_file(output_filename)

access = 是一个标志,提示libvips它没关系如果底层文件格式不支持随机访问,则流式传输此图像。对于支持随机访问的格式(如平铺TIFF),您不需要这样。

The access= is a flag that hints to libvips that it's OK to stream this image, in case the underlying file format does not support random access. You don't need this for formats that do support random access, like tiled TIFF.

您无需写入文件。例如,这将写入 stdout 上的MIME类型:

You don't need to write to a file. For example, this will write to a MIME type on stdout:

tile.jpegsave_mime(Q=85)

或者这将使包含编码为a的文件的缓冲区对象成为JPG:

Or this will make a buffer object containing the file encoded as a JPG:

buffer = tile.write_to_buffer('.jpg', Q=85)

Q = 85 是设置JPG Q因子的可选参数。您可以设置任何文件保存选项

The Q=85 is an optional argument to set the JPG Q factor. You can set any of the file save options.

这篇关于从图像文件中提取感兴趣的区域而不读取整个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆