Lesson 16-Image Processing Basics and Morphological Operations

Introduction

Image processing is one of the most visually rewarding areas of MATLAB programming. Unlike pure mathematical computation or data analysis, image processing allows you to see immediate, tangible results from your code. You can take a noisy, poorly contrasted photograph and transform it into a clean, segmented image where objects of interest are clearly identified and counted. This visceral feedback makes image processing an excellent domain for developing your MATLAB skills.

MATLAB’s Image Processing Toolbox is a comprehensive suite of functions that spans the entire image processing workflow: from reading and displaying images, through preprocessing and enhancement, to advanced operations like morphological processing, edge detection, and object analysis. Whether you’re working with medical imagery, satellite photos, industrial inspection systems, or scientific microscopy, the fundamental techniques remain the same, and MATLAB provides a unified environment to apply them.

In this extensive tutorial, we will journey from the absolute fundamentals—understanding how MATLAB represents images in memory—to sophisticated morphological operations that can extract meaningful structure from binary images. We’ll explore why the choice between uint8 and double precision matters more than you might think, how different color spaces reveal different information in your images, and how morphological operations can be combined to solve complex segmentation problems. By the end of this article, you’ll have both the theoretical understanding and the practical MATLAB skills to tackle real-world image processing challenges.

The Image Processing Toolbox: An Overview

The Image Processing Toolbox is one of MathWorks’ most mature and widely-used toolboxes. It provides a rich set of functions for reading, writing, displaying, analyzing, and processing images. The toolbox supports a wide variety of image formats including JPEG, PNG, TIFF, BMP, GIF, and specialized scientific formats like DICOM (medical imaging) and FITS (astronomy).

What sets MATLAB’s Image Processing Toolbox apart from general-purpose image libraries is its integration with MATLAB’s numeric computing environment. Images in MATLAB are simply matrices (or arrays of matrices), which means you can apply all of MATLAB’s mathematical and statistical functions directly to images. Want to compute the mean intensity of a region? Use mean(). Want to apply a custom convolution kernel? Use conv2() or imfilter(). This seamless integration between image data and mathematical computation is MATLAB’s greatest strength for image processing.

The toolbox is organized around several key functional areas:

Image Import and Export: Functions like imread(), imwrite(), imfinfo() handle reading images from disk and writing processed images back. MATLAB automatically handles the conversion between file formats and in-memory representations.

Display and Visualization: imshow(), imtool(), montage(), implay() provide various ways to visualize images. The imtool() function opens an interactive image viewing environment with pixel value readout, distance measurement, and zooming—invaluable for debugging.

Image Arithmetic and Logic: Since images are matrices, you can add, subtract, multiply, or logically combine images using standard MATLAB operators. The toolbox also provides specialized functions like imadd(), imsubtract(), immultiply() that handle data type scaling automatically.

Geometric Transformations: Functions for resizing (imresize()), rotating (imrotate()), cropping (imcrop()), and general geometric warping (imwarp()).

Image Enhancement: Contrast adjustment (imadjust()), histogram equalization (histeq(), adapthisteq()), denoising filters (imgaussfilt(), medfilt2(), wiener2()).

Morphological Operations: A rich set of operations for processing binary and grayscale images based on shape: imerode(), imdilate(), imopen(), imclose(), bwareaopen(), bwlabel(), bwconncomp().

Image Analysis and Statistics: Region properties (regionprops()), edge detection (edge()), Hough transform (hough(), houghpeaks(), houghlines()), and intensity statistics.

Color Processing: Color space conversions (rgb2gray(), rgb2hsv(), ind2rgb()), color-based segmentation, and colormap manipulation.

Understanding this organization helps you navigate the toolbox efficiently. When you need to accomplish a task, you can often guess the function name based on MATLAB’s consistent naming conventions: im prefix for image functions, bw prefix for binary image functions, fspecial() and related for filter design, and so on.

Let’s begin our deep dive by understanding the most fundamental concept: how MATLAB represents images in memory.

Image Representation: Understanding uint8, uint16, double, and Indexing

The Matrix Nature of Images

