AI: Introduction to Computer Vision with OpenCV

Computer vision is the machine learning field that teaches computers to see and understand digital images. OpenCV is the leading open-source library for computer vision, providing tools for image reading, resizing, drawing, transformations, edge detection, contour detection, and video processing in Python and C++.

Human vision is so effortless that we rarely appreciate how computationally complex it actually is. Recognizing a face, reading text on a sign, or distinguishing a dog from a cat in an image are tasks that take a human milliseconds — and that computers have struggled with for decades.

Computer vision is the AI field dedicated to closing that gap.


What Is Computer Vision?

Computer vision is a machine learning field whose goal is to help computers see. Its end goal is to make intelligent systems that can understand digital images.

md
Computer Vision Goal

Digital Image (array of pixels)
         |
         v
+---------------------------+
|   Computer Vision System  |
|                           |
|  Object Detection         |
|  Face Recognition         |
|  Scene Understanding      |
|  Handwriting Recognition  |
+---------------------------+
         |
         v
Structured Understanding
("This image contains a dog in the park")

Computer vision is not just about recognizing what is in an image — it encompasses:

  • Image classification — what is this image?
  • Object detection — where are specific objects in this image?
  • Segmentation — which pixels belong to which object?
  • Facial recognition — whose face is this?
  • OCR (Optical Character Recognition) — what text appears in this image?

What Is OpenCV?

OpenCV (Open Source Computer Vision Library) is the most widely used open-source library for computer vision, machine learning, and image processing.

OpenCV can be used to process images and videos to:

  • Identify objects and faces
  • Recognize handwriting
  • Detect edges and contours
  • Apply transformations and filters
  • Process video streams in real time

md
OpenCV Capabilities Overview

+--------------------------------------------------+
|                    OpenCV                        |
|                                                  |
|  Image I/O    Drawing    Transformations         |
|  Blurring     Filters    Color Spaces            |
|  Contours     Edges      Cascade Classifiers     |
|  Video        Bitwise    Feature Detection       |
+--------------------------------------------------+

OpenCV supports:

  • C++ (native implementation)
  • Python (most widely used for learning and prototyping)
  • Java

Installing OpenCV

bash
pip install opencv-python

For extended functionality (including non-free algorithms):

bash
pip install opencv-contrib-python


Reading and Displaying Images

The foundation of any computer vision workflow is loading an image and displaying it.

python
import cv2 as cv

# Read an image from disk
# cv.IMREAD_COLOR (default): load as color (BGR)
# cv.IMREAD_GRAYSCALE: load as grayscale
image = cv.imread('path/to/image.jpg')

# Display the image in a window
cv.imshow('My Image', image)

# Wait for a key press before closing
cv.waitKey(0)
cv.destroyAllWindows()

OpenCV reads images in **BGR** format (Blue, Green, Red) — the reverse of the more common RGB format. This matters when converting between color spaces.

Resizing Images

Images loaded from disk may not be the right size for processing or display. Resizing is a fundamental preprocessing step.

python
# Resize to exact dimensions
resized = cv.resize(image, (width, height))

# Resize with interpolation control
# cv.INTER_AREA: good for shrinking
# cv.INTER_LINEAR: good for enlarging
resized = cv.resize(image, (640, 480), interpolation=cv.INTER_AREA)

# Resize by scale factor (e.g., half size)
scale = 0.5
resized = cv.resize(image, None, fx=scale, fy=scale, interpolation=cv.INTER_AREA)


Drawing on Images

OpenCV allows drawing geometric shapes and text directly onto images — useful for annotating detections and visualizing results.

python
# Draw a rectangle
cv.rectangle(image, (x1, y1), (x2, y2), color=(0, 255, 0), thickness=2)

# Draw a circle
cv.circle(image, (center_x, center_y), radius=50, color=(255, 0, 0), thickness=3)

# Draw a line
cv.line(image, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=2)

# Add text
cv.putText(image, 'Object Detected', (x, y),
           fontFace=cv.FONT_HERSHEY_SIMPLEX,
           fontScale=1,
           color=(255, 255, 255),
           thickness=2)


Basic Image Functions

Blurring

Blurring removes noise from an image by applying a low-pass filter. It smooths edges and transitions between colors — useful as a preprocessing step to improve detection accuracy.

python
# Gaussian blur (most common)
blurred = cv.GaussianBlur(image, (5, 5), 0)

# Median blur (good for salt-and-pepper noise)
blurred = cv.medianBlur(image, 5)

# Simple average blur
blurred = cv.blur(image, (5, 5))

md
Original Image:     Blurred Image:
  *  *  *            ~  ~  ~
  * sharp *     ->   ~ smooth ~
  *  *  *            ~  ~  ~

High-frequency noise removed.


