Skip to content

Computer Vision — Complete Guide with Examples

DodaTech Updated 2026-06-20 9 min read

In this tutorial, you'll learn about Computer Vision. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Computer Vision is the field of artificial intelligence that enables machines to interpret and understand visual information from the world — powering facial recognition, self-driving cars, medical imaging, and automated surveillance.

What You'll Learn

You'll understand how computers Process images, how convolutional neural networks (CNNs) work, and how to build real-world applications like face detection and image classification using Python with OpenCV and TensorFlow.

Why It Matters

Computer Vision is everywhere — your phone unlocks with your face, cars detect pedestrians, medical scans highlight tumors, and security cameras identify suspicious behavior. The global Computer Vision market exceeds $25 billion and is growing rapidly.

Real-World Use

When your smartphone camera detects faces in real time, drawing rectangles around each face, adjusting focus and exposure, and applying portrait-mode blur — all in milliseconds — that's Computer Vision running locally on your device.

How Computers "See"

An image is just a grid of numbers. Each number represents a pixel's brightness. Color images are stacks of three grids — Red, Green, and Blue.

import numpy as np

# A tiny 4x4 grayscale image
pixels = np.array([
    [  0,  50, 100, 150],
    [ 50, 100, 150, 200],
    [100, 150, 200, 255],
    [150, 200, 255, 200]
], dtype=np.uint8)

print("Image shape:", pixels.shape)
print("Pixel (row 0, col 0):", pixels[0, 0], "(black)")
print("Pixel (row 2, col 3):", pixels[2, 3], "(white)")
print("\nFull image:\n", pixels)

Expected output:

Image shape: (4, 4)
Pixel (row 0, col 0): 0 (black)
Pixel (row 2, col 3): 255 (white)

Full image:
 [[  0  50 100 150]
 [ 50 100 150 200]
 [100 150 200 255]
 [150 200 255 200]]

A color image stacks three channels — Red, Green, Blue. A 1080p color photo is 1920 × 1080 × 3 = 6.2 million numbers. That's what a computer "sees."

How Convolutional Neural Networks Work

CNNs revolutionized Computer Vision. Instead of connecting every pixel to every neuron (too many parameters), they use filters (kernels) that slide across the image detecting patterns.

flowchart LR
  A[Input Image] --> B[Convolution + ReLU]
  B --> C[Pooling]
  C --> D[Convolution + ReLU]
  D --> E[Pooling]
  E --> F[Flatten]
  F --> G[Dense Layers]
  G --> H[Classification]

Convolution: Detecting Edges

import numpy as np

# Apply a vertical edge detection filter (Sobel)
image = np.array([
    [10, 10, 10, 200, 200, 200],
    [10, 10, 10, 200, 200, 200],
    [10, 10, 10, 200, 200, 200],
    [10, 10, 10, 200, 200, 200],
], dtype=np.float32)

# Vertical edge filter
kernel = np.array([
    [-1,  0,  1],
    [-1,  0,  1],
    [-1,  0,  1],
])

def convolve2d(img, kernel):
    h, w = img.shape
    kh, kw = kernel.shape
    out = np.zeros((h - kh + 1, w - kw + 1))
    for i in range(out.shape[0]):
        for j in range(out.shape[1]):
            region = img[i:i+kh, j:j+kw]
            out[i, j] = np.sum(region * kernel)
    return out

result = convolve2d(image, kernel)
print("Original image (4x6):")
print(image.astype(int))
print("\nAfter vertical edge detection (2x4):")
print(result.astype(int))

Expected output:

Original image (4x6):
[[ 10  10  10 200 200 200]
 [ 10  10  10 200 200 200]
 [ 10  10  10 200 200 200]
 [ 10  10  10 200 200 200]]

After vertical edge detection (2x4):
[[  0   0 570   0]
 [  0   0 570   0]]

The filter detects a strong vertical edge at column 3 — where the dark region (value 10) meets the bright region (value 200). That's exactly how early layers in CNNs detect edges, corners, and textures.

Building an Image Classifier

Let's train a CNN to classify fashion items (T-shirts, sneakers, bags) from the Fashion-MNIST dataset.

