Open In App

Transition from OpenCV 2 to OpenCV 3.x

OpenCV is one of the most popular and most used Computer vision libraries. It contains tools to carry out image and video processing.

When OpenCV 3..4.1 is an improved version of OpenCV 2.4 as it introduced new algorithms and features. Although some of the existing modules were rewritten and moved to sub-modules.  In this articles, I will focus on the changes made in the existing modules of OpenCV 2.4 and how they can be implemented in OpenCV 3.4.1.



Feature Detection

Some of the feature detection algorithms (FREAK, BRIEF, SIFT and SURF) have been moved to opencv_contrib repository and xfeatures2d module. SIFT and SURF algorithms are patented by their creators and are non-free. Although they can be used for educational and research purposes.

SIFT : Create SIFT feature detector object.






# OpenCV 2.4
sift = cv2.SIFT()
  
# OpenCV 3.4.1
sift = cv2.xfeatures2d.SIFT_create()

SURF : Create SURF feature detector object




# OpenCV 2.4
surf = cv2.SURF()
  
# OpenCV 3.4.1
surf = cv2.xfeatures2d.SURF_create()

FAST : Create FAST detector object




# OpenCV 2.4
fast = cv2.FastFeatureDetector()
  
# OpenCV 3.4.1
fast = cv2.FastFeatureDetector_create()

ORB : Create ORB detector object




# OpenCV 2.4
orb = cv2.ORB()
  
# OpenCV 3.4.1
orb = cv2.ORB_create()

Simple Blob Detector




# OpenCV 2.4
detector = cv2.SimpleBlobDetector()
  
# OpenCV 3.4.1
detector = cv2.SimpleBlobDetector_create()

CIRCLE DETECTION

OpenCV uses Hough Gradient Method to detect circles that uses gradient information of the edges.




# OpenCV 3.4.1
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 4, 10)

The name of the method has been changed from CV_HOUGH_GRADIENT in 2.4 version to HOUGH_GRADIENT in 3.4 version.

CONTOURS

Initially the findContours() function returned only two parameters in OpenCV 2.4 . In OpenCV 3.2 onwards, the function was modified to return three parameters i.e. the modified image, contours and hierarchy.




# OpenCV 2.4
contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  
# OpenCV 3.4.1
im, contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, 
                                           cv2.CHAIN_APPROX_NONE)

These were a few important changes that could be useful while migrating the code from OpenCV 2 .4 to OpenCV 3.x .


Article Tags :