Sunday, January 30, 2011

Reading a bmp image directly in C++ : Part 1

For this we need to first understand the bmp file format

As illustrated by the figure below:

The bitmap image consists of 3 main parts:

1) Header ( Including the signature and the image info data)

2) Colour Profile

3) Image data

Image source:

The header data is of size 54 bytes. This can be observed by adding up the size requirements of the individual entries as written below. Also mentioned is the offset value for individual entries. An interesting observation is that the total size is not a multiple of 4 which might create several problems as in most compilers memory is read in chunks of 4 bytes.

offset size description
0 &nbsp&nbsp&nbsp 2 &nbsp&nbsp&nbsp&nbsp&nbspsignature, must be 4D42 hex
2 &nbsp&nbsp&nbsp 4 &nbsp&nbsp&nbsp&nbsp&nbsp size of BMP file in bytes (not very reliable)
6 &nbsp&nbsp&nbsp 2 &nbsp&nbsp&nbsp&nbsp&nbsp reserved, must be zero
8 &nbsp&nbsp&nbsp 2 &nbsp&nbsp&nbsp&nbsp&nbsp reserved, must be zero
10 &nbsp&nbsp 4 &nbsp&nbsp&nbsp&nbsp&nbsp offset to start of image data in bytes
14 &nbsp&nbsp 4 &nbsp&nbsp&nbsp&nbsp&nbsp size of BITMAPINFOHEADER structure, must be 40
18 &nbsp&nbsp 4 &nbsp&nbsp&nbsp&nbsp&nbsp image width in pixels
22 &nbsp&nbsp4 &nbsp&nbsp&nbsp&nbsp&nbsp image height in pixels
26 &nbsp&nbsp2 &nbsp&nbsp&nbsp&nbsp&nbsp number of planes in the image, must be 1
28 &nbsp&nbsp2 &nbsp&nbsp&nbsp&nbsp&nbsp number of bits per pixel (1, 4, 8, or 24)
30 &nbsp&nbsp4 &nbsp&nbsp&nbsp&nbsp&nbsp compression type (0=none, 1=Run Length Encoding (RLE)-8, 2=RLE-4)
34 &nbsp&nbsp4 &nbsp&nbsp&nbsp&nbsp&nbsp size of image data in bytes (including padding, to make it a multiple of 4)
38 &nbsp&nbsp4 &nbsp&nbsp&nbsp&nbsp&nbsp horizontal resolution in pixels per meter
42 &nbsp&nbsp4 &nbsp&nbsp&nbsp&nbsp&nbsp vertical resolution in pixels per meter
46 &nbsp&nbsp4 &nbsp&nbsp&nbsp&nbsp&nbsp number of colours in image, or zero
50 &nbsp&nbsp4 &nbsp&nbsp&nbsp&nbsp&nbsp number of important colours, or zero

(To be continued in next post)

Friday, January 21, 2011

Increasing RAM using a USB drive

A disk cache component called ReadyBoost has been introduced by Microsoft in its two latest offerings Windows Vista and also Windows 7.

Although it is best to physical add new RAM to your system, it is possible to improve the performance by using an external hard drive by about 5-10% using flash memory, a USB flash drive, SD card, CompactFlash or any kind of portable flash mass storage system as a cache.

How to enable Ready Boost in your external disk:

1)Go to My Computer
2)Right click on your device
3)Select Properties
4)Click on the ReadyBoost tab
5)You can select various options/space allotment from there.

How to enable/disable Ready Boost in your system:

1)Go to the Control Panel menu option.
2)Click on Performance Information and Tools
3)Click on Advanced Tools in the left hand navigation bar.
4)Click on Configure my Windows ReadyBoost device
5) Select the options you want.

You can even check the performance imporvement by :

1) Go to Control Panel menu option. Change to classic view options

2) Click on the Administrative Tools icon.

3) Click on Reliability and Performance Monitor icon.

Under the monitoring tools category, click on performance monitor

Thursday, January 20, 2011

Matlab-4 Display multiple images, plots in a single window in MATLAB

How to display multiple images or graphs in a single window (frame) in MATLAB

This is done by using the subplot command in MATLAB

Suppose we have to display 4 images (aa,bb,cc,dd) in a single figure and arrange them in 2x2 fashion.

