Friday, October 23, 2009

Object Detection using opencv III - Training an svm for the extracted hog features

This is a follow up post to an earlier post on calculation of hog feature vectors for object detection using opencv. Here I describe how a support vector machine (svm) can be trained for a dataset containing positive and negative examples of the object to detected. The code has been commented for easier understanding of how it works :



/*This function takes in a the path and names of
64x128 pixel images, the size of the cell to be
used for calculation of hog features(which should
be 8x8 pixels, some modifications will have to be 
done in the code for a different cell size, which
could be easily done once the reader understands
how the code works), a default block size of 2x2
cells has been considered and the window size
parameter should be 64x128 pixels (appropriate
modifications can be easily done for other say
64x80 pixel window size). All the training images
are expected to be stored at the same location and
the names of all the images are expected to be in
sequential order like a1.jpg, a2.jpg, a3.jpg ..
and so on or a(1).jpg, a(2).jpg, a(3).jpg ... The
explanation of all the parameters below will make
clear the usage of the function. The synopsis of
the function is as follows :

prefix : it should be the path of the images, along
with the prefix in the image name for
example if the present working directory is
/home/saurabh/hog/ and the images are in
/home/saurabh/hog/images/positive/ and are
named like pos1.jpg, pos2.jpg, pos3.jpg ....,
then the prefix parameter would be
"images/positive/pos" or if the images are
named like pos(1).jpg, pos(2).jpg,
pos(3).jpg ... instead, the prefix parameter
would be "images/positive/pos("

suffix : it is the part of the name of the image
files after the number for example for the
above examples it would be ".jpg" or ").jpg"

cell   : it should be CvSize(8,8), appropriate changes
need to be made for other cell sizes

window : it should be CvSize(64,128), appropriate
changes need to be made for other window sizes

number_samples : it should be equal to the number of
training images, for example if the
training images are pos1.jpg, pos2.jpg
..... pos1216.jpg, then it should be
1216

start_index : it should be the start index of the images'
names for example for the above case it
should be 1 or if the images were named
like pos1000.jpg, pos1001.jpg, pos1002.jpg
.... pos2216.jpg, then it should be 1000

end_index : it should be the end index of the images'
name for example for the above cases it
should be 1216 or 2216

savexml   : if you want to store the extracted features,
then you can pass to it the name of an xml
file to which they should be saved

normalization : the normalization scheme to be used for
computing the hog features, any of the
opencv schemes could be passed or -1
could be passed if no normalization is
to be done */

