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:

1
2
3
4
5
6
7
import cv2.cv as cv

im = cv.LoadImage("img/lena.jpg")

for i in range(im.height):
    for j in range(im.width):
        im[i,j] #Do whatever you want with your pixel

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.

Image pixel iterator
1
2
3
4
5
6
7
8
import cv2.cv as cv

im = cv.LoadImage("img/lena.jpg")

li = cv.InitLineIterator(im, (0, 0), (im.rows, im.cols)) #So loop the entire matrix

for (r, g, b) in li:
    #Do whatever you want with the three channels values

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import cv2.cv as cv

import random

im = cv.LoadImage("img/lena.jpg") #or LoadImage and access pixel with Get2D/Set2D

for k in range(5000): #Create 5000 noisy pixels
    i = random.randint(0,im.height-1)
    j = random.randint(0,im.width-1)
    color = (random.randrange(256),random.randrange(256),random.randrange(256))
    im[i,j] = color

cv.ShowImage("Noize", im)
cv.WaitKey(0)

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import cv2.cv as cv

im = cv.LoadImageM("img/fruits.jpg",cv.CV_32F)

def getDistance(pixel,refcolor):
    return abs( (pixel[0]-refcolor[0]) + (pixel[1]-refcolor[1]) + (pixel[2]-refcolor[2]) )


refcolor = (0,0,0)
minDist = 100

for row in range(im.rows):
    for col in range(im.cols):
        if getDistance(im[row,col], refcolor)<minDist:
            im[row,col] = (255,255,255)

cv.ShowImage("Distance", im)

cv.WaitKey(0)

<<Filters and Arithmetics | Home | Histogram And Backprojection>>