Catching Opencv assertion errors: (-215:Assertion failed) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function ‘Mat’

Sometimes our c++ app may supply input dimensions that are incompatible for a Mat ROI, which results in the following exception from the Opencv library.

Although we are able to debug this in a full debugging environment, is there an easy way to detect the faulting line in application code that causes this assertion failure?

Surrounding every opencv api call with Try/catch is something we would like to avoid and have try/catch only at the higher levels; but this does not give us enough information currently for these type of assertion failures.

Exception caught: OpenCV(4.5.3-pre) /home/t1/gani/src/opencv/modules/core/src/matrix.cpp:811: error: (-215:Assertion failed) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function ‘Mat’

however you calculate those regions, your math is broken.

this is a programming/logic problem.

an exception is a message to you, that your code is doing something wrong
(and that the code / assumption behind it must be fixed.)

do your own (similar) checks before calling the resp. function, where still you can react accordingly.