{% extends "base.html" %} {% block title %}Computer Vision - Data Science Lab{% endblock %} {% block content %}

๐Ÿ“– What is Computer Vision?

Computer vision enables machines to interpret and understand visual information. Applications include traffic monitoring, crowd detection, and urban planning analysis.

๐Ÿ–ผ๏ธ Generated Sample Images

{% if images %}
{% for img in images %}
{{ img.type }}

{{ img.type | title }}

{% endfor %}
{% else %}

Sample images will be generated when this page loads. Check that Pillow is installed.

{% endif %}

๐Ÿ“– Image Preprocessing Pipeline

{% if preprocessing %} {% for step in preprocessing.steps %} {% endfor %}
StepInput ShapeOutput ShapeDescription
{{ step.step }} {{ step.input_shape }} {{ step.output_shape }} {{ step.description }}
{% endif %}
Python
from PIL import Image, ImageFilter
import numpy as np

def preprocess_pipeline(image_path):
    """Standard CV preprocessing pipeline."""
    img = Image.open(image_path)

    # 1. Resize to consistent dimensions
    img = img.resize((128, 128))

    # 2. Convert to grayscale (if needed)
    gray = img.convert('L')

    # 3. Convert to numpy array and normalize
    arr = np.array(gray, dtype=np.float32) / 255.0
    # Now pixel values are 0.0-1.0 instead of 0-255

    # 4. Edge detection (Sobel-like filter)
    edges = gray.filter(ImageFilter.FIND_EDGES)

    # 5. Gaussian blur (noise reduction)
    blurred = img.filter(ImageFilter.GaussianBlur(radius=2))

    return {
        'resized': img,
        'grayscale': gray,
        'normalized': arr,
        'edges': edges,
        'blurred': blurred
    }

๐Ÿ“– Color Histogram Analysis

Python
def extract_color_features(image_path):
    """Extract color histogram features for classification."""
    img = Image.open(image_path).resize((128, 128))
    arr = np.array(img)

    features = {}
    for i, channel in enumerate(['red', 'green', 'blue']):
        channel_data = arr[:, :, i].flatten()
        features[f'{channel}_mean'] = np.mean(channel_data)
        features[f'{channel}_std'] = np.std(channel_data)
        features[f'{channel}_median'] = np.median(channel_data)

    # Derived features
    features['brightness'] = np.mean(arr)
    features['green_ratio'] = features['green_mean'] / (features['brightness'] + 1)

    return features

# Classify based on color features
# Parks: high green ratio
# Buildings: high gray/blue mean
# transit stations: distinctive color per line
{% if classification %}

Classification Results

Predicted ClassConfidence
{{ classification.prediction }} {{ "%.1f" | format(classification.confidence * 100) }}%
{% endif %}

๐Ÿ“– Data Augmentation

Python
from PIL import ImageEnhance, ImageOps

def augment_image(img):
    """Generate augmented versions to increase training data."""
    augmented = []

    # Horizontal flip
    augmented.append(ImageOps.mirror(img))

    # Rotation (small angle)
    augmented.append(img.rotate(15, fillcolor=(0, 0, 0)))
    augmented.append(img.rotate(-15, fillcolor=(0, 0, 0)))

    # Brightness adjustment
    enhancer = ImageEnhance.Brightness(img)
    augmented.append(enhancer.enhance(0.7))  # Darker
    augmented.append(enhancer.enhance(1.3))  # Brighter

    # Contrast adjustment
    enhancer = ImageEnhance.Contrast(img)
    augmented.append(enhancer.enhance(0.8))
    augmented.append(enhancer.enhance(1.2))

    return augmented

# From 100 images โ†’ 800 with augmentation (8x increase)
# This helps prevent overfitting on small datasets
โš ๏ธ Common Mistakes
๐Ÿงช Exercises

1. Look at the generated sample images. What visual features distinguish buildings from parks?

2. Why would edge detection be useful for identifying building outlines?

3. Design a feature set for classifying satellite images into: residential, commercial, green space.

{% endblock %}