-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHOGDescriptorOCV.cpp
231 lines (202 loc) · 8.13 KB
/
HOGDescriptorOCV.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//////////////////////////////////////////////////////////////////////////
// Creates C++ MEX-file for extraction of Histogram of Oriented Gradients
// Features Algorithm in OpenCV. This uses HOGDescriptor class in OpenCV.
//////////////////////////////////////////////////////////////////////////
#include "opencvmex.hpp"
// On some platforms, the following include is needed for "placement new".
// For more information see: http://en.wikipedia.org/wiki/Placement_syntax
#include <memory>
//#include "objdetect.hpp"
using namespace cv;
static HOGDescriptor *hog = NULL;
//////////////////////////////////////////////////////////////////////////////
// Check inputs
//////////////////////////////////////////////////////////////////////////////
void checkInputs(int nrhs, const mxArray *prhs[])
{
if ((nrhs < 1) || (nrhs > 2))
{
mexErrMsgTxt("Incorrect number of inputs. Function expects 1 or 2 inputs.");
}
// const int * imDims;
//
// // Check input dimensions
// imDims = mxGetDimensions(prhs[0]);
//
// if (mxGetNumberOfDimensions(prhs[0])>2)
// {
// mexErrMsgTxt("Incorrect number of dimensions. First input must be a matrix.");
// }
//
// // Check image data type
// if (!mxIsUint8(prhs[0]))
// {
// mexErrMsgTxt("Image must be UINT8.");
// }
}
//////////////////////////////////////////////////////////////////////////////
// Get MEX function inputs
//////////////////////////////////////////////////////////////////////////////
void getParams(Size &winSize, Size &cellSize, Size &blockSize, Size &blockStride, int &numBins, const mxArray* mxParams)
{
const mxArray* mxfield;
int *winSizeInt = NULL,
*cellSizeInt = NULL,
*blockSizeInt = NULL,
*blockOverlap = NULL;
//--winSize--
mxfield = mxGetField(mxParams, 0, "WindowSize");
if (mxfield)
winSizeInt = (int*)mxGetData(mxfield);
winSize = Size(winSizeInt[1], winSizeInt[0]);
//--cellSize--
mxfield = mxGetField(mxParams, 0, "CellSize");
if (mxfield)
cellSizeInt = (int*)mxGetData(mxfield);
cellSize = Size(cellSizeInt[1], cellSizeInt[0]);
//--blockSize--
mxfield = mxGetField(mxParams, 0, "BlockSize");
if (mxfield)
blockSizeInt = (int*)mxGetData(mxfield);
blockSize = Size(blockSizeInt[1]*cellSizeInt[1], blockSizeInt[0]*cellSizeInt[0]);
//--blockOverlap--
mxfield = mxGetField(mxParams, 0, "BlockOverlap");
if (mxfield)
blockOverlap = (int*)mxGetData(mxfield);
blockStride = Size(blockOverlap[1]*cellSizeInt[1],
blockOverlap[0]*cellSizeInt[0]);
//--numBins--
mxfield = mxGetField(mxParams, 0, "NumBins");
if (mxfield)
numBins = (int)mxGetScalar(mxfield);
}
//////////////////////////////////////////////////////////////////////////////
// Exit function for freeing persistent memory
//////////////////////////////////////////////////////////////////////////////
void exitFcn()
{
if (hog != NULL){
// explicitly call destructor for "placement new"
hog->~HOGDescriptor();
mxFree(hog);
hog = NULL;
}
}
//////////////////////////////////////////////////////////////////////////////
// Construct object
//////////////////////////////////////////////////////////////////////////////
void constructObject(const mxArray *prhs[])
{
Size winSize,
cellSize,
blockSize,
blockStride;
int numBins;
// second input must be struct
if (mxIsStruct(prhs[1]))
getParams(winSize, cellSize, blockSize, blockStride, numBins, prhs[1]);
// Allocate memory for HOG Descriptor model
hog = (HOGDescriptor *)mxCalloc(1, sizeof(HOGDescriptor));
// Make memory allocated by MATLAB software persist after MEX-function completes.
// This lets us use the updated HOG Descriptor model for the next frame.
mexMakeMemoryPersistent(hog);
// Use "placement new" to construct an object on memory that was
// already allocated using mxCalloc
new (hog) HOGDescriptor(winSize, blockSize, blockStride, cellSize, numBins, 1, 8);
// Register a function that gets called when the MEX-function is cleared.
// This function is responsible for freeing persistent memory
mexAtExit(exitFcn);
}
//////////////////////////////////////////////////////////////////////////////
// Check compute inputs
//////////////////////////////////////////////////////////////////////////////
void checkComputeInputs(int nrhs, const mxArray *prhs[])
{
const int * imDims;
// Check input image dimensions
imDims = mxGetDimensions(prhs[1]);
if (mxGetNumberOfDimensions(prhs[1])>2)
{
mexErrMsgTxt("Incorrect number of dimensions. Second input must be a matrix.");
}
// Check image data type
if (!mxIsUint8(prhs[0]))
{
mexErrMsgTxt("Image must be UINT8.");
}
}
//////////////////////////////////////////////////////////////////////////////
// Get MexArray from Vector of float
//////////////////////////////////////////////////////////////////////////////
mxArray * getMexArray (const vector<float> &v)
{
// Create mex array of type single (real) with size of input vector (v)
size_t num = sizeof(float);
int vectorLength = v.size();
mxArray *mx = mxCreateNumericMatrix(1, vectorLength, mxSINGLE_CLASS, mxREAL);
// Copy each element of input vector (v) to mex array
memcpy( mxGetData(mx), &v[0], vectorLength*num );
return mx;
}
//////////////////////////////////////////////////////////////////////////////
// Compute HOG features
//////////////////////////////////////////////////////////////////////////////
void computeHOGFeatures(int nlhs, mxArray *plhs[], const mxArray *prhs[])
{
if (nlhs != 1)
mexErrMsgTxt("Incorrect number of outputs, must be 1.");
if (hog!=NULL)
{
cv::Ptr<cv::Mat> imgCV;//, resimg(hog->winSize.width, hog->winSize.height, CV_8UC1);
cv::Mat myImg;//, croppedImg;
// Calculate resized image size (for computing over cropped image)
cv::Mat resizedImg(hog->winSize.width, hog->winSize.height, CV_8UC1);
Size imSize;
vector<Point> locations;
int outDim;
// Convert mxArray input into OpenCV types
imgCV = ocvMxArrayToImage_uint8(prhs[1], true);
myImg = *imgCV;
// Resize Image
cv::resize(myImg, resizedImg, cv::Size(hog->winSize.width, hog->winSize.height));
// Calculate dimensions of output vector
outDim = (mwSize)( (size_t) hog->nbins
* ( hog->blockSize.width / hog->cellSize.width )
* ( hog->blockSize.height / hog->cellSize.height )
* ( ( hog->winSize.width - hog->blockSize.width )
/ hog->blockStride.width + 1 )
* ( ( hog->winSize.height - hog->blockSize.height )
/ hog->blockStride.height + 1 ) );
// Create output vector
vector<float> descriptorValues(outDim);
//mxArray *outMatrix;
// Compute HOG features
//hog->compute(myImg, descriptorValues, Size(), Size(), locations);
hog->compute(resizedImg, descriptorValues, Size(), Size(), locations);
for (int i = 0; i<outDim; i = i +9)
{
std::reverse(&(descriptorValues[i]),&(descriptorValues[i+9]));
}
// Return features in MexArray
plhs[0] = getMexArray(descriptorValues);
}
}
//////////////////////////////////////////////////////////////////////////////
// The main MEX function entry point
//////////////////////////////////////////////////////////////////////////////
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
checkInputs(nrhs, prhs);
const char *str = mxIsChar(prhs[0]) ? mxArrayToString(prhs[0]) : NULL;
if (str != NULL)
{
if (strcmp (str,"construct") == 0)
constructObject(prhs);
else if (strcmp (str,"compute") == 0)
computeHOGFeatures(nlhs, plhs, prhs);
else if (strcmp (str,"destroy") == 0)
exitFcn();
// Free memory allocated by mxArrayToString
mxFree((void *)str);
}
}