Academic Block

Logo of Academicblock.net

Computer Vision System in MATLAB

Computer Vision in MATLAB provides tools and functions to process, analyze, and interpret images and video data. MATLAB’s Computer Vision Toolbox includes pre-built functions for image processing, feature detection, object recognition, and more.

Basic Concepts of Computer Vision implemented in Matlab

Computer Vision tasks often involve image manipulation, feature extraction, and object recognition. Below are the fundamental operations:

Reading and Displaying Images

To process an image, first read it into MATLAB and then display it:

% Reading and displaying an image
img = imread('peppers.png'); % Reads an image file
imshow(img); % Displays the image
title('Original Image');

Image Processing Operations

Common image processing techniques include resizing, converting to grayscale, and applying filters:

Resizing an Image

% Resize an image
resizedImg = imresize(img, 0.5); % Scale down the image to 50% of its size
imshow(resizedImg);
title('Resized Image');

Converting to Grayscale

% Convert image to grayscale
grayImg = rgb2gray(img);
imshow(grayImg);
title('Grayscale Image');

Applying Filters

% Apply a Gaussian filter
filteredImg = imgaussfilt(grayImg, 2); % Standard deviation of 2
imshow(filteredImg);
title('Filtered Image');

Feature Detection and Recognition

Detecting features is a critical step in Computer Vision tasks such as object tracking and recognition.

Edge Detection

% Edge detection using Canny method
edges = edge(grayImg, 'Canny');
imshow(edges);
title('Edge Detection');

Object Detection using Pre-Trained Models

MATLAB supports object detection using pre-trained deep learning models, such as YOLO and SSD.

% Load pre-trained YOLOv2 network
net = load('yolov2ResNet50VehicleExample.mat');
detector = net.detector;
% Perform object detection
[bboxes, scores, labels] = detect(detector, img);
% Annotate detections on the image
detectedImg = insertObjectAnnotation(img, 'rectangle', bboxes, cellstr(labels));
imshow(detectedImg);
title('Object Detection');

Working with Videos

Processing videos involves reading video frames and performing operations on each frame.

% Read a video file
videoObj = VideoReader('traffic.mp4');
% Process and display each frame
while hasFrame(videoObj)
frame = readFrame(videoObj);
imshow(frame);
pause(1/videoObj.FrameRate); % Pause for display
end

Advance Example Problems on Computer Vision Systems in Matlab

Example 1: Object Tracking in Video using Optical Flow

In this example, we’ll track motion in a video using the Lucas-Kanade method for optical flow.

% Load video file
videoObj = VideoReader('traffic.mp4');

% Read the first frame and convert to grayscale
frame1 = rgb2gray(readFrame(videoObj));

% Initialize points to track using Harris corners
corners = detectHarrisFeatures(frame1);
points = corners.selectStrongest(50).Location;

% Initialize optical flow tracker
tracker = vision.PointTracker('MaxBidirectionalError', 1);
initialize(tracker, points, frame1);

% Process subsequent frames
while hasFrame(videoObj)
frame2 = rgb2gray(readFrame(videoObj));
[points, validity] = tracker(frame2);
frameWithPoints = insertMarker(frame2, points(validity, :), '+', 'Color', 'red');
imshow(frameWithPoints);
title('Object Tracking');
pause(1/videoObj.FrameRate);
end

Explanation: This script detects Harris corners in the first frame, tracks them using optical flow, and visualizes the motion.

Example 2: Image Stitching (Panorama Creation)

Here, we’ll combine two overlapping images into a single panorama using feature matching and transformation.

% Read two overlapping images
img1 = imread('scene1.jpg');
img2 = imread('scene2.jpg');

% Convert to grayscale
gray1 = rgb2gray(img1);
gray2 = rgb2gray(img2);

% Detect and extract features
points1 = detectSURFFeatures(gray1);
points2 = detectSURFFeatures(gray2);
[features1, validPoints1] = extractFeatures(gray1, points1);
[features2, validPoints2] = extractFeatures(gray2, points2);

% Match features
indexPairs = matchFeatures(features1, features2);
matchedPoints1 = validPoints1(indexPairs(:, 1));
matchedPoints2 = validPoints2(indexPairs(:, 2));

% Estimate geometric transformation
[tform, inlierPoints1, inlierPoints2] = estimateGeometricTransform(matchedPoints1, matchedPoints2, 'projective');

% Warp the second image
outputView = imref2d(size(img1));
warpedImg2 = imwarp(img2, tform, 'OutputView', outputView);

% Blend the images
panorama = max(img1, warpedImg2);
imshow(panorama);
title('Panorama');

Explanation: This code uses SURF features to align two overlapping images and creates a stitched panorama.

Example 3: Face Detection using Haar Cascades

Detect human faces in an image using Haar cascade classifiers.

% Load an image
img = imread('group_photo.jpg');

% Load Haar cascade classifier
faceDetector = vision.CascadeObjectDetector();

% Detect faces
bboxes = step(faceDetector, img);

% Annotate detected faces
annotatedImg = insertObjectAnnotation(img, 'rectangle', bboxes, 'Face');
imshow(annotatedImg);
title('Face Detection');

Explanation: The Haar cascade classifier identifies regions containing faces, and these are highlighted in the output image.

Example 4: Image Segmentation using k-Means Clustering

Segment an image into different regions based on color clustering.

% Read the input image
img = imread('peppers.png');

% Reshape image into a 2D array of RGB values
rows = size(img, 1);
cols = size(img, 2);
data = reshape(img, rows*cols, 3);

% Perform k-means clustering
k = 3; % Number of clusters
[labels, centroids] = kmeans(double(data), k);

% Reshape clustered labels back to the image dimensions
segmentedImg = reshape(labels, rows, cols);
imshow(label2rgb(segmentedImg));
title('Image Segmentation');

Explanation: This script uses k-means clustering to group pixels into k regions based on color similarity.

Example 5: Lane Detection in Driving Scenes

Detect lane lines in a driving scene using Hough transform.

% Read the driving scene image
img = imread('road.jpg');

% Convert to grayscale and detect edges
grayImg = rgb2gray(img);
edges = edge(grayImg, 'Canny');

% Define a region of interest
mask = poly2mask([50, 200, 300, 400], [400, 200, 200, 400], size(edges, 1), size(edges, 2));
roiEdges = edges & mask;

% Perform Hough transform
[H, T, R] = hough(roiEdges);
lines = houghlines(roiEdges, T, R);

% Draw lane lines on the image
imshow(img);
hold on;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:, 1), xy(:, 2), 'LineWidth', 2, 'Color', 'yellow');
end title('Lane Detection');

Explanation: The Hough transform identifies straight lines in the edge-detected image, representing lane markings.

Useful MATLAB Functions for Computer Vision

Function
Explanation
imread
Reads an image from a file.
imshow
Displays an image.
rgb2gray
Converts an RGB image to grayscale.
imresize
Resizes an image to the specified dimensions.
edge
Detects edges in an image using specified methods.
detect
Performs object detection using a pre-trained network.

Practice Questions

Test Yourself

1. Load an image and apply a Gaussian filter to it.

2. Detect edges in an image using different methods (e.g., Sobel, Canny).

3. Write a script to process each frame of a video and apply edge detection.

4. Use a pre-trained YOLO network to detect objects in an image.

5. Use optical flow to track objects across frames in a new video.

6. Segment an image of your choice into five regions using k-means clustering.

7. Detect and annotate all cars in a traffic video using Haar cascades.

8. Implement face recognition by combining face detection and a pre-trained deep learning model.