Python OpenCV2 vs. OpenCV3 API compatibility

Related: Install OpenCV for Python


Some legacy functions were removed completely 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 OpenCV 3 in Python. Upgrade to OpenCV 3 in Python to get the best performance and features.

FourCC and SimpleBlobDetector:

try:
    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

drawKeypoints

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

calcOpticalFlowFarneback

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

Leave a Comment