-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomGrubPartOfImg.cpp
68 lines (54 loc) · 2.01 KB
/
randomGrubPartOfImg.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
#include <iostream>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <string>
#include <vector>
#include <random>
using namespace std;
using namespace cv;
const string DATAPATH = "/home/tau/INRIAPerson/";
const string negPATH = "/home/tau/INRIAPerson/train_64x128_H96/";
int numOfImg = 10;
int main()
{
std::random_device rd; // only used once to initialise (seed) engine
std::mt19937 rng(rd()); // random-number engine used (Mersenne-Twister in this case)
ifstream negList(negPATH+ "/neg.lst");
if(!negList.is_open()){
cout << "please check the path of neg.lst" << endl;
return -1;
}
ofstream outList(string(DATAPATH + "neg.lst"),ofstream::out); // clear all the things in neg.lst
outList.close();
outList.open(DATAPATH + "neg.lst",ofstream::app);
string Line;
Mat img;
if(!outList.is_open()){
cout << "error writing img path to the neg.lst" << endl;
return -1;
}
int count = 0;
while(getline(negList,Line)){
string PATH = DATAPATH + Line;
img = imread(PATH);
if(!img.empty() &&
img.rows > 64 &&
img.cols > 128){
std::uniform_int_distribution<int> row(0,img.rows-128); // guaranteed unbiased // 概率相同
std::uniform_int_distribution<int> col(0,img.cols-64); // guaranteed unbiased // 概率相同
int t = numOfImg;
while(t--){
int x = col(rng);
int y = row(rng);
string filename = to_string(count) + ".png";
string path = DATAPATH + "neg/" + filename;
Mat grub(img,cv::Rect(x,y,64,128));
imwrite(path,grub);
outList << path << '\n' ;
++count;
}
}else
cout << "please check the neg files" << endl;
}
negList.close();
}