Skip to main content
Engineering LibreTexts

5.2: Two-Dimensional Image Representation

  • Page ID
    9976
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Point Matrix

    To represent a straight-line image in computer memory, we must store a list of all the endpoints of the line segments that comprise the image. If the point \(P_i=(x_i,y_i)\) is such an endpoint, we write it as the column vector

    \[\mathrm{p}_{i}=\left[\begin{array}{l}
    x_{i} \\
    y_{i}
    \end{array}\right] \nonumber \]

    Suppose there are \(n\) such endpoints in the entire image. Each point is included only once, even if several lines end at the same point. We can arrange the vectors \(P_i\) into a point matrix:

    \[\begin{align}
    \mathrm{G} &=\left[\mathrm{p}_{1} \mathrm{p}_{2} \mathrm{p}_{3} \ldots \mathrm{p}_{n}\right] \nonumber \\
    &=\left[\begin{array}{llll}
    x_{1} & x_{2} & x_{3} & x_{n} \\
    y_{1} & y_{2} & y_{3} & y_{n}
    \end{array}\right] .
    \end{align} \nonumber \]

    We then store the point matrix \(\mathrm{G} \in \mathscr{R}^{2 \mathrm{x} n}\) as a two-dimensional array in computer memory.

    Example \(\PageIndex{1}\)

    Consider the list of points

    \(\begin{gathered}
    P_{1}=(0,0) \\
    P_{2}=(-1.5,5) \\
    P_{3}=(4,2.3) \\
    P_{4}=(4,-1)
    \end{gathered}\)

    The corresponding point matrix is

    \[G=\left[\begin{array}{llll}
    0 & -1.5 & 4 & 4 \\
    0 & 5 & 2.3 & -1
    \end{array}\right] \nonumber \]

    Line Matrix

    The next thing we need to know is which pairs of points to connect with lines. To store this information for \(m\) lines, we will use a line matrix, \(\mathrm{H} \in \mathscr{R}^{2 \times m}\). The line matrix does not store line locations directly. Rather, it contains references to the points stored in \(G\). To indicate a line between points \(p_i\) and \(p_j\), we store the indices \(i\) and \(j\) as a pair. For the \(k^{th}\) line in the image, we have the pair

    \[\mathrm{h}_{k}=\left[\begin{array}{c}
    i_{k} \\
    j_{k}
    \end{array}\right] \nonumber \]

    The order of \(i\) and \(j\) does not really matter since a line from \(p_i\) to \(p_j\) is the same as a line from \(p_j\) to \(p_i\). Next we collect all the lines \(h_k\) into a line matrix \(H\):

    \[\mathrm{H}=\left[\begin{array}{lllll}
    i_{1} & i_{2} & i_{3} & \ldots & i_{m} \\
    j_{1} & j_{2} & j_{3} & \ldots & j_{m}
    \end{array}\right] . \nonumber \]

    All the numbers in the line matrix \(H\) will be positive integers since they point to columns of \(G\). To find the actual endpoints of a line, we look at columns \(i\) and \(j\) of the point matrix \(G\).

    Example \(\PageIndex{2}\)

    To specify line segments connecting the four points of Example 1 into a quadrilateral, we use the line matrix

    \[\mathrm{H}_{1}=\left[\begin{array}{llll}
    1 & 2 & 3 & 4 \\
    2 & 3 & 4 & 1
    \end{array}\right] \nonumber \]

    Alternatively, we can specify line segments to form a triangle from the first three points plus a line from \(P_3\) to \(P_4\) :

    \[\mathrm{H}_{2}=\left[\begin{array}{llll}
    1 & 2 & 3 & 3 \\
    2 & 3 & 1 & 4
    \end{array}\right] . \nonumber \]

    Figure 1 shows the points \(G\) connected first by \(H_1\) and then by \(H_2\).

    Screen Shot 2021-08-11 at 5.51.18 PM.png
    Figure \(\PageIndex{1}\): Two Sets of Lines

    Demo 1 (MATLAB)

    Use your editor to enter the following MATLAB function file. Save it as vgraphl.m.

    function vgraphl (points, lines);
    % vgraphl (points, lines) plots the points as *'s and
    % connects the points with specified lines. The points
    % matrix should be 2xN, and the lines matrix should be 2xM.
    % The field of view is preset to (-50,50) on both axes.
    %
    % Written by Richard T. Behrens, October 1989
    %
    m=length(lines);                    % find the number of
                                        % lines.
    axis([-50 50 -50 50])               % set the axis scales
    axis('square')
    plot(points(1,:),points(2,:),'*')   % plot the points as *
    hold on                             % keep the points...
    for i=i:m                           % while plotting the
                                        % lines
        plot([points(1,lines(1,i)) points(1,lines(2,i))],..
            [points(2,lines(2,lines(1,i)) points(2,lines(2,i))],'-')
        
        end
        
        hold off

    After you have saved the function file, run MATLAB and type the following to enter the point and line matrices. (We begin with the transposes of the matrices to make them easier to enter.)

    >> G = [
        0.6052 -0.4728;
       -0.4366  3.5555;
       -2.6644  7.9629;
       -7.2541 10.7547;
      -12.5091 11.5633;
      -12.5895 15.1372;
       -6.5602 13.7536;
      -31.2815 -7.7994;
      -38.8314 -9.9874;
      -44.0593 -1.1537;
      -38.8314  2.5453;
      -39.4017  9.4594;
      -39.3192 15.0932;
      -45.9561 23.4158]
    
    >> G = G'
    
    >> H = [
       1   2;
       2   3;
       3   4;
       4   5;
       4   7;
       5   6;
       8   9;
       9   10;
       10  11;
       11  12;
       12  13;
       12  14]
    
    >> H = H'

    At this point you should use MATLAB's “save” command to save these matrices to a disk file. Type

    >> save dippers

    After you have saved the matrices, use the function VGRAPH1 to draw the image by typing

    >> vgraph1(G,H)

    The advantage of storing points and lines separately is that an object can be moved and scaled by operating only on the point matrix \(G\). The line information in \(H\) remains the same since the same pairs of points are connected no matter where we put the points themselves.

    Surfaces and Objects. To describe a surface in three dimensions is a fairly complex task, especially if the surface is curved. For this reason, we will be satisfied with points and lines, sometimes visualizing flat surfaces based on the lines. On the other hand, it is a fairly simple matter to group the points and lines into distinct objects. We can define an object matrix \(K\) with one column for each object giving the ranges of points and lines associated with that object. Each column is defined as

    \[\mathrm{k}_{i}=\left[\begin{array}{cc}
    \text { first } & \text { point } \\
    \text { last } & \text { point } \\
    \text { first } & \text { line } \\
    \text { last } & \text { line }
    \end{array}\right] \nonumber \]

    As with the line matrix \(H\), the elements of \(K\) are integers.

    Example \(\PageIndex{3}\)

    Consider again Demo 1. We could group the points in \(G\) and the lines in \(H\) into two objects with the matrix

    \[K=\left[\begin{array}{ll}
    1 & 8 \\
    7 & 14 \\
    1 & 7 \\
    6 & 12
    \end{array}\right] \nonumber \]

    The first column of \(K\) specifies that the first object (Ursa Minor) is made up of points 1 through 7 and lines 1 through 6, and the second column of \(K\) defines the second object (Ursa Major) as points 8 through 14 and lines 7 through 12.


    This page titled 5.2: Two-Dimensional Image Representation is shared under a CC BY 3.0 license and was authored, remixed, and/or curated by Louis Scharf (OpenStax CNX) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.