-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
338 lines (269 loc) · 8.84 KB
/
mainwindow.cpp
File metadata and controls
338 lines (269 loc) · 8.84 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
/*
Created by: Jason Carlisle Mann (on2valhalla | jcm2207@columbia.edu)
Main Window for the Image Matching program
*/
//local
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "utils.h"
using namespace util;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::run()
{
//Retrieve the images from the filesystem
vector<string> fileNames;
vector<Mat> images;
getImages(images, fileNames, IMG_DIR, NUM_IMAGES);
// -1 for y val because you dont want to include self match
// it messes with the minmax function (see inside for workaround)
Mat colorVals(NUM_IMAGES, NUM_IMAGES -1, CV_32F);
Mat textureVals(NUM_IMAGES, NUM_IMAGES -1, CV_32F);
Mat comboVals(NUM_IMAGES, NUM_IMAGES -1, CV_32F);
QTextCursor curs = this->ui->textEdit->textCursor();
curs.insertText("COLOR\n---------\n");
colorMatch(fileNames, images, colorVals);
textureMatch(fileNames, images, textureVals);
comboMatch(fileNames, colorVals, textureVals, comboVals);
// convert the combination values to distances
// and disregard matches to same element
// (2 is out of range for min)
comboVals = 1 - comboVals;
for (int i = 0; i < comboVals.rows; i++)
comboVals.at<float>(i,i) = 2;
DendNode *completeTree = linkage(comboVals, 0);
DendNode *singleTree = linkage(comboVals, 1);
completeTree->toJson("/Users/on2valhalla/Documents/school/Visual Interfaces/QTest/ImageMatching/completeLink.json");
singleTree->toJson("/Users/on2valhalla/Documents/school/Visual Interfaces/QTest/ImageMatching/singleLink.json");
comboVals = 1 - comboVals;
matToJson(comboVals, "/Users/on2valhalla/Documents/school/Visual Interfaces/QTest/ImageMatching/allLinks.json");
if(DISPLAY)
{
QTextCursor curs = this->ui->textEdit->textCursor();
curs.insertText("\n\n\n\nDendrogram For Complete Link");
curs.insertTable(1,2);
QTextCursor imgCurs(curs);
curs.movePosition(QTextCursor::NextCell);
curs.insertText("\n\n");
drawDendrogram(curs, imgCurs, fileNames, completeTree, 40, 0);
curs.movePosition(QTextCursor::End);
curs.insertText("\n\n\n\nDendrogram For Single Link");
curs.insertTable(1,2);
imgCurs = curs;
curs.movePosition(QTextCursor::NextCell);
curs.insertText("\n\n");
drawDendrogram(curs, imgCurs, fileNames, singleTree, 100, 0);
}
// To write the contents of the textEdit to an html file
// QTextDocument* document = this->ui->textEdit->document();
// QTextDocumentWriter writer("/Users/on2valhalla/Documents/school/Visual Interfaces/someFile.html", "HTML");
// if (!writer.write(document))
// {
// cout << "error";
// }
}
void MainWindow::colorMatch(vector<string> &fileNames, vector<Mat> &images,
Mat &colorVals)
{
const int BUCKETS = 10, B_THRESH = 30, W_THRESH = 0;
//get their respective histograms
vector<Mat> histograms;
getHistograms(images, histograms, BUCKETS, B_THRESH, W_THRESH);
//will hold the normalized L1 comparison data
double locals[NUM_IMAGES][2][2];
double globals[2][3];
calcL1Norm(histograms, locals, globals, colorVals);
if(DISPLAY)
{
QTextCursor curs = this->ui->textEdit->textCursor();
curs.insertText("COLOR\n---------\n");
displayResults(curs, fileNames, colorVals);
Mat bigImage = manyToOne(images, 10, 4);
namedWindow("all");
imshow("all", bigImage);
}
}
void MainWindow::textureMatch(vector<string> &fileNames, vector<Mat> &images,
Mat &textureVals)
{
int NUM_IMAGES = images.size();
//will hold processed histograms
vector<Mat> histograms(NUM_IMAGES);
vector<Mat> laplacians(NUM_IMAGES);
// for testing the min and max ranges of the laplacians
// Mat minMax(NUM_IMAGES, 2, CV_32F);
for (int i = 0; i < NUM_IMAGES; i++)
{
// convert image to greyscale
Mat grey;
bgrToGrey(images[i], grey);
// apply the laplacian kernel
Mat laplacian;
applyLaplacian(grey, laplacian);
// // FOR TESTING
// // find local and global max
// double min =0, max =0;
// minMaxIdx(laplacian, &min, &max);
// minMax.at<float>(i,0) = min;
// minMax.at<float>(i,1) = max;
// Scalar avg = mean(laplacian);
// this->ui->textEdit->append(QString("i: %3\tmin: %1\tavg: %4\tmax: %2")
// .arg(min,5,'f').arg(max).arg(i).arg(avg[0]*100,5,'f'));
// HISTOGRAM the laplacian
Mat histogram;
getLaplaceHistogram(laplacian, histogram);
// add the histogram to the vector
histograms[i] = histogram;
// keep the laplacian for display
laplacian *= 1./200;
laplacians[i] = laplacian;
}
// // FOR TESTING
// // for testing global maxs for laplacian
// double min =0, max =0;
// minMaxIdx(minMax, &min, &max);
// this->ui->textEdit->append(QString("global min: %1\tglobal max: %2")
// .arg(min).arg(max));
// calculate the normalized L1 comparison of the histograms
double locals[NUM_IMAGES][2][2], globals[2][3];
calcL1Norm(histograms, locals, globals, textureVals);
if(DISPLAY)
{
// display the results
QTextCursor curs = this->ui->textEdit->textCursor();
curs.insertText("\n\n\nTEXTURE\n---------\n");
displayResults(curs, fileNames, textureVals);
// display the laplacian images
Mat bigImage = manyToOne(laplacians, 10, 4);
namedWindow("laplacian");
imshow("laplacian", bigImage);
}
}
void MainWindow::comboMatch(vector<string> &fileNames,
Mat &colorVals, Mat &textureVals, Mat &comboVals)
{
float r = .5, s = 1.0;
QTextCursor curs = this->ui->textEdit->textCursor();
for (int i = 0; i < NUM_IMAGES; i ++)
for (int j = 0; j < NUM_IMAGES; j ++)
{
// keep track of all the values
if(j < i)
{
float colorVal = colorVals.at<float>(i,j);
float textureVal = textureVals.at<float>(i,j);
float comboVal = (r * colorVal) + ((s - r) * textureVal);
comboVals.at<float>(i, j) = comboVal;
}
else if(j > i)
{
float colorVal = colorVals.at<float>(i,j-1);
float textureVal = textureVals.at<float>(i,j-1);
float comboVal = (r * colorVal) + ((s - r) * textureVal);
comboVals.at<float>(i, j-1) = comboVal;
}
}
if(DISPLAY)
{
curs.insertText("\n\n\nCOMBO\n---------\n");
displayResults(curs, fileNames, comboVals);
}
}
// Clustering images together based on their similarity data
// linkage type = 0 for complete link
// linkage type = 1 for single link
DendNode* MainWindow::linkage(Mat &comboVals, int linkageType)
{
// initialize a node on the list for every image
list<DendNode*> nodes;
for (int i = 0; i < NUM_IMAGES; i++)
{
DendNode *node = new DendNode(i);
nodes.push_back(node);
}
// loop until only one element remains
while (nodes.size() > 1)
{
/*
for every node in the list, check the rest of the list
for the minimum match... remember to check all the children
of every node (all indicies are contained in a childIndicies
list)
*/
float min = 2;
list<DendNode*>::iterator it, jt, mini, minj;
for (it = nodes.begin(); it != nodes.end(); it++)
for (jt = nodes.begin(); jt != nodes.end(); jt++)
{
if(it == jt)
continue;
float match;
if(linkageType == 0)
match = (*it)->maxMatch( *jt, comboVals );
else
match = (*it)->minMatch( *jt, comboVals );
if( match < min)
{
min = match;
mini = it;
minj = jt;
}
}
// Once the min value and indicies are found, create a new node
// with the two indicies as children
DendNode *match = new DendNode(*mini, *minj, min);
nodes.erase(mini);
nodes.erase(minj);
nodes.push_back(match);
}
// pop the final node
DendNode* root = *nodes.begin();
return root;
}
// Draw the created dendrogram from the root node (recursive)
// Note: Very finicky.... dont mess with the spacing
int MainWindow::drawDendrogram(QTextCursor &texCurs,QTextCursor &imgCurs, vector<string> &imgNames,
DendNode *tree, int width, int startWidth)
{
if (tree->distance == -1)
{
int i = tree->getIdx();
QImage image(imgNames[i].c_str());
imgCurs.insertImage(image);
if (i % 4 == 0)
imgCurs.insertText(QString("\n%1\n\n\n").arg(imgNames[i].c_str()));
else
imgCurs.insertText(QString("\n%1\n\n").arg(imgNames[i].c_str()));
return 0;
}
int baseD = (int)(tree->distance * width);
// in order recursive traversal
int leftD = drawDendrogram(texCurs, imgCurs, imgNames, tree->left, width, startWidth);
//draw linkage
int length = baseD - leftD;
for (int i = 0; i < length; ++i)
texCurs.insertText("X");
texCurs.insertText("▼\n");
for(int j = 0; j < 6; j++)
{
for (int i = 0; i < baseD+2; i++)
texCurs.insertText(" ");
texCurs.insertText("|\n");
}
// fill in the right node
int rightD = drawDendrogram(texCurs, imgCurs, imgNames, tree->right, width, startWidth);
if(rightD != leftD)
length = baseD - rightD;
for (int i = 0; i < length; i++)
texCurs.insertText("X");
texCurs.insertText("▲");
return baseD;
}