Image Transformation

Images can be transformed in various ways to support analysis:

python
# Translation (shift along x or y axis)
import numpy as np
M = np.float32([[1, 0, tx], [0, 1, ty]])  # tx, ty = shift amount
translated = cv.warpAffine(image, M, (width, height))

# Rotation
center = (width // 2, height // 2)
M = cv.getRotationMatrix2D(center, angle=45, scale=1.0)
rotated = cv.warpAffine(image, M, (width, height))

# Flipping
flipped_h = cv.flip(image, 1)   # horizontal flip
flipped_v = cv.flip(image, 0)   # vertical flip

# Cropping (slicing the array)
cropped = image[y1:y2, x1:x2]


Contour Detection

A contour is a curve joining all points with the same color or intensity. Contours help find the boundaries of objects within an image.

python
# Convert to grayscale first
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)

# Apply threshold to create binary image
_, thresh = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)

# Find contours
contours, hierarchy = cv.findContours(thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)

# Draw contours on the original image
cv.drawContours(image, contours, -1, (0, 255, 0), 2)


Color Channels

Every color image is a combination of three color channels: Red, Green, Blue (RGB) — or in OpenCV's case, BGR.

python
# Split image into individual color channels
blue, green, red = cv.split(image)

# Merge channels back
merged = cv.merge([blue, green, red])

# Convert between color spaces
gray  = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
hsv   = cv.cvtColor(image, cv.COLOR_BGR2HSV)
rgb   = cv.cvtColor(image, cv.COLOR_BGR2RGB)

md
Image Pixel = [B=120, G=80, R=200]
              Blue    Green  Red

Splitting the channels isolates each color component,
enabling operations like color-based object detection.


Bitwise Operations

Bitwise operations work on images pixel by pixel, applying logical operators (AND, OR, XOR, NOT). They are used for masking — isolating specific regions of an image.

python
# Create a mask (white region = area to keep)
mask = np.zeros(image.shape[:2], dtype='uint8')
cv.circle(mask, (center_x, center_y), radius, 255, -1)

# Apply mask using bitwise AND
masked_image = cv.bitwise_and(image, image, mask=mask)


Edge Detection

Edge detection finds the boundaries of objects in an image by locating inconsistencies in pixel brightness. It is foundational to object detection and image segmentation.

python
# Canny edge detection (most popular algorithm)
gray  = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
edges = cv.Canny(gray, threshold1=100, threshold2=200)

cv.imshow('Edges', edges)
cv.waitKey(0)

md
Original Image:          Edge Detection Output:
  ___________             ___________
 |           |           |           |
 |   Object  |    -->    |___________|
 |___________|

Only the boundaries remain -- the "skeleton" of the image.

Canny edge detection steps:

  1. Apply Gaussian blur to reduce noise
  2. Calculate intensity gradient
  3. Apply non-maximum suppression
  4. Apply double threshold to identify strong and weak edges
  5. Track edges through hysteresis

Reading Videos

Computer vision extends naturally to video — a video is simply a sequence of image frames processed at a frame rate.

python
import cv2 as cv

# Open a video file (or use 0 for webcam)
capture = cv.VideoCapture('video.mp4')

while True:
    # Read the next frame
    isTrue, frame = capture.read()

    # Stop if no more frames
    if not isTrue:
        break

    # Display the frame
    cv.imshow('Video', frame)

    # Break loop if 'd' key is pressed
    if cv.waitKey(20) & 0xFF == ord('d'):
        break

# Release resources
capture.release()
cv.destroyAllWindows()

md
Video Processing Pipeline

Video File
     |
     v
+----------------+
| Read Frame     |  (capture.read())
+----------------+
     |
     v
+----------------+
| Process Frame  |  (detect, transform, analyze)
+----------------+
     |
     v
+----------------+
| Display Frame  |  (imshow)
+----------------+
     |
     v
Next Frame (repeat)


Common Computer Vision Pipeline

A typical computer vision application follows this structure:

md
Input (image or video frame)
        |
        v
Preprocessing
(resize, grayscale, blur)
        |
        v
Feature Extraction
(edges, contours, color)
        |
        v
Analysis / Detection
(object detection, classification)
        |
        v
Output
(annotated image, bounding boxes, labels)


Final Thoughts

OpenCV is the starting point for anyone entering computer vision. It provides the fundamental building blocks — reading images, transforming them, detecting edges and contours, processing video — that underpin virtually every computer vision application.

From here, the path leads to:

  • Deep learning-based vision (CNNs, YOLO, ResNet) for more complex detection tasks
  • Video analytics (optical flow, object tracking)
  • 3D vision (depth estimation, point clouds)

The computer does not see the way you do — but with the right tools and techniques, it can learn to understand what it looks at.