forked from btgraham/SparseConvNet-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenCVPicture.cpp
More file actions
154 lines (146 loc) · 5.5 KB
/
OpenCVPicture.cpp
File metadata and controls
154 lines (146 loc) · 5.5 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
#include "OpenCVPicture.h"
#include <cmath>
float OpenCVPicture::scaleUCharColor(float col) {
float div=std::max(255-backgroundColor,backgroundColor);
return (col-backgroundColor)/div;
}
OpenCVPicture::OpenCVPicture(int xSize, int ySize, int nInputFeatures, unsigned char backgroundColor,int label) :
Picture(label), backgroundColor(backgroundColor) {
xOffset=-xSize/2;
yOffset=-ySize/2;
mat.create(xSize,ySize, CV_32FC(nInputFeatures));
}
OpenCVPicture::OpenCVPicture(std::string filename, unsigned char backgroundColor, int label_) :
filename(filename), backgroundColor(backgroundColor) {
label=label_;
}
OpenCVPicture::~OpenCVPicture() {}
void OpenCVPicture::jiggle(RNG &rng, int offlineJiggle) {
xOffset+=rng.randint(offlineJiggle*2+1)-offlineJiggle;
yOffset+=rng.randint(offlineJiggle*2+1)-offlineJiggle;
}
void OpenCVPicture::colorDistortion(RNG &rng, int sigma1, int sigma2, int sigma3, int sigma4) {
distortImageColor(mat, rng, sigma1, sigma2, sigma3, sigma4);
}
void OpenCVPicture::randomCrop(RNG &rng, int subsetSize) {
assert(subsetSize<=std::min(mat.rows,mat.cols));
cropImage(mat, rng.randint(mat.cols-subsetSize),rng.randint(mat.rows-subsetSize), subsetSize, subsetSize);
xOffset=yOffset=-subsetSize/2;
}
void OpenCVPicture::affineTransform(float c00, float c01, float c10, float c11) {
transformImage(mat, backgroundColor, c00, c01, c10, c11);
xOffset=-mat.cols/2;
yOffset=-mat.rows/2;
}
void OpenCVPicture::jiggleFit(RNG &rng, int subsetSize, float minFill) {
if (minFill<0) { //Just pick a random subsetSize x subsetSquare that overlaps the picture as much as possible.
if (mat.cols>=subsetSize)
xOffset=-rng.randint(mat.cols-subsetSize+1)-subsetSize/2;
else
xOffset=rng.randint(subsetSize-mat.cols+1)-subsetSize/2;
if (mat.rows>=subsetSize)
yOffset=-rng.randint(mat.rows-subsetSize+1)-subsetSize/2;
else
yOffset=rng.randint(subsetSize-mat.rows+1)-subsetSize/2;
} else {
int fitCtr=100; //Give up after 100 failed attempts to find a good fit
bool goodFit=false;
while (!goodFit and fitCtr --> 0) {
xOffset=-subsetSize/2-rng.randint(mat.cols-subsetSize);//-rng.randint(mat.cols);
yOffset=-subsetSize/2-rng.randint(mat.rows-subsetSize);//-rng.randint(mat.rows);
int pointsCtr=0;
int interestingPointsCtr=0;
for (int X=5; X<subsetSize; X+=10) {
for (int Y=5; Y<subsetSize; Y+=10) {
int x=X-xOffset-subsetSize/2;
int y=Y-yOffset-subsetSize/2;
pointsCtr++;
if (0<=x and x<mat.cols and 0<=y and y<mat.rows)
interestingPointsCtr+=(mat.ptr()[(pointsCtr%mat.channels())+x*mat.channels()+y*mat.channels()*mat.cols]!=backgroundColor);
}
}
assert(pointsCtr>=10);
if (interestingPointsCtr>pointsCtr*minFill)
goodFit=true;
}
if (!goodFit) {
std::cout << filename << " " << std::flush;
xOffset=-mat.cols/2-16+rng.randint(32);
yOffset=-mat.rows/2-16+rng.randint(32);
}
}
}
void OpenCVPicture::centerMass() {
float ax=0, ay=0, axx=0, ayy=0, axy, d=0.001;
for (int i=0; i<mat.channels(); i++) {
for (int x=0;x<mat.cols;++x) {
for (int y=0;y<mat.rows;++y) {
float f=powf(backgroundColor-mat.ptr()[i+x*mat.channels()+y*mat.channels()*mat.cols],2);
ax+=x*f;
ay+=y*f;
axx+=x*x*f;
axy+=x*y*f;
ayy+=y*y*f;
d+=f;
}
}
}
ax/=d;
ay/=d;
axx/=d;
axy/=d;
ayy/=d;
xOffset=-ax/2;
yOffset=-ay/2;
scale2xx=axx-ax*ax;
scale2xy=axy-ax*ay;
scale2yy=ayy-ay*ay;
scale2=powf(scale2xx+scale2yy,0.5);
}
void OpenCVPicture::loadDataWithoutScaling(int flags) {
readImage(filename,mat,flags);
xOffset=-mat.cols/2;
yOffset=-mat.rows/2;
}
void OpenCVPicture::loadData (int scale, int flags) {
readTransformedImage(filename,mat,scale,flags,1,0,0,1,backgroundColor);
xOffset=-mat.cols/2;
yOffset=-mat.rows/2;
}
std::string OpenCVPicture::identify() {
return filename;
}
void OpenCVPicture::codifyInputData(SparseGrid &grid, std::vector<float> &features, int &nSpatialSites, int spatialSize) {
assert(!mat.empty());
assert(mat.type()%8==5);
for (int i=0; i<mat.channels(); i++)
features.push_back(0); //Background feature
grid.backgroundCol=nSpatialSites++;
int x0=std::max(0,-xOffset-spatialSize/2); //If x0<=x<x1 and y0<=y<y1 then the (x,y)-th pixel is in the CNN's visual field.
int x1=std::min(mat.cols,spatialSize-xOffset-spatialSize/2);
int y0=std::max(0,-yOffset-spatialSize/2);
int y1=std::min(mat.rows,spatialSize-yOffset-spatialSize/2);
float* matData=((float*)(mat.data));
for (int x=x0; x<x1; x++) {
for (int y=y0; y<y1; y++) {
bool interestingPixel=false; //Check pixel differs from the background color
for (int i=0; i<mat.channels(); i++)
if (std::abs(scaleUCharColor(matData[i+x*mat.channels()+y*mat.channels()*mat.cols]))>0.02)
interestingPixel=true;
if (interestingPixel) {
int n=(x+xOffset+spatialSize/2)*spatialSize+(y+yOffset+spatialSize/2); //Determine location in the input field.
grid.mp[n]=nSpatialSites++;
for (int i=0; i<mat.channels(); i++) {
features.push_back
(scaleUCharColor(matData[i+x*mat.channels()+y*mat.channels()*mat.cols]));
}
}
}
}
}
void matrixMul2x2inPlace(float& c00, float& c01, float& c10, float& c11, float a00, float a01, float a10, float a11) { //c<-c*a
float t00=c00*a00+c01*a10; float t01=c00*a01+c01*a11;
float t10=c10*a00+c11*a10; float t11=c10*a01+c11*a11;
c00=t00;c01=t01;
c10=t10;c11=t11;
}