Unable to run tune_blockmatcher and image_to_pointcloud programs

ghz 12hours ago ⋅ 7 views

I am able to capture images on webcams and calibrate them well using StereoVision library from erget.

But, while trying for execution of tune_blockmatcher and images_to_pointcloud, i found below errors as StereoBM and StereoSGBM classes are unable to import.

>import cv2
>from stereovision.blockmatchers import StereoBM, StereoSGBM
Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
 File "C:\Python27\lib\site-packages\stereovision\blockmatchers.py", line 111,
in <module>
   class StereoBM(BlockMatcher):
 File "C:\Python27\lib\site-packages\stereovision\blockmatchers.py", line 117,
in StereoBM
   "stereo_bm_preset": cv2.STEREO_BM_NARROW_PRESET}
AttributeError: 'module' object has no attribute 'STEREO_BM_NARROW_PRESET'
>`

C:\s\StereoVision-master\stereovision>python tune_blockmatcher.py
Traceback (most recent call last):
  File "tune_blockmatcher.py", line 34, in <module>
    from stereovision.blockmatchers import StereoBM, StereoSGBM
  File "build\bdist.win-amd64\egg\stereovision\blockmatchers.py", line 111, in <
module>
    class StereoBM(BlockMatcher):
  File "build\bdist.win-amd64\egg\stereovision\blockmatchers.py", line 117, in S
tereoBM
    "stereo_bm_preset": cv2.STEREO_BM_NARROW_PRESET}
AttributeError: 'module' object has no attribute 'STEREO_BM_NARROW_PRESET'

OS i am running is Windows7, running OPENCV 3.4.0 and Python2.7, installed stereovision library with 'pip install StereoVision'

Any solution for this issue ?

Answer

The error you're encountering:

AttributeError: 'module' object has no attribute 'STEREO_BM_NARROW_PRESET'

indicates that the cv2 module in your environment does not have the STEREO_BM_NARROW_PRESET attribute, which is likely due to the OpenCV version you are using.

Explanation:

The STEREO_BM_NARROW_PRESET constant is part of OpenCV, but it was introduced in later versions of OpenCV (specifically OpenCV 4.x). Since you're using OpenCV 3.4.0, the error occurs because OpenCV 3.4.0 does not have this constant.

In OpenCV 3.x, cv2.STEREO_BM_NARROW_PRESET is not available. This constant is part of OpenCV 4.x, which has improvements in the stereo block matching (StereoBM) algorithms.

Solution:

  1. Upgrade OpenCV: The easiest solution is to upgrade your OpenCV version to 4.x, which includes the STEREO_BM_NARROW_PRESET constant.

    You can upgrade OpenCV to version 4.x using pip:

    pip install opencv-python==4.5.1.48
    

    This version should resolve the STEREO_BM_NARROW_PRESET error since it contains the relevant constants and enhancements for stereo vision.

  2. Alternative Workaround: If upgrading OpenCV is not an option or if you need to continue using OpenCV 3.x for compatibility reasons, you can modify the blockmatchers.py file in the StereoVision library to avoid using STEREO_BM_NARROW_PRESET.

    Here is how you can modify the code:

    • Open the file blockmatchers.py located in the stereovision directory.
    • Find the line where cv2.STEREO_BM_NARROW_PRESET is used, around line 117.
    • Replace cv2.STEREO_BM_NARROW_PRESET with cv2.STEREO_BM_BASIC or cv2.STEREO_BM_DEFAULT, which are available in OpenCV 3.x.

    For example:

    "stereo_bm_preset": cv2.STEREO_BM_BASIC
    

    or

    "stereo_bm_preset": cv2.STEREO_BM_DEFAULT
    

    These presets should work with OpenCV 3.x and are also valid for StereoBM in older OpenCV versions.

  3. Reinstall StereoVision: After making the modifications or upgrading OpenCV, ensure you reinstall the StereoVision library:

    pip uninstall StereoVision
    pip install StereoVision
    

Steps for upgrading OpenCV:

  1. Uninstall the current OpenCV version:

    pip uninstall opencv-python
    
  2. Install a compatible version of OpenCV (for example, 4.5.1.48):

    pip install opencv-python==4.5.1.48
    
  3. Re-run your script to check if the error is resolved.

Additional Notes:

  • OpenCV 3.x and OpenCV 4.x have differences in both APIs and functionality, so if you upgrade to OpenCV 4.x, make sure to verify other parts of your code for compatibility.
  • If upgrading OpenCV causes other issues with your environment or dependencies, you may need to resolve version conflicts between packages.

Let me know if you need further assistance!