⬡ Hub
Skip to content

OpenCV: Open Source Computer Vision Library

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in commercial products. It is a highly optimized library with a strong focus on real-time applications.

Key Features:

  • Image and Video I/O: Reading and writing images and videos.
  • Image Processing: Extensive functions for image manipulation (e.g., resizing, cropping, color conversion, filtering).
  • Feature Detection: Algorithms for detecting keypoints, edges, and corners.
  • Object Detection: Pre-trained models and algorithms (e.g., Haar cascades, YOLO, SSD) for detecting objects.
  • Machine Learning Integration: Includes modules for various ML algorithms like SVMs, K-Nearest Neighbors, etc.
  • Camera Calibration and 3D Reconstruction: Tools for understanding camera parameters and reconstructing 3D scenes.
  • Cross-platform: Available on Windows, Linux, macOS, Android, and iOS.

Getting Started: Installation

You can install OpenCV for Python using pip. There are different packages for different needs: * opencv-python: Main module. * opencv-contrib-python: Includes additional (non-free/patented) algorithms.

Using pip:

pip install opencv-python

If you need the contrib modules:

pip install opencv-contrib-python

Basic Concepts: Images as NumPy Arrays

OpenCV represents images as NumPy arrays. This allows for seamless integration with NumPy and other scientific libraries, and efficient array operations.

Reading, Displaying, and Saving an Image

import cv2
import numpy as np
import matplotlib.pyplot as plt

# Create a dummy image (e.g., a black image with a white square)
# In a real scenario, you would read an image from a file: img = cv2.imread('path/to/image.jpg')
dummy_img = np.zeros((300, 400, 3), dtype=np.uint8) # Black image, 300x400, 3 channels (RGB)
dummy_img[100:200, 150:250] = [255, 255, 255] # Add a white square

# Save the dummy image (optional)
cv2.imwrite('dummy_image.png', dummy_img)
print("Dummy image saved as 'dummy_image.png'")

# Read the image
# For OpenCV, images are typically loaded in BGR format
img = cv2.imread('dummy_image.png')

if img is None:
    print("Error: Could not load image.")
else:
    # Display the image using Matplotlib (OpenCV uses BGR, Matplotlib uses RGB)
    # Convert BGR to RGB for correct display with Matplotlib
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    plt.imshow(img_rgb)
    plt.title('Dummy Image with White Square')
    plt.axis('off') # Hide axes
    plt.show()

    # Get image properties
    print(f"Image shape: {img.shape}") # (height, width, channels)
    print(f"Image data type: {img.dtype}")
    print(f"Image size (pixels): {img.size}")

    # You can also use cv2.imshow to display, but it requires specific window management
    # cv2.imshow('Image Window', img)
    # cv2.waitKey(0) # Waits indefinitely until a key is pressed
    # cv2.destroyAllWindows() # Closes all OpenCV windows

Basic Image Manipulation

Grayscale Conversion

import cv2
import matplotlib.pyplot as plt
import numpy as np

# Assuming 'dummy_image.png' exists from the previous example
img = cv2.imread('dummy_image.png')

if img is not None:
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    plt.imshow(gray_img, cmap='gray')
    plt.title('Grayscale Image')
    plt.axis('off')
    plt.show()

    print(f"Grayscale image shape: {gray_img.shape}") # (height, width) - single channel
else:
    print("Error: Image not found for grayscale conversion.")

Resizing

import cv2
import matplotlib.pyplot as plt
import numpy as np

img = cv2.imread('dummy_image.png')

if img is not None:
    resized_img = cv2.resize(img, (200, 150)) # new_width, new_height

    # Convert BGR to RGB for display
    resized_img_rgb = cv2.cvtColor(resized_img, cv2.COLOR_BGR2RGB)

    plt.imshow(resized_img_rgb)
    plt.title('Resized Image (200x150)')
    plt.axis('off')
    plt.show()

    print(f"Resized image shape: {resized_img.shape}")
else:
    print("Error: Image not found for resizing.")

Further Topics:

  • Drawing Functions (lines, rectangles, circles, text)
  • Image Filtering (blurring, edge detection with Canny, Sobel)
  • Morphological Operations
  • Color Spaces (HSV, LAB)
  • Thresholding
  • Contour Detection
  • Feature Detection and Description (SIFT, SURF, ORB)
  • Object Detection (Haar Cascades, Deep Learning-based methods)
  • Video Processing
  • Camera Calibration

This document provides a basic introduction to OpenCV. More detailed topics, advanced computer vision algorithms, and practical applications will be covered in subsequent files.