I am using ECC for camera motion compensation but it fails with the following error:
OpenCV(4.11.0) /Users/xperience/GHA-Actions-OpenCV/_work/opencv-python/opencv-python/opencv/modules/video/src/ecc.cpp:589: error: (-7:Iterations do not converge) The algorithm stopped before its convergence. The correlation is going to be minimized. Images may be uncorrelated or non-overlapped in function 'findTransformECC'
My guess is that I have two images that are too dissimilar and hence the algorithm does not converge, but in the context of tracking for low FPSs and heavy camera motion this is not too uncommon. This must be failing already in the c++ layer, not throwing a catchable exception:
warp_matrix = np.eye(2, 3, dtype=np.float32)
try:
(ret_val, warp_matrix) = cv2.findTransformECC(
prev_img,
curr_img,
warp_matrix,
cv2.MOTION_EUCLIDEAN,
(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 100, 1e-5),
None,
1
)
except Exception as e:
LOGGER.warning(f'Affine matrix could not be generated: {e}. Returning identity')
return warp_matrix
I also tried to redirect an error with a callback, catching cv2.error
, but nothing. And GPT is giving me BS answers.
OpenCV maps all those errors to cv.error
(a subclass of Exception
) with differing e.code
attribute. Your error has code -7
, which is cv.Error.StsNoConv
or cv.Error.STS_NO_CONV
. The exception has a few other attributes but they're all included in the error message. e.func
here should be "cv::findTransformECC"
(a str
).
You could do something like this:
try:
... # cv.findTransformECC() call
except cv.error as e:
if e.code == cv.Error.StsNoConv:
...
else: # other error codes
raise