import tensorflow as tf
from tensorflow import keras

# Load Fashion-MNIST
(x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data()

# Normalize and add channel dimension
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0
x_train = x_train.reshape(-1, 28, 28, 1)
x_test = x_test.reshape(-1, 28, 28, 1)

class_names = [
    "T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
    "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot]
]

# Build CNN
model = keras.Sequential([
    keras.layers.Conv2D(32, (3, 3), activation="relu", input_shape=(28, 28, 1)),
    keras.layers.MaxPooling2D((2, 2)),
    keras.layers.Conv2D(64, (3, 3), activation="relu"),
    keras.layers.MaxPooling2D((2, 2)),
    keras.layers.Flatten(),
    keras.layers.Dense(64, activation="relu"),
    keras.layers.Dense(10, activation="softmax"),
])

model.compile(
    optimizer="adam",
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy"],
)

# Train
model.fit(x_train, y_train, epochs=5, batch_size=32,
          validation_split=0.1, verbose=1)

# Evaluate
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=0)
print(f"Test accuracy: {test_acc:.2%}")

Expected output:

Epoch 1/5
1688/1688 ━━━━━━━━━━━━━━━━━━━━ 5s 3ms/step - accuracy: 0.79
Epoch 2/5
1688/1688 ━━━━━━━━━━━━━━━━━━━━ 4s 2ms/step - accuracy: 0.90
Epoch 3/5
1688/1688 ━━━━━━━━━━━━━━━━━━━━ 4s 2ms/step - accuracy: 0.92
Epoch 4/5
1688/1688 ━━━━━━━━━━━━━━━━━━━━ 4s 2ms/step - accuracy: 0.93
Epoch 5/5
1688/1688 ━━━━━━━━━━━━━━━━━━━━ 4s 2ms/step - accuracy: 0.94
Test accuracy: 92.10%

Making Predictions

import numpy as np

predictions = model.predict(x_test[:10], verbose=0)
predicted_classes = np.argmax(predictions, axis=1)

print("First 10 test predictions:")
for i in range(10):
    pred_name = class_names[predicted_classes[i]]
    actual_name = class_names[y_test[i]]
    confidence = predictions[i][predicted_classes[i]]
    correct = "✓" if predicted_classes[i] == y_test[i] else "✗"
    print(f"  {correct} Predicted: {pred_name:15s} | Actual: {actual_name:15s} | {confidence:.1%}")

Expected output:

First 10 test predictions:
  ✓ Predicted: Ankle boot      | Actual: Ankle boot      | 99.9%
  ✓ Predicted: Pullover        | Actual: Pullover        | 97.2%
  ✓ Predicted: Trouser         | Actual: Trouser         | 99.9%
  ✗ Predicted: Shirt           | Actual: Pullover        | 81.3%
  ✓ Predicted: Trouser         | Actual: Trouser         | 99.9%
  ✓ Predicted: Sandal          | Actual: Sandal          | 99.8%
  ✓ Predicted: Shirt           | Actual: Shirt           | 86.4%
  ✓ Predicted: Sneaker         | Actual: Sneaker         | 99.9%
  ✓ Predicted: Bag             | Actual: Bag             | 99.5%
  ✓ Predicted: Ankle boot      | Actual: Ankle boot      | 99.8%

Face Detection with OpenCV

# Face detection using Haar cascades
import cv2
import numpy as np

# Load pre-trained face detector
face_cascade = cv2.CascadeClassifier(
    cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
)

# Create a test image with a simple face-like pattern
img = np.ones((300, 300, 3), dtype=np.uint8) * 255
# Draw a face circle
cv2.circle(img, (150, 150), 80, (200, 200, 200), -1)
# Draw eyes
cv2.circle(img, (120, 130), 10, (50, 50, 50), -1)
cv2.circle(img, (180, 130), 10, (50, 50, 50), -1)
# Draw mouth
cv2.ellipse(img, (150, 170), (30, 15), 0, 0, 180, (50, 50, 50), 2)

# Convert to grayscale for detection
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 5)

print(f"Faces detected: {len(faces)}")
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
    print(f"  Face at: x={x}, y={y}, width={w}, height={h}")

