# Coordinate system in digital image processing

I am trying to understand image coordinate system where I read that it is reversed.

So if $$f(x,y)$$ is actually $$f(y,x)$$ where $$f(\cdot, \cdot)$$ is the image. I am a bit confused and would appreciate help in clarifying certain points.

1. In digital image processing, an image is a matrix of rows ($$m$$) and columns ($$n$$) denoted by $$[m,n]$$. When showing as an image output would the rows become the $$Y$$ axis and and column the $$X$$ axis? Just wanted to confirm this as sometime it is confusing. For example, consider a $$3 \times 5$$ matrix:
[ 30   40  20  30  0 60
10   20  15  10  0 20
20   5   10  30  0 10 ]


where the elements represent the pixel intensities. If I want to access the element 60, how would I specify? Would row, $$m = 1$$ and column, $$n = 5?$$ Is row the $$X$$ or $$Y$$ axis?

1. In Maths, if I were to plot in a graph the coordinate $$(5,1)$$ then 5 refers to the $$x=5$$ (plotted on horizontal $$X$$ axis) and $$y=1$$ (plotted on vertical $$Y$$ axis). However, in image representation the coordinate $$(5,1)$$ means go to row 5 and column 1 ie., vertical axis is the row, $$m=5$$ and the horizontal is the column, $$n=1$$? Is there a reversal going on or am I misunderstanding something. What is the convention?

## So in general, in image Columns --> X axis and Rows ---> Y Axis? But in graphs it is the opposite?

Consider a matrix

A = [1,2,3
4,5,6
7,8,9]


On the graph the coordinate point is $$(3,2)$$. Here $$x=3$$, $$y=2$$. If I want to access element at $$A(3,2)$$ then this means row (horizontal) = 3, column (vertical) = 2 and the element at $$A(3,2) = 2$$.

In images does the vertical axis become the rows and the horizontal the columns or is it the same as that of matrices, except for the top to bottom traversal constraint? Something seems to be changing, that is the confusing part.

Different operating systems, standards, and graphical softwares implement distinct (but quite related) conventions for representing, storing, manipulating, or displaying two-dimensional data on computers. The distinction is about the orientation of the axes.

Basically we have 4 main object types :

• $$f(x,y)$$ , x-y continuous, is a function of mathematical relevance, such as an analog image.
• $$f[n,m]$$, n-m integers, is a sequence obtained by sampling $$f(x,y)$$, or synth-generated.
• $$F(i,j)~$$, i-j integers, is a matrix to represent the data in its rows-i and columns-j.
• $$f[i,j]~~$$ , i-j integers, is an array to store the data used in a computer program.

The arguments $$x,y,n,m,i,j$$ can also be replaced with $$x_1,x_2,n_1,n_2,i_1,i_2$$, the subscripts indicating the dimension order of the variables, such as the first or the second dimension.

A sequence $$f[n_1,n_2]$$ can represent a 2D digital image, or a mathematical function defined over the coordinate system of axes $$n_1,n_2$$. Its samples are placed in the correct elements of the associated matrix $$f(i,j)$$ for the operation to produce the desired effect at the output.

MATLAB fundamentally uses MATRIX based dimension ordering. The first sample A(1,1) is at the top-left corner, first dimenison $$i$$ is along vertical-down the rows, and the second dimension, $$j$$, is along the columns (horizontal-right).

This is equivalent to placing a coordinate system of $$n_1 ~, ~n_2$$ with its origin (0,0) at the element A(1,1); the first axis $$n_1$$ points along the first dimension $$i$$ (vertical down); and the second axis $$n_2$$ points along the second dimension $$j$$ (horizontal right) of the matrix A(i,j).

This mapping is obtained when you rotate the conventional coordinate system ($$n_1$$ horizontal-right, and $$n_2$$ vertical-up, origin at bottom-left) by 90 degrees clockwise with respect to its origin (0,0) and placing the origin at the element A(1,1) of the matrix.

In a parallel fashion, the 2D-DFT (discrete Fourier transform) $$F[k_1,k_2]$$ of the sequence $$f[n_1,n_2]$$ is computed by the statement: F = fft2( f ) which places the first dimension, $$k_1$$, along the rows $$i$$, and the second dimension, $$k_2$$, along the columns $$j$$ of the output matrix $$F(i,j)$$ to represent the DFT sequence $$F[k_1,k_2]$$. Which is also alligned with the first and second dimenions of the input matrix $$f(i,j)$$ that represents the seqeunce $$f[n_1,n_2]$$.

