-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
288 lines (248 loc) · 9.55 KB
/
mainwindow.cpp
File metadata and controls
288 lines (248 loc) · 9.55 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <algorithm>
#include <QFileDialog>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QPixmap>
#include <QGraphicsItem>
#include <QTextBrowser>
#include <QtDebug>
#include <QTableWidgetItem>
#include <QDirIterator>
#include <QLCDNumber>
#include <QMessageBox>
#include <QCheckBox>
#include <QProgressDialog>
#include "opencv2/xfeatures2d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
MainWindow::setWindowTitle("Image Comparison");
}
MainWindow::~MainWindow()
{
delete ui;
}
double MainWindow::runSIFT(cv::Mat firstImg, cv::Mat secondImg)
{
// Parameters ...
int nfeatures = 0;
int nOctaveLayers = 3;
double contrastThreshold = 0.04;
double edgeThreshold = 10.0;
double sigma = 1.6;
cv::Ptr<cv::Feature2D> siftDetector = cv::xfeatures2d::SIFT::create(nfeatures, nOctaveLayers, contrastThreshold, edgeThreshold, sigma);
if((firstImg.cols < 100) && (firstImg.rows < 100))
{
cv::resize(firstImg, firstImg, cv::Size(), 200/firstImg.rows, 200/firstImg.cols);
}
if((secondImg.cols < 100) && (secondImg.rows < 100))
{
cv::resize(secondImg, secondImg, cv::Size(), 200/secondImg.rows, 200/secondImg.cols);
}
// Check if the Images are loaded correctly ...
if(firstImg.empty() || secondImg.empty())
{
qDebug() << "Error while trying to read one of the input files!";
return 0;
}
// Keypoints Vectors for the First & Second Image ...
std::vector<cv::KeyPoint> firstImgKeypoints, secondImgKeypoints;
// Detecting Keypoints ...
siftDetector->detect(firstImg, firstImgKeypoints);
siftDetector->detect(secondImg, secondImgKeypoints);
// Descriptors for the First & Second Image ...
cv::Mat firstImgDescriptor, secondImgDescriptor;
// Computing the descriptors
siftDetector->compute(firstImg, firstImgKeypoints, firstImgDescriptor);
siftDetector->compute(secondImg, secondImgKeypoints, secondImgDescriptor);
// Find the matching points
cv::DescriptorMatcher *matcher;
matcher = new cv::BFMatcher(cv::NORM_L1);
std::vector< cv::DMatch > firstMatches, secondMatches;
matcher->match( firstImgDescriptor, secondImgDescriptor, firstMatches );
matcher->match( secondImgDescriptor, firstImgDescriptor, secondMatches );
delete matcher;
int bestMatchesCount = 0;
std::vector< cv::DMatch > bestMatches;
for(uint i = 0; i < firstMatches.size(); ++i)
{
cv::Point matchedPt1 = firstImgKeypoints[i].pt;
cv::Point matchedPt2 = secondImgKeypoints[firstMatches[i].trainIdx].pt;
bool foundInReverse = false;
for(uint j = 0; j < secondMatches.size(); ++j)
{
cv::Point tmpSecImgKeyPnt = secondImgKeypoints[j].pt;
cv::Point tmpFrstImgKeyPntTrn = firstImgKeypoints[secondMatches[j].trainIdx].pt;
if((tmpSecImgKeyPnt == matchedPt2) && ( tmpFrstImgKeyPntTrn == matchedPt1))
{
foundInReverse = true;
break;
}
}
if(foundInReverse)
{
bestMatches.push_back(firstMatches[i]);
bestMatchesCount++;
}
}
double minKeypoints = firstImgKeypoints.size() <= secondImgKeypoints.size() ? firstImgKeypoints.size() : secondImgKeypoints.size();
qDebug() << "Probability = " + QString::number((bestMatchesCount / minKeypoints) * 100);
double number = ((bestMatchesCount/minKeypoints) * 100);
return number;
}
void MainWindow::on_pushButton_2_clicked() //open folder button
{
directory_ = QDir::toNativeSeparators(QFileDialog::getExistingDirectory(this, tr("Select images/folder"), QDir::currentPath()));
currentDir = directory_;
QVector<QString> fileNames;
QVector<QString> fileSize;
QVector<QString> file_dir;
QStringList filter;
filter << "*.jpg" << "*.jpeg" << "*.png" << "*.bmp";
QDirIterator it(directory_, QStringList() << filter, QDir::Files, QDirIterator::Subdirectories);
while(it.hasNext())
{
it.next();
qDebug() << it.fileName() << "\n";
QFileInfo fileInfo(it.fileName());
QString fileNameStr(fileInfo.fileName());
fileNames.append(fileNameStr);
QString file_path(it.filePath());
QFile file_size(file_path);
long long img_size = 0;
img_size = file_size.size();
fileSize.append(QString::number(img_size));
file_dir.append(file_path);
ui->tableWidget->setRowCount(fileNames.size());
for(int i = 0; i < fileNames.size(); ++i)
{
label << "File Name" << "File Size";
ui->tableWidget->setColumnCount(2);
ui->tableWidget->setHorizontalHeaderLabels(label);
ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
//ui->tableWidget->verticalHeader()->hide();
ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
ui->tableWidget->setItem(i, 0, new QTableWidgetItem(fileNames[i]));
ui->tableWidget->setItem(i, 1, new QTableWidgetItem(fileSize[i]));
}
}
}
void MainWindow::on_pushButton_3_clicked() //find button
{
QStringList files;
QVector<QString> fileNames;
QStringList filter;
double match_per = 0.0;
double match = 0;
QProgressDialog progress(this);
progress.setCancelButtonText(tr("&Cancel"));
filter << "*.jpg" << "*.jpeg" << "*.png" << "*.bmp";
QDirIterator it(directory_, QStringList() << filter, QDir::Files, QDirIterator::Subdirectories);
while(it.hasNext())
{
files << it.next();
QFileInfo fileInfo(it.fileName());
QString fileNameStr(fileInfo.fileName());
fileNames.push_back(fileNameStr);
}
for(int i = 0; i < fileNames.size(); ++i)
{
progress.setRange(0, fileNames.size());
progress.setWindowTitle(tr("Finding Match"));
progress.setValue(i);
progress.setLabelText(tr("Searching... file %1 of %n", 0, fileNames.size()).arg(i));
QCoreApplication::processEvents();
if(!files.at(i).isNull())
{
cv::Mat first_img = cv::imread(files.at(i).toStdString());
for(int j = i + 1; j < fileNames.size(); ++j)
{
if(!files.at(j).isNull())
{
cv::Mat sec_img = cv::imread(files.at(j).toStdString());
match = runSIFT(first_img, sec_img);
match_per = ui->lcdNumber->value();
}
if(match >= match_per)
{
matchedfileNames.push_back(fileNames[j]);
matchedfilePath.push_back(files.at(j));
}
}
}
}
std::sort(matchedfilePath.begin(), matchedfilePath.end());
matchedfilePath.erase(std::unique(matchedfilePath.begin(), matchedfilePath.end()), matchedfilePath.end());
std::sort(matchedfileNames.begin(), matchedfileNames.end());
matchedfileNames.erase(std::unique(matchedfileNames.begin(), matchedfileNames.end()), matchedfileNames.end());
if(matchedfileNames.isEmpty())
{
QMessageBox Msgbox;
Msgbox.setStyleSheet("QLabel{min-width: 300px;}");
Msgbox.setText("No match found");
Msgbox.exec();
}
if(!matchedfileNames.isEmpty())
{
QMessageBox Msgbox;
Msgbox.setStyleSheet("QLabel{min-width: 300px;}");
Msgbox.setText("Match found");
ui->tableWidget->clearContents();
Msgbox.exec();
}
for(int i = 0; i < matchedfileNames.size(); ++i)
{
ui->tableWidget->setRowCount(matchedfileNames.size());
qDebug() << "matched files" << matchedfileNames[i];
label.insert(1, "Select Files");
ui->tableWidget->setColumnCount(2);
ui->tableWidget->setHorizontalHeaderLabels(label);
ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
//ui->tableWidget->verticalHeader()->hide();
ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
ui->tableWidget->setItem(i, 0, new QTableWidgetItem(matchedfileNames[i]));
checkBoxWidget = new QWidget;
QCheckBox* checkBox = new QCheckBox;
QHBoxLayout* layoutCheckBox = new QHBoxLayout(checkBoxWidget);
layoutCheckBox->addWidget(checkBox);
layoutCheckBox->setAlignment(Qt::AlignCenter);
layoutCheckBox->setContentsMargins(0, 0, 0, 0);
ui->tableWidget->setCellWidget(i, 1, checkBoxWidget);
}
}
bool MainWindow::fileExists(const QString& str)
{
if(std::find(matchedfileNames.begin(), matchedfileNames.end(), str) != matchedfileNames.end())
return true;
else
return false;
}
void MainWindow::on_horizontalSlider_valueChanged(int value) //slider
{
ui->lcdNumber->display(value);
}
void MainWindow::on_pushButton_4_clicked() //Delete button
{
for(int i = 0; i < matchedfileNames.size(); ++i)
{
QWidget* item = (ui->tableWidget->cellWidget(i, 1));
QCheckBox* checkB = qobject_cast<QCheckBox*>(item->layout()->itemAt(0)->widget());
if(checkB->isChecked())
{
QDir file;
file.remove(matchedfilePath[i]);
}
}
QMessageBox Msgbox;
Msgbox.setStyleSheet("QLabel{min-width: 300px;}");
Msgbox.setText("File deleted sucessfully");
Msgbox.exec();
ui->tableWidget->clearContents();
ui->tableWidget->setColumnCount(0);
ui->tableWidget->verticalHeader()->hide();
}