Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/opencv/opencv/llms.txt

Use this file to discover all available pages before exploring further.

Prerequisites

Before starting, ensure you have OpenCV installed. See the Installation Guide if you haven’t already set up OpenCV.

Your First OpenCV Program

Let’s create a simple program to load and display an image.
1

Create your project

Create a new directory for your project:
mkdir opencv-quickstart
cd opencv-quickstart
2

Write your first program

Create a file and load an image:
Create display_image.py:
import cv2 as cv
import sys

# Load an image
img = cv.imread('image.jpg')

# Check if image was loaded successfully
if img is None:
    sys.exit("Could not read the image.")

# Display the image
cv.imshow("Display window", img)
k = cv.waitKey(0)

# Save if 's' key is pressed
if k == ord("s"):
    cv.imwrite("output.png", img)
Make sure you have an image file named image.jpg in the same directory, or use cv.samples.findFile("starry_night.jpg") to use a built-in sample image.
3

Run your program

Execute your program:
python display_image.py
A window will appear displaying your image. Press any key to close it, or press ‘s’ to save the image.

Basic Image Processing

Now let’s do some actual image processing:
import cv2 as cv

# Read the image
img = cv.imread('image.jpg')

# Convert to grayscale
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

# Apply Gaussian blur
blurred = cv.GaussianBlur(gray, (5, 5), 0)

# Detect edges using Canny
edges = cv.Canny(blurred, 50, 150)

# Display all results
cv.imshow('Original', img)
cv.imshow('Grayscale', gray)
cv.imshow('Edges', edges)

cv.waitKey(0)
cv.destroyAllWindows()

Working with Video

Process video from a file or webcam:
import cv2 as cv

# Open webcam (0 = default camera)
cap = cv.VideoCapture(0)

# Or open a video file
# cap = cv.VideoCapture('video.mp4')

while True:
    # Read frame
    ret, frame = cap.read()
    
    if not ret:
        break
    
    # Convert to grayscale
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    
    # Display
    cv.imshow('Webcam', gray)
    
    # Break on 'q' key
    if cv.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv.destroyAllWindows()

Common Operations

Here are some frequently used OpenCV operations:

Reading and Writing

# Read image
img = cv.imread('input.jpg')

# Save image
cv.imwrite('output.jpg', img)

# Read video
cap = cv.VideoCapture('video.mp4')

# Write video
fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

Image Transformations

# Resize
resized = cv.resize(img, (640, 480))

# Rotate
(h, w) = img.shape[:2]
center = (w // 2, h // 2)
M = cv.getRotationMatrix2D(center, 45, 1.0)
rotated = cv.warpAffine(img, M, (w, h))

# Flip
flipped = cv.flip(img, 1)  # 1 = horizontal, 0 = vertical, -1 = both

Drawing

# Draw line
cv.line(img, (0, 0), (100, 100), (255, 0, 0), 2)

# Draw circle
cv.circle(img, (50, 50), 25, (0, 255, 0), -1)

# Draw rectangle
cv.rectangle(img, (10, 10), (100, 100), (0, 0, 255), 2)

# Put text
cv.putText(img, 'Hello OpenCV', (10, 30), 
           cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)

Next Steps

Now that you’ve created your first OpenCV program, explore more features:

Image Processing

Learn about filtering, transformations, and color spaces

Object Detection

Detect objects, faces, and features in images

Video Analysis

Process video streams and track objects

API Reference

Explore the complete OpenCV API

Troubleshooting

Make sure OpenCV is installed:
pip install opencv-python
Verify installation:
import cv2 as cv
print(cv.__version__)
This means the image couldn’t be loaded. Check:
  • The file path is correct
  • The image file exists
  • You have read permissions
  • The image format is supported
For headless environments, use a different backend:
export QT_QPA_PLATFORM=offscreen
Or save images instead of displaying them.
Try different camera indices:
cap = cv.VideoCapture(0)  # Try 0, 1, 2, etc.
On Linux, ensure you have camera permissions.

Resources