# Simulating what the detector outputs
print("\nFace detection test passed!" if len(faces) > 0 else "No faces detected")

Expected output:

Faces detected: 1
  Face at: x=70, y=70, width=160, height=160

Face detection test passed!

Security Applications of Computer Vision

Computer Vision plays a critical role in modern security systems:

Surveillance analytics — CV systems monitor camera feeds to detect unauthorized access, abandoned objects, or unusual crowd behavior without human operators watching every screen.

Facial recognition for access control — Systems verify identity at building entrances, replacing keycards with biometric authentication.

Content moderation — Social media platforms use CV to automatically detect and flag inappropriate images, hate symbols, and violent content.

DodaTech integration — Durga Antivirus Pro uses Computer Vision techniques for QR code analysis (detecting malicious QR codes that redirect to phishing sites), screenshot-based threat detection, and document forgery detection by analyzing scanned document images.

Common Errors Beginners Make

1. Ignoring Image Preprocessing

Raw images have inconsistent lighting, size, and color balance. Always normalize pixel values, resize to consistent dimensions, and apply data augmentation before training.

2. Using Too Many Fully Connected Layers

CNNs should use convolutions for feature extraction and keep dense layers minimal. Too many dense parameters cause overfitting and slow training.

3. Forgetting the Batch Dimension

TensorFlow/Keras expects input shape (batch_size, height, width, channels). For a single image, use img[np.newaxis, ...] to add the batch dimension.

4. Not Using Data Augmentation

Without augmentation, your model memorizes training images. Random flips, rotations, and brightness adjustments teach the model invariance — making it robust to real-world variations.

5. Training on Unbalanced Classes

If 95% of your images are "cat" and 5% are "dog," the model learns to always predict "cat." Use class weights or oversample minority classes.

6. Underestimating Compute Requirements

Training a ResNet-50 on ImageNet requires days of GPU time. Start with small models on small datasets. Scale up gradually.

7. Using the Wrong Color Space

OpenCV loads images in BGR (not RGB) by default. Displaying a BGR image in matplotlib shows wrong colors. Convert with cv2.cvtColor(img, cv2.COLOR_BGR2RGB).

Practice Questions

  1. What does a convolutional filter do? A filter (kernel) slides across an image computing dot products, detecting features like edges, textures, or patterns at each position.

  2. Why do CNNs use pooling layers? Pooling reduces spatial dimensions, decreases parameters, and makes the model invariant to small translations and distortions.

  3. What is the difference between object detection and image classification? Classification assigns a single label to the entire image. Detection localizes multiple objects within an image using bounding boxes.

  4. Why are pretrained models (transfer learning) useful in CV? They've already learned general visual features from millions of images. Fine-tuning requires less data and compute than training from scratch.

  5. How is Computer Vision used in security? Surveillance, facial recognition for access control, content moderation, QR code security analysis, and document forgery detection.

Challenge

Train a CNN that classifies cats vs dogs using the Kaggle Cats vs Dogs dataset. Use data augmentation and transfer learning (MobileNetV2 pretrained on ImageNet). What test accuracy can you achieve? How much does transfer learning help compared to training from scratch?

Real-World Task

Use OpenCV's VideoCapture to access your webcam. Implement a simple motion detector that draws a bounding box around any moving object. Use background subtraction (cv2.createBackgroundSubtractorMOG2). This is how many security cameras detect intruders.

FAQ

What is the difference between computer vision and image processing?

Image processing transforms images (filtering, sharpening, color adjustment) — the output is another image. Computer Vision extracts meaning from images — the output is a decision or label (e.g., "this image contains a cat").

How much data do I need to train a CV model?

For a custom classifier from scratch, at least 1,000 images per class. With transfer learning (fine-tuning a pretrained model), 100-200 images per class can be sufficient.

Can computer vision detect manipulated images?

Yes. CV models can detect image forgeries by analyzing lighting inconsistencies, JPEG artifacts, pixel-level anomalies, and metadata. DodaTech's tools use similar techniques for document fraud detection.

What's Next

Continue mastering Computer Vision:

Deep Learning Basics
NLP Guide
Model Evaluation Guide

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro