# 5.2: Two-Dimensional Image Representation

$$\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}}$$

$$\newcommand{\vectorA}[1]{\vec{#1}} % arrow$$

$$\newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow$$

$$\newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vectorC}[1]{\textbf{#1}}$$

$$\newcommand{\vectorD}[1]{\overrightarrow{#1}}$$

$$\newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}$$

$$\newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}}$$

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\avec}{\mathbf a}$$ $$\newcommand{\bvec}{\mathbf b}$$ $$\newcommand{\cvec}{\mathbf c}$$ $$\newcommand{\dvec}{\mathbf d}$$ $$\newcommand{\dtil}{\widetilde{\mathbf d}}$$ $$\newcommand{\evec}{\mathbf e}$$ $$\newcommand{\fvec}{\mathbf f}$$ $$\newcommand{\nvec}{\mathbf n}$$ $$\newcommand{\pvec}{\mathbf p}$$ $$\newcommand{\qvec}{\mathbf q}$$ $$\newcommand{\svec}{\mathbf s}$$ $$\newcommand{\tvec}{\mathbf t}$$ $$\newcommand{\uvec}{\mathbf u}$$ $$\newcommand{\vvec}{\mathbf v}$$ $$\newcommand{\wvec}{\mathbf w}$$ $$\newcommand{\xvec}{\mathbf x}$$ $$\newcommand{\yvec}{\mathbf y}$$ $$\newcommand{\zvec}{\mathbf z}$$ $$\newcommand{\rvec}{\mathbf r}$$ $$\newcommand{\mvec}{\mathbf m}$$ $$\newcommand{\zerovec}{\mathbf 0}$$ $$\newcommand{\onevec}{\mathbf 1}$$ $$\newcommand{\real}{\mathbb R}$$ $$\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}$$ $$\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}$$ $$\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}$$ $$\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}$$ $$\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}$$ $$\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}$$ $$\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}$$ $$\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}$$ $$\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}$$ $$\newcommand{\laspan}[1]{\text{Span}\{#1\}}$$ $$\newcommand{\bcal}{\cal B}$$ $$\newcommand{\ccal}{\cal C}$$ $$\newcommand{\scal}{\cal S}$$ $$\newcommand{\wcal}{\cal W}$$ $$\newcommand{\ecal}{\cal E}$$ $$\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}$$ $$\newcommand{\gray}[1]{\color{gray}{#1}}$$ $$\newcommand{\lgray}[1]{\color{lightgray}{#1}}$$ $$\newcommand{\rank}{\operatorname{rank}}$$ $$\newcommand{\row}{\text{Row}}$$ $$\newcommand{\col}{\text{Col}}$$ $$\renewcommand{\row}{\text{Row}}$$ $$\newcommand{\nul}{\text{Nul}}$$ $$\newcommand{\var}{\text{Var}}$$ $$\newcommand{\corr}{\text{corr}}$$ $$\newcommand{\len}[1]{\left|#1\right|}$$ $$\newcommand{\bbar}{\overline{\bvec}}$$ $$\newcommand{\bhat}{\widehat{\bvec}}$$ $$\newcommand{\bperp}{\bvec^\perp}$$ $$\newcommand{\xhat}{\widehat{\xvec}}$$ $$\newcommand{\vhat}{\widehat{\vvec}}$$ $$\newcommand{\uhat}{\widehat{\uvec}}$$ $$\newcommand{\what}{\widehat{\wvec}}$$ $$\newcommand{\Sighat}{\widehat{\Sigma}}$$ $$\newcommand{\lt}{<}$$ $$\newcommand{\gt}{>}$$ $$\newcommand{\amp}{&}$$ $$\definecolor{fillinmathshade}{gray}{0.9}$$

## 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$$.

## 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.