OpenCV image sizes without reading the entire image

I use OpenCV and read gigabytes of images - too much to fit into memory at a time. However, I need to initialize some basic structures that require image sizes. At the moment, I use imreadand then immediately release the image, and it is really inefficient.

Is there a way to get image sizes without reading the whole file using opencv? If you hadn’t proposed another library (preferably light, seeing that all this will be used), which can analyze the headers? Ideally, it would support at least as many formats as OpenCV.

+4
source share
2 answers

I do not think this is possible in opencv directly.

Although not specified in the docs, Java ImageReader.getHight (and getWidth) only handle the image header, not the entire image.

Alternatively, here is a reasonably looking lightweight library that definitely checks for headers and supports a good number of image formats.

Finally, if you are working on Linux, the “identify” command of the terminal will also give you sizes that you could then read programmatically.

+2
source

You can use boost gil:

#include <boost/gil/extension/io/jpeg_io.hpp>

int main(int argc, char *argv[])
{
    //set/get file_path
    auto dims = boost::gil::jpeg_read_dimensions(file_path);
    int width = dims.x;
    int height = dims.y;
}

libjpeg, -ljpeg . .

0

All Articles