In MATLAB, every image is fundamentally a matrix (or an array of matrices). A grayscale image is a 2D matrix where each element (pixel) contains the intensity value at that location. A color image is typically represented as a 3D array where the third dimension corresponds to color channels—typically Red, Green, and Blue (RGB).

This matrix representation is both powerful and sometimes confusing for beginners. When you read an image using imread(), what you get back is not a special “image object” but a plain MATLAB array. This means all of MATLAB’s array manipulation capabilities are immediately available for image processing.

Consider reading a simple grayscale image:

% Read a grayscale image
img = imread('coins.png');

% Display basic information about the image
whos img
% Output:  Name      Size              Bytes  Class    Attributes
%          img      246x300            73800  uint8              

% Display the image matrix
disp(img(1:5, 1:5));
% Output (typical for coins.png):
%   148   148   149   149   150
%   148   148   149   149   150
%   148   148   149   149   150
%   148   148   149   149   150
%   148   148   149   149   150

The output shows that img is a 246×300 matrix of uint8 values. Each value represents the intensity of a pixel, ranging from 0 (black) to 255 (white) for uint8 images.

Understanding Numeric Types for Images

One of the most common sources of confusion in MATLAB image processing is the relationship between numeric types and image representation. MATLAB supports several numeric types for images, and choosing the right one affects both memory usage and processing precision.

uint8: The Standard for 8-bit Images

uint8 (unsigned 8-bit integer) is the most common image representation. It uses 8 bits per pixel, allowing values from 0 to 255. This matches the standard representation used by most image file formats (JPEG, PNG, etc.) and display systems.

% Create a simple uint8 image
img_uint8 = uint8([0, 128, 255; 64, 192, 100; 200, 50, 150]);

% Display the image
figure;
subplot(1, 2, 1);
imshow(img_uint8);
title('uint8 Image (0-255)');
colorbar;

% Check the data type
disp(['Data type: ', class(img_uint8)]);
% Output: Data type: uint8

The advantages of uint8 are:

  • Memory efficiency: Only 1 byte per pixel per channel
  • Speed: Many operations are optimized for uint8
  • Compatibility: Matches most image file formats and display systems

The disadvantage is limited precision: with only 256 distinct values, subtle intensity variations can be lost. This becomes problematic in applications like medical imaging or scientific measurement where precision matters.

uint16: Higher Precision When You Need It

uint16 uses 16 bits per pixel, allowing values from 0 to 65535. This is commonly used in medical imaging (DICOM), scientific cameras, and applications where higher dynamic range is needed.

% Read a 16-bit image (if available) or convert
img = imread('mri.tif');  % Might be uint16
if isa(img, 'uint8')
    img_uint16 = uint16(img) * 257;  % Scale uint8 to use full uint16 range
else
    img_uint16 = img;
end

disp(['Data type: ', class(img_uint16)]);
disp(['Value range: ', num2str(min(img_uint16(:))), ' to ', num2str(max(img_uint16(:)))]);

double: The Precision Choice

double is MATLAB’s default numeric type for general computation, and it’s also used for images when precision is paramount. Double-precision images typically use the range 0.0 to 1.0 (though they can use any range).

% Convert a uint8 image to double
img_uint8 = imread('coins.png');
img_double = im2double(img_uint8);

% Display information
disp(['uint8 type: ', class(img_uint8), ', range: ', ...
    num2str(min(img_uint8(:))), ' to ', num2str(max(img_uint8(:)))]);
% Output: uint8 type: uint8, range: 27 to 255

disp(['double type: ', class(img_double), ', range: ', ...
    num2str(min(img_double(:))), ' to ', num2str(max(img_double(:)))]);
% Output: double type: double, range: 0.10588 to 1

The conversion from uint8 to double using im2double() scales the values to the range [0, 1]. This is crucial: if you simply use double(img_uint8), the values will be 0-255 as doubles, which can cause problems with functions expecting the [0, 1] range.

Why does this matter? Many image processing functions in MATLAB are designed to work with the [0, 1] range for double images. For example, imshow() expects double images to be in [0, 1]; if you pass it a double image with values in [0, 255], it will appear white (saturated).

