Key takeaways
- Install OpenCV for Python with pip install opencv-python and import it as cv2.
- Install only one OpenCV package per environment: opencv-python, opencv-contrib-python or a headless variant.
- Most 'module not found' errors come from installing into a different Python environment from the one your editor uses.
- cv2.resize takes the size as (width, height); use INTER_AREA to shrink and INTER_CUBIC or INTER_LINEAR to enlarge.
- findContours needs a binary image and returns outlines you can measure, filter and draw.
To install OpenCV for Python, run pip install opencv-python in the environment you use, then import cv2 in your code. Use opencv-contrib-python if you need extra modules, and a -headless variant on servers without a display. Most installation problems come from installing into a different Python environment from the one your editor or notebook runs. This page covers pip and conda, VS Code, PyCharm and Jupyter, macOS and Linux, and two functions everyone uses early: resize and findContours.
How do I install OpenCV?
For Python, create a virtual environment, activate it, and run pip install opencv-python. Check it with python -c "import cv2; print(cv2.__version__)".
Also asked as: how to install opencv · how to install opencv in vscode
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install opencv-python
python -c "import cv2; print(cv2.__version__)"
For C++ projects, install OpenCV through your system's package manager or build it from source with CMake, following the official installation guides. For most students, the Python package is enough for image processing, video and classic computer vision, and pairs well with NumPy, and with PyTorch or ONNX Runtime for deep learning models.
Can you pip install OpenCV?
Yes. The official Python packages on PyPI are opencv-python for the main modules, opencv-contrib-python for main plus extra modules, and opencv-python-headless and opencv-contrib-python-headless for servers without GUI functions.
Rules that avoid strange errors:
- Install only one of these packages in an environment; they conflict.
- Use headless versions in Docker and on servers, where
cv2.imshowis not needed. - Upgrade pip first if installation tries to build from source:
python -m pip install --upgrade pip. - Use
python -m pipso pip matches the Python you run.
If import cv2 fails after installing, the package is almost certainly in another environment.
How do I install OpenCV in VS Code?
Open your project folder, create a virtual environment, select it as the interpreter with "Python: Select Interpreter" in the command palette, then run pip install opencv-python in VS Code's terminal, which uses that environment.
If VS Code shows "Import cv2 could not be resolved", the editor is using a different interpreter from the one where you installed OpenCV. Check the interpreter shown in the status bar, select the .venv one, and reload the window. Also confirm the integrated terminal has the environment activated before installing. Install the Python extension from Microsoft for IntelliSense and debugging support.
How do I install OpenCV in PyCharm?
Open Settings, then Project, then Python Interpreter, make sure your project's virtual environment is selected, click the plus button, search for opencv-python and install it. Or run pip install opencv-python in PyCharm's terminal, which uses the project interpreter.
Also asked as: how to install opencv in pycharm
If the import still fails, check that the run configuration uses the same interpreter as the project. PyCharm can create a separate environment per project, so a package installed in one project will not appear in another. For autocompletion of cv2, recent OpenCV packages include type hints; if completion is poor, restart PyCharm so it re-indexes the environment.
How do I install OpenCV in a Jupyter Notebook?
Install OpenCV into the same environment as the notebook's kernel. The safest way is to run %pip install opencv-python in a notebook cell, which installs into the active kernel, then restart the kernel.
Also asked as: how to install opencv in jupyter notebook
If you use several environments, register each as a Jupyter kernel with python -m ipykernel install --user --name myenv and select the right kernel in the notebook. Note that cv2.imshow opens a separate window and does not display inline; in notebooks, convert the image from BGR to RGB with cv2.cvtColor and show it with Matplotlib's plt.imshow. In Google Colab, OpenCV is usually preinstalled, and Colab provides a patched display helper for images.
How do I install OpenCV through conda?
Use the conda-forge channel: conda install -c conda-forge opencv, inside a conda environment you created for the project. Then import cv2 as usual.
Also asked as: how do i install python opencv through conda
conda create -n cv python=3.11
conda activate cv
conda install -c conda-forge opencv
Avoid mixing conda's OpenCV with a pip-installed OpenCV in the same environment, because they can conflict. If you mostly use pip packages, a plain virtual environment with opencv-python is simpler. Conda is useful when you also need other compiled libraries that conda manages well, such as specific scientific packages.
Does OpenCV work on Mac?
Yes. OpenCV works on macOS, including Apple Silicon Macs, through pip with pip install opencv-python or through conda-forge or Homebrew. Prebuilt wheels support both Apple Silicon and Intel Macs.
Also asked as: how does opencv find contours work
Tips for Mac users: use a Python from python.org, Homebrew or a virtual environment rather than the system Python; grant camera permission to your terminal or editor in System Settings when using cv2.VideoCapture(0); and remember that cv2.imshow windows need cv2.waitKey to refresh. For deep learning inference on Apple Silicon, combine OpenCV for image processing with frameworks that use the Mac's GPU.
Does OpenCV work on Linux?
Yes. OpenCV works very well on Linux, which is the most common platform for computer vision servers and research. Install it with pip in a virtual environment, or through your distribution's package manager.
On servers and in Docker, use opencv-python-headless to avoid GUI dependencies; otherwise you may see errors about missing libGL libraries, which you can fix by installing the system library or switching to the headless package. For GPU-accelerated OpenCV functions with CUDA, you need to build from source with CUDA enabled; the pip packages do not include CUDA support. Many projects instead run deep learning on the GPU with PyTorch or ONNX Runtime and use OpenCV for pre- and post-processing.
How does OpenCV resize work?
cv2.resize scales an image to a new width and height, computing new pixel values with an interpolation method. Note that the size argument is (width, height), while NumPy image shapes are (height, width, channels).
import cv2
img = cv2.imread("photo.jpg")
small = cv2.resize(img, (640, 360), interpolation=cv2.INTER_AREA) # shrinking
big = cv2.resize(img, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC) # enlarging by factor
Use INTER_AREA for shrinking, which avoids moire patterns, and INTER_LINEAR or INTER_CUBIC for enlarging. To keep the aspect ratio, compute one dimension from the other. For OCR, upscaling small text images often improves recognition.
How does OpenCV findContours work?
cv2.findContours traces the boundaries of white shapes in a binary image and returns them as lists of points, plus a hierarchy describing which contours are inside others. You then measure, filter or draw them.
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
big = [c for c in contours if cv2.contourArea(c) > 500]
cv2.drawContours(img, big, -1, (0, 255, 0), 2)
Use RETR_EXTERNAL for outer shapes only, RETR_TREE for nested ones. Objects should be white on black; invert the threshold if needed. Contours are useful for counting objects, measuring sizes and cropping documents.