Below is an oldskool discussion of a few mapping modes, and functions in MATLAB related with data orientation. As long as mappings are used consistently, they all yield the same results when interpreted correctly.

% SEQUENCES, BMP IMAGES, MATRICES and MATLAB FUNCTIONS :
% ------------------------------------------------------
%
% All data processing and display in Matlab is done via MATRICES A(i,j).
% But, theory of image processing is based on SEQUENCES f[n1,n2], F[k1,k2].
% When processing images, orientation of axes become relevant across
% functions such as CONV2(), FFT2(), IMSHOW(), STEM3(), SURF().
% And a MAPPING convention from f[n1,n2] into A(i,j) should be used.
%
%
% A matrix A(i,j) is indexed by vert rows i, and horz columns j.
% A sequence f[n1,n2] is indexed by horz-right n1, and vert-up n2.
% Which is the most typical, and natural, orientations for n1 and n2.
%
%
% The mapping convention depends on the functions being called :
%
%           1- PROCESS functions: conv2(), fft2(), filter2()
%           2- DISPLAY functions: imshow(), surf(), stem3()
%
%
% We consider following mapping modes between the samples of a sequence
% f[n1,n2] and elements of a matrix A(i,j) as follows:
%
%
% MM-0 : 90d CW ROTATED MAPPING :
% ----------------------------------------------------------------------
%  f[0,0] --> A(1,1), and "n1" grows DOWN from the top-row of A.
%
%    n1 = i-1   , n2 = j-1     ===>          A(i,j) = f[i-1, j-1]
%    i  = n1+1  , j  = n2+1    ===>        f[n1,n2] = A(n1+1,n2+1)
%
%  ---o---1--------2-----> j (n2)
%   1 | f[0,0]  f[1,0]
%   2 | f[1,0]  f[1,1]    Ex mapping of f[n1,n2] into 3x2 matrix A(i,j)
%   3 | f[2,0]  f[2,1]
%     |
%   i v
%  n1
%
%
%
% MM-1 : FLIP-DOWN MAPPING :
% ----------------------------------------------------------------------
% f[0,0] ---> A(1,1) and "n2" grows DOWN from the top-row of A.
%
%    n1 = j-1   , n2 = i-1     ===>          A(i,j) = f[j-1, i-1]
%    i  = n2+1  , j  = n1+1    ===>        f[n1,n2] = A(n2+1,n1+1)
%
%  ---o---1--------2-----3---> j (n1)
%   1 | f[0,0]  f[1,0]  f[2,0]
%   2 | f[0,1]  f[1,1]  f[2,1] Ex mapping of f[n1,n2] into 3x2 matrix A(i,j)
%     |
%   i v
%  n2
%
%
% MM-2 : BMP MAPPING :
% ----------------------------------------------------------------------
% f[0,0] is stored in A(N2,1) and "n2" grows UP from the bottom-row of A
%
%    n1 = j-1   , n2 = N2-i     ===>          A(i,j) = f[j-1, N2-i]
%    i  = N2-n2 , j  = n1+1     ===>        f[n1,n2] = A(N2-n2,n1+1)
%
%  n2 ^
%   2 | A(1,1)  A(1,2)         An example mapping into 3x2 matrix A
%   1 | A(2,1)  A(2,2)
%   0 | A(3,1)  A(3,2)
%  ---o---0--------1-----> n1 (j)
%
%
%
% Axis orientations of functions CONV(),FILTER(),FFT2():
% ----------------------------------------------------------------------
% They assume 90d ROTATED / or FLIP DOWN index mapping by default.
%
%      A(1,1)---------> n2,k2,j (horizontal - column variable)
%           |
%           |  A(i,j)
%           |
%           v n1,k1,i (vertical - row variable)
%
%
% NOTE: Strictly speaking, MATLAB does not care which mapping was used to
% generate the matrix A(i,j). Rather it treats "i" as the first dimension
% and "j" as the second dimension, and outputs acordingly.
%
%
% Axis orientation of DISPLAY functions STEM3(),  SURF()            :
% -------------------------------------------------------------------
% It produces the plots according to NATURAL X-Y orientation in which
% n1 point horizontal right, and n2 point verticcal up and (0,0) being
% at the bottom. But this requires that the MATRIX A(i,j) was filled in
% according to FLIP-DOWN mapping mode.
%
%                          A(N2,N1)
%                          f22
%                       f12   f21
%     A(N2,1) (n2-i) f02  f11  f20 (n1-j) A(1,N1)
%                      f01  f10
%            n2-axis      f00       n1-axis
%                        A(1,1)
%
%
% Image display function IMSOW(A(i,j)) assumes a BMP mapping:
% -------------------------------- --------------------------
% IMSHOW displays the matrix contents in its row-column order.
% Therefore if you want to display a sequence f[n1,n2], or F[k1,k2] using
% the IMSHOW() function, then in order to have the correct orientation
% according to natural x-y coordinates, you should use BMP mapping mode
% on the matrix which is to be displayed.
%


