Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV image dimensions without reading entire image

Tags:

c++

image

opencv

I'm using OpenCV and am reading gigabytes of images -- too much to fit into memory at a single time. However, I need to initialize some basic structures which require the image dimensions. At the moment I'm using imread and then freeing the image right away, and this is really inefficient.

Is there a way to get the image dimensions without reading the entire file, using opencv? If not could you suggest another library (preferably lightweight, seeing as that's all it'll be used for) that can parse the headers? Ideally it would support at least as many formats as OpenCV.

like image 455
user1520427 Avatar asked Nov 27 '13 05:11

user1520427


People also ask

How do I get the height and width of an image in OpenCV?

When working with OpenCV Python, images are stored in numpy ndarray. To get the image shape or size, use ndarray. shape to get the dimensions of the image. Then, you can use index on the dimensions variable to get width, height and number of channels for each pixel.

How do I get the size of an image in Python?

open() is used to open the image and then . width and . height property of Image are used to get the height and width of the image.

How do you find the shape of an image in cv2?

In order to find the shape of a given image, we make use of a function in OpenCV called shape() function. The shape() function can provide the dimension of a given image.


2 Answers

I don't think this is possible in opencv directly.

Although it isn't specified in the docs, Java's ImageReader.getHight (and getWidth) only parse the image header, not the whole image.

Alternatively here is a reasonable looking lightweight library that definitely only checks the headers, and supports a good amount of image formats.

Finally, if you're on Linux the 'identify ' terminal command will also give you the dimensions, which you could then read in programmatically.

like image 77
James Barnett Avatar answered Oct 20 '22 01:10

James Barnett


You could 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;
}

You will have to link against libjpeg, by adding -ljpeg flag to the linker. You can get some more information here.

like image 45
Jaka Avatar answered Oct 20 '22 01:10

Jaka