-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl2texture.cpp
More file actions
151 lines (133 loc) · 5.53 KB
/
Copy pathl2texture.cpp
File metadata and controls
151 lines (133 loc) · 5.53 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
#include "l2texture.h"
#include "iostream"
L2Texture::L2Texture()
{
}
L2Texture::L2Texture(QString path) {
lowerTexture = new Texture(path);
upperTexture = new Texture(lowerTexture, 2); // 2 means the size of upper is half of lower
}
L2Texture::L2Texture(IplImage* input) {
lowerTexture = new Texture(input);
upperTexture = new Texture(lowerTexture, 2);
// cvNamedWindow("haha");
// cvShowImage("haha", upperTexture->img);
// cvWaitKey(0);
// cvDestroyWindow("haha");
}
L2Texture::~L2Texture() {
delete lowerTexture;
delete upperTexture;
}
void L2Texture::init(){
nPts = 0; // read data points
k = 1;
eps = 0;
}
void L2Texture::search(L2Neighbor* 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);
L2Neighbor* n = new L2Neighbor(lowerTexture->img, upperTexture->img, this->nnIdx[0]/lowerTexture->img->height, this->nnIdx[0]%lowerTexture->img->height, neighborWidth);
color[0] = n->lowerNeighbor->data[n->lowerNeighbor->size*3 - 3];
color[1] = n->lowerNeighbor->data[n->lowerNeighbor->size*3 - 2];
color[2] = n->lowerNeighbor->data[n->lowerNeighbor->size*3 - 1];
}
void L2Texture::readNeighbor(L2Neighbor* n, ANNpoint p){
for(int i = 0; i < dim; ++i){
p[i] = n->getRGB(i/3, i%3);
}
//printPt(cout, p);
}
void L2Texture::preprocess(){
init();
dim = ((neighborWidth*neighborWidth)/2)*3 + ((neighborWidth+2)/4*2+1)*((neighborWidth+2)/4*2+1);
maxPts = this->lowerTexture->img->height * this->lowerTexture->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 L2Texture::readAll(int neighborWidth){
cout << "start read all" << endl;
L2Neighbor *n;
for(int j=0; j<lowerTexture->img->height; j++) {
for(int i=0; i<lowerTexture->img->width; i++) {
n = new L2Neighbor(lowerTexture->img, upperTexture->img, j, i, neighborWidth); //jÐÐiÁÐ
this->readNeighbor(n, this->dataPts[j*lowerTexture->img->height + i]);
delete n;
}
}
}
void L2Texture::textureGen(int width, int height, int neighborWidth) {
this->neighborWidth = neighborWidth;
// 3->3 5->3 7->5 9->5 11->7 13->7...
upperTexture->textureGen((width+1)/2, (height+1)/2, (neighborWidth+2)/4*2+1);
lowerTexture->whiteNoiseGen(width, height);
this->preprocess();
CvScalar s;
char bestColor[3];
L2Neighbor *n;
readAll(neighborWidth);
kdTree = new ANNkd_tree( // build search structure
dataPts, // the data points
maxPts, // number of points
dim); // dimension of space
for(int j=0; j<lowerTexture->whiteNoise->height; j++) {
for(int i=0; i<lowerTexture->whiteNoise->width; i++) {
n = new L2Neighbor(lowerTexture->whiteNoise, upperTexture->whiteNoise, j, i, neighborWidth); //jÐÐiÁÐ
// findBest(n, bestColor, neighborWidth);
this->search(n, bestColor, neighborWidth);
s.val[0]=char2int(bestColor[2]); //B
s.val[1]=char2int(bestColor[1]); //G
s.val[2]=char2int(bestColor[0]); //R
cvSet2D(lowerTexture->whiteNoise,j,i,s);
delete n;
// cout << "result"<<j<<" "<<i<<":";
// cout << char2int(bestColor[0])<<" "
// << char2int(bestColor[1])<<" "
// << char2int(bestColor[2])<<endl;
}
}
// cout<<lowerTexture->img->width<<" "<<lowerTexture->img->height<<endl;
// cout<<lowerTexture->whiteNoise->width<<" "<<lowerTexture->whiteNoise->height<<endl;
// cout<<upperTexture->img->width<<" "<<upperTexture->img->height<<endl;
// cout<<upperTexture->whiteNoise->width<<" "<<upperTexture->whiteNoise->height<<endl;
}
void L2Texture::findBest(L2Neighbor* origin, char* color, int neighborWidth){
L2Neighbor *n;
double min = 255*255*3*(origin->lowerNeighbor->size-1 + origin->upperNeighbor->size);
for(int j=0; j<lowerTexture->img->height; j++) {
for(int i=0; i<lowerTexture->img->width; i++) {
n = new L2Neighbor(lowerTexture->img, upperTexture->img, j, i, neighborWidth); //jÐÐiÁÐ
double result;
if((result = origin->difL2Neighbor(n)) < min) {
color[0] = n->lowerNeighbor->data[n->lowerNeighbor->size*3 - 3];
color[1] = n->lowerNeighbor->data[n->lowerNeighbor->size*3 - 2];
color[2] = n->lowerNeighbor->data[n->lowerNeighbor->size*3 - 1];
min = result;
cout <<"min"<<endl;
}
// cout<<j<<" "<<i<<" "<<result<<" ";
// cout << char2int(n->lowerNeighbor->data[n->lowerNeighbor->size*3 - 3])<<" "
// << char2int(n->lowerNeighbor->data[n->lowerNeighbor->size*3 - 2])<<" "
// << char2int(n->lowerNeighbor->data[n->lowerNeighbor->size*3 - 1])<<endl;
delete n;
}
}
}
int L2Texture::char2int(char c) {
int i = (int) c;
if(i >= 0)
return i;
else
return i+256;
}