How to bypass large Image

If the image is too big, the program crashes when imread, if I want to throw an error when the image can’t be loaded, let the program continue to run, if I don’t want to use exception capture, then is there a good way to deal with it?

You need to use some library or external program to read the image’s dimensions without actually loading it.

If the dimensions are too big, skip that image. This is just general programming, not an OpenCV problem.

OpenCV has some env vars to limit how large an image will be loaded.

all of them, specifically imgcodecs-related ones:

https://docs.opencv.org/4.x/d6/dea/tutorial_env_reference.html#autotoc_md990

here’s an excerpt. pick what looks good to you.

name type default description
OPENCV_IMGCODECS_AVIF_MAX_FILE_SIZE num 64MB limit input AVIF size
OPENCV_IMGCODECS_WEBP_MAX_FILE_SIZE num 64MB limit input WEBM size
OPENCV_IO_MAX_IMAGE_PARAMS num 50 limit maximum allowed number of parameters in imwrite and imencode
OPENCV_IO_MAX_IMAGE_WIDTH num 1 << 20, limit input image size to avoid large memory allocations
OPENCV_IO_MAX_IMAGE_HEIGHT num 1 << 20
OPENCV_IO_MAX_IMAGE_PIXELS num 1 << 30
OPENCV_IO_ENABLE_OPENEXR bool true (set build option OPENCV_IO_FORCE_OPENEXR or use external OpenEXR), false (otherwise) enable OpenEXR backend
OPENCV_IO_ENABLE_JASPER bool true (set build option OPENCV_IO_FORCE_JASPER), false (otherwise) enable Jasper backend

Because I don’t want to use some other libraries to process this image, if you can’t handle too large a picture when imread in OpenCV, why not return a null value, but let the program just crash? I don’t really understand this design idea.

I feel that environmental variables are a palliative solution, and there is a better way to deal with it. I feel that environmental variables are a palliative solution, and there are better ones

you could always submit an issue on different handling of large images.

you know you can set env vars from inside your program, right?

if you don’t want to use the facilities that OpenCV offers, you’ll have to use image loading libraries directly, instead of through OpenCV’s abstraction.

if you don’t want to use such libraries either, you are out of options.

there is nothing more we can do for you. good luck.

Thank you very much for your answer

ah, cmon, it’s just fopen() / fseek() to find out how many bytes there are on disk. built into the language since like 1970 !
(really, general progging skills, do you have any ??)

then, just skip any file > 10mb, done.

a decent heuristic but nothing prevents one from crafting large images that compress well.