-
Notifications
You must be signed in to change notification settings - Fork 4
/
data_generation.cpp
129 lines (119 loc) · 2.87 KB
/
data_generation.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
#include<bits/stdc++.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include<ctime>
#include <fstream>
#include <sstream>
using namespace cv;
using namespace std;
int isValid(int i , int j , Mat img, int r)
{
if ( i < img.rows-r/2 && j < img.cols-r/2 && i > r/2 && j > r/2)
return 1;
return 0;
}
Mat circle(Mat a, int i, int j, int r)
{
int p,q;
i = i-r;
j = j-r;
for(p=i-r;p<i+r;p++)
for(q=j-sqrt(pow(r,2)-pow(p-i,2));q<j+sqrt(pow(r,2)-pow(p-i,2));q++)
{
if(isValid(q,p,a,r))
a.at<uchar>(p,q)=255;
}
return a;
}
Mat square(Mat a , int j , int k , int r )
{
int m,n;
for(m = j - r; m <= j + r; m++)
{
for(n = k - r; n <= k + r; n++)
{
if(isValid(m, n, a,r))
{
a.at<uchar>(m,n) = 255;
}
}
}
return a;
}
Mat triangle(Mat a , int j , int k , int r )
{
int m,n;
for(m = j - r; m <= j + r; m++)
{
for(n = m; n <= k + r; n++)
{
if(isValid(m, n, a,r))
{
a.at<uchar>(m,n) = 255;
}
}
}
return a;
}
int main()
{
int a[3] = {0,1,2};
srand(time(0));
int number = 100000;
//int num[10000];
ofstream myfile1;
myfile1.open("data/number.txt");
Mat img1(300,300,CV_8UC1,Scalar(0));
Mat img;
namedWindow("Image", WINDOW_NORMAL);
ofstream myfile2;
myfile2.open("data/obst.txt");
while(number--)
{
img = img1.clone();
int obs_number = 12+ rand()%8;
// vector<int> x(obs_number);
// vector<int> y(obs_number);
// vector<float> area(obs_number);
int max1 = obs_number;
//num[10000-number]=max;
myfile1 << max1 << endl;
while(obs_number--)
{
//int shape = rand()%3;
int x = rand()%(img.rows);
int y = rand()%(img.cols);
int r = 15+rand()%10;
img = circle(img,x,y,r);
float area = 3.14*r*r;
myfile2 << x << " " << y << " " << area << endl;
}
//std::string s = std::to_string(number);
stringstream ss;
ss<<number;
string s = "dataset/";
string s1 = "img";
string s2 = ss.str();
string s3 = ".jpg";
//string s4 = "1";
// imshow("Image", img);
// while(1)
// {
// int flag = waitKey(0);
// if(flag == 27)
// break;
// }
imwrite(s+s1+s2+s3,img);
}
if(number%10 != 0)
{
int x = rand()%(img.rows);
//int y = rand()%(img.cols);
int r = 15+rand()%10;
img = circle(img,x,x,r);
float area = 3.14*r*r;
myfile2 << x << " " << x << " " << area << endl;
}
return 0;
}