Synthetic Image Example

From MATLAB Techniques for Image Processing by Steve Eddins.

In 1988, Gerard Holzmann of AT&T Bell Labs (back when there was such a thing) published a fun book called Beyond Photography: The Digital Darkroom. The book shows how some simple math on pixel values and coordinates can transform images in fascinating ways.

In the first part of the book, Holzmann shows how to synthesize interesting images using functions of both Cartesian and polar coordinates. Let's see how to do that in MATLAB.

The MATLAB function meshgrid is extremely useful for computing a function of two Cartesian coordinates, and you can make some interesting images this way.

meshgrid takes a vector of x coordinates and a vector of y coordinates. It produces an X matrix and a Y matrix containing coordinates for an entire Cartesian grid.

Here's an example.

x = 1:3
x =

     1     2     3

y = 10:14
y =

    10    11    12    13    14

[X,Y] = meshgrid(x,y)
X =

     1     2     3
     1     2     3
     1     2     3
     1     2     3
     1     2     3


Y =

    10    10    10
    11    11    11
    12    12    12
    13    13    13
    14    14    14

For convenience, you can use the same vector for both x and y.

v = linspace(-1,5,4)
v =

    -1     1     3     5

[X,Y] = meshgrid(v)
X =

    -1     1     3     5
    -1     1     3     5
    -1     1     3     5
    -1     1     3     5


Y =

    -1    -1    -1    -1
     1     1     1     1
     3     3     3     3
     5     5     5     5

Let's make an image of concentric rings using the equation:

$$sin(A(x^2 + y^2))$$

v = linspace(-pi,pi,201);
[X,Y] = meshgrid(v);
A = 10;
I = sin(A*(X.^2 + Y.^2));

% Specify the range -1 to 1 when displaying the image.
imshow(I,[-1 1])

If you want to construct an image from a function of polar coordinates, use cart2pol in conjunction with meshgrid. The example above can be done in terms of polar coordinates.

[THETA,RHO] = cart2pol(X,Y);
I = sin(A*RHO.^2);

imshow(I,[-1 1])

Make a radially symmetric test image by computing a function of theta.

I = sin(50*THETA);
imshow(I,[-1 1])

The function repmat ("replicate matrix") is useful for creating test images with repeated rows or columns. For example, here's a horizontal grayscale ramp.

row = linspace(0,1,200);
J = repmat(row,[200 1]);
imshow(J)

Flip and concatenate the matrix above to make an up-down ramp.

J2 = [J fliplr(J)];
imshow(J2)

The modulo function is always fun.

row2 = 10*mod(row,0.1);
J3 = repmat(row2,[200 1]);
imshow(J3)

And then there's my favorite way to make a checkerboard image: using $(-1)^(n_1 + n_2)$.

[n1,n2] = meshgrid(0:199);
F = (-1).^(n1 + n2);
F = (F + 1)/2;
imshow(F)

The blocks are only one pixel wide. Let's make them bigger by scaling the n1 and n2 variables and throwing in a call to floor.

b = 10;
G = (-1).^(floor(n1/b) + floor(n2/b));
G = (G + 1)/2;
imshow(G)