Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 45 ยท Modeling and Applications

Image Processing Basics

Treat images as matrices in MATLAB and Octave: imread and imshow, grayscale, conv2 filters, thresholding and edge detection, with runnable NumPy stand-ins.

A digital image is a grid of numbers. A grayscale picture is a matrix with one number per pixel; a color picture is a three-layer array, one matrix each for red, green and blue. That is exactly the kind of data MATLAB and Octave were built for, so every matrix tool you already know works on pictures: indexing cuts out a region, arithmetic changes brightness, and conv2 blurs or sharpens.

MATLAB and Octave are not available in your browser here, so the MATLAB code is read-only. There are no image files in this lesson either: the runnable stand-ins build a small synthetic picture with NumPy and process it with SciPy and matplotlib, which makes every result reproducible. The matrix operations are the same ones you would run on a photo loaded with imread.

A picture is an array

Grayscale One matrix, rows by columns.
Color Rows by columns by 3 layers.
Values 0 to 255 as uint8, or 0 to 1 as double.
1 / 2

Images as matrices

imread loads a file into an array, and imshow shows it. The class of the array matters: most files give uint8 values from 0 (black) to 255 (white). im2double converts to double in the range 0 to 1, which is what most processing code expects.

MATLAB
A = imread('photo.jpg');     % 480-by-640-by-3 uint8 for a color JPEG
size(A)                      % rows, columns, channels
class(A)                     % uint8
imshow(A)

G = rgb2gray(A);             % 480-by-640 uint8, one matrix
D = im2double(G);            % same picture as doubles in [0, 1]
D(100:200, 50:150)           % a rectangular region: ordinary indexing

Rows are counted from the top, so A(1, 1) is the top-left pixel and A(r, c) means row r (vertical) and column c (horizontal). That is the reverse of the usual x/y order, a common source of mixed-up coordinates. rgb2gray mixes the three channels using weights that reflect how bright each color looks to the eye: roughly 0.30 red, 0.59 green and 0.11 blue. Beware of arithmetic on uint8 values: 200 + 100 saturates at 255 instead of reaching 300 (see Numeric Types and Precision), so convert to double before you calculate.

imread and imwrite are core functions in MATLAB; imshow, rgb2gray and most of the filters below belong to the Image Processing Toolbox. Octave provides the same names in its image package (pkg load image), and its imread needs GraphicsMagick support. With only the core you can use image(A) or imagesc(G) with colormap(gray), as shown in Bar, Scatter, Histogram and Images.

Python Build a synthetic image and inspect it as a matrix

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Filtering with conv2

Most classic filters are convolutions: slide a small matrix, the kernel, over the image; at each position multiply the kernel with the pixels beneath it and add up the products. conv2(A, K, 'same') does exactly that and keeps the original size.

A blur uses a kernel of equal weights that add up to 1, so the average brightness stays unchanged:

MATLAB
D = im2double(G);
K = ones(5) / 25;               % 5-by-5 average
B = conv2(D, K, 'same');        % blurred image, same size as D
imshow(B)

A bigger kernel blurs more. Edge pixels are computed as if the picture were surrounded by black (zeros), so a dark border can appear; imfilter(D, K, 'replicate') in the toolbox avoids it by repeating the border pixels. A blur is useful for noise reduction: random speckles average out, while large smooth regions survive. The price is that real detail blurs as well. imgaussfilt(D, 2) blurs with a Gaussian-shaped kernel, which looks more natural than a flat average, and medfilt2(D) replaces every pixel with the median of its neighbours, which removes isolated bright or dark dots without smearing edges as much.

Thresholding

A threshold turns a grayscale image into a black-and-white mask: pixels brighter than the level become 1 (true), the rest 0. It is a comparison on a matrix, so it is logical indexing in two dimensions.

MATLAB
mask = D > 0.5;                 % logical matrix: true where brighter than half
level = graythresh(D);          % Otsu's method: picks a level automatically
mask2 = imbinarize(D, level);   % same as D > level
area = nnz(mask)                % number of pixels above the level

Picking the level by hand works when the lighting is steady. graythresh (Image Processing Toolbox) chooses the level that best separates the histogram into two groups, which is robust to changes in overall brightness. Counting nnz(mask) gives the area of the bright object in pixels, and mean(mask(:)) gives the fraction of the image it covers.

Edge detection

An edge is a place where brightness changes quickly. The change in the horizontal direction is a convolution with a kernel that subtracts the left pixels from the right ones; the same idea rotated gives the vertical change. The Sobel kernels are the best-known pair. The edge strength is the length of the two changes combined:

MATLAB
Kx = [-1 0 1; -2 0 2; -1 0 1];
Ky = Kx';
Gx = conv2(D, Kx, 'same');
Gy = conv2(D, Ky, 'same');
E  = sqrt(Gx.^2 + Gy.^2);      % edge strength at each pixel
edges = E > 0.5 * max(E(:));   % keep only the strongest edges

The toolbox bundles the whole pipeline in edge(G, 'sobel') or edge(G, 'canny'). Noise creates false edges, so blur first: smoothing followed by an edge filter is the heart of the Canny detector. A small detail: conv2 really flips the kernel, as the mathematical definition of convolution says, so the sign of Gx is opposite to what imfilter gives for the same kernel. The edge strength E does not care.

Python Blur, threshold and edge detection

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Writing the result back

imwrite(uint8(255 * B), 'blurred.png') saves an image; convert doubles in [0, 1] back to uint8 first, or let im2uint8 do it. imwrite needs write access to the folder; fullfile(tempdir, 'blurred.png') gives a path that is always writable. When you only want to look, montage shows several images side by side and imtool opens an interactive viewer with pixel values.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What are the dimensions of a color image stored as a 480-by-640 pixel picture?

    Rows (height) come first, then columns (width), then the three color channels red, green and blue.
  2. 2. Why convert a uint8 image with im2double before arithmetic?

    200 + 100 stays at 255 in uint8 arithmetic. In double, calculations are exact enough, and im2double also rescales to 0..1.
  3. 3. What does conv2(D, ones(3)/9, 'same') do?

    A kernel of equal weights summing to 1 is a mean filter. 'same' keeps the output the size of the input.
  4. 4. How do you count the pixels brighter than 0.5 in a double image D?

    D > 0.5 is a logical matrix; nnz counts all its true entries. sum(D > 0.5) would count per column.
  5. 5. Why blur an image before running an edge detector?

    Edge filters respond to any rapid change, including random noise. Smoothing first leaves the real edges.

Summary

  • A grayscale image is a matrix and a color image is rows by columns by 3; A(r, c) means row r, column c.
  • imread loads, imshow shows, rgb2gray makes grayscale, and im2double avoids uint8 saturation.
  • conv2(D, K, 'same') applies a kernel: equal weights blur, Sobel kernels measure change.
  • A threshold is a matrix comparison; graythresh picks the level automatically and nnz counts the result.
  • Edge strength is sqrt(Gx.^2 + Gy.^2); blur first to avoid false edges.
  • imread and conv2 are core, but imshow, graythresh, edge and friends need the Image Processing Toolbox (Octave: pkg load image).

Next: Simulink, Toolboxes and Octave Packages.