forked from higerra/HoughForest
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCRForestDetector.cpp
135 lines (97 loc) · 3.71 KB
/
CRForestDetector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
// Author: Juergen Gall, BIWI, ETH Zurich
// Email: [email protected]
*/
#include "CRForestDetector.h"
#include <vector>
using namespace std;
using namespace cv;
void CRForestDetector::detectColor(const cv::Mat& img, vector<Mat>& imgDetect, std::vector<float>& ratios) {
// extract features
vector<Mat> vImg;
CRPatch::extractFeatureChannels(img, vImg);
// reset output image
for(int c=0; c<(int)imgDetect.size(); ++c)
imgDetect[c].setTo(Scalar::all(0));
//cvSetZero( imgDetect[c] );
// get pointers to feature channels
int stepImg;
vector<uchar*> ptFCh(vImg.size());
vector<uchar*> ptFCh_row(vImg.size());
for(unsigned int c=0; c<vImg.size(); ++c) {
ptFCh[c] = vImg[c].data;
// cvGetRawData( vImg[c], (uchar**)&(ptFCh[c]), &stepImg);
}
// stepImg /= sizeof(ptFCh[0][0]);
stepImg = vImg[0].cols;
// get pointer to output image
int stepDet;
vector<float*> ptDet(imgDetect.size());
for(unsigned int c=0; c<imgDetect.size(); ++c)
ptDet[c] = (float*) imgDetect[c].data;
// cvGetRawData( imgDetect[c], (uchar**)&(ptDet[c]), &stepDet);
// stepDet /= sizeof(ptDet[0][0]);
stepDet = imgDetect[0].cols;
int xoffset = width/2;
int yoffset = height/2;
int x, y, cx, cy; // x,y top left; cx,cy center of patch
cy = yoffset;
for(y=0; y<img.rows-height; ++y, ++cy) {
// Get start of row
for(unsigned int c=0; c<vImg.size(); ++c)
ptFCh_row[c] = &ptFCh[c][0];
cx = xoffset;
for(x=0; x<img.cols-width; ++x, ++cx) {
// regression for a single patch
vector<const LeafNode*> result;
crForest->regression(result, ptFCh_row.data(), stepImg);
// vote for all trees (leafs)
for(vector<const LeafNode*>::const_iterator itL = result.begin(); itL!=result.end(); ++itL) {
// To speed up the voting, one can vote only for patches
// with a probability for foreground > 0.5
//
// if((*itL)->pfg>0.5) {
// voting weight for leaf
float w = (*itL)->pfg / float( (*itL)->vCenter.size() * result.size() );
// vote for all points stored in the leaf
for(auto it = (*itL)->vCenter.begin(); it!=(*itL)->vCenter.end(); ++it) {
for(int c=0; c<(int)imgDetect.size(); ++c) {
int xx = int(cx - (*it)[0].x * ratios[c] + 0.5);
int yy = cy-(*it)[0].y;
if(yy>=0 && yy<imgDetect[c].rows && xx>=0 && xx<imgDetect[c].cols) {
*(ptDet[c]+xx+yy*stepDet) += w;
}
}
}
// } // end if
}
// increase pointer - x
for(unsigned int c=0; c<vImg.size(); ++c)
++ptFCh_row[c];
} // end for x
// increase pointer - y
for(unsigned int c=0; c<vImg.size(); ++c)
ptFCh[c] += stepImg;
} // end for y
// smooth result image
for(int c=0; c<(int)imgDetect.size(); ++c)
cv::blur(imgDetect[c], imgDetect[c], cv::Size(3,3));
//cvSmooth( imgDetect[c], imgDetect[c], CV_GAUSSIAN, 3);
}
void CRForestDetector::detectPyramid(const cv::Mat& img, vector<vector<Mat> >& vImgDetect, std::vector<float>& ratios) {
if(img.channels() == 1) {
std::cerr << "Gray color images are not supported." << std::endl;
} else { // color
cout << "Timer" << endl;
float tstart = cv::getTickCount();
for(int i=0; i<int(vImgDetect.size()); ++i) {
// IplImage* cLevel = cvCreateImage( cvSize(vImgDetect[i][0]->width,vImgDetect[i][0]->height) , IPL_DEPTH_8U , 3);
Mat cLevel(vImgDetect[i][0].size(), CV_8UC3);
cv::resize(img, cLevel, cLevel.size(), CV_INTER_LINEAR);
//cvResize( img, cLevel, CV_INTER_LINEAR );
// detection
detectColor(cLevel,vImgDetect[i],ratios);
}
cout << "Time: " << ((float)getTickCount() - tstart) / (float)getTickFrequency() << " sec" << endl;
}
}