Showing posts with label algorithm. Show all posts
Showing posts with label algorithm. Show all posts

Saturday, July 26, 2014

Reading a bmp image directly using C++: Part 2


As discussed in an earlier post about the header portion of a bmp file and the information it contains.

We can observe that byte 18 and byte 22 respectively contain the height and width of the image which we will be using to read the total information in the image.

Also note that the total size of the header is 54 bytes, which is the offset obtained before we can start getting pixel level data.

Initalise the function as :
unsigned char* ReadBMP(char* filename, int* array)


Notice that we are reading the data into a 1-D array which we can always read as 3D (length, width, and colorscale) while reading as-

pixel at position x,y would be array[width*x+y]

Red value of pixel x,y would be [widht*x+y +0 ]
Blue value of pixel x,y would be [width*x+y +1 ]
Green value of pixel x,y would be [width*x+y +2 ]

To start reading the image, we will first open the file and store it in a variable img as follows-
 
 FILE* img = fopen(filename, "rb");   //read the file
Allocate 54 bytes for the image header and read it using fread-
 
unsigned char header[54];
fread(header, sizeof(unsigned char), 54, img); // read the 54-byte header
 
Obtain the height and width information from the header as discussed earlier-
 
   // extract image height and width from header   
  int width = *(int*)&header[18];       
There is extra padding present generally in BMP files if the image width/row is not a multiple of 4 bytes. Also the actual row size is 3 times the width returned by the header since we have three values each of R, G and B respectively being stored for each pixel. In BMP windows save pixels as B, G, R format instead of the conventional R, G,B order. We will be taking care of this as well.

  int padding=0; while (width*3+padding) % 4!=0 padding++;
We will create a dummy width - widthnew which we will used for reading the file

  int widthnew=width*3+padding;
The final piece of code is to read the pixels one by one through iteration-

 for (i=0; i<height; i++ ) {                                      

fread( data, sizeof(unsigned char), widhthnew, img);

for (int j=0; j<width*3; j+=3)                                  

 { //Convert BGR to RGB and store                         

array[i*width+j+0] = data[j+2];                                

array[i*width+j+1] = data[j+1];                                

array[i* width+j+2] = data[j+0]; }}                          

fclose(img); //close the file   

The complete code has been shared in the nextpost

Saturday, July 12, 2014

Dealing with different python versions


One can install multiple python versions in unix based operating sytems such as Mac and Linux

To work on a specific python version one can simply use the commands - python'version' in the terminals

Examples:

python2.6 mycode.py would run python 2.6
python3 mycode.py would run python version3.*


Similarly for specific library installation using pip on can directly call 

pip2.6 or pip-2.6 (respectively depending on using pip version 1.5 or lower)


The syntax for using easy_install is also similar:

sudo easy_install2.6 package
sudo easy_install package (would install the package for the default python)


Also to change the version of python one can simply create an alias for the command 'python' to the version of choice in the bashrc file

open the bashrc file with your favorite editor and place the following line in the end- 
eg- vim ~/.bashrc

alias "python" "python'version' "

example: alias "python" "python2.6"


If you dont have a bashrc file in your home folder, create one and paste the code


Also never delete your original python in Linux/Mac OS because a lot of the OS software dependencies are linked to python and deleting can simply make your OS stop working


PIP installation - dealing with multiple python versions

Recently, I was facing this problem  of having multiple versions of python installed and pip installs the install library to the wrong python version

The solution to this is very simple:

Suppose you have different python versions, say:
1) Python 2.6
2) Python 2.7
3) Python 3.1

Pip supports the format pip-{version}

So to install a package for say python2.7 one can simply call

pip-2.7 install packagename

Similary for 3.1

pip-3.1 install packagename


Also for one having pip version 1.5 or higher the format would be pip{version}
that is the above commands will become

pip2.7 install packagename
pip3.1 install packagename

^notice that the '-' is missing in the above commands.

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:

figure;

subplot(2,2,1);imshow(aa);
subplot(2,2,2);imshow(bb);
subplot(2,2,3);imshow(cc);
subplot(2,2,4);imshow(dd);


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