CvMat* train_64x128(char *prefix, char *suffix, CvSize cell,
CvSize window, int number_samples, int start_index,
int end_index, char *savexml = NULL, int canny = 0,
int block = 1, int normalization = 4) 
{

char filename[50] = "\0", number[8];
int prefix_length;
prefix_length = strlen(prefix);
int bins = 9;

/* A default block size of 2x2 cells is considered */

int block_width = 2, block_height = 2;

/* Calculation of the length of a feature vector for
an image (64x128 pixels)*/

int feature_vector_length;
feature_vector_length = (((window.width -
cell.width * block_width)/ cell.width) + 1) *
(((window.height - cell.height * block_height)
/ cell.height) + 1) * 36;

/* Matrix to store the feature vectors for
all(number_samples) the training samples */

CvMat* training = cvCreateMat(number_samples,
feature_vector_length, CV_32FC1);

CvMat row;
CvMat* img_feature_vector;
IplImage** integrals;
int i = 0, j = 0;

printf("Beginning to extract HoG features from
positive images\n");

strcat(filename, prefix);

/* Loop to calculate hog features for each
image one by one */

for (i = start_index; i <= end_index; i++) 
{
cvtInt(number, i);
strcat(filename, number);
strcat(filename, suffix);
IplImage* img = cvLoadImage(filename);

/* Calculation of the integral histogram for
fast calculation of hog features*/

integrals = calculateIntegralHOG(img);
cvGetRow(training, &row, j);
img_feature_vector
= calculateHOG_window(integrals, cvRect(0, 0,
window.width, window.height), normalization);
cvCopy(img_feature_vector, &row);
j++;
printf("%s\n", filename);
filename[prefix_length] = '\0';
for (int k = 0; k < 9; k++) 
{
cvReleaseImage(&integrals[k]);
}
}
if (savexml != NULL) 
{
cvSave(savexml, training);
}

return training;
}

/* This function is almost the same as
train_64x128(...), except the fact that it can
take as input images of bigger sizes and
generate multiple samples out of a single
image.

It takes 2 more parameters than
train_64x128(...), horizontal_scans and
vertical_scans to determine how many samples
are to be generated from the image. It
generates horizontal_scans x vertical_scans
number of samples. The meaning of rest of the
parameters is same.

For example for a window size of
64x128 pixels, if a 320x240 pixel image is
given input with horizontal_scans = 5 and
vertical scans = 2, then it will generate to
samples by considering windows in the image
with (x,y,width,height) as (0,0,64,128),
(64,0,64,128), (128,0,64,128), .....,
(0,112,64,128), (64,112,64,128) .....
(256,112,64,128)

The function takes non-overlapping windows
from the image except the last row and last
column, which could overlap with the second
last row or second last column. So the values
of horizontal_scans and vertical_scans passed
should be such that it is possible to perform
that many scans in a non-overlapping fashion
on the given image. For example horizontal_scans
= 5 and vertical_scans = 3 cannot be passed for
a 320x240 pixel image as that many vertical scans
are not possible for an image of height 240
pixels and window of height 128 pixels. */

CvMat* train_large(char *prefix, char *suffix,
CvSize cell, CvSize window, int number_images,
int horizontal_scans, int vertical_scans,
int start_index, int end_index,
char *savexml = NULL, int normalization = 4)
{
char filename[50] = "\0", number[8];
int prefix_length;
prefix_length = strlen(prefix);
int bins = 9;

/* A default block size of 2x2 cells is considered */

int block_width = 2, block_height = 2;

/* Calculation of the length of a feature vector for
an image (64x128 pixels)*/

int feature_vector_length;
feature_vector_length = (((window.width -
cell.width * block_width) / cell.width) + 1) *
(((window.height - cell.height * block_height)
/ cell.height) + 1) * 36;

/* Matrix to store the feature vectors for
all(number_samples) the training samples */

CvMat* training = cvCreateMat(number_images
* horizontal_scans * vertical_scans,
feature_vector_length, CV_32FC1);

CvMat row;
CvMat* img_feature_vector;
IplImage** integrals;
int i = 0, j = 0;
strcat(filename, prefix);

printf("Beginning to extract HoG features
from negative images\n");

/* Loop to calculate hog features for each
image one by one */

for (i = start_index; i <= end_index; i++) 
{
cvtInt(number, i);
strcat(filename, number);
strcat(filename, suffix);
IplImage* img = cvLoadImage(filename);
integrals = calculateIntegralHOG(img);
for (int l = 0; l < vertical_scans - 1; l++)
{
for (int k = 0; k < horizontal_scans - 1; k++)
{
cvGetRow(training, &row, j);
img_feature_vector = calculateHOG_window(
integrals, cvRect(window.width * k,
window.height * l, window.width,
window.height), normalization);

cvCopy(img_feature_vector, &row);
j++;
}

cvGetRow(training, &row, j);

img_feature_vector = calculateHOG_window(
integrals, cvRect(img->width - window.width,
window.height * l, window.width,
window.height), normalization);

cvCopy(img_feature_vector, &row);
j++;
}

for (int k = 0; k < horizontal_scans - 1; k++)
{
cvGetRow(training, &row, j);

img_feature_vector = calculateHOG_window(
integrals, cvRect(window.width * k,
img->height - window.height, window.width,
window.height), normalization);

cvCopy(img_feature_vector, &row);
j++;
}
cvGetRow(training, &row, j);

img_feature_vector = calculateHOG_window(integrals,
cvRect(img->width - window.width, img->height -
window.height, window.width, window.height),
normalization);

cvCopy(img_feature_vector, &row);
j++;

printf("%s\n", filename);
filename[prefix_length] = '\0';
for (int k = 0; k < 9; k++)
{
cvReleaseImage(&integrals[k]);
}

cvReleaseImage(&img);

}

printf("%d negative samples created \n",
training->rows);

if (savexml != NULL)
{
cvSave(savexml, training);
printf("Negative samples saved as %s\n",
savexml);
}

return training;

}


/* This function trains a linear support vector
machine for object classification. The synopsis is
as follows :

pos_mat : pointer to CvMat containing hog feature
vectors for positive samples. This may be
NULL if the feature vectors are to be read
from an xml file

neg_mat : pointer to CvMat containing hog feature
vectors for negative samples. This may be
NULL if the feature vectors are to be read
from an xml file

savexml : The name of the xml file to which the learnt
svm model should be saved

pos_file: The name of the xml file from which feature
vectors for positive samples are to be read.
It may be NULL if feature vectors are passed
as pos_mat

neg_file: The name of the xml file from which feature
vectors for negative samples are to be read.
It may be NULL if feature vectors are passed
as neg_mat*/


void trainSVM(CvMat* pos_mat, CvMat* neg_mat, char *savexml,
char *pos_file = NULL, char *neg_file = NULL) 
{


/* Read the feature vectors for positive samples */
if (pos_file != NULL) 
{
printf("positive loading...\n");
pos_mat = (CvMat*) cvLoad(pos_file);
printf("positive loaded\n");
}

/* Read the feature vectors for negative samples */
if (neg_file != NULL)
{
neg_mat = (CvMat*) cvLoad(neg_file);
printf("negative loaded\n");
}

int n_positive, n_negative;
n_positive = pos_mat->rows;
n_negative = neg_mat->rows;
int feature_vector_length = pos_mat->cols;
int total_samples;
total_samples = n_positive + n_negative;

CvMat* trainData = cvCreateMat(total_samples,
feature_vector_length, CV_32FC1);

CvMat* trainClasses = cvCreateMat(total_samples,
1, CV_32FC1 );

CvMat trainData1, trainData2, trainClasses1,
trainClasses2;

printf("Number of positive Samples : %d\n",
pos_mat->rows);

/*Copy the positive feature vectors to training
data*/

cvGetRows(trainData, &trainData1, 0, n_positive);
cvCopy(pos_mat, &trainData1);
cvReleaseMat(&pos_mat);

/*Copy the negative feature vectors to training
data*/

cvGetRows(trainData, &trainData2, n_positive,
total_samples);

cvCopy(neg_mat, &trainData2);
cvReleaseMat(&neg_mat);

printf("Number of negative Samples : %d\n",
trainData2.rows);

/*Form the training classes for positive and
negative samples. Positive samples belong to class
1 and negative samples belong to class 2 */

cvGetRows(trainClasses, &trainClasses1, 0, n_positive);
cvSet(&trainClasses1, cvScalar(1));

cvGetRows(trainClasses, &trainClasses2, n_positive,
total_samples);

cvSet(&trainClasses2, cvScalar(2));


/* Train a linear support vector machine to learn from
the training data. The parameters may played and
experimented with to see their effects*/

CvSVM svm(trainData, trainClasses, 0, 0,
CvSVMParams(CvSVM::C_SVC, CvSVM::LINEAR, 0, 0, 0, 2,
0, 0, 0, cvTermCriteria(CV_TERMCRIT_EPS,0, 0.01)));

printf("SVM Training Complete!!\n");

/*Save the learnt model*/

if (savexml != NULL) {
svm.save(savexml);
}
cvReleaseMat(&trainClasses);
cvReleaseMat(&trainData);

}




I hope the comments were helpful to understand and use the code. To see how a large collection of files can be renamed to a sequential order which is required by this implementation refer here. Another way to read in the images of dataset could be to store the paths of all files in a text file and parse then parse the text file. I will follow up this post soon, describing how the learnt model can be used for actual detection of an object in an image.

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.

Friday, October 16, 2009

Radix 2 floating point FFT Implementation

I searched for fft code over the internet but couldn't find a simple code for the FFT. So I am posting the code. I think I have explained everything in the code code is what follows:

/*
The program follows is for the FFT of a one dimensional input
fft algorithm used is radix 2
so works only for input lengths powers of 2
input is complex and output obviously is complex
changes can be made to use the function for your requirement.
I have tried to comment the code anyway there shouldnt be any
problem its such a small program.
*/
//Header Files
#include <stdio.h>
#include<stdlib.h>
#include<math.h>

//Complex number structure
typedef struct complex
{
float real;            //Real part of the number
float imaginary;    //Imaginary part of the number
} complex;

//Function to calculate the FFT Method used is Radix 2
complex* fft(complex *in,int N){
//Local variables
complex *X,*Xg,*Xh;
complex *xe,*xo;
int i=0;
//Memory allocation
xe = (complex*)malloc(sizeof(complex)*(N/2));
xo = (complex*)malloc(sizeof(complex)*(N/2));
X = (complex *)malloc(sizeof(complex)*N);
//every recursion has to stop
//so here is the stopping condition of the recursion
if(N==2){
X[0].real        = in[0].real + in[1].real;
X[0].imaginary    = in[0].imaginary + in[1].imaginary;
X[1].real        = in[0].real - in[1].real;
X[1].imaginary    = in[0].imaginary - in[1].imaginary;
return X;
}
//Breaking the input for recursion
for (i=0;i<N;i++){
if(i%2==0){//even indexes
xe[i/2].real        = in[i].real;
xe[i/2].imaginary    = in[i].imaginary;
}
else{//odd indexes
xo[(i-1)/2].real        = in[i].real;
xo[(i-1)/2].imaginary    = in[i].imaginary;
}
}
//recursive calls
Xg = fft(xe,N/2);
Xh = fft(xo,N/2);
//Combining the two halfs here is where magic happens
for (i=0;i<N;i++){
X[i].real=Xg[i%(N/2)].real + Xh[i%(N/2)].real*cos(2*3.14*i/N)+Xh[i%(N/2)].imaginary*sin(2*3.14*i/N);
X[i].imaginary=Xg[i%(N/2)].imaginary+Xh[i%(N/2)].imaginary*cos(2*3.14*i/N)-Xh[i%(N/2)].real*sin(2*3.14*i/N);
}
//deallocating the waste memory
free(Xg);
free(Xh);
return X;//returning the result
}
//the main function
int main(int argc,char *argv[])
{
int N,i;
complex *x;//input
complex *X;//variable to store FFT of the input
//Note : N has to be a power of 2 otherwise the function doesnt work
//asking for the no of points in the FFT
printf("\nenter N must be a power of 2:");
scanf("%d",&N);
//allocating memory for the input
x = (complex*)malloc(N*sizeof(complex));
//taking the input
for(i=0;i<N;i++){
printf("\nenter %dth element real then imaginary",i);
scanf("%f",&(x[i].real));  
scanf("%f",&(x[i].imaginary));
}
//calling the fft function
X = fft(x,N);
//displaying the result
printf("\nFFT of the input is as follows");
for(i=0;i<N;i++){
printf("\nXr[%d] = %.2f + j %.2f",i,X[i].real,X[i].imaginary);
}
return 0;
}
//end of the program

Monday, October 12, 2009

Virtual Box - Shared Folders

The VirtualBox Guest Additions are software packages which can be installed inside of supported guest systems to improve their performance and to provide additional integration and communication with the host system.

After installing the Guest Additions, a virtual machine will support automatic adjustment of video resolutions, seamless windows and more.

In particular, Guest Additions provide for “shared folders”, which let you access files from the host system from within a guest machine.

This is how shared folders can be setup on a windows (xp) or linux (ubuntu) guest operating system :

1. Open up the virtual box interface by clicking on the virtual box shortcut or from the start menu.
2. In the left pane which contains the list of installed guest operating systems, select the system with which you want to share folders from the host operating system.
3. In the right pane, click on shared folders and select the folder you want to share. After this the number of shared folders which appears just beneath the shared folders option will increment by 1.
4. Now boot the virtual system.

For windows:

5. Open My computer and right click on network places in the right pane and select map network drive.
6. Choose a drive letter and to select the folder click on the browse... button. In the browse dialog box, expand the VirtualBox Shared Folders option and you'll find the folder you had shared there. Select it and click Ok.
7.In the Map Network Drive dialog box, check the Reconnect at logon checkbox if not already so and click on finish.
8. Now your shared folder will appear in the Network drives everytime you boot your virtual system. You can access the virtual drives just like normal drives from My Computer.

For linux:


5. To make folders mount everytime an ubuntu guest system is booted , you will have to edit the file with the filesystem static information, /etc/fstab.
6. To edit it open up a terminal and give the following commands :

cp /etc/fstab ~/fstab.backup
sudo nano /etc/fstab

6.Append at the end of the file

{name of shared folder} {path of the mount point}
(which will be something like /home/{user}/Desktop/downloads) vboxsf rw 0 2

7.To save press CTRL+O, then press CTRL+X to exit.
8.Restart the guest system and the shared folders will be mounted at the respective mount points.

Monday, August 24, 2009

Make Image of a drive

You can make image of you drive by using a windows tool ImageX all you have to do is open command prompt from vista CD , click here to know how to open command prompt from vista cd, and go to the path of imagex.exe file and then type in the following command ( I am assuming you want to make image of c: drive and want to store at d:\image\vista.wim you can change as per your requirements)
wait before I tell you the command make points of the following
1) you should clean the drive as much as possible otherwise it will take a long time. typically for more than 15 GB it will take really long.
2) if you want to make image of a drive which is not your OS drive then you can make image from the OS itself no need to boot from vista CD.
and the command is

