Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
Fix OpenCV 3 compatibility in OpenCVMatcher (stereo 3D reconstruction)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonard-Plotkin committed Oct 22, 2015
1 parent 3f8937b commit 338ae1e
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions pydriver/stereo/stereo.pyx
Expand Up @@ -136,29 +136,38 @@ class OpenCVMatcher(object):
def __init__(self, **kwargs):
"""Initialize OpenCVMatcher instance
Keyword arguments are the non-default parameters to pass to *cv2.StereoSGBM()*.
Keyword arguments are the non-default parameters to pass to *cv2.StereoSGBM_create()*.
The class uses its own default parameters which do not match OpenCV defaults.
"""
# keyword arguments for cv2.StereoSGBM()
SADWindowSize = kwargs.get('SADWindowSize', 1)
# import OpenCV here so we can check whether OpenCV is installed before using this class
import cv2

# keyword arguments for cv2.StereoSGBM_create()
blockSize = kwargs.get('blockSize', 1)
self.params = {
'minDisparity': kwargs.get('minDisparity', 0),
'numDisparities': kwargs.get('numDisparities', 128),
'SADWindowSize': SADWindowSize,
'P1': kwargs.get('P1', 16 * 3 * min(SADWindowSize, 7)**2), # X * 3[channels] * SADWindowSize**2
'P2': kwargs.get('P2', 128 * 3 * min(SADWindowSize, 7)**2), # X * 3[channels] * SADWindowSize**2
'blockSize': blockSize,
'P1': kwargs.get('P1', 16 * 3 * min(blockSize, 7)**2), # X * 3[channels] * blockSize**2
'P2': kwargs.get('P2', 128 * 3 * min(blockSize, 7)**2), # X * 3[channels] * blockSize**2
'disp12MaxDiff': kwargs.get('disp12MaxDiff', 1),
'preFilterCap': kwargs.get('preFilterCap', 0),
'uniquenessRatio': kwargs.get('uniquenessRatio', 10),
'speckleWindowSize': kwargs.get('speckleWindowSize', 200),
'speckleRange': kwargs.get('speckleRange', 16),
'fullDP': True, # True uses a lot of memory
}
# import OpenCV here so we can dynamically decide whether to use this class
import cv2
# initialize StereoSGBM() instance
self._StereoSGBM = cv2.StereoSGBM(**self.params)
if 'StereoSGBM_create' in dir(cv2):
# OpenCV 3
self.params['mode'] = kwargs.get('mode', cv2.STEREO_SGBM_MODE_SGBM)
self._StereoSGBM = cv2.StereoSGBM_create(**self.params)
else:
# OpenCV 2 compatibility
self.params['SADWindowSize'] = self.params['blockSize']
self.params['fullDP'] = bool(kwargs.get('mode', False))
del self.params['blockSize']
self._StereoSGBM = cv2.StereoSGBM(**self.params)

def computeDisparity(self, cnp.ndarray img_left, cnp.ndarray img_right):
"""Compute left disparity map for a pair of left and right images
Expand Down

0 comments on commit 338ae1e

Please sign in to comment.