Skip to content

Workbench article

OpenCV Environment Setup

Check whether VideoCapture failed, produced an empty frame, or returned a valid but dark image.

2. OpenCV Environment Setup: Building a Robust Lab

A messy environment is the #1 cause of "it works on my machine" bugs. We will set up a professional vision development environment using Python virtual environments.

2.1 Creating a Virtual Environment

Always isolate your vision projects. OpenCV has many dependencies that can conflict with system-wide packages.

# Create the environment
python -m venv benchleaf-vision
# Activate it
source benchleaf-vision/bin/activate # Linux/Mac
benchleaf-vision\Scripts\activate # Windows

2.2 Installing the Right Version

We recommend the opencv-contrib-python package, which includes extra modules for advanced features like ArUco markers and specialized filters.

pip install opencv-contrib-python numpy matplotlib
Note: If you are working on a headless server (no monitor), use opencv-python-headless to avoid issues with GUI library dependencies like libGL or Qt.

Once installed, you can verify your installation by running: python -c "import cv2; print(cv2.__version__)".