1

I realized a memory usage issue when I try to display huge images such as a 2GB of TIFF image. The code is below:

Mat image, output;

image = imread("img.tif", IMREAD_UNCHANGED);
//Memory usage : 2GB

normalize(image, output, 0, 0xFFFF, cv::NORM_MINMAX);
//Memory usage : 4GB

namedWindow("Display Image", WINDOW_NORMAL);
imshow("Display Image", output);
//Memory usage : 6.5GB

waitKey(0);

When I run this program step-by-step in Debug mode on Ubuntu environment, I saw that memory usage is increasing tremendously from System Monitor.

When imread is hit, my program uses approximately 2GB of memory. When normalize is called, it increases to approximately 4GB. And finally when imshow is called, it increases to ~6.5GB.

So what i think it creates new copies everytime I call imread, normalize and imshow.

Is there any way to overcome this issue ? What I think is that maybe i can manually normalize my original image, but is it possible to use imshow with reference of my original Mat object instead of creating a new one ?

  • Since your .tif file is 2GB, I am wondering if your .tif image is a single image or contains multiple images (image sequence)? – Ali Mar 12 '20 at 09:38
  • 1
    "When normalize is called, it increases to approximately 4GB." -- you did tell it to store the result in a second variable... | `imshow` uses an internal buffer to store the image in a format it can render. It's also a rudimentary convenience function. If you need a smarter UI, you most likely need to write your own. – Dan Mašek Mar 12 '20 at 09:41
  • @Ali it is one plain image which is constrcuted by stiching several images side-by-side. – Levent Bekdemir Mar 12 '20 at 09:53
  • @Dan Mašek I understand the case so i think i can conclude that i'm not doing something pointless, but the internal mechanics of opencv works that way. I am now wondering if there is any alternative solution for me to basically display a huge tif image on screen without consuming too much memory ? – Levent Bekdemir Mar 12 '20 at 10:15
  • Yeah, such large images are quite a corner case. How exactly do you want to display it -- based on the amount of data, I assume at original scale it is much too large to fit all on a single screen, correct? Do you want to display it scaled down (that's simple, just scale it down prior to calling `imshow`)? Or do you want to show it at original scale and be able to scroll around (that will take some work). – Dan Mašek Mar 12 '20 at 10:55
  • It is actually a panchromatic image taken from a satellite and the pixel size is roughly 36000x32000 which is of course not possible to fit into the screen. Since this is a satellite image, there will be necessity to zoom in or zoom out which means i don't want to lose any resolution (compression is not an option in that case) – Levent Bekdemir Mar 12 '20 at 11:14
  • 1
    You might consider looking at `libvips` and DeepZoom https://stackoverflow.com/a/29996099/2836621 – Mark Setchell Mar 12 '20 at 11:45
  • 2
    Also https://libvips.github.io/libvips/API/current/Making-image-pyramids.md.html – Mark Setchell Mar 12 '20 at 11:47

0 Answers0