Python OpenCV2 vs. OpenCV3 API compatibility

The OpenCV2 API in Python is a vast improvement over the non-Numpy cumbersomeness of OpenCV1. Moving to OpenCV3 in Python, some slight changes were made to the API, that so far I have found some easy workarounds for.

Not so easy was the removal of “legacy” functions from OpenCV3, such as cv.CalcOpticalFlowHS(). I don’t plan to find a workaround for those since Horn-Schunck and Lucas-Kanade are available as pure Python.

Here are some example workarounds that allow the same Python code to work in OpenCV 2 and OpenCV3 in Python 3.

FourCC and SimpleBlobDetector:

    from cv2 import cv
    from cv import FOURCC as fourcc
    from cv2 import SimpleBlobDetector as SimpleBlobDetector
except ImportError:
    from cv2 import VideoWriter_fourcc as fourcc
    from cv2 import SimpleBlobDetector_create as SimpleBlobDetector


In OpenCV 3, you must pass the outImage argument, and OpenCV 2 is happy with this as well.


The third argument must be specified as flow= to be OpenCV 2/3 compatible.

Leave a Comment