Image Processing – An Easy Intro

What I’m using

For anyone ( specially non-programmers ) who want to take a shot at Image processing SimpleCV is the easiest way. It’s easy to use, written for Python and reasonably fast for most applications. I happen to be a GSoC student for the same this year and I’m working to improve it’s display system ( more about it here )

You can visit the Homepage to read up and pre-configured installers.

Or to be extra awesome you can clone the latest source code from here.

Let’s get going

>>> from SimpleCV import *

Start by importing all names in the SimpleCV namespace.

>>> img = Image('lenna') # strings like 'lenna' , 'lyle'
#and 'logo' load default stored images
>>>  # calling show() displays the image 
>>> img = Image('lyle')
>>> img = Image('file.png') # specifying a filename
#will load an image from the file if it's present

The R,G,B colorspace

Images are stored in memory by storing the amount of Red, Green and Blue Colors. Let’s see this in action

>>> img = Image((500,500))

The above code will create a small image 500×500 pixels wide. It’s all black initially :(.

>>> print img[25,37]
(0.0, 0.0, 0.0)
>>> print img[25,37,0]

The red,green and clue color values are stored in this case as an integer between 0 and 255. 0 means that color isn’t present and 255 means the color is present as much as can be.The images can be accessed as matrices . img[x,y] signifies a pixel in the row number x and column number y. Each pixel is associated with 3 values, for R,G and B in that order. As python correctly tells us, all 3 colors are 0. The same is true for all x and y values as of now.

>>> for i in range(100,200):
>>>     for j in range(30,70):
>>>         img[i,j] = 255,0,0


If you run the above code you’ll notice a small, bright red rectangle on the image. The code sets the red value of all pixels in the range (100:200,30:70)

Please note, this in not the correct way to draw shapes, I’m just using for loops for illustrations

>>> for i in range(400,450):
>>>     for j in range(130,170):
>>>         img[i,j] = 128,0,0


This code draws a second rectangle. But this time around the rectangle is a little dull. That’s because the red value is 128.

>>> for i in range(0,50):
>>>     for j in range(200,300):
>>>         img[i,j] = 0,255,0

Wow ! This gives us green
We can have many combinations of colors ( 16581375 to be precise ). The following snippet shows a few examples

from SimpleCV import *

def paintSquare(img,x,y,color):
    draws a square at x,y in img, with the given color of 50px X 50px
    for i in range(x,x+50):
        for j in range(y,y+50):
            img[i,j] = color

img = Image((500,500))

# red
color = (255,0,0)

# green
color = (0,255,0)

color = (0,0,255)

color = (255,255,0)

color = (255,0,255)

color = (255,255,255)

#gray (ish)
color = (100,100,100)

color = (0,255,255)

color = (239,208,207)

The code snippet contains a function paintSquare which paints a 50 x 50 px square at the given position.

Note that all 255 shades of gray(:D) have equal R,G,B values, with (0,0,0) being black and (255,255,255) being white. (120,120,120) will be greyish.

After This

This was a post to illustrate the very basic fundamentals of images. You can go to to learn more things. If you would like to see anything else explained by me , comment below.


2 thoughts on “Image Processing – An Easy Intro

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s