#include "stdafx.h"
#include "hog.h"
channel::channel()
{
pixels.clear();
}
gradient::gradient()
{
magnitude = orient = rgb_channel = -1;
}
hogOfBlock::hogOfBlock()
{
memset(votes, 0, sizeof(double) * BLOCK_SIZE * BLOCK_SIZE * DIR);
}
void hogOfImage::DrawHistogramImage(CvHistogram *Histogram,IplImage *HistogramImage,int HistogramBins)
{
CvPoint Point1;
CvPoint Point2;
for(int i=0;i<HistogramBins;i++)
{
Point1=cvPoint(i,(int)(cvQueryHistValue_1D(Histogram,i)/20));
Point2=cvPoint(i,0);
cvLine(HistogramImage,Point1,Point2,CV_RGB(127,127,127));
}
}
hogOfImage::hogOfImage()
{
//m_detectPath = path;
out.open("test.txt", ios::out);
}
hogOfImage::~hogOfImage()
{
out.close();
}
int hogOfImage::CountExtAdd(const char* ext)
{
int cnt=0;
BOOL working = m_finder.FindFile(m_detectPath + "\\*"+ext);
while (working){
working = m_finder.FindNextFile();
cnt++;
m_filelists += "|";
m_filelists += m_finder.GetFilePath();
CString filename=m_finder.GetFilePath();
m_file.push_back(filename);
}
return cnt;
}
void hogOfImage::printSilhouette(IplImage *srcImage)
{
double max = -1;
int length = gradients.size();
for (int i = 0; i < length; i++)
if (max < gradients[i].magnitude)
max = gradients[i].magnitude;
IplImage *ptr = cvCreateImage(cvSize(srcImage->width, srcImage->height), srcImage->depth, srcImage->nChannels/3);
for (int i = 0; i < length; i++)
{
double temp = gradients[i].magnitude / max * 255;
int temp_pixel = (int)temp;
if (temp > 127)
temp -= 256;
ptr->imageData[i] = (char)temp_pixel;
}
cvReleaseImage(&ptr);
}
void hogOfImage::gammaCompression(IplImage *srcImage)
{
int length = srcImage->imageSize / 3;
for (int i = 0; i < length; i++)
{
for (int j = 0; j < 3; j++)
channels[j].pixels[i] = sqrt(channels[j].pixels[i]);
}
}
void hogOfImage::calGradients(int height, int width)
{
//ofstream out("test.txt");
gradients.clear();
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
double temp, left, right , top, bottom, angle;
gradient point;
for (int k = 0; k < 3; k++)
{
left = (j == 0 ? -1 : channels[k].pixels[i*width+j-1]);
right = (j == width-1 ? -1 : channels[k].pixels[i*width+j+1]);
top = (i == 0 ? -1 : channels[k].pixels[(i-1)*width+j]);
bottom = (i == height-1 ? -1 : channels[k].pixels[(i+1)*width+j]);
temp = deviation(left, right, top, bottom, &angle);
if (point.magnitude < temp)
{
point.magnitude = temp;
point.orient = angle;
point.rgb_channel = k;
}
}
gradients.push_back(point);
//out << point.magnitude << endl;
}
}
}
double hogOfImage::deviation(double left, double right, double top, double bottom, double* angle)
{
if (left == -1)
{
if (right == -1)
{
*angle = 0;
return 0;
}
else
left = right;
}
else
{
if (right == -1)
right = left;
}
if (top == -1)
{
if (bottom == -1)
{
*angle = 0;
return 0;
}
else
top = bottom;
}
else
{
if (bottom == -1)
bottom = top;
}
double var = sqrt((right - left) * (right - left) + (top - bottom) * (top - bottom));
if (var == 0)
{
*angle = 0;
return 0;
}
double sin_dir = (top - bottom) / var;
double cos_dir = (right - left) / var;
//pedestrain 0~pi
*angle = acos(cos_dir);
return var;
//car 0~2*pi
/*
if (sin_dir > 0)
*angle = acos(cos_dir);
else
*angle = 2*pi = acos(cos_dir);
return var;
*/
}
void hogOfImage::calHogs(int width, int height,IplImage *image)
{
block = 0;
blocks.clear();
//i, j as row, col of the top-left corner of the block
for (int i = 0; i <= height - CELL_SIZE * BLOCK_SIZE; i += CELL_SIZE)
{
for (int j = 0; j <= width - CELL_SIZE * BLOCK_SIZE; j += CELL_SIZE)
{
block++;
hogOfBlock temp;
double total = 0;
for (int a = 0; a < BLOCK_SIZE * CELL_SIZE; a++)
{
vector<gradient>::iterator iii = (gradients.begin() + (i+a)*width + j);
for (int b = 0; b < BLOCK_SIZE * CELL_SIZE; b++)
{
total += (*(iii+b)).magnitude;
}
}
if (total == 0)
total = 0.001;
calBlockHogs(i, j, width, temp.votes, total);
// double sum = 0;
// for (int a = 0; a < 36; a++)
// {
// sum += temp.votes[a];
// }
// if (sum != 0)
// {
// cout <<"block: "<<block<< "sum: " << sum <<endl;
// }
blocks.push_back(temp);
}
}
}
void hogOfImage::calBlockHogs(int row, int col, int width, double *votes, double total)
{
//x~2 row, y~2 col, z~9 orietation
//ofstream out("test.txt");
//memset(votes, 0, 36*sizeof(double));
int index_x, index_y, index_z;
for (int i = 0; i < BLOCK_SIZE * CELL_SIZE; i++)
{
for (index_x = 0; index_x < 2; index_x++)
{
if ((i - posx[index_x]) < 0)
break;
}
vector<gradient>::iterator ii = (gradients.begin() + (row + i) * width + col);
for (int j = 0; j < BLOCK_SIZE * CELL_SIZE; j++)
{
gradient temp = *(ii + j);
for (index_y = 0; index_y < 2; index_y++)
{
if ((j - posy[index_y]) < 0)
break;
}
for (index_z = 0; index_z < 9; index_z++)
{
if ((temp.orient - dirs[index_z]) < 0)
break;
}
/*
cout << "x: " << i << ", index_x: " << index_x << endl;
cout << "y: " << j << ", index_y: " << index_y << endl;
cout << "z: " << temp.orient << ", index_z: " << index_z << endl;
out << "orietation: " << temp.orient << endl;
out << "magnitude: " << temp.magnitude << endl;
*/
if (index_x == 0)
{
if (index_y == 0)
{
if (index_z == 0)
{
votes[0] += temp.magnitude;
}
else if(index_z == 9)
{
votes[8] += temp.magnitude;
}
else
{
votes[index_z-1] += temp.magnitude * (1-(temp.orient - dirs[index_z-1])*9/PI);
votes[index_z] += temp.magnitude * ((temp.orient-dirs[index_z-1])*9/PI);
}
}
else if (index_y == 1)
{
if (index_z == 0)
{
votes[0] += temp.magnitude * (1-(j-posy[0])/8);
votes[DIR] += temp.magnitude *((j-posy[0])/8);
}
else if(index_z == 9)
{
votes[8] += temp.magnitude * (1-(j-posy[0])/8);
votes[DIR+9] += temp.magnitude * ((j-posy[0])/8);
}
else
{
votes[index_z-1] += temp.magnitude * (1-(j-posy[0])/8) * (1-(temp.orient - dirs[index_z-1])*9/PI);
votes[index_z] += temp.magnitude * (1-(j-posy[0])/8) * ((temp.orient-dirs[index_z-1])*9/PI);
votes[DIR+index_z-1] += temp.magnitude * ((j-posy[0])/8) * (1-(temp.orient - dirs[index_z-1])*9/PI);
votes[DIR+index_z] += temp.magnitude * ((j-posy[0])/8) * ((temp.orient-dirs[index_z-1])*9/PI);
}
}
else
{
if (index_z == 0)
{
votes[DIR] += temp.magnitude;
}
else if(index_z == 9)
{
votes[DIR+8] += temp.magnitude;
}
else
{
votes[DIR+index_z-1] += temp.magnitude * (1-(temp.orient - dirs[index_z-1])*9/PI);
votes[DIR+index_z] += temp.magnitude * ((temp.orient-dirs[index_z-1])*9/PI);
}
}
}
else if(index_x == 1)
{
if (index_y == 0)
{
if (index_z == 0)
{
votes[0] += temp.magnitude * (1-(i-posx[0])/8);
votes[BLOCK_SIZE*DIR] += temp.magnitude * ((i-posx[0])/8);
}
else if(index_z == 9)
{
votes[8] += temp.magnitude * (1-(i-posx[0])/8);
votes[BLOCK_SIZE*DIR+8] += temp.magnitude * ((i-posx[0])/8);
}
else
{
votes[index_z-1] += temp.magnitude * (1-(i-posx[0])/8) * (1-(temp.orient - dirs[index_z-1])*9/PI);
votes[index_z] += temp.magnitude * (1-(i-posx[0])/8) * ((temp.orient - dirs[index_z-1])*9/PI);
votes[BLOCK_SIZE*DIR+index_z-1] += temp.magnitude * ((i-posx[0])/8) * (1-(temp.orient - dirs[index_z-1])*9/PI);
votes[BLOCK_SIZE*D
评论0