Ask Your Question

Is cv::imread able to load huge files?

asked 2013-09-08 13:35:32 -0500

Ramiro gravatar image

updated 2013-09-09 09:12:08 -0500


I'm using cv::imread to read a 10GB PGM file, but I'm getting the following error:

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

The machine running this has 64GB RAM, most of it currently free. The ulimit -a yelds the following output.

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 515986
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 515986
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

Here's the program I wrote.

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main(int argc, char* args[])
    if (argc != 2)
        return 1;

    cv::Mat img = cv::imread(args[1], cv::IMREAD_GRAYSCALE);
    if ( == 0)
        return 2;

    std::cout << img.rows << " - " << img.cols;

    return 0;

Is cv::imread unable to load images this size? Is there an alternative in the OpenCV library?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-09-10 09:04:33 -0500

OpenCV simple 32-bit integer as buffer size, image width, height and so on. In your case it looks like integer overflow.

edit flag offensive delete link more


Yes, looks like that's the issue, but I've read some parts of the OpenCV code (both from 2.4 and master branches) and, as far as I can tell, the casts to long are properly made... I think I'll open an issue.

Ramiro gravatar imageRamiro ( 2013-09-10 13:15:28 -0500 )edit
Ramiro gravatar imageRamiro ( 2013-09-10 16:18:50 -0500 )edit

answered 2013-09-09 00:41:42 -0500

Michael Burdinov gravatar image

As you can see from the name of error 'std::bad_alloc' is not an error of OpenCV library but an error of STD library (which is used by OpenCV for some operations). If STD was unable to allocate the memory it means you just don't have enough. Normally computers don't have 10GB RAM so they won't be able to perform operations on images of this size (at least not all at once). How much RAM do you have?

edit flag offensive delete link more


The machine running the program has 64GB RAM. I'll adjust the post to provide some aditional information about what I'm trying to do and the circunstances.

Ramiro gravatar imageRamiro ( 2013-09-09 09:07:10 -0500 )edit

Hmm. If that is the case I don't know how to solve the problem. I never had computer with enough resources to put this to the test.

Michael Burdinov gravatar imageMichael Burdinov ( 2013-09-10 01:07:06 -0500 )edit

Question Tools


Asked: 2013-09-08 13:35:32 -0500

Seen: 2,870 times

Last updated: Sep 10 '13