# Chapter 3: Pixel Access and Matrix Iteration

### a. Pixel access

There is multiple ways to access a pixel. They are:

• The better way is to do like for a multi-dimensional list. For an image “im” if you want to access the pixel at the coordinate (3,3) just do: im[3,3]. This basically return a tuple of values. A tuple of 3 values from 0 to 255 for color images.
• Or you can use the OpenCV functions which are Get1D for one dimension image, Get2D for 2 dimension arrays and so on..

### b. Rows and column access

You may also want to access a row or a column even a set of rows or a set of columns. In order to get it you should use the opencv provided functions which are:

• cv.GetCol(im, 0): Return the first column as a 1 dimension array
• cv.GetCols(im, 0, 10): Return the 10 first columns as a matrix
• cv.GetRow(im, 0): Return the first row
• cv.GetRows(im, 0, 10): Return the 10 first rows

### c. Iterate a matrix

The easiest way to iterate through a matrix is to loop it like:

The other way to iterate a matrix is to use a LineIterator that allow to iterate matrix. You should specify the coordinate of the starting point and the coordinate of the ending point. So it will iterate every pixels between the two points.

Note: The two methods are complementary because in the first within the loop you keep the coordinate of the pixel you are working on while in the second you loose this information.

### c. Let’s play with pixels

This useless example shows an example of a program which replaces randomly pixels on the image with a random color. ### d. Distance

The programm under shows the distance technique in action. This method compute the distance of the given pixel given from a given referenced color and allow to isolate it if this is the case. In this example the reference color will be black (0,0,0) and the minimum distance will be 100. So every pixels which have a distance lower than 100 will be replace by another colour. In our case white. This technique allow to isolate a range of color and apply an operation on the matching pixels. 