[go: up one dir, main page]

0% found this document useful (0 votes)
38 views6 pages

Installing Open CV

This document provides a step-by-step guide for installing OpenCV on Windows using Python 3.x, covering methods via pip and conda, as well as verification and troubleshooting steps. It includes instructions for creating a virtual environment, installing necessary packages, and testing the installation with sample code. Additionally, it offers advanced options for building OpenCV from source for GPU support and common troubleshooting tips for installation issues.

Uploaded by

Bidyut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

Installing Open CV

This document provides a step-by-step guide for installing OpenCV on Windows using Python 3.x, covering methods via pip and conda, as well as verification and troubleshooting steps. It includes instructions for creating a virtual environment, installing necessary packages, and testing the installation with sample code. Additionally, it offers advanced options for building OpenCV from source for GPU support and common troubleshooting tips for installation issues.

Uploaded by

Bidyut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

How to install open cv step by step

guide_Bidyut Maishal.
Here’s a clear, practical, step-by-step guide to install OpenCV on Windows (works for Python
3.x). I’ll cover the easiest pip method, the conda alternative, verify steps, common
troubleshooting, and short notes on advanced (build-from-source / CUDA) if you need GPU
support.

1) Prerequisites

Python installed (3.7+) and working. Verify in a new terminal:

python --version

Use a virtual environment for projects (strongly recommended).

2) Create & activate a virtual environment (recommended)

Open Command Prompt or PowerShell and run:

# create project folder

mkdir my_opencv_project

cd my_opencv_project

# create venv

python -m venv venv

# activate (PowerShell)

.\venv\Scripts\Activate.ps1

# or activate (cmd.exe)

venv\Scripts\activate

When active your prompt shows (venv).

Upgrade pip, setuptools, wheel:


python -m pip install --upgrade pip setuptools wheel

3) Install OpenCV (simple & quick — pip)

There are two common pip packages:

opencv-python — core OpenCV (most users).


opencv-contrib-python — OpenCV + extra (“contrib”) modules (SIFT, xfeatures2d, etc).

Install one of these (don’t install both at the same time — uninstall one first if switching):

# core OpenCV

pip install opencv-python

# OR, for contrib modules (recommended if you need extra features)

pip install opencv-contrib-python

For headless servers (no GUI):

pip install opencv-python-headless

# or

pip install opencv-contrib-python-headless

4) Verify the installation

Run Python and try importing OpenCV and printing the version:

python - <<'PY'

import cv2

print("OpenCV version:", cv2.__version__)

PY

Or start an interactive Python shell:

>>> import cv2

>>> cv2.__version__

'4.x.x'

If that prints a version, installation worked.


5) Quick runtime tests (image + webcam)

Save a test image test.jpg in your folder and run:

# show_image.py

import cv2

img = cv2.imread('test.jpg') # load image

if img is None:

raise SystemExit("Failed to load test.jpg")

cv2.imshow('Test', img)

cv2.waitKey(0)

cv2.destroyAllWindows()

Run:

python show_image.py

Webcam capture test:

# webcam_test.py

import cv2

cap = cv2.VideoCapture(0) # 0 = default camera

if not cap.isOpened():

raise SystemExit("Cannot open camera")

ret, frame = cap.read()

if not ret:

raise SystemExit("Failed to grab frame")

cv2.imshow('Frame', frame)
cv2.waitKey(0)

cap.release()

cv2.destroyAllWindows()

Notes:

cv2.imshow opens a GUI window — this works on normal Windows desktop. It won’t work
inside some remote terminals or WSL without an X server.
If cap.isOpened() is False, check camera permissions/drivers or change index 0→1.

6) Optional: Install additional helpful packages

pip install numpy matplotlib pillow

(OpenCV uses NumPy; many tutorials use matplotlib for plots.)

7) Conda alternative (recommended if you use Anaconda/Miniconda)

If you prefer conda (often simpler for compiled libs):

conda create -n cvenv python=3.10

conda activate cvenv

conda install -c conda-forge opencv

Conda-forge builds often handle dependencies and codecs better.

8) Video codecs & ffmpeg (if video read/write fails)

If cv2.VideoCapture or cv2.VideoWriter has problems with some formats, install ffmpeg and
add it to PATH:

1. Download ffmpeg static build for Windows (from ffmpeg.org builds).


2. Unzip and add the bin folder (contains ffmpeg.exe) to your Windows PATH.
3. Restart your terminal.

9) Troubleshooting (common errors & fixes)

ImportError: DLL load failed or module 'cv2' has no attribute 'imshow':


Install Microsoft Visual C++ Redistributable (x64). Download from Microsoft (search
"Visual C++ Redistributable for Visual Studio"). Install the x64 version matching your
Python bitness.
Ensure Python (32-bit vs 64-bit) matches the pip wheel; use 64-bit Python for
modern wheels.
Multiple OpenCV packages conflict:
Uninstall first, then reinstall:
pip uninstall opencv-python opencv-contrib-python opencv-python-headless -y
pip install opencv-contrib-python
Camera not opening:
Check camera drivers, privacy settings (Windows → Settings → Privacy → Camera), try
different index (0,1).
cv2.imshow doesn't open window in WSL:
WSL doesn't provide Windows GUI by default. Use Windows Python or configure an X
server/GUI for WSL or use headless display (saving images to disk).
Video codecs missing:
Install ffmpeg or use conda-forge build.

10) Advanced: Build OpenCV from source (when you need CUDA/GPU support or custom
options)

If you need GPU (CUDA) support or custom compile flags, you must build from source. High-
level steps:

1. Install prerequisites: Visual Studio (with Desktop dev. workload), CMake, NVIDIA CUDA
toolkit (if GPU), Python dev headers, Git.
2. Clone OpenCV & opencv_contrib:
3. git clone https://github.com/opencv/opencv.git
4. git clone https://github.com/opencv/opencv_contrib.git
5. Create a build folder, run CMake GUI or CLI, set options:
PYTHON_EXECUTABLE → your python.exe in venv
OPENCV_EXTRA_MODULES_PATH → path to opencv_contrib/modules
Enable WITH_CUDA (if you have CUDA and compatible GPU)
6. Configure, generate Visual Studio solution, then build (Release config).
7. After build, copy generated cv2.pyd to your venv Lib\site-packages or install via
generated wheel.

This process is non-trivial — ask me if you want a step-by-step build script for your exact
CUDA + Visual Studio versions.

11) Recommended workflow summary

1. Create venv → activate.


2. pip install opencv-contrib-python numpy matplotlib (or use conda).
3. Verify import cv2 and cv2.__version__.
4. Install ffmpeg if you work with many video formats.
5. If you need GPU acceleration, plan to build from source.

Want me to:

Give you an exact copy-paste PowerShell script that creates a venv and installs OpenCV
+ test files, or
Help you build OpenCV with CUDA for your GPU (tell me your GPU model, CUDA
version, Visual Studio version), or
Provide a small sample project (README + code) you can clone and run?

You might also like