This is what we will be doing here today:
– Understand a digital image in terms of it’s pixel arrangements and it’s values and get started with MATLAB.
– Loading and displaying images
– Converting a color image to a gray scale image
– Creating negative of an image
I hope you know these beforehand! (If not, be ready to get your hands really really dirty!!)
– Have heard of MATLAB or Octave and are willing to “google” for information.
– A digital image (Actually, any image on your computer!)
We are here to learn the absolute basics of digital image processing. But before that, you need to understand the utility of the technology you are about to learn. Knowingly or unknowingly, all of us use image processing application several times a day. Be it the edited image (more beautified!) of yours on internet, the smile detection algorithm on your camera or the security webcams used for surveillance; image processing is everywhere. And not just this, all the robotics lovers out there need to know that robotics involves a lot of image processing and computer vision. This makes high level tasks such as object detection and recognition, localization and mapping etc really efficient.
The best thing about image processing is the ease of getting started. So in comparison to the other tutorials related to robotics in this blog itself, this one would require the least amount of investment in terms of money. To get started, all you need is a collection of images you want to play with. In addition to this, we are going to manipulate images by writing code. So,this gives rise to another basic requirement which is the computing environment Matlab (seriously amazing but expensive) or octave (almost as amazing as Matlab and Free!!). You can find links in the useful reference section that would help you install octave in case you don’t possess Matlab.
Picture 1: Windows Sample Image
Enough of chit chating!Let’s start image processing. For this tutorial, you can use any picture that you are fond of. I will demonstrate using an image that comes with windows, the beautiful Tulips (picture 1). To begin with, Open Matlab/Octave and identify what is known as the command window (See picture 2). Now, you need to make Matlab read the image that you want to work on. To do so, you can use the command imread(filename). Here, filename referes to the path in your computer where you have saved the image. So, if you are using windows, you can load the Tulip image onto your Matlab/Octave work space by the following command:
a= imread(‘C:UsersPublicPicturesSample PicturesTulips.jpg’);
Picture 2: Matlab Command Window
Here, a is a short and sweet variable which refers to the “not so elegant” command in the right hand side. You have already loaded your image into your workspace. How about having a look at the image just to confirm whether we have achieved our task successfully or not. To view the image we will use the command imshow(variable) as shown below (also see picture 3):
Picture 3: Matlab window
Whoa!! Just two commands and you are already on the go!
Let’s now dig somewhat deeper. You might remember from high school that computer understands the language of zeros and ones. So naturally, everything inside a computer is stored in the form of numbers. For example, even when I am writing English characters, the computer ultimately interprets it in the form of numbers (ASCII and UTF-8 are two popular character encoding schemes which maps unique numbers to characters for computer to understand and store). Then, how are images represented? Images are represented as a matrix of numbers known as pixels. A pixel is the fundamental unit of a digital image. It is the smallest unit of the image that represents a single color (or intensity). So, if we are taking about a gray image, then each pixel is represented by a single number (generally between 0-255) which shows the amount of “gray-ness” that the pixel represents. And, for a colored image, each pixel can be thought of as a group of three different pixels representing green, blue and red color intensities respectively (don’t worry if everything sounds alien, it will soon be clear). Picture 4 gives a visual display of pixels in the tulip image that we are working on.
Picture 4: Pixels zoomed
Now that you have a rough idea of what a pixel is and how the computer stores an image, let’s start working with gray scale images (only because they are easier to work with!). I am not going to ask you to go and find another gray image from somewhere. Instead, let’s create our own grey scale image using Matlab. There is a function in Matlab known as rgb2gray(image) which converts any image that you have already loaded in your Matlab workspace into a gray scale image. So, for the image that we were working on, the command is as follows (see picture 5):
g = rgb2gray(a);
Picture 5: Color to gray conversion
Picture 6: Output gray image
Cool! So let’s now see how pixels look like numerically. I told you that a gray scale image consists of pixels having values from 0-255. So, when a pixel has a value 0, you are void of colors i.e. the pixel represents black. While 255 represents white. Picture 7 gives a chart of the gray intensities and the corresponding pixel value. Through picture 8 you can have a look at a magnified portion of the image where pixels can be easily identified as separate elements.
Picture 7: Chart of gray Intensities
Picture 8: Magnified gray scale image
Do you want to have a look at the numerical representation of the pixels contained in the image you have been working on? If yes, go on and just enter g in the command window (g is the name we just gave to our gray scale image). But do remember, you will be seeing a lot of numbers on your screen depending on the resolution of the image you have been working on. Since our Tulip image is 1024 x 768 pixel in dimension, we will have that many numbers printed our command window.
I totally understand if you did not like what I asked you to do in the previous paragraph. There should rather be a method to display a particular pixel and all the information surrounding it. In Matlab, it is possible just by calling the image with the location of the pixel in terms of row and the column in the image. For example (see picture 9):
g(row number, column number)Picture 9: Displaying pixel at position (1,1)
Here, g was the gray scale image that we were working with. Another useful thing to find out would be the size of an image that we happen to work on. For this, you can simply use the size function of Matlab as shown in picture 10. The output of size function gives the number of rows in the image (768) followed by the number of columns (1024).Picture 10: Size function
Gray images are somewhat boring! So let’s now try to tinker with color images. In grayscale images we have a single intensity plane segmented into rows and columns. In color images, we have 3 such intensity planes for Red, Green and Blue (RGB), each representing a value corresponding to the intensity of red, green and blue respectively as shown in picture 11.Picture 11: RGB Color plane in a color image
So, for a single pixel we have three numeric values (for R, G and B). All other colors are then formed by different combinations of these three primary colors. Now, if we look into the size() of a color image we will find the existence of the three color planes as shown in picture 12.Picture 12: Output of size function for colored image
Through picture 12, we find that the size function putputs three values. the firt two values corresponds to the rows and columns in the image (which are same as that of grayscale image). The third value represents the number of color panes (which is 3 in our image). If we consider rows and columns as first and second dimensions, the three color panes are in the third dimension. Mathematically grayscale images are represented as single 2D matrix and color images as set of three 2D matrices. In computer we store matrices as arrays where grayscale images are stored in 2D arrays and color images are stored in 3D arrays. Here, a is a 3D array. We can access pixel values of a color image as
>> a(row number, column number, color pane index)
where, color pane index of Red is 1, Green is 2 and Blue is 3.
Using the above syntax, let’s try to find out the pixel intensity values for each plane for the pixel at location (1, 1) for our original image which is shown in picture 13. As shown in picture 14, the value at location a(1, 1, 1) gives the red intensity, that at location (1, 1, 2) gives the intensity of green and the value at (1, 1, 3) gives the intensity of blue.Picture 13: Original Color Image Picture 14: Pixel values for colored image
You can see that the value for blue is much higher than the red and green values. It is justified as the pixel at row=1 and column=1 is at top left corner of the image, which looks blue.
Now, as we have learnt the basics of a digital image, let us try to apply it!
Creating a Color Image’s Negative
As defined in Wikipedia, a negative is an image, usually on a strip or sheet of transparent plastic film, in which the lightest areas of the photographed subject appear darkest and the darkest areas appear lightest.
We will try to see what the negative of the Tulip image would look like. We know that the maximum value of intensity is 255 and minimum is zero. In the negative, the lightest areas of the photographed subject appear darkest and the darkest areas appear lightest. Mathematically we can do so by subtracting each pixel values from 255 (the largest possible value).
If a is the color image and n is the image negative, then we can create a negative of an image by
>> n = 255 – a(:,:,:);
The colon (:) is used in place of row number, column number and color pane index. Colon means to consider all values rather than a specific value. Therefore, we considered all rows and all columns and all the color panes. Hence, we subtracted each and every pixel from 255 and stored the result in n. Now using imshow(n),we can view the negative of our image (see picture 15, 16).Picture 15: Creating negative of colored image Picture 16: Negative image
Now let us check the RGB pixel value for the pixel at row =1 and column = 1. On the left of picture 17 is the RGB values of the original image where as on the right is the RGB values of the negative.Picture 17: Comparison of RBG values
Likewise we can create negative of a grayscale image too (see picture 18 and 19).
In the MATLAB figure window (in which image is displayed), we have a tool called – ‘Data Cursor’. We can use it to click and get RGB values as well as row number and column number of a pixel directly from the image.
1. Octave Software download