imagex /capture c: d\image\vista.wim "d drive"

you can type in imagex /capture /? for help on this command.

Now this image can come in great use, you can anytime revert your system at the point you made the image by simply applying the following command

imagex /apply d\image\vista.wim 1 c:

and you will have all your software and drivers installed already, no more headache of installing all software and drivers.

Saturday, August 8, 2009

Object Detection Using opencv I - Integral Histogram for fast Calculation of HOG Features

Histograms of Oriented Gradients or HOG features in combination with a support vector machine have been successfully used for object Detection (most popularly pedestrian detection).
An Integral Histogram representation can be used for fast calculation of Histograms of Oriented Gradients over arbitrary rectangular regions of the image. The idea of an integral histogram is analogous to that of an integral image, used by viola and jones for fast calculation of haar features for face detection. Mathematically,



where b represents the bin number of the histogram. This way the calculation of hog over any arbitrary rectangle in the image requires just 4*bins number of array references. For more details on integral histogram representation, please refer,

Integral Histogram

The following demonstrates how such integral histogram can be calculated from an image and used for the calculation of hog features using the opencv computer vision library :

/*Function to calculate the integral histogram*/

IplImage** calculateIntegralHOG(IplImage* in)

{

/*Convert the input image to grayscale*/

IplImage* img_gray = cvCreateImage(cvGetSize(in), IPL_DEPTH_8U,1);
cvCvtColor(in, img_gray, CV_BGR2GRAY);
cvEqualizeHist(img_gray,img_gray);

/* Calculate the derivates of the grayscale image in the x and y directions using a sobel operator and obtain 2 gradient images for the x and y directions*/

IplImage *xsobel, *ysobel;
xsobel = doSobel(img_gray, 1, 0, 3);
ysobel = doSobel(img_gray, 0, 1, 3);
cvReleaseImage(&img_gray);


/* Create an array of 9 images (9 because I assume bin size 20 degrees and unsigned gradient ( 180/20 = 9), one for each bin which will have zeroes for all pixels, except for the pixels in the original image for which the gradient values correspond to the particular bin. These will be referred to as bin images. These bin images will be then used to calculate the integral histogram, which will quicken the calculation of HOG descriptors */

IplImage** bins = (IplImage**) malloc(9 * sizeof(IplImage*));
for (int i = 0; i < 9 ; i++) {
bins[i] = cvCreateImage(cvGetSize(in), IPL_DEPTH_32F,1);
cvSetZero(bins[i]);
}


/* Create an array of 9 images ( note the dimensions of the image, the cvIntegral() function requires the size to be that), to store the integral images calculated from the above bin images. These 9 integral images together constitute the integral histogram */

IplImage** integrals = (IplImage**) malloc(9 * sizeof(IplImage*)); for (int i = 0; i < 9 ; i++) {
integrals[i] = cvCreateImage(cvSize(in->width + 1, in->height + 1),
IPL_DEPTH_64F,1);
}

/* Calculate the bin images. The magnitude and orientation of the gradient at each pixel is calculated using the xsobel and ysobel images.{Magnitude = sqrt(sq(xsobel) + sq(ysobel) ), gradient = itan (ysobel/xsobel) }. Then according to the orientation of the gradient, the value of the corresponding pixel in the corresponding image is set */


int x, y;
float temp_gradient, temp_magnitude;
for (y = 0; y <in->height; y++) {

/* ptr1 and ptr2 point to beginning of the current row in the xsobel and ysobel images respectively. ptrs[i] point to the beginning of the current rows in the bin images */

float* ptr1 = (float*) (xsobel->imageData + y * (xsobel->widthStep));
float* ptr2 = (float*) (ysobel->imageData + y * (ysobel->widthStep));
float** ptrs = (float**) malloc(9 * sizeof(float*));
for (int i = 0; i < 9 ;i++){
ptrs[i] = (float*) (bins[i]->imageData + y * (bins[i]->widthStep));
}

/*For every pixel in a row gradient orientation and magnitude are calculated and corresponding values set for the bin images. */

for (x = 0; x <in->width; x++) {

/* if the xsobel derivative is zero for a pixel, a small value is added to it, to avoid division by zero. atan returns values in radians, which on being converted to degrees, correspond to values between -90 and 90 degrees. 90 is added to each orientation, to shift the orientation values range from {-90-90} to {0-180}. This is just a matter of convention. {-90-90} values can also be used for the calculation. */

if (ptr1[x] == 0){
temp_gradient = ((atan(ptr2[x] / (ptr1[x] + 0.00001))) * (180/ PI)) + 90;
}
else{
temp_gradient = ((atan(ptr2[x] / ptr1[x])) * (180 / PI)) + 90;
}
temp_magnitude = sqrt((ptr1[x] * ptr1[x]) + (ptr2[x] * ptr2[x]));

/*The bin image is selected according to the gradient values. The corresponding pixel value is made equal to the gradient magnitude at that pixel in the corresponding bin image */

if (temp_gradient <= 20) {

ptrs[0][x] = temp_magnitude;
}
else if (temp_gradient <= 40) {
ptrs[1][x] = temp_magnitude;
}
else if (temp_gradient <= 60) {

ptrs[2][x] = temp_magnitude;
}
else if (temp_gradient <= 80) {

ptrs[3][x] = temp_magnitude;
}
else if (temp_gradient <= 100) {

ptrs[4][x] = temp_magnitude;
}
else if (temp_gradient <= 120) {

ptrs[5][x] = temp_magnitude;
}
else if (temp_gradient <= 140) {

ptrs[6][x] = temp_magnitude;
}
else if (temp_gradient <= 160) {

ptrs[7][x] = temp_magnitude;
}
else {

ptrs[8][x] = temp_magnitude;
}
}
}

cvReleaseImage(&xsobel);
cvReleaseImage(&ysobel);

/*Integral images for each of the bin images are calculated*/

for (int i = 0; i <9 ; i++){
cvIntegral(bins[i], integrals[i]);
}

for (int i = 0; i <9 ; i++){
cvReleaseImage(&bins[i]);
}

/*The function returns an array of 9 images which consitute the integral histogram*/

return (integrals);

}


