-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture.cpp
More file actions
232 lines (208 loc) · 7.01 KB
/
Copy pathtexture.cpp
File metadata and controls
232 lines (208 loc) · 7.01 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
#include <time.h>
#include <windows.h>
#include "texture.h"
#include "iostream"
using namespace std;
int global;
Texture::Texture(QString path)
{
img = cvLoadImage(path.toStdString().c_str());
// cout << "channels" << img->nChannels <<endl;
// cout << "depth" << img->depth <<endl;
// cout << "width" << img->width <<endl;
// cout << "height" << img->height <<endl <<endl;
}
Texture::Texture(IplImage* input) {
img = input;
}
Texture::Texture(Texture* texture, int size) {
int width = (texture->img->width+1)/size;
int height = (texture->img->height+1)/size;
img = cvCreateImage(cvSize(width, height),8,3);
cvResize(texture->img,img,CV_INTER_LINEAR);
}
Texture::~Texture()
{
cvReleaseImage(&img);
cvReleaseImage(&whiteNoise);
}
void Texture::init(){
nPts = 0; // read data points
k = 1;
eps = 0;
}
void Texture::search(Neighbor* origin, char* color, int neighborWidth){
readNeighbor(origin, p);
kdTree->annkSearch( // search
p, // query point
k, // number of near neighbors
nnIdx, // nearest neighbors (returned)
dists, // distance (returned)
eps);
Neighbor* n = new Neighbor(img, this->nnIdx[0]/img->height, this->nnIdx[0]%img->height, neighborWidth);
color[0] = n->data[n->size*3 - 3];
color[1] = n->data[n->size*3 - 2];
color[2] = n->data[n->size*3 - 1];
}
void Texture::findBest(Neighbor* origin, char* color, int neighborWidth) {
Neighbor *n;
double min = 255*255*3*(origin->size-1);
/*
for(int j=0; j<img->height; j++) {
for(int i=0; i<img->width; i++) {
n = new Neighbor(img, j, i, neighborWidth); //j行i列
double result;
if((result = origin->difNeighbor(n)) < min) {
color[0] = n->data[n->size*3 - 3];
color[1] = n->data[n->size*3 - 2];
color[2] = n->data[n->size*3 - 1];
min = result;
// cout <<"min"<<endl;
}
// cout<<j<<" "<<i<<" "<<result<<" ";
// cout << char2int(n->data[n->size*3 - 3])<<" "
// << char2int(n->data[n->size*3 - 2])<<" "
// << char2int(n->data[n->size*3 - 1])<<endl;
delete n;
}
}
*/
for(int j=0; j<allNeighbor.size(); j++) {
for(int i=0; i<allNeighbor[j].size(); i++) {
n = allNeighbor[j][i];
double result;
if((result = origin->difNeighbor(n)) < min) {
color[0] = n->data[n->size*3 - 3];
color[1] = n->data[n->size*3 - 2];
color[2] = n->data[n->size*3 - 1];
min = result;
// cout <<"min"<<endl;
}
// cout<<j<<" "<<i<<" "<<result<<" ";
// cout << char2int(n->data[n->size*3 - 3])<<" "
// << char2int(n->data[n->size*3 - 2])<<" "
// << char2int(n->data[n->size*3 - 1])<<endl;
}
}
}
void Texture::whiteNoiseGen(int width, int height) {
whiteNoise = cvCreateImage(cvSize(width, height), img->depth, img->nChannels);
whiteNoise->origin = img->origin;
// whiteNoise->widthStep = width;
cvSetZero(whiteNoise);
srand(time(NULL));
int count=0;
CvScalar s;
for(int j=0; j<whiteNoise->height; j++) {
for(int i=0; i<whiteNoise->width; i++, count++) {
s.val[0]=rand()%256; //B
s.val[1]=rand()%256; //G
s.val[2]=rand()%256; //R
// printf("%d, B=%f, G=%f, R=%f\n",count,s.val[0],s.val[1],s.val[2]);
cvSet2D(whiteNoise,j,i,s);
}
}
}
void Texture::preprocess(){
init();
dim = ((neighborWidth*neighborWidth)/2)*3;
maxPts = img->height * img->width;
k = 1;
queryPt = annAllocPt(dim); // allocate query point
dataPts = annAllocPts(maxPts, dim); // allocate data points
nnIdx = new ANNidx[k]; // allocate near neigh indices
dists = new ANNdist[k]; // allocate near neighbor dists
p = annAllocPt(dim);
cout << "*******************dim : " << dim << "************************" << endl;
return ;
}
void Texture::textureGen(int width, int height, int neighborWidth) {
this->neighborWidth = neighborWidth;
double t0 = GetTickCount();
this->Width = width;
this->Height = height;
whiteNoiseGen(width, height);
this->preprocess();
double t1 = GetTickCount();
allNeighborGen(neighborWidth);
kdTree = new ANNkd_tree( // build search structure
dataPts, // the data points
maxPts, // number of points
dim); // dimension of space
// cout << "2" << endl;
CvScalar s;
char bestColor[3];
Neighbor *n;
double t2 = GetTickCount();
//n = new Neighbor(whiteNoise, 0, 0, neighborWidth); //j行i列
for(int j=0; j<whiteNoise->height; j++) {
for(int i=0; i<whiteNoise->width; i++) {
//n->set(whiteNoise, j, i, neighborWidth); //j行i列
n = new Neighbor(whiteNoise, j, i, neighborWidth); //j行i列
//findBest(n, bestColor, neighborWidth);
// cout << "1" << endl;
this->search(n, bestColor, neighborWidth);
// cout << "3" << endl;
s.val[0]=char2int(bestColor[2]); //B
s.val[1]=char2int(bestColor[1]); //G
s.val[2]=char2int(bestColor[0]); //R
cvSet2D(whiteNoise,j,i,s);
delete n;
}
}
cout << "lala" << endl;
double t3 = GetTickCount();
// allNeighborRelease();
double t4 = GetTickCount();
cout << "GetTickCount:" << t1-t0 << endl;
cout << "GetTickCount:" << t2-t1 << endl;
cout << "GetTickCount:" << t3-t2 << endl;
cout << "GetTickCount:" << t4-t3 << endl;
}
void Texture::allNeighborGen(int neighborWidth) {
cout<<"startGen"<<endl;
Neighbor *n;
for(int j=0; j<img->height; j++) { //j行i列
vector<Neighbor*> line;
allNeighbor.push_back(line);
for(int i=0; i<img->width; i++) {
n = new Neighbor(img, j, i, neighborWidth);
allNeighbor[j].push_back(n);
// cout << "before read" << endl;
readNeighbor(n, dataPts[j*img->height + i]);
// cout << j*img->height + i << " read f" << endl;
// cout<<j<<" "<<i<<endl;
delete n;
}
}
}
void Texture::printPt(ostream &out, ANNpoint p) // print point
{
out << "(" << p[0];
for (int i = 1; i < dim; i++) {
out << ", " << p[i];
}
out << ")\n";
}
void Texture::readNeighbor(Neighbor* n, ANNpoint p){
for(int i = 0; i < dim; ++i){
p[i] = n->getRGB(i/3, i%3);
}
//printPt(cout, p);
}
void Texture::allNeighborRelease(void) {
Neighbor *n;
for(int j=0; j<allNeighbor.size(); j++) { //j行i列
for(int i=0; i<allNeighbor[j].size(); i++) {
n = allNeighbor[j][i];
delete n;
}
}
}
int Texture::char2int(char c) {
int i = (int) c;
if(i >= 0)
return i;
else
return i+256;
}