Stereo Algorthim fails: cv2.StereoSGBM

asked 2018-11-27 02:41:06 -0600

Raki gravatar image

I am trying out various stereo matchers to build a disparity map of two image I took and I am using the following piece of code:

import numpy as np
import cv2
print('loading images...')
imgL = cv2.imread('imgL.jpg')  # downscale images for faster processing if you like
imgR = cv2.imread('imgR.jpg')


# SGBM Parameters -----------------
window_size = 15                     # wsize default 3; 5; 7 for SGBM reduced size image; 15 for SGBM full size image (1300px 

    and above); 5 Works nicely

left_matcher = cv2.StereoSGBM_create(
    minDisparity=0,
    numDisparities=160,             # max_disp has to be dividable by 16 f. E. HH 192, 256
    blockSize=5,
    P1=8 * 3 * window_size ** 2,    # wsize default 3; 5; 7 for SGBM reduced size image; 15 for SGBM full size image (1300px and above); 5 Works nicely
    P2=32 * 3 * window_size ** 2,
    disp12MaxDiff=1,
    uniquenessRatio=15,
    speckleWindowSize=0,
    speckleRange=2,
    preFilterCap=63,
    mode=cv2.STEREO_SGBM_MODE_SGBM_3WAY
)

right_matcher = cv2.ximgproc.createRightMatcher(left_matcher)

# FILTER Parameters
lmbda = 80000
sigma = 1.2
visual_multiplier = 1.0

wls_filter = cv2.ximgproc.createDisparityWLSFilter(matcher_left=left_matcher)
wls_filter.setLambda(lmbda)
wls_filter.setSigmaColor(sigma)

print('computing disparity...')
displ = left_matcher.compute(imgL, imgR)  # .astype(np.float32)/16
dispr = right_matcher.compute(imgR, imgL)  # .astype(np.float32)/16
displ = np.int16(displ)
dispr = np.int16(dispr)
filteredImg = wls_filter.filter(displ, imgL, None, dispr)  # important to put "imgL" here!!!

filteredImg = cv2.normalize(src=filteredImg, dst=filteredImg, beta=0, alpha=255, norm_type=cv2.NORM_MINMAX);
filteredImg = np.uint8(filteredImg)
cv2.imshow('Disparity Map', filteredImg)
cv2.waitKey()
cv2.destroyAllWindows()

and getting the following error:

computing disparity...
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-17-6d8b62cb8a7b> in <module>
      1 print('computing disparity...')
----> 2 displ = left_matcher.compute(imgL, imgR)  # .astype(np.float32)/16
      3 dispr = right_matcher.compute(imgR, imgL)  # .astype(np.float32)/16
      4 displ = np.int16(displ)
      5 dispr = np.int16(dispr)

error: OpenCV(3.4.3) /io/opencv/modules/core/src/parallel.cpp:240: error: (-2:Unspecified error) in function 'finalize'
> Exception in parallel_for() body: OpenCV(3.4.3) /io/opencv/modules/core/src/matrix.cpp:669: error: (-215:Assertion failed) nelems <= size_t(INT_MAX)*size_t(INT_MAX) in function 'reserveBuffer'

Is there something I am missing?

edit retag flag offensive close merge delete

Comments

try to disable parallel_for using setNumthreads

LBerger gravatar imageLBerger ( 2018-11-27 07:34:19 -0600 )edit

Sorry I didn't get what exactly I have to use. I don't have setNumthreads() in my code.

Raki gravatar imageRaki ( 2018-11-27 07:37:51 -0600 )edit
1

Add after import cv2 cv2.setNumThreads( 0)

LBerger gravatar imageLBerger ( 2018-11-27 07:40:33 -0600 )edit

It worked, thanks

Raki gravatar imageRaki ( 2018-11-27 07:51:29 -0600 )edit

and please always add after imread :

if imgL is None or imgR is None:
    print ("Check path")
    exit()
LBerger gravatar imageLBerger ( 2018-11-27 07:52:55 -0600 )edit

and I don't need setNumthreads when imgL and imgR are not None

LBerger gravatar imageLBerger ( 2018-11-27 07:55:03 -0600 )edit

Okay, noted, thanks

Raki gravatar imageRaki ( 2018-11-27 08:27:58 -0600 )edit