The following demonstrates how the integral histogram calculated using the above function can be used to calculate the histogram of oriented gradients for any rectangular region in the image:

/* The following function takes as input the rectangular cell for which the histogram of oriented gradients has to be calculated, a matrix hog_cell of dimensions 1x9 to store the bin values for the histogram, the integral histogram, and the normalization scheme to be used. No normalization is done if normalization = -1 */


void calculateHOG_rect(CvRect cell, CvMat* hog_cell,
IplImage** integrals, int normalization) {


/* Calculate the bin values for each of the bin of the histogram one by one */

for (int i = 0; i < 9 ; i++){

float a =((double*)(integrals[i]->imageData + (cell.y)
* (integrals[i]->widthStep)))[cell.x];

float b = ((double*) (integrals[i]->imageData + (cell.y + cell.height)
* (integrals[i]->widthStep)))[cell.x + cell.width];
float c = ((double*) (integrals[i]->imageData + (cell.y)
* (integrals[i]->widthStep)))[cell.x + cell.width];
float d = ((double*) (integrals[i]->imageData + (cell.y + cell.height)
* (integrals[i]->widthStep)))[cell.x];

((float*) hog_cell->data.fl)[i] = (a + b) - (c + d);

}


/*Normalize the matrix*/
if (normalization != -1){

cvNormalize(hog_cell, hog_cell, 1, 0, normalization);
}

}



