OpenCV Contrib Python Headless
OpenCV (Open Source Computer Vision Library) is a powerful, open-source computer vision and machine learning software library. This specific Python binding package, `opencv-contrib-python-headless`, provides access to both the main OpenCV modules and the 'contrib' (extra) modules, but without any graphical user interface (GUI) dependencies like Qt. This makes it ideal for server-side processing, cloud deployments, Docker containers, and embedded systems where no display is available. It is currently at version 4.13.0.92 and follows a frequent release cadence, often tied to major OpenCV C++ library releases.
Warnings
- breaking This 'headless' package specifically excludes GUI functionality. Functions like `cv2.imshow()`, `cv2.waitKey()`, `cv2.imwrite()` (for some formats requiring GUI backends), and others that rely on windowing systems (like X11/Qt) will not work and may raise errors.
- breaking Version 4.13.0.90 of `opencv-python-headless` (and thus `opencv-contrib-python-headless`) introduced an accidental dependency on `libxcb.so.1` on Linux, which is part of X11 libraries. This caused import failures in truly headless environments.
- gotcha Do NOT install multiple different OpenCV Python packages (e.g., `opencv-python`, `opencv-contrib-python`, `opencv-python-headless`, `opencv-contrib-python-headless`) in the same Python environment. They all use the same `cv2` namespace, leading to conflicts and unpredictable behavior.
- gotcha NumPy version compatibility can be a common issue. OpenCV's Python bindings are compiled against specific NumPy versions. Incompatible NumPy versions can lead to `ImportError` or runtime crashes.
- breaking OpenCV 4.x introduced significant API changes compared to 2.x and 3.x, including the removal of many C API functions and structures, reorganization of modules (some moved to `opencv_contrib`), and changes in function signatures (e.g., `findContours` returning a pair instead of a triple in Python bindings). OpenCV now requires C++17.
- gotcha This package includes 'contrib' (extra) modules. While these offer advanced functionalities (like SIFT, SURF, some machine learning algorithms), they may sometimes be less stable, less well-tested, or include patented/non-free algorithms compared to the main modules. Some `contrib` algorithms (e.g., `xfeatures2d`) require explicit import or different instantiation patterns.
Install
-
pip install opencv-contrib-python-headless
Imports
- cv2
import cv2
Quickstart
import cv2
import numpy as np
# Create a dummy image (e.g., a black 100x100 pixel image)
# In a headless environment, you would typically load an image from disk or a stream
image = np.zeros((100, 100, 3), dtype=np.uint8)
# Perform a simple operation: draw a white rectangle
cv2.rectangle(image, (20, 20), (80, 80), (255, 255, 255), -1)
# Convert image to grayscale
grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Print image properties (no GUI interaction)
print(f"OpenCV Version: {cv2.__version__}")
print(f"Original Image Shape: {image.shape}")
print(f"Grayscale Image Shape: {grayscale_image.shape}")
# Example of using a contrib module (if applicable and available in this build)
# Note: Not all contrib features are guaranteed to be present or stable.
# For example, SIFT/SURF are in contrib and often patented/non-free. We'll use a simpler one.
# Example: Feature2D algorithms are common in contrib.
# This specific example might not run without a specific image for feature detection
# For simplicity, we'll stick to basic operations for quickstart.