subplot(2,1,1);imshow(aa);
subplot(2,1,2);imshow(bb);

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);
imshow(X,map);
figure, imshow(grayim);

Matlab basics-2 : Format conversions in Matlab

Suppose we have an image

aa=imread("image.jpg");

Conversion to grayscale:

we can convert it to gray scale using the following operation

grayaa=rgb2gray(aa);

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.

Thursday, October 22, 2009

Object Detection using opencv II - Calculation of Hog Features

This is follow up post to an earlier post where I have described how an integral histogram can be obtained from an image for fast calculation of hog features. Here I am posting the code for how this integral histogram can be used to calculate the hog feature vectors for an image window. I have commented the code for easier understanding of how it works :



/* This function takes in a block as a rectangle and
calculates the hog features for the block by dividing
it into cells of size cell(the supplied parameter),
calculating the hog features for each cell using the
function calculateHOG_rect(...), concatenating the so
obtained vectors for each cell and then normalizing over
the concatenated vector to obtain the hog features for a
block */ 

void calculateHOG_block(CvRect block, CvMat* hog_block, 
IplImage** integrals,CvSize cell, int normalization) 
{
int cell_start_x, cell_start_y;
CvMat vector_cell;
int startcol = 0;
for (cell_start_y = block.y; cell_start_y <= 
block.y + block.height - cell.height; 
cell_start_y += cell.height) 
{
for (cell_start_x = block.x; cell_start_x <= 
block.x + block.width - cell.width; 
cell_start_x += cell.width) 
{
cvGetCols(hog_block, &vector_cell, startcol, 
startcol + 9);

calculateHOG_rect(cvRect(cell_start_x,
cell_start_y, cell.width, cell.height), 
&vector_cell, integrals, -1);

startcol += 9;
}
}
if (normalization != -1)
cvNormalize(hog_block, hog_block, 1, 0, 
normalization);
}

/* This function takes in a window(64x128 pixels,
but can be easily modified for other window sizes)
and calculates the hog features for the window. It
can be used to calculate the feature vector for a 
64x128 pixel image as well. This window/image is the
training/detection window which is used for training
or on which the final detection is done. The hog
features are computed by dividing the window into
overlapping blocks, calculating the hog vectors for
each block using calculateHOG_block(...) and
concatenating the so obtained vectors to obtain the
hog feature vector for the window*/

CvMat* calculateHOG_window(IplImage** integrals,
CvRect window, int normalization) 
{

/*A cell size of 8x8 pixels is considered and each
block is divided into 2x2 such cells (i.e. the block
is 16x16 pixels). So a 64x128 pixels window would be
divided into 7x15 overlapping blocks*/ 

int block_start_x, block_start_y, cell_width = 8;
int cell_height = 8;
int block_width = 2, block_height = 2;

/* The length of the feature vector for a cell is
9(since no. of bins is 9), for block it would  be
9*(no. of cells in the block) = 9*4 = 36. And the
length of the feature vector for a window would be
36*(no. of blocks in the window */

CvMat* window_feature_vector = cvCreateMat(1,
((((window.width - cell_width * block_width)
/ cell_width) + 1) * (((window.height -
cell_height * block_height) / cell_height)
+ 1)) * 36, CV_32FC1);

CvMat vector_block;
int startcol = 0;
for (block_start_y = window.y; block_start_y
<= window.y + window.height - cell_height
* block_height; block_start_y += cell_height)
{
for (block_start_x = window.x; block_start_x
<= window.x + window.width - cell_width
* block_width; block_start_x += cell_width)
{
cvGetCols(window_feature_vector, &vector_block,
startcol, startcol + 36);

calculateHOG_block(cvRect(block_start_x,
block_start_y, cell_width * block_width, cell_height
* block_height), &vector_block, integrals, cvSize(
cell_width, cell_height), normalization);

startcol += 36;
}
}
return (window_feature_vector);
}




I will very soon post how a support vector machine (svm) can trained using the above functions for an object using a dataset and how the learned model can be used to detect the corresponding object in an image.