I will describe how the HOG features for pedestrian detection can be obtained using the above framework and how an svm can be trained for such features for pedestrian detection in a later post.





Thursday, June 18, 2009

Multi-threading in Java

Multi threading is really a nice feature a programming language can have. In multi threading you can make new threads in your program while it is running, which means program will be running in more than one thread simultaneously(parallel). It can be a very useful feature in some server client system. In java you can use multi threading in the following way.

points
1) to be able to run a class in a thread the class need to either extend Thread class or should implement Runnable.

2) The second requirement to be able to run a class into a thread is to have a method named run(), this method will contain what to run in the thread.


public ClassName extends Thread{
ClassName(){
}
run(){
}
}

public ClassName implements Runnable{
ClassName(){
}
run(){
}
}

3) Syntax to start the thread in some other class

ClassName name = new ClassName();
Thread t = newThread(name);
t.start();

How to rename a large number of files at once in windows?

If you have a large number of files with random names ( lets say images from different sources with random names) and want to rename them all systematically for some reason.
On Microsoft windows there a simple trick to do this.

Select all the files.
Right click on any file and select rename ( or press F2 for rename)
Then give a name like img(1).jpg
And you are done. This will rename all the selected files as img(1).jpg, img(2).jpg, img(3).jpg .......

Please note that this will work for all kinds of files and extensions, not just images ( I took jpgs just for an example to illustrate)

