-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.h
More file actions
392 lines (313 loc) · 10.3 KB
/
utils.h
File metadata and controls
392 lines (313 loc) · 10.3 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
Created by: Jason Carlisle Mann (on2valhalla | jcm2207@columbia.edu)
A utility class containing useful tools for interacting with images in
QT and OpenCV, as well as providing other functions for Image Matching
*/
#ifndef UTILS_H
#define UTILS_H
//QT
#include <QTextCursor>
//OpenCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
//SYS
#include <vector>
#include <string>
#include <iostream>
namespace util
{
using namespace std;
using namespace cv;
static const string IMG_DIR = "../../../../ImageMatching/img/";
static const int NUM_IMAGES = 40;
//copies many images to one image
//all images must be the same size as the first
Mat manyToOne(vector<Mat> &images, int numRows, int numCols)
{
Mat sampleImage = images[0];
int imageWidth = sampleImage.cols;
int imageHeight = sampleImage.rows;
int buffer = 5;
int bigWidth = numRows * (imageHeight + 2*buffer);
int bigHeight = numCols * (imageWidth + 2*buffer);
Mat bigImage ( bigWidth, bigHeight, sampleImage.type());
for (unsigned int i = 0; i < images.size(); i++)
{
int row = i / numCols;
int col = i % numCols;
int x = col * imageWidth + 2*col*buffer + buffer;
int y = row * imageHeight + 2*row*buffer + buffer;
Mat roi(bigImage, Rect(x, y, imageWidth, imageHeight));
images[i].copyTo(roi);
}
return bigImage;
}
//Gets number of images from a specified directory
void getImages(vector<Mat> &images, vector<string> &fileNames, string dir, int count)
{
for (int i = 1; i <= count; i++)
{
stringstream sstm;
if( i >= 10)
sstm << dir << "i" << i << ".ppm";
else
sstm << dir << "i0" << i << ".ppm";
string fileName = sstm.str();
fileNames.push_back(fileName);
Mat image = imread(fileName, 1);
images.push_back(image);
}
}
// take a list of images and give back histograms
//
void getHistograms(const vector<Mat> &images, vector<Mat> &histograms, int buckets = 10, int blackThresh = 25, int whiteThresh = 25)
{
const int histSize[] = {buckets, buckets, buckets};
for (unsigned int i = 0; i < images.size(); i++)
{
Mat image = images[i];
Mat histogram(3, histSize, CV_32F, Scalar(0));
CV_Assert(image.type() == CV_8UC3);
int numPixels = 0;
for (int j = 0; j < image.rows; j++)
for (int k = 0; k < image.cols; k++)
{
const Vec3b& pix = image.at<Vec3b>(j,k);
//threshold black values by skipping them
if(pix[0] + pix[1] + pix[2] < blackThresh*3)
continue;
//threshold white values by skipping them
if(pix[0] + pix[1] + pix[2] > (255 - whiteThresh)*3 )
continue;
histogram.at<float>(pix[0]*buckets/256, pix[1]*buckets/256, pix[2]*buckets/256) += 1.0;
numPixels +=1;
}
//Get a 3d iterator that returns a Mat that is
//a slice (plane) of the original nary matrix
cv::Mat plane;
const cv::Mat* hists[] = { &histogram, 0 };
cv::NAryMatIterator itN(hists, &plane, 1);
//Normalize the histogram
double normRatio = 1./numPixels;
for(unsigned int p = 0; p < itN.nplanes; p++, ++itN)
itN.planes[0] *= normRatio;
histograms.push_back(histogram);
}
}
// calculate the L1 norm of all the histograms
void calcL1Norm(vector<Mat> &histograms, double locals[NUM_IMAGES][2][2],
double globals[2][3], Mat &allVals)
{
globals[0][0] = 10000; //global min val
globals[1][0] = 0; // global max val
for (int i = 0; i < NUM_IMAGES; i++)
{
locals[i][0][0] = 10000; //local min val
locals[i][1][0] = 0; //local max val
for (int j = 0; j < NUM_IMAGES; j++)
{
// get the l1 norm of the two histograms
double normVal = norm(histograms[i],histograms[j], NORM_L1);
// map the value to [0,1] (1 is the same image)
normVal = 1 - (normVal/2.);
if(normVal < 0)
normVal *= -1;
// keep track of all the values
if(j < i)
allVals.at<float>(i, j) = (float) normVal;
else if(j > i)
allVals.at<float>(i, j-1) = (float) normVal;
// cout << allVals.at<float>(i,j) <<endl;
//hack to make min max work
if( i == j)
continue;
if (normVal < locals[i][0][0])
{
locals[i][0][0] = normVal;
locals[i][0][1] = j;
}
if (normVal > locals[i][1][0])
{
locals[i][1][0] = normVal;
locals[i][1][1] = j;
}
if (normVal < globals[0][0])
{
globals[0][0] = normVal;
globals[0][1] = i;
globals[0][2] = j;
}
if (normVal > globals[1][0])
{
globals[1][0] = normVal;
globals[1][1] = i;
globals[1][2] = j;
}
}
}
}
// converts image to grey
void bgrToGrey(const Mat &image, Mat &grey)
{
// recreate if necessary
grey.create(image.size(), CV_32F);
for( int j = 0; j < image.rows; j++)
for( int k = 0; k < image.cols; k++)
{
const Vec3b& pix = image.at<Vec3b>(j,k);
grey.at<float>(j,k) = (pix[0] + pix[1] + pix[2])/3;
}
}
// input an image in greyscale
void applyLaplacian(const Mat &grey, Mat &laplacian)
{
//make kernel for laplacian
float kernel[3][3] = {{1., 1., 1.},
{1., -8., 1.},
{1., 1., 1.}};
//create the Mat if necessary
laplacian.create(grey.size(), CV_32F);
for( int j = 0; j < grey.rows; j++)
for( int k = 0; k < grey.cols; k++)
{
laplacian.at<float>(j,k) = grey.at<float>(j,k);
//skip the edge elements
if(j == 0 || k == 0 ||
j == grey.rows -1 || k == grey.cols -1)
continue;
//apply kernel
laplacian.at<float>(j,k) *= kernel[1][1];
laplacian.at<float>(j,k) += grey.at<float>(j-1,k-1) * kernel[0][0];
laplacian.at<float>(j,k) += grey.at<float>(j-1,k) * kernel[0][1];
laplacian.at<float>(j,k) += grey.at<float>(j-1,k+1) * kernel[0][2];
laplacian.at<float>(j,k) += grey.at<float>(j,k-1) * kernel[1][0];
laplacian.at<float>(j,k) += grey.at<float>(j,k+1) * kernel[1][2];
laplacian.at<float>(j,k) += grey.at<float>(j-1,k-1) * kernel[2][0];
laplacian.at<float>(j,k) += grey.at<float>(j,k-1) * kernel[2][1];
laplacian.at<float>(j,k) += grey.at<float>(j+1,k-1) * kernel[2][2];
// // testing functionality to find the range
// // for background exclusion
// if(fabs(laplacian.at<float>(j,k)) < 10)
// laplacian.at<float>(j,k) = 1;
// else
// laplacian.at<float>(j,k) = 0;
}
}
void getLaplaceHistogram(const Mat &laplacian, Mat &histogram,
float buckets = 4000, float lapAbsMax = 1800, float laplaceThresh = 12)
{
// init the histogram and clear it out
histogram = Mat(1, buckets, CV_32F, Scalar(0));
int totalPix = 0;
for (int j=0; j <laplacian.rows; j++)
for (int k=0; k <laplacian.rows; k++)
{
//count the pixels for each bucket
float pixel = laplacian.at<float>(j,k);
// //threshold here if you will, but i do not
// if(fabs(pixel) < laplaceThresh)
// continue;
// have to take the negative values into account
int idx = (int) pixel * buckets/(2*lapAbsMax) + buckets/2;
histogram.at<float>(0,idx) += 1.0;
totalPix +=1;
}
// NORMALIZE the histogram
float normRatio = 1./totalPix;
histogram *= normRatio;
}
void displayResults(QTextCursor &curs, vector<string> &fileNames,
Mat &values)
{
//get display images
vector<QImage> qImages(NUM_IMAGES);
for (int i = 0; i< NUM_IMAGES; i++)
qImages[i] = QImage(fileNames[i].c_str());
curs.insertText("Local Matches (Max/Min)");
curs.insertTable(40,3);
for (int i = 0; i< NUM_IMAGES; i++)
{
curs.insertImage(qImages[i]);
curs.insertText("\n" + QString(fileNames[i].c_str()).section("/",-1));
curs.movePosition(QTextCursor::NextCell);
//find min/max
double min, max;
int minIdx[2], maxIdx[2];
minMaxIdx(values.row(i), &min, &max, minIdx, maxIdx);
if( minIdx[1] >= i)
minIdx[1]++;
if( maxIdx[1] >= i)
maxIdx[1]++;
curs.insertImage(qImages[maxIdx[1]]);
curs.insertText("\n" + QString(fileNames[maxIdx[1]].c_str()).section("/",-1)
+ QString("\n%1").arg(max,4,'f',5));
curs.movePosition(QTextCursor::NextCell);
curs.insertImage(qImages[minIdx[1]]);
curs.insertText("\n" + QString(fileNames[minIdx[1]].c_str()).section("/",-1)
+ QString("\n%1").arg(min,4,'f',5));
curs.movePosition(QTextCursor::NextCell);
}
curs.movePosition(QTextCursor::End);
curs.insertText("\n\nGlobal Matches (Max/Min)");
curs.insertTable(2,2);
//find min/max
double min, max;
int minIdx[2], maxIdx[2];
minMaxIdx(values, &min, &max, minIdx, maxIdx);
if(minIdx[1] >= minIdx[0])
minIdx[1]++;
if(maxIdx[1] >= maxIdx[0])
maxIdx[1]++;
curs.insertImage(qImages[maxIdx[0]]);
curs.insertText("\n" + QString(fileNames[maxIdx[0]].c_str()).section("/",-1)
+ QString("\n%1").arg(max));
curs.movePosition(QTextCursor::NextCell);
curs.insertImage(qImages[maxIdx[1]]);
curs.insertText("\n" + QString(fileNames[maxIdx[1]].c_str()).section("/",-1)
+ QString("\n%1").arg(max));
curs.movePosition(QTextCursor::NextCell);
curs.insertImage(qImages[minIdx[0]]);
curs.insertText("\n" + QString(fileNames[minIdx[0]].c_str()).section("/",-1)
+ QString("\n%1").arg(min));
curs.movePosition(QTextCursor::NextCell);
curs.insertImage(qImages[minIdx[1]]);
curs.insertText("\n" + QString(fileNames[minIdx[1]].c_str()).section("/",-1)
+ QString("\n%1").arg(min));
curs.movePosition(QTextCursor::NextCell);
}
// The parameter data should be representative of distance and normalized
static const float VALUE = 10.;
void matToJson(cv::Mat &data, std::string fileName)
{
std::ofstream outputFile;
std::cout << "Writing JSON to: " << fileName <<std::endl;
outputFile.open(fileName.c_str(), std::ofstream::trunc | std::ofstream::out);
outputFile << "{\"nodes\":[\n";
for (int i = 1; i <= data.rows; i++)
{
outputFile << "\t{\"name\":" << "\"" << i << "\", \"group\":" << i / 10 ;
if(i < 10)
outputFile << ", \"img\":\"http://0.0.0.0:8000/img/i0" << i << ".jpg\"}, \n";
else
outputFile <<", \"img\":\"http://0.0.0.0:8000/img/i" << i << ".jpg\"}, \n";
}
// remove the comma
long pos = outputFile.tellp();
outputFile.seekp(pos-3);
outputFile << "\n],\n" << "\"links\":[\n";
for (int i = 0; i < data.rows; i++)
for (int j = 0; j < data.rows; j++)
{
if (i == j)
continue;
outputFile << "\t{\"source\":" << i << ", \"target\":" << j
<< ", \"value\":" << data.at<float>(i,j) * VALUE << "}, \n";
}
// remove the comma
pos = outputFile.tellp();
outputFile.seekp(pos-3);
outputFile << "\n]}";
outputFile.close();
}
}
#endif // UTILS_H