We will use the following commands:



similary to display two image in 2x1 fashion, we will use:


Matlab Image Processing-3 Converting a RGB image to indexed image

We need to use the command rgb2ind to do this.

A conversion like

[X,map] = rgb2ind(aa, n) %% here aa is a rgb color image. n is an integer less than or equal to 65536.

By using this operation we can reduce a RGB having almost 256*256*256 possible colors into an index image X with colormap map having the colors of RGB image mapped to the nearest color values.

Further making changes into the colour map.

Suppose we need to change the total colours into say n.

We can do the following:

[X, map] = imread('image.jpg');
[Y, newmap] = imapprox(X, map, n); % n is the new number of colours (a positive int value)
imshow(Y, newmap);

Converting the Ind Image into Grayscale:

ind2gray removes the hue and saturation information from the input image while retaining the intensity portion(i.e the luminiscence).

grayim = ind2gray(X,map);
figure, imshow(grayim);

Matlab basics-2 : Format conversions in Matlab

Suppose we have an image


Conversion to grayscale:

we can convert it to gray scale using the following operation


the matrix grayaa is the gray scale version of the image aa.

rgb2gray converts RGB values to grayscale values by forming a weighted sum of the R, G, and B components:

0.2989 * R + 0.5870 * G + 0.1140 * B

Converting the image into black and white:

It can be done in various ways depending on the threshold values.

bw1=im2bw(aa); %% Converts the image into bw using a default value

bw2=im2bw(aa,i); %%here i lies between 0 and 1. 'i' having a value of 0.3 means a threshold value of nearly 77 ( 0.3*256).

An important observation here is that the input image aa can be a grayscale image, an intensity image or even a RGB image.

If we give a RGB image as an input, MATLAB converts the RGB to grayscale first and then into black and white by itself.

Matlab Basics 1 : How to read and display image

Reading an image

The simplest way to read an image is to store the image in the current directory (workspace) of Matlab and then use the function imread as follows:

im = imread('aa.bmp'); %% where aa.bmp is the name of the image

%% the image data has been stored in the variable 'im'

Most of the commonly used formats like jpg, bmp, png are accepted by the imread function.

Alternatively if the image is in a different directory one can use the imread function by specifying the total path of the image

for example:

im= imread('C:/Users/Admin/Desktop/aa.jpg'); %% reads from the folder admin

Displaying an image

The command to display images is as follows:

imshow(im); %% will show the image

The problem here would be that the image would replace the current open image (if any).

For this it is better to use :

figure, imshow(im); %% will open the image in a new window

Once can even name the window using int values to use them for various purposes of display and processing post the display. For example :

figure(5), imshow(im);

To operate several graphic commands (or plot commands) one can use the option 'hold'.

Default value of hold is set to the stage off. Enabling it to 'on' state enables it to hold the current plot and all axis properties so that subsequent graphing commands add to the existing graph.

An interesting graphing command :

Suppose you want to find the co ordinates of a particular point in an image using matlab.

you can use the command:

[x, y]=ginput(i); plot(x, y, '+r'); %% where i is to replaced with the figure number in this case, for the above example we used 5

and then click on the point on the respective image.

the co ordinates will be stored in the matrix [x y].

Loading images from a Camera or Video

If you would like to do real time video processing you can even integrate your cameras into OpenCV.

It is easy to load an image from a camera. Ensure that camera is properly connected and detected by your computer.

CvCapture* capture = cvCaptureFromCAM(0); // capture from video device #0
CvCapture* capture = cvCaptureFromAVI("infile.avi");

Incase there is only one camera, or to select any camera from multiple connecterd cameras, 0 can be replaced with -1.

The camera link is stored in the capture structure.

Alternatively, frames can also be processed from a video :

CvCapture* capture = cvCaptureFromAVI("filename.avi");

Retreiving the image frame from the link:

IplImage* img = 0; // Declare a new image pointer

if(!cvGrabFrame(capture)){ // capture a frame
printf("Could not grab a frame\n\7");

img=cvRetrieveFrame(capture); // retrieve the captured frame

Important Caution :

One must release the image source after using it ->

cvReleaseCapture(&capture); // releases the capture link