Saturday, June 6, 2009

Amarok internet access behind proxy server

If you use amarok behind a proxy server and it cannot access internet (for example to fetch lyrics using a lyrics script, or to submit data to last.fm or any other task which requires it access the internet). The reason for this is that amarok does not read the system wide proxy settings from GNOME. To solve this problem you need to edit the following file :

$HOME/.kde/share/config/kioslaverc

and add the following entries to it :


[Proxy Settings][$i]
ProxyType=1
httpProxy=http://username:password@proxyserver:port/
httpsProxy=http://username:password@proxyserver:port/
ftpProxy=http://username:password@proxyserver:port/

Save the file and Restart amarok.

Wednesday, June 3, 2009

OpenCV shared libraries error

If you installed opencv on your linux machine and are able to compile opencv programs but cannot run them and are receiving the following error :

error while loading shared libraries: libcxcore.so.2: cannot open shared object file: No such file or directory

This means that the program is not able to locate the shared libraries it needs to execute. To sort this out you to have set the LD_LIBRARY_PATH environment variable to the path of your opencv lib directory. This directory should be located in your opencv installation directory(if you installed opencv using the tar rather than using your linux distribution repositories). You can do this by the following command (assuming you have installed opencv in your user directory in a folder named "opencv" :

export LD_LIBRARY_PATH=/home/{username}/opencv/lib/

If you are using the eclipse IDE and getting the same error then to fix it you need to open the Run configurations dialogue box which is located in the run menu, select the environment tab and add a new variable named LD_LIBRARY_PATH with value as described above.

Sunday, May 24, 2009

Change default path of command prompt in windows

I wont suggest you to change the default path, But I will give you smart solution to do it. only thing you will have to do is to run a simple command and it will go to the location where you wanted to go. Do the following to accomplish this

open the default path in windows explorer
open notepad and write cd
save this file named somename.bat and as all files (NOT TXT).
then whenever you open command prompt write this somename you given and you will be at your desired location.

Friday, May 8, 2009

Gmail voice and video chat installation problem

if you have problem in installing gmail voice and video chat program it stucks in detecting devices... then you just start "run" in windows OS and type in "%USERPROFILE%\Local Settings\Application Data\Google\Google Talk Plugin" press enter you will see a window opens up. In this window just delete the file "googletalkplugin_port" and re open gmail.

Sunday, March 22, 2009

How to restore dell factory image when PC restore does not work?

Here I will give you instructions as to how you can reformat your dell laptop hard disk and restore the factory image ( the hard disk contents with which your laptop came when you bought it). You will need your vista dvd for this.

I will be assuming that C: is your Operating system partition and D: is your recovery partition.

1. Boot the computer using your vista dvd.
2. Select Repair. Then select command line.
3. Goto your recovery drive and then to the tools folder by typing following on the command prompt :

d:
cd tools

4. Then type this on the command prompt :

imagex /apply d:\dell\image\factory.wim 1 c:\

5. This will start the recovery process which will complete itself in 10-15 minutes.
6. Type exit on the command prompt.
7. Reboot your computer from your hard disk.

Saturday, March 21, 2009

How to burn videos to play on a dvd player?

Older dvd players cannot play data dvd's , only newer divx compatible players can play them.
So if you wish to play them on such a dvd player, you would have to first convert the files to a dvd format.
There are many softwares which do both the conversion and burning for you. If you are using windows vista, you can use windows dvd maker.
Otherwise Total Video converter is a very good option.
But beware that the conversion process takes a lot of time ( say an hour for a 2 hour video or may be even more)

Wednesday, March 18, 2009

Generate numbers in Gaussian (normal) Distribution in C

C doesn't have direct function to generate numbers which follow Gaussian Distribution. Here is how one an do this. We can use the cumulative distribution function of the Normal Distribution to generate the required. The code below illustrates how to generate a point between -3 to +3 with resolution of .005 and which follows the standard Normal Distribution. The code can accordingly be changed for other normal distribution and other range. step size multiplied by the no of points/2 in CDF array gives the range.



#include "stdio.h" // change accordingly
#include "stdlib.h"
#include "math.h"

double CDF[1202];//gloabal array which contains the CDF of the normal distribution

double generateAWGN(){
double temp;
int i=0,j=0;
temp = CDF[1]+(CDF[1201]-CDF[1])*rand()/RAND_MAX;//uniformly distributed temp
if(temp>.5)
{
for(i=602;i<1202;i++){>=temp){
return (i-601)*.005;
}
}
}
else
{
for(j=600;j>0;j--){
if(CDF[j]<=temp) return (j-601)*.005; } } } int main(int argc, char *argv[]) { int i=0; double step = .005,tmp,avg,pns; double past_tmp = .399, extra,a; CDF[601]=.5;//mean has CDF .5 for(i=1;i<=600;i++) { tmp = .399*step*exp(-((step*i)*(step*i))/2);//value at step*i of pdf extra = (past_tmp-tmp)*step*.5; past_tmp = tmp; CDF[601+i] = CDF[601+i-1]+tmp+extra; CDF[601-i] = CDF[601-i+1]-tmp-extra; } //printf("%f and %f \n",CDF[1201],CDF[1]); printf("the elements which follow gaussina distribution\n"); for(i=0;i<1000;i++) { pns = generateAWGN(); printf("%f\n",pns); avg = pns/1000; } printf("average of the elements = %f",avg); }

Thursday, February 26, 2009

Vista runs very slow

If your windows vista runs very slow I mean it takes time to open even my computer and other folders. then you can do the following things ---

1) make sure that there are less files in the folder if there are many then you pack them in folder.
2) you should have some space free in the hard disk 10% at least.
3) run de-fragmentor as it helps hard disk run faster.
4) one trick to make explorer run faster is that you set the priority of explorer process to high click here to know how to do that.