SUMMARY

If IMSHOW() iwill be used to display images, or their FFT results, then correct alignment with $$n_1,n_2$$ and $$k_1,k_2$$ requires BMP based mapping to be used to fill in the associated matrix. If STEM3() or SURF() will be used to get a 3D display of the sequences, or FFT results, then flipp-down mapping produces correct orientation. Other functions CONV2(), FFT2() etc., work equally well with either Rotated or Flip-Down mapping modes.

• THank you for your answer. Just to clarify 2 things because I have begun to doubt what I had studied in elementary maths after reading through the confusing image coordinate representation (1) In Maths, in general a 2D matrix A(i,j) would have the rows i horizontal and columns j vertical? If I were to plot on a graph the element (3,2) from A then i=3 denotes the horizontal x axis coordinate & j=2 denotes the vertical y axis coordinate. If I imagine A as a graph then y=2 axis denotes the rows and x=3 axis denotes the column. Am I correct?
– Sm1
Oct 3, 2020 at 16:02
• (2) The same convention is followed for images, however the 'y` axis (rows) increases from top to bottom.
– Sm1
Oct 3, 2020 at 16:05
• @Sm1 No. The convention when defining systems of linear equations $$a_{11} x_1 + a_{12} x_2 = b_1 \\ a_{21} x_1 + a_{22} x_2 = b_2$$ using the equivalent system matrix $A(i,j)$, as $Ax = b$ , is such that $i$ refers to the number of equation (rows) and $j$ refers to the number of variable (columns)... $i$ is vertical down, $j$ is horizontal right. Refer to a Linear Algebra book to clarify this issue for you. Oct 3, 2020 at 16:30
• @Sm1 row index-i of the matrix A(i,j) mathces with vertical y-axis (or n2-axis with sequences) but points vertical-down from the top row. Column index-j of the matrix A(i,j) matches with the horizontal x-axis (or n1-axis) and grows to right on the top row. When you map A(i,j) without flipping i axis, then yes the graph will show vertically flipped. Al have already shown its graph on the MAtlab code at the Flip-Down MAping Mode section... Have a look at it ? Oct 3, 2020 at 17:29
• @Sm1 I know tha my answer is actually longer than necessary. Let me add a summary. Oct 3, 2020 at 17:53

This isn't universally defined. As everywhere else, coordinate axes are convention. You can have row- and column-major images.

In most modern programming languages, row-major is slightly more common, but it's really not like this is a fixed convention throughout the image processing literature.

• Thank you for your answer, but it is not clear to me. Could you please elaborate a bit more whether the rows become the Y axis and the column becomes the X axis in image based on the question under point (1).
– Sm1
Oct 2, 2020 at 19:48
• Whenever I say it. It's just something you need to define if you care about it. Oct 4, 2020 at 12:28
• What do column/row major images mean? Nov 4, 2020 at 19:03
• @OlliNiemitalo row major is if your storage order is "first row, first column; first row, second; … ; first row, last column; second row, first column", i.e. your image rows are "contiguous" in memory. Nov 4, 2020 at 19:06
• en.wikipedia.org/wiki/Row-_and_column-major_order Nov 4, 2020 at 19:07