k-Wave Toolbox |

## Loading External Image Maps Example

On this page…
Overview Loading the image map

### Overview

This example demonstrates how to assign an external image to the initial pressure distribution for the simulation of an initial value problem within a two-dimensional homogeneous propagation medium. It builds on the Homogeneous Propagation Medium Example.

- open the file in the MATLAB Editor
- run the file in MATLAB

Back to Top

### Loading the image map

The initial pressure distribution `source.p0` used by `kspaceFirstOrder2D` is simply a two-dimensional matrix filled with arbitrary numeric values. Consequently, any data may be used to define this distribution. Here an external image map is loaded using `loadImage`. This function converts an external image into a matrix, sums the colour channels (for colour images), and scales the pixel values from 0 to 1.

    % load the initial pressure distribution from an image
    p0_magnitude = 5;
    p0 = p0_magnitude*loadImage('EXAMPLE_source.bmp');

The medium discretisation is again performed by calling `makeGrid` with the required grid dimensions, after which `resize` is used to scale the loaded image map to the required dimensions.

    % create the computational grid
    Nx = 128;           % number of pixels in the x (column) direction
    Nz = 128;           % number of pixels in the z (row) direction
    dx = 0.1e-3;        % pixel width [m]
    dz = 0.1e-3;        % pixel height [m]
    kgrid = makeGrid(Nx, dx, Nz, dz);

    % resize the image to the desired number of pixels and assign to the
    % source input structure
    source.p0 = resize(p0, [Nz, Nx]);

The simulation is invoked in the same way as in the previous examples. A plot of the initial pressure distribution and utilised Cartesian sensor mask (using nearest neighbour interpolation) and a visualisation of the recorded pressure field are shown below.

Back to Top

© 2009, 2010, 2011 Bradley Treeby and Ben Cox.