Friday, February 20, 2009

Firefox starts very slowly

Mozilla firefox takes so much of time to start. here is a easy solution.
1. start firefox
2. start task manager
3. go to process and
4. then to firefox process in the processes tab.
5. right click and set the pririty to high.
6. and then close the firefox and start it again you will see the difference.

only problem is you have to do it every time you start your computer.
you can do this to any other program as well.

Wednesday, February 4, 2009

How to manually install vmware tools in guest windows operating system

If you can not install tools directly go to installation directory of the vmware and find windows.iso file and mount this in guest operating system and install the tools.

Tuesday, February 3, 2009

Change default directory for Google Talk Received Files in vista

If you need to change the default directory of the Google talk received files in vista then Google doesn't help you. There is a trick you can change your default my documents directory.
Follow the steps below-
1) Go to C\users\my username.
2) Then right click on documents folder.
3) Choose Properties.
4) Switch to Location tab.
5) Click on move and choose the other location where you want your gtalk received files to be.
6) Give the answer to the dialog box comes and you have done your job.

Enjoy!!!!


Wednesday, January 28, 2009

Speed Up Firefox

Yes, firefox is already pretty damn fast but did you know that you can tweak it and improve the speed even more?

That's the beauty of this program being open source.
Here's what you do:
In the URL bar, type “about:config” and press enter. This will bring up the configuration “menu” where you can change the parameters of Firefox.