% Demonstrating the problem
img = imread('coins.png');
img_double_wrong = double(img);  % Values 0-255 as double
img_double_correct = im2double(img);  % Values 0-1 as double

figure;
subplot(1, 3, 1);
imshow(img);
title('Original uint8');

subplot(1, 3, 2);
imshow(img_double_wrong);
title('Wrong: double(0-255)');

subplot(1, 3, 3);
imshow(img_double_correct);
title('Correct: im2double');

The middle image will appear completely white because imshow() interprets any value greater than 1 as “full intensity.”

When to Use Each Type

Use uint8 when:

  • Memory is constrained
  • You’re working with standard 8-bit image formats
  • You don’t need sub-intensity precision
  • You’re displaying images or writing to standard image files

Use uint16 when:

  • Working with medical or scientific images with higher bit depth
  • You need more than 256 intensity levels but don’t need floating-point
  • The imaging hardware provides 16-bit data

Use double when:

  • Performing complex mathematical operations on images
  • Precision is critical (e.g., filtering, frequency domain operations)
  • You’re developing new algorithms
  • You need to avoid quantization errors in repeated operations

The key insight: Every conversion between types involves either quantization (losing precision) or scaling. Plan your workflow to minimize unnecessary conversions. A common pattern is: read as uint8, convert to double for processing, convert back to uint8 for display or file output.

Indexing into Images: Rows, Columns, and Channels

Understanding how to index into images is fundamental to image processing. In MATLAB, matrices are indexed as (row, column), which corresponds to (y, x) in image coordinates. This is sometimes confusing because we typically think of coordinates as (x, y).

% Read a color image
img = imread('peppers.png');

% Display image size
disp(size(img));
% Output: 384   512     3
% Interpretation: 384 rows, 512 columns, 3 color channels (RGB)

% Access a single pixel at row 100, column 200
pixel = img(100, 200, :);
disp(squeeze(pixel)');
% Output: R G B values, e.g., 55  89  23

% Access a rectangular region (rows 50-100, columns 150-250)
region = img(50:100, 150:250, :);

% Access just the Red channel
red_channel = img(:, :, 1);

% Access just the Green channel
green_channel = img(:, :, 2);

% Access just the Blue channel
blue_channel = img(:, :, 3);

% Display individual channels
figure;
subplot(2, 2, 1);
imshow(img);
title('Original RGB Image');

subplot(2, 2, 2);
imshow(red_channel);
title('Red Channel');

subplot(2, 2, 3);
imshow(green_channel);
title('Green Channel');

subplot(2, 2, 4);
imshow(blue_channel);
title('Blue Channel');

Important: When you extract a region from a color image, the result maintains the 3D structure:

% Extract a 51x101 region from the color image
region = img(50:100, 150:250, :);
disp(size(region));
% Output: 51    101     3

If you extract a region from a grayscale image, the result is 2D:

% Read as grayscale
img_gray = imread('coins.png');
region_gray = img_gray(50:100, 150:250);
disp(size(region_gray));
% Output: 51    101

Logical Indexing with Images

Logical indexing is extremely powerful for image processing. You can create binary masks and use them to extract or modify pixel values.

% Read a grayscale image
img = imread('coins.png');

% Create a binary mask: pixels with intensity > 150
mask = img > 150;

% Display the mask
figure;
subplot(1, 3, 1);
imshow(img);
title('Original Image');

subplot(1, 3, 2);
imshow(mask);
title('Binary Mask (intensity > 150)');

% Use the mask to set those pixels to 255 (white)
img2 = img;
img2(mask) = 255;

subplot(1, 3, 3);
imshow(img2);
title('Masked Pixels Set to White');

% Count how many pixels satisfy the condition
num_pixels = sum(mask(:));
disp(['Number of pixels with intensity > 150: ', num2str(num_pixels)]);

This technique of creating binary masks is fundamental to image segmentation, which we’ll explore in depth later.

Membership Required

You must be a member to access this content.

View Membership Levels

Already a member? Log in here

Share your love