A taste of Image Processing

Image processing involves extracting information from images and using the information so obtained for various operations and tasks. Don’t confuse image processing image processing with image manipulation that involves adjusting the images. Photoshop and similar software are used for image manipulation.

Application areas

  • Medical Applications
  • Industrial Applications
  • Military Applications: Some of the most challenging and performance-critical scenarios for image processing solutions have been developed for military needs, ranging from detection of soldiers or vehicles to missile guidance and object recognition and reconnaissance tasks using unmanned aerial vehicles (UAVs). In addition, military applications often require the use of different imaging sensors, such as range cameras and thermo-graphic forward-looking infrared (FLIR) cameras.
  • Law Enforcement and Security: Surveillance applications have become one of the most intensely researched areas within the video processing community. Biometric techniques (e.g., fingerprint, face, iris, and hand recognition), which have been the subject of image processing research for more than a decade, have recently become commercially available.
  • Consumer Electronics
  • The World Wide Web

So as we can see image processing has wide areas of application.

That being said you need some software or programming language for making this image processing possible. There are various ways by which this can be done. You can use c, NI LabVIEW, MATLAB etc. for image processing. For this post I’ll be using MATLAB. Now we did this just so as to understand the concepts of image processing. So if you feel that its meagre you can build on this.


close all;
clear all;
video = videoinput('winvideo');%Create video variable

set(video,'FramesPerTrigger',1); % Setting frames per trigger
preview(video);%Preview the video
rgb_image = getsnapshot(video); % Storing Image in an array variable
[y x c]= size(rgb_image); % Determining the size of the captured frame.
x1 = (x/2)-(0.2*x);
x2 = (x/2)+(0.2*x);
y1 = (y/2)-(0.25*y);
y2 = (y/2)+(0.25*y);
global s;
s = serial('COM4')
image = getsnapshot(video);
fR = image(:,:,1);
fG = image(:,:,2);
fB = image(:,:,3);
I = fR>200;
for k=1:n
if x1<cbar<x2 &&  rbar<y1
    disp('Move forward');
    global s;
elseif cbar<x1 && y1<rbar<y2
    disp('Move right');
    global s;
elseif cbar>x2 && y1<rbar<y2
    disp('Move left');
    global s;
elseif x1<cbar<x2 && rbar>y2
    disp('Move back');    
    global s;
elseif x1<cbar<x2 && y1<rbar<y2
    disp('Move stop');
    global s;

Now this is the code. I am thinking I’ll just explain the logic and then you can use MATLAB help for the rest. Seriously the help provides is simply awesome. You can understand how the functions word using the help and then there is the world wide net. So I am assuming that those of you who has interest will read further.

Now let’s begin understanding the code.

video = videoinput(‘winvideo’);

This will create a video object from the available cameras. You can check the available ones by using imaqhwinfo


So for the windows winvideo is installed adapter.


Next give winvideo as the agrument to the imaqhwinfo(). Now if you have an external webcam connected to your computer you will se two device ID’s. So now suppose you want to know about the device with ID=1 all you need to do is pass device ID as second argument.


You can see the properties you the webcam.

So just use the videoinput() for creating a variable attached to the particular webcam in MATLAB. In our case video is the variable.

Next you set the frames per trigger i.e. whenever you give the capture commands how many frames will be captured every time.

Then you see a preview of the video feed so that you come to know what exactly is the camera viewing.

Next up you take a sample shot so as to determine the dimensions of the camera.

You will get a 3D matrix of the image. Extract the x and y resolution.

Now for this application what we will do is divide the webcam field into 9 quadrants and take decision depending on position of the image of the object to be detected.

Now since we wanted to learn the basic we used white light such as a torch as the source. If you have a proper webcam and good lighting conditions you can detect normal coloured balls using this program. All you need to do is some thresholding.

Now as usual open the serial port and send a particular character depending upon the quadrant in which the object lies.

So you basically take a snapshot again and again and manipulate that snap. So each image is made of 3 components red, blue and green. Since we are using white light as the object it does not matter which component you choose. But if there is a particular colour that the object has take that particular matrix. Now do the thresholding so that you get only the torch circle on the screen.

Then you remove the noise and coalesce the remaining parts to form a single body.

Then you calculate the centroid and take decision.


This is a sample of what the image will look like after thresholding. So as you can see it lies in the middle quadrant.

We had written the code such that it will transmit w,a,s,d,f depending on the quadrant. Do top middle corresponds to w and so on you can figure that yourself.

On the controller side we manipulate the data to control the bot.

I’ll be uploading the video soon. Thank you for reading. Hope this was useful.


GUI controlled bot

Well in this post I’ll be covering how to create a GUI for controlling a bot using MATLAB. Now this concept can be readily extended to various other stuff. As after learning alphabets one can make words and sentences and essays so can one make various applications using basics. So let’s start, shall we?

To begin with you need to read this post on creating GUI because I’ll be assuming you have read that. So we know how to make buttons now. So this tutorial will focus on using this GUI along with serial class of MATLAB.

Serial Communication in MATLAB

Well we all know man is a social animal. We need someone or the other to communicate with others. We may use words or gestures or expressions etc. Similarly MATLAB needs to communicate with the outside world to get data in order to manipulate the data and send the processed result back. The way to communicate is via RS232 protocol.

There is a serial class in MATLAB that lets you to create objects and manipulate its properties.


obj = serial(‘port’)

Here port is the COM name and obj is the object name. You can change the baud rate and other properties like number of party bits and stop bits etc. I won’t be covering that in this post. If you have further interest you may refer to their website.

Now that we have a object for the serial COM port we need to open that port for serial communication. Now the syntax for the same is as follows.


This will open the COM port for communication. Similarly there is fclose(obj) that will close the port.

Now that we know how to open and close the port might as well learn how to write data to the port.


Here obj is the object created and data is 1 byte data that you want to send/write on the port.

For example :


Well that takes care of the serial part that is required for our task. Remember that internet is a universe of knowledge and you can travel it to unravel new mysteries whenever you like.

Now many of you out there who are programmers are already aware of the concept of global and local variables. We’ll require this concept as well. For creating a global object just type global keyword before the object. Now whenever you want to access the global version of the variable just type global before the object or variable.

Now in the GUI code you need to create a global object, open the port. Then the logic we’ll be using for making this GUI work is as follows:

1) Write code in the button callback function to send a specific character. For example send ‘W’ for forward and so on.

2) Now the controller comes into picture wherein you have to receive the character and take some action depending on the character received.

So basically you need to use the UART module of your controller whether it be msp430 or PIC or 51 or arduino or atmel or and processor for that matter. The logic is simple if you are using embedded c or assembly language you can write a simple ISR that will check the character received serially and send some data to port pins that will drive the motor via the motor driver.

I think this much explanation is enough. If you have any queries you can always mail me or comment, I’ll get back to you as soon as possible.


 * serial_reception.c
 *  Created on: 30-Mar-2014
 *      Author: MANPREET
 *     Website: https://learningmsp430.wordpress.com/

#include <msp430g2553.h>
#include "serial.h"

void main(void)
	WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
	uart_init(); // Refer the UART post for the functions
	P1OUT = 0;
	 IE2 |= UCA0RXIE;     // Enable USCI_A0 RX interrupt
	 __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled

#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
 if(UCA0RXBUF=='W' | UCA0RXBUF == 'w')
	 P1OUT |= BIT3|BIT5;
	 P1OUT &= ~(BIT4|BIT6);// Send 1010 for forward
 if(UCA0RXBUF=='S' | UCA0RXBUF == 's')
	 P1OUT |= BIT4|BIT6;
	 P1OUT &= ~(BIT3|BIT5);// Send 0101 for backward
 if(UCA0RXBUF=='A' | UCA0RXBUF == 'a')
	 P1OUT |= BIT4|BIT5;
	 P1OUT &= ~(BIT3|BIT6);// Send 0110 for left
 if(UCA0RXBUF=='D' | UCA0RXBUF == 'd')
	 P1OUT |= BIT3|BIT6;
	 P1OUT &= ~(BIT4|BIT5);// Send 1001 for right
 if(UCA0RXBUF=='F' | UCA0RXBUF == 'f')
	 P1OUT &= ~(BIT3|BIT6|BIT4|BIT5);// Send 1001 for right

MATLAB Code link:






Note :

The connections are obvious if you read the code. You have to use a driver like L293d of l298. If you face any difficulties doing this feel free to contact me. I’m happy to help.

Line Follower using msp430g2 launchpad

In this post I’ll be writing about line follower bot. This post covers how a light sensor works and how to make your own light sensor. All criticisms are welcome.


A line follower bot is as the name suggests a bot that follows a line. Now this line can be either a dark one on a white surface or a white line on a black surface. So once you switch the bot ON it will keep on following the path that you create using the line.



For this we’ll be needing a light sensor which may be based on visible light or infrared. The concept used is that different colours absorb different wavelengths of light and reflect different wavelengths. Have you ever wondered why a book that appears red to you is that coloured? The physics behind is that a red material will absorb all other colours of white light and reflect the red colour. With that being said it will make sense that black colour absorbs all wavelengths or colours while white reflects all colours. We will use this concept to make our sensors.


We can make the sensor using the following combinations.

  • LED and LDR
  • IR LED and IR diode

What remains common in both the combinations is the use of transistor as a switch. We use transistor as a switch to make the sensor give us digital output i.e. high and low.

The theory on transistor as a switch can be found in the following literatures.

I’ve made one circuit taking reference from Boylestad and the site whose link I’ve posted above. This circuit will give you ~2V when there is no IR light falling on the IR receiver and will give you ~0V when IR light falls on the receiver. So whenever there is any obstacle near the IR led we’ll get logic 0 and whenever there is no obstacle near the IR led we will get logic 1. Note that you can get ready made IR sensors for this application. I’ve given this basic introduction so that those of you who want to know how to make your own sensor get some guidance.



Well with that being taken care let’s develop the logic for our line follower.

Line follower logic and concept

Well I’ll be using a sensor that I got after I attended an internship on embedded c and advanced robotics. This sensor gives logic 0 when there is no IR light reflected to it and logic 1 when there is IR light reflection. To keep things simple our track background colour is white and the path is black. So when the sensor is on white background it’ll reflect light and when it is on the path it wont reflect light. Now lets develop our logic. We’ll need two sensors in order for this line follower to work. When there is a straight path both the sensors will point on white surface thus giving logic 1. So when we have this condition satisfied we’ll send data 1010 to the motor driver port pins.(I’ve connected the motors in such a way that 10 corresponds to forward movement and 01 corresponds to backward movement. This is done so that there is no confusion.) Now lets imagine there is a smooth left turn on the path. The left sensor will go on the black path first while the right sensor will be on the white background. Thus l=0 and r=1.(‘l’ corresponds to the left sensor data and ‘r’ corresponds to right sensor data.) To stay on track we need to make a left turn. This can be done in two ways. First is to make the left wheel stop so that the right wheel rotates and the bot takes a left turn. Second is to make left wheel rotate in backward direction and the right wheel move in forward direction. On similar grounds when there is a smooth right turn the bot needs to turn right. This can be achieved by same two methods just the motor data is interchanged. So this is the concept behind a line follower.

Motor driver

A microcontroller can supply limited current, so in order to drive any heavy load requiring high voltage and current we need to connect a motor driver IC between the microcontroller and the load which in this case is a motor. So I’ll be using the driver IC L239D. This IC has four H bridges that will allow us to control two motors using the same IC. The connections are pretty simple. You just need to see the data sheet for the connections. If you find any difficulty just Google how to use that IC you’ll get required information. And you can comment here posting your doubts and I’ll revert as soon as possible.

Code in Embedded C

 *  line_follower.c
 *  Created on	: 10-Jan-2014 4:19:47 PM
 *  Author	  	: Manpreet Singh Minhas
 *  Website		: https://learningmsp430.wordpress.com/

#include <msp430g2553.h>
// P1IN&BIT0 I've connected the left sensor to P1.0
// P1IN&BIT1 right sensor to P1.1
void main()
	WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer
	P1DIR |= BIT2|BIT3|BIT4|BIT5;// Make P1.2 P1.3 P1.4 P1.5 as output
	P1DIR &= ~BIT0|~BIT1;	// Make P1.0 P1.1 as input
	P1OUT =0;
		if((P1IN&0x03) == 0x03)
			P1OUT |= BIT2|BIT4;
			P1OUT &= ~(BIT3|BIT5);
		if((P1IN&0x03) == 0x02)
			P1OUT |= BIT4;
			P1OUT &= ~(BIT2|BIT3|BIT5);
		if((P1IN&0x03) ==0x01)
			P1OUT |= BIT2;
			P1OUT &= ~(BIT3|BIT5|BIT4 );


Code in Arduino

 *  line_follower.ino
 *  Created on	: 10-Jan-2014 4:19:47 PM
 *  Author	  	: Manpreet Singh Minhas
 *  Website		: https://learningmsp430.wordpress.com/
void setup()


void loop()
  int l = digitalRead(2);
  int r = digitalRead(3);
  if(l==HIGH && r==HIGH)
        digitalWrite(5,LOW); // 12 and 13 for left motor
        digitalWrite(6,HIGH);// 10 and 11 for right motor
        digitalWrite(7,LOW );

    if(l==LOW && r==HIGH)
        digitalWrite(6,LOW);  // 
        digitalWrite(7,LOW );

    if(l==HIGH && r==LOW)
        digitalWrite(7,LOW );

I’ve written the code in both embedded C and arduino. You will energia for burning the ‘.ino’ code.

Circuit Diagram



Please visit http://robokart.com/ for the sensors,chassis and other robotics related parts(for people living in India)