Skip to content
Home
OpenCV Beginners Guide: Installation, Core Operations, and...

OpenCV Beginners Guide: Installation, Core Operations, and...

Computer Vision Computer Vision 8 min read 1531 words Beginner ExcellentWiki Editorial Team

What Is OpenCV?

OpenCV (Open Source Computer Vision Library) is the most widely used computer vision library in the world. Originally developed by Intel in 1999, it now contains over 2,500 optimized algorithms covering image processing, video analysis, object detection, machine learning, camera calibration, and 3D reconstruction. The library supports C++, Python, Java, and JavaScript, and runs on Windows, Linux, macOS, Android, and iOS.

OpenCV’s Python bindings provide access to the entire library through an intuitive NumPy-compatible interface. Images are represented as NumPy arrays, enabling seamless integration with the Python scientific computing ecosystem. The library is released under the Apache 2.0 license, permitting use in commercial applications. According to the OpenCV documentation, the library has been downloaded over 18 million times and powers applications in autonomous vehicles, medical imaging, security systems, and consumer photography.

Installation

Installing OpenCV for Python is straightforward with pip. The main package provides core functionality, while the contrib package includes additional modules (SIFT, SURF, face recognition, text detection):

pip install opencv-python opencv-contrib-python

The opencv-python package (approximately 30 MB) includes the core modules: core (data structures), imgproc (image processing), imgcodecs (image I/O), videoio (video I/O), highgui (GUI), and calib3d (camera calibration). The contrib package adds xfeatures2d (SIFT, SURF), face (face recognition), text (OCR), and tracking modules.

Verify the installation by checking the version:

import cv2
print(cv2.__version__)
# Output example: '4.10.0'

Reading, Displaying, and Writing Images

OpenCV provides simple functions for image I/O. The imread function loads an image from file, imshow displays it in a window, and imwrite saves it to disk:

import cv2

# Read an image
img = cv2.imread('photo.jpg')

# Display in a window
cv2.imshow('Window', img)
cv2.waitKey(0)  # Wait for any key press
cv2.destroyAllWindows()

# Save to file
cv2.imwrite('output.jpg', img)

The imread function returns a NumPy array. For a color image, the array has shape (height, width, 3) with the channels in BGR (Blue, Green, Red) order. This is a critical detail: OpenCV uses BGR color ordering, while most other libraries (Matplotlib, PIL, TensorFlow) use RGB. Always convert before displaying with other libraries:

rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

Basic Pixel Operations

Since images are NumPy arrays, pixel access and manipulation use standard array operations. Individual pixel values are accessed via row and column indices:

# Access pixel at row 100, column 100
pixel = img[100, 100]  # Returns [B, G, R] array

# Modify pixel
img[100, 100] = [0, 0, 255]  # Set to red (in BGR)

# Access region of interest
roi = img[50:150, 100:200]  # 100x100 pixel region

# Modify region
img[50:150, 100:200] = [255, 255, 255]  # Fill region with white

NumPy array operations work directly: img[:, :, 0] accesses the blue channel, img[:, :, 1] the green channel, img[:, :, 2] the red channel. Using NumPy slicing for vectorized operations is dramatically faster than Python for-loops.

Drawing on Images

OpenCV provides drawing functions that modify images in-place. Common drawing operations include lines, rectangles, circles, and text:

# Line (image, start, end, color, thickness)
cv2.line(img, (0, 0), (511, 511), (0, 255, 0), 3)

# Rectangle (image, top-left, bottom-right, color, thickness)
cv2.rectangle(img, (100, 100), (200, 200), (255, 0, 0), 2)

# Filled circle (thickness=-1)
cv2.circle(img, (255, 255), 50, (0, 0, 255), -1)

# Text (image, text, position, font, scale, color, thickness)
cv2.putText(img, 'Hello', (10, 50),
            cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)

These functions are essential for visualizing detection results, drawing bounding boxes, and creating debugging overlays.

Camera Capture

OpenCV provides a unified interface for camera and video file access through VideoCapture:

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

while True:
    ret, frame = cap.read()
    if not ret:
        break

    # Convert to grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the frame
    cv2.imshow('Camera', gray)

    # Exit on 'q' key press
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

The camera capture loop is the foundation for all real-time computer vision applications: face detection, object tracking, motion analysis, and augmented reality.

Edge Detection with Canny

The Canny edge detector is one of OpenCV’s most useful algorithms. It combines noise reduction (Gaussian smoothing), gradient computation, non-maximum suppression, and hysteresis thresholding in a single function:

edges = cv2.Canny(img, threshold1=100, threshold2=200)

The two threshold parameters control edge detection sensitivity. threshold1 (the lower threshold) identifies weak edges, while threshold2 (the upper threshold) identifies strong edges. Weak edges are only retained if connected to strong edges. The 1:2 or 1:3 ratio between thresholds is recommended. Canny edge detection is used in lane detection, document scanning, and object tracking pipelines.