Note that these are what I’ve found to REALLY speed up my Firefox significantly - and these settings seem to be common among everybody else as well. But these settings are optimized for broadband connections - I mean with as much concurrent requests we’re going to open up with pipelining… lol… you’d better have a big connection.

Double Click on the following settins and put in the numbers below - for the true / false booleans - they’ll change when you double click.

Code:
browser.tabs.showSingleWindowModePrefs – true
network.http.max-connections – 48
network.http.max-connections-per-server – 16
network.http.max-persistent-connections-per-proxy – 8
network.http.max-persistent-connections-per-server – 4
network.http.pipelining – true
network.http.pipelining.maxrequests – 100
network.http.proxy.pipelining – true
network.http.request.timeout – 300


One more thing… Right-click somewhere on that screen and add a NEW -> Integer. Name it “nglayout.initialpaint.delay” and set its value to “0”. This value is the amount of time the browser waits before it acts on information it receives. Since you’re broadband - it shouldn’t have to wait.

Now you should notice you’re loading pages MUCH faster now!

Saturday, January 17, 2009

Set which Programs to run on Start Up

If you have vista type msconfig in start search box you will see msconfig.exe open it Or if you have Xp or some other than open Run and type msconfig and enter you will see a new window opened now open StartUp tab and you can enable or disable programs at start up.

Dell TouchPad does not Work Properly

When I bought my laptop the touch pad was working fine. But some days later I came to know that scroll of my touch pad doesn't work. I googled it and found that some program has been disabled form start up. If you have Alps Electric manufactured touch pad then enable the driver of alps at start up. If you need help to open start up programs click here
















Sunday, January 4, 2009

Enable your Registry

All of a sudden you find that you can not edit your registry. Here is the solution-

download this script click here(you may have to confirm the download as well do it as it is a vbscript) and run you will be asked to verify your administrative rights give your password or permission and you can now you have solved your problem go enjoy editing your registry.

Tip- you can save your registry and store it whenever you find that you have some problem with registry then simply restore the saved registry.