Remove OpenCV image size limitation

Viewed 3720

I am loading in a very large image (60,000 x 80,000 pixels) and am exceeding the max pixels I can load:

cv2.error: OpenCV(4.2.0) /Users/travis/build/skvark/opencv-python/opencv/modules/imgcodecs/src/loadsave.cpp:75: 
    error: (-215:Assertion failed) pixels <= CV_IO_MAX_IMAGE_PIXELS in function 'validateInputImageSize'

From what I have found this is referring to the limitation imposed on line 65

Ideally I'd change that to deal with at least 5 gigapixel images

#define CV_IO_MAX_IMAGE_PIXELS (1<<33)

I have seen some workarounds for this (OpenCV image size limit) but those don't seem to address the problem which is an arbitrary definition (I'm working off a high performance server with 700gb ram so compute not an issue).

My issue is that I have no idea where this file is. The error points me towards this "travis" directory which doesn't exist locally for me and in my local environment the c++ files aren't available.

Any idea on where to look to find the c++ library?

3 Answers

You have to modify the openCV source files and then compile it your own.

EDIT: You can also modify environment variables

export CV_IO_MAX_IMAGE_PIXELS=1099511627776

For my problem I should have specified it was a .tif file (NOTE most large images will be in this file format anyway). In which case a very easy way to load it in to a numpy array (so it can then work with OpenCV) is with the package tifffile.

pip install tifffile as tifi

This will install it in your python environment.

import tifffile as tifi
img = tifi.imread("VeryLargeFile.tif")

From here you can use it as you would with any numpy array and it is fully compatible with OpenCV etc.

Adding the following to your program should fix the issue in python opencv.

import os
os.environ["OPENCV_IO_MAX_IMAGE_PIXELS"] = str(pow(2,40))
import cv2
Related