Image Transformations

Geometric transformations modify the spatial arrangement of pixels. Scaling and resizing change image dimensions:

resized = cv2.resize(img, (300, 300))
# or scale by factor
resized = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_LINEAR)

Rotation around a specified center point uses a rotation matrix:

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

Perspective transformation, essential for document scanning and image rectification, maps four source points to four destination points:

src_pts = np.float32([[50,50], [200,50], [50,200], [200,200]])
dst_pts = np.float32([[0,0], [300,0], [0,300], [300,300]])
M = cv2.getPerspectiveTransform(src_pts, dst_pts)
warped = cv2.warpPerspective(img, M, (300, 300))

Thresholding

Thresholding converts grayscale images to binary images by classifying each pixel as foreground or background based on intensity:

# Simple binary threshold
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

# Adaptive threshold (handles varying illumination)
adaptive = cv2.adaptiveThreshold(
    gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
    cv2.THRESH_BINARY, 11, 2)

# Otsu's method (automatically determines optimal threshold)
otsu_val, otsu = cv2.threshold(
    gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

Otsu’s thresholding automatically computes the optimal threshold value by maximizing the variance between foreground and background pixel classes, making it the preferred method for documents and images with bimodal histograms.

Contour Detection and Analysis

Contour detection identifies the boundaries of objects in binary images. OpenCV’s findContours function extracts connected components as lists of points. Contour analysis enables object measurement, shape classification, and region-of-interest extraction:

contours, hierarchy = cv2.findContours(
    binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    area = cv2.contourArea(cnt)
    perimeter = cv2.arcLength(cnt, True)
    x, y, w, h = cv2.boundingRect(cnt)
    center, radius = cv2.minEnclosingCircle(cnt)

Contour properties enable powerful filtering: area filters small noise blobs, aspect ratio distinguishes squares from rectangles, circularity (4piarea/perimeter^2) identifies round objects. The convex hull and convexity defects reveal shape characteristics useful for hand gesture recognition and defect detection.

Blob Detection and Connected Components

Blob detection identifies regions with distinctive properties (brightness, color, texture) compared to their surroundings. OpenCV’s SimpleBlobDetector applies thresholding at multiple levels, extracting connected regions and filtering by area, circularity, convexity, and inertia. This classical approach remains effective for detecting cells in microscopy images, solder joints in PCB inspection, and stars in astronomical imaging.

Connected component analysis labels each distinct foreground region with a unique integer ID. OpenCV’s connectedComponentsWithStats returns labeled regions with bounding boxes, area, and centroid coordinates. Connected component labeling is the foundation for object counting (cell colonies, particles on a surface) and region-based analysis.

Histogram Computation and Analysis

Image histograms provide the statistical distribution of pixel intensities. OpenCV’s calcHist function computes histograms for one or more channels with configurable bin sizes and ranges. Histogram analysis is used for: exposure assessment (checking if images are overexposed or underexposed), contrast measurement (spread of histogram width), automatic brightness adjustment, and image similarity comparison using histogram correlation or chi-square distance.

Back-projection identifies image regions that match a target histogram. Given a histogram of a target object’s color distribution, back-projection highlights pixels with similar colors in a new image. This technique enables object tracking by color (mean-shift and camshift tracking in OpenCV) and skin detection for gesture recognition. Histogram comparison metrics — correlation, chi-square, intersection, and Bhattacharyya distance — provide quantitative similarity measures between distributions.

Frequently Asked Questions

Can OpenCV be used for deep learning? Yes. OpenCV’s dnn module can load models from PyTorch, TensorFlow, Caffe, and Darknet. It supports inference with ONNX models and provides GPU acceleration through CUDA. However, for training and research, dedicated frameworks (PyTorch, TensorFlow) are better suited.

What is the difference between opencv-python and opencv-contrib-python? The contrib package includes experimental and non-free modules: SIFT and SURF feature detectors (patented), face recognition, text detection, tracking, and RGB-D processing. The main package covers all other functionality. Install contrib if you need SIFT or face recognition.

Why does OpenCV use BGR instead of RGB? Historical reasons: early camera hardware and Windows BMP format used BGR ordering. OpenCV adopted this convention and has maintained it for backward compatibility. Always convert to RGB when displaying with matplotlib or processing with other libraries.

How do I read a video file instead of a camera? Pass the video file path instead of the camera index: cv2.VideoCapture(‘input.mp4’). The VideoWriter class writes processed frames back to a video file with configurable codec (mp4v, avc1, XVID) and frame rate.

Does OpenCV support GPU acceleration? Yes. OpenCV can be built with CUDA support for GPU-accelerated functions. The cv2.cuda module provides GPU versions of many algorithms. The dnn module can use CUDA backend for neural network inference. However, the standard pip packages do not include CUDA support — you must build from source with CUDA enabled.

Related Articles

Section: Computer Vision 1531 words 8 min read Beginner 756 articles in section Report inaccuracy Back to top