7

I tried to run code from the OpenCV docs for creating a disparity image from left and right stereo images, but it gives me an error. Here is the code I ran.

import cv2
from matplotlib import pyplot as plt

imgL = cv2.imread(r'D:\left.png', 0)
imgR = cv2.imread(r'D:\right.png', 0)

stereo = cv2.createStereoBM(numDisparities=16, blockSize=15)
disparity = stereo.compute(imgL, imgR)
plt.imshow(disparity, 'gray')
plt.show()

I am getting an error in the line

stereo = cv2.createStereoBM(numDisparities=16, blockSize=15)

And this is the error

AttributeError: module 'cv2' has no attribute 'createStereoBM'

I have tried many other solutions previously listed on stackoverflow, github, and other forums but none of them seemed to work.

The link to the OpenCV documentation can be found here

Any help will be appreciated. Thanks.

ntd
  • 1,222
  • 1
  • 10
  • 18
  • 1
    have you tried `stereo = cv2.StereoBM(numDisparities=16, blockSize=15)`? i.e. remove the `create` prefix from the method name – wpercy Mar 04 '19 at 21:27
  • @wpercy Yes, I have tried that. In that case, I get error in the next line `disparity = stereo.compute(imgL, imgR)`. The error reads `TypeError: Incorrect type of self (must be 'StereoMatcher' or its derivative)` – ntd Mar 04 '19 at 21:38
  • 7
    How about `cv2.StereoBM_create(numDisparities=16, blockSize=15)`? – Oluwafemi Sule Mar 04 '19 at 21:39
  • @OluwafemiSule This seemed to work for me! Thanks a lot! I had actually tried this method before but didn't get the results I was hoping for that time. So this time I changed the value of `numDisparities` to `64` and got the result I wanted. – ntd Mar 04 '19 at 21:59
  • Does this answer your question? [AttributeError: 'module' object has no attribute](https://stackoverflow.com/questions/21702945/attributeerror-module-object-has-no-attribute) – vwvw Jan 21 '20 at 10:13

1 Answers1

3

As oluwafemi-sule mentioned in the comments you just need to use cv2.StereoBM_create instead of cv2.createStereoBM and it will work

enter image description here