Repository navigation
Expand file tree
/
Copy pathOpenCvUtilities.cpp
More file actions
260 lines (217 loc) · 8.02 KB
/
Copy pathOpenCvUtilities.cpp
File metadata and controls
260 lines (217 loc) · 8.02 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "OpenCvUtilities.h"
#include "CommonMacros.h"
#include <iostream>
#include <math.h>
namespace OpenCvWrapper
{
/********************************************************************
Utils::CreateHistogram
CreateHistogram
Exceptions:
None
*********************************************************************/
CvHistogram* Utils::CreateHistogram( )
{
CvHistogram* pHistogram = NULL;
try
{
int h_bins = 30;
int s_bins = 32;
int hist_size[] = { h_bins, s_bins };
float h_ranges[] = { 0, 180 };
float s_ranges[] = { 0, 255 };
float* ranges[] = { h_ranges, s_ranges };
CvHistogram* pHistogram = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 );
}
EXCEPTION_CATCH_AND_ABORT("Failed to create histogram");
return pHistogram;
}
/********************************************************************
Utils::CropImage
CropImage
Exceptions:
None
*********************************************************************/
IplImage* Utils::CropImage( IplImage* pIplImage,
CvRect* pRectangle )
{
IplImage* pCroppedImage = NULL;
try
{
//set region of interest in the input pIplImage
cvSetImageROI( pIplImage, *pRectangle );
pCroppedImage = cvCreateImage( cvSize( pRectangle->width, pRectangle->height),
pIplImage->depth,
pIplImage->nChannels );
// Copy the pIplImage
cvCopy( pIplImage, pCroppedImage );
// Reset the ROI
cvResetImageROI(pIplImage);
}
EXCEPTION_CATCH_AND_ABORT( "Failed to crop image" );
return pCroppedImage;
}
/********************************************************************
Utils::CalculateColorHistogram
CalculateColorHistogram
Exceptions:
None
*********************************************************************/
void Utils::CalculateColorHistogram( IplImage* pIplImage,
CvHistogram* pHistogram,
CvRect* pRectangle )
{
try
{
IplImage* pObjectImage;
if ( pRectangle != NULL )
{
pObjectImage = OpenCvWrapper::Utils::CropImage( pIplImage,
pRectangle );
}
else
{
pObjectImage = pIplImage;
}
IplImage* h_plane = cvCreateImage( cvGetSize( pObjectImage ), 8, 1 );
IplImage* s_plane = cvCreateImage( cvGetSize( pObjectImage ), 8, 1 );
IplImage* v_plane = cvCreateImage( cvGetSize( pObjectImage ), 8, 1 );
IplImage* planes[] = { h_plane, s_plane };
//convert pixel to plane
cvCvtPixToPlane( pObjectImage, h_plane, s_plane, v_plane, 0 );
//calculate the histogram
cvCalcHist( planes, pHistogram, 0, 0 );
//normalize the histogram
cvNormalizeHist( pHistogram, 1.0 );
//release the locally created images
cvReleaseImage( &h_plane );
cvReleaseImage( &s_plane );
cvReleaseImage( &v_plane );
if ( pRectangle != NULL )
{
cvReleaseImage( &pObjectImage );
}
}
EXCEPTION_CATCH_AND_ABORT( "Failed to Calculate Color Histogram" );
}
/********************************************************************
Utils::GetRectangleCenter
GetRectangleCenter
Exceptions:
None
*********************************************************************/
CvPoint* Utils::GetRectangleCenter( CvRect* pRectangle )
{
CvPoint* pCenterPoint = new CvPoint();
pCenterPoint->x = static_cast<int>( pRectangle->x + (pRectangle->width/2.0) );
pCenterPoint->y = static_cast<int>( pRectangle->y + (pRectangle->height/2.0) );
return pCenterPoint;
}
/********************************************************************
Utils::CalculateEuclideanDistance
CalculateEuclideanDistance
Exceptions:
None
*********************************************************************/
double Utils::CalculateEuclideanDistance( CvPoint* pPoint1,
CvPoint* pPoint2 )
{
return sqrt( pow( ( pPoint1->x - pPoint2->x ), 2.0) + pow( ( pPoint1->y - pPoint2->y ), 2.0 ) );
}
/********************************************************************
Utils::SetParameters
Sets parameters for different opencv modules
Exceptions:
None
*********************************************************************/
void Utils::SetParameters( char* pParamStr,
CvVSModule* pModule,
const char* pPrefixStr,
const char* pModuleStr )
{
int prefixLength = std::strlen( pPrefixStr );
while ( pParamStr[0] != '\0' )
{
int j;
char* ptr_eq = NULL;
int cmd_param_len=0;
char* cmd = pParamStr;
if ( MY_STRNICMP( pPrefixStr, cmd, prefixLength )!= 0 )
{
pParamStr++;
continue;
}
cmd += prefixLength;
if ( cmd[0] != ':' )
{
pParamStr++;
continue;
}
cmd++;
ptr_eq = std::strchr(cmd,'=');
if ( ptr_eq )
{
cmd_param_len = ptr_eq-cmd;
}
ptr_eq = strchr(cmd,';'); //look for next command
if ( ptr_eq )
{
*ptr_eq = '\0';
pParamStr = ptr_eq+1;
}
else
{
break; //command must ends with ";"
}
for ( j=0; ; ++j )
{
int param_len;
const char* param = pModule->GetParamName(j);
if ( param == NULL )
{
break;
}
param_len = std::strlen(param);
if ( cmd_param_len != param_len )
{
continue;
}
if ( MY_STRNICMP( param,cmd,param_len ) !=0 )
{
continue;
}
cmd+=param_len;
if ( cmd[0]!='=' )
{
continue;
}
cmd++;
pModule->SetParamStr(param,cmd);
printf("%s:%s param set to %g\n",pModuleStr,param,pModule->GetParam(param));
}
}
pModule->ParamUpdate();
}
/********************************************************************
Utils::HistogramEqualize
Performs Histogram Equalization on the given image
Exceptions:
None
*********************************************************************/
cv::Mat Utils::HistogramEqualize(const cv::Mat& inputImage)
{
if ( inputImage.channels() >= 3 )
{
cv::Mat ycrcb;
cvtColor(inputImage,ycrcb,CV_BGR2YCrCb);
std::vector<cv::Mat> channels;
split(ycrcb,channels);
equalizeHist(channels[0], channels[0]);
cv::Mat result;
merge(channels,ycrcb);
cvtColor(ycrcb,result,CV_YCrCb2BGR);
return result;
}
return cv::Mat();
}
}