-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.cpp
More file actions
53 lines (45 loc) · 1.42 KB
/
Utility.cpp
File metadata and controls
53 lines (45 loc) · 1.42 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
#include "Utility.h"
void Utility::loadImgs(String dir, vector<Mat> &imgs){
vector<String> f;
glob(dir, f);
for(int i = 0; i < f.size(); i++){
Mat img = imread(f[i]);
if (img.empty())
continue;
imgs.push_back(img);
}
}
vector<Mat> Utility::cropImgRandom(vector<Mat> negSet, Size size){
Rect rect;
rect.width = size.width;
rect.height = size.height;
vector<Mat> cropSet;
srand((unsigned int)time(NULL));
int x = rect.width;
int y = rect.height;
for(int i = 0; i < negSet.size(); i++){
rect.x = rand() % (negSet[i].cols-x);
rect.y = rand() % (negSet[i].rows-y);
Mat neg = negSet[i](rect);
cropSet.push_back(neg);
}
return cropSet;
}
//OpenCV Function to convert data to be used on ML algorithms
void Utility::convertData( const vector< Mat > & train_samples, Mat& train_matrix ){
int rows = train_samples.size();
int cols = (int)std::max( train_samples[0].cols, train_samples[0].rows );
Mat tmp( 1, cols, CV_32FC1 );
train_matrix = Mat( rows, cols, CV_32FC1 );
for(int i = 0 ; i < train_samples.size(); i++){
if( train_samples[i].cols == 1 )
{
transpose( train_samples[i], tmp );
tmp.copyTo( train_matrix.row( i ));
}
else if( train_samples[i].rows == 1 )
{
train_samples[i].copyTo( train_matrix.row( i ));
}
}
}