-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
257 lines (239 loc) · 6.9 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "imagelist.h"
#include "betafacepoints.h"
#include "dlibxml.h"
#include <iostream>
#include <QPixmap>
#include <glob.h>
#include <cmath>
#include <QDir>
#include <QCloseEvent>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
this->setFixedSize(700,700);
ui->setupUi(this);
ui->centralWidget->layout()->setMargin(0);
ui->centralWidget->layout()->setSpacing(0);
ui->label_image->setAlignment(Qt::AlignTop);
ImageList imagelist;
namesList = imagelist.getNameList();
name = namesList.back();
int size = namesList.size();
namesList.pop_back();
int size1 = namesList.size();
}
void MainWindow :: mousePressEvent(QMouseEvent *e)
{
mFirstX=0;
mFirstY=0;
mFirstClick=true;
mpaintflag=false;
if(e->button() == Qt::LeftButton)
{
if(1)
{
QPointF point;
point.rx() = e->x();
point.ry() = e->y();
modifyNewPoints(point);
update();
}
}
}
void MainWindow :: paintEvent(QPaintEvent * e)
{
QMainWindow::paintEvent(e);
if(1)
{
QImage uiImage = displayImage(name);
QPainter painter(&uiImage);
QPen paintpen(Qt::red);
paintpen.setWidth(5);
painter.setPen(paintpen);
getCorrectPoints();
map<double,QPointF> :: iterator itr;
for(itr=newPoints.begin();itr!=newPoints.end();++itr){
QPointF point = itr->second;
painter.drawPoint(point);
}
painter.end();
ui->label_image->setPixmap(QPixmap::fromImage(uiImage));
}
}
void MainWindow::getCorrectPoints()
{
map<double,QPointF>::iterator itr;
//for first time showing points
if(newPoints.empty()){
for(itr=facialPoints.begin();itr!=facialPoints.end();++itr){
double type = itr->first;
QPointF point = itr->second;
double x = point.rx();
double y = point.ry();
x = (x-cropLeft)*scalingFactorX;
y = (y-cropTop)*scalingFactorY;
point.setX(x);
point.setY(y);
newPoints.insert(pair<double,QPointF>(type,point));
}
}
}
void MainWindow::modifyNewPoints(QPointF point)
{
map<double,QPointF>::iterator itr;
int flag=0;
if(removePoint==1){
for(itr=newPoints.begin();itr!=newPoints.end();++itr){
QPointF oldPoint = itr->second;
oldPoint = oldPoint-point;
if(abs(oldPoint.rx())<=2 && abs(oldPoint.ry())<=2 && abs(oldPoint.rx())>=0 && abs(oldPoint.ry())>=0){
type = itr->first;
flag=1;
}
}
if(flag==1){
//qDebug()<<"Type : "<<type;
removePoint=0;
newPoints.erase(type);
}
}else{
newPoints.insert(pair<double,QPointF>(type,point));
removePoint=1;
}
}
QImage MainWindow::displayImage(string name)
{
Mat srcImage = getCurrentImage(name);
BetafacePoints betafacePoints(name);
facialPoints = betafacePoints.generatePoints();
QImage scaledImage;
if(facialPoints.size()==0){
qDebug()<<"Files not found for "<<QString::fromStdString(name);
on_saveQuit_clicked();
}else{
QImage newImage = getBoundingBoxImage(srcImage,facialPoints);
int w = this->width();
int h = 600;
scaledImage = newImage.scaled(w,h,Qt::KeepAspectRatio);
scalingFactorX = (double)scaledImage.width()/newImage.width();
scalingFactorY = (double)scaledImage.height()/newImage.height();
}
return scaledImage;
}
QImage MainWindow::getBoundingBoxImage(Mat tempImage,map<double,QPointF> facialPoints)
{
map<double,QPointF> :: iterator itr;
double topLeftX=99999, topLeftY=99999;
double width=0, height=0;
QPointF point;
for(itr=facialPoints.begin();itr!=facialPoints.end();++itr){
point = itr->second;
double pointX = point.rx();
double pointY = point.ry();
if(topLeftX > pointX){
topLeftX = pointX;
}
if(topLeftY > pointY){
topLeftY = pointY;
}
if(width<pointX){
width = pointX;
}
if(height<pointY){
height=pointY;
}
}
if(topLeftX<0){
topLeftX = 0;
}
if(topLeftY<0){
topLeftY = 0;
}
if(topLeftX-10>0){
topLeftX = topLeftX-10;
}
if(topLeftY-20>0){
topLeftY = topLeftY-20;
}
width = width-topLeftX;
height = height-topLeftY;
if(width+20<tempImage.size().width){
width = width+20;
}
if(height+20<tempImage.size().height){
height = height+20;
}
Rect rectangle(topLeftX,topLeftY,width,height);
rect = rectangle;
cropLeft = topLeftX;
cropTop = topLeftY;
Mat subImage(tempImage,rect);
QImage resultImage = Mat2QImage(subImage);
return resultImage;
}
Mat MainWindow::getCurrentImage(string name){
ImageList imageList;
string path = imageList.getPath();
path = path + "/" + name + "/shape/input.jpg";
Mat srcImage = cv::imread(path);
return srcImage;
}
Mat MainWindow::QImage2Mat(QImage const& src)
{
cv::Mat tmp(src.height(),src.width(),CV_8UC3,(uchar*)src.bits(),src.bytesPerLine());
cv::Mat result; // deep copy just in case (my lack of knowledge with open cv)
cvtColor(tmp, result,CV_BGR2RGB);
return result;
}
QImage MainWindow::Mat2QImage(Mat const& src)
{
cv::Mat temp; // make the same cv::Mat
cvtColor(src, temp,CV_BGR2RGB); // cvtColor Makes a copt, that what i need
QImage dest((const uchar *) temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
dest.bits(); // enforce deep copy, see documentation
// of QImage::QImage ( const uchar * data, int width, int height, Format format )
return dest;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initializeMyWidget()
{
myWidget = new QWidget();
myWidget->show();
}
void MainWindow::on_saveNext_clicked()
{
DlibXML dlibXML(name,newPoints,rect,scalingFactorX,scalingFactorY);
if(namesList.size()==0){
dlibXML.generateXML();
this->close();
}else{
dlibXML.createSingleXMLFile();
name = namesList.back();
namesList.pop_back();
newPoints.clear();
}
}
void MainWindow::on_saveQuit_clicked()
{
DlibXML dlibXML(name,newPoints,rect,scalingFactorX,scalingFactorY);
dlibXML.createSingleXMLFile();
ImageList imageList;
imageList.createFileWithNames(namesList);
this->close();
}
void MainWindow::on_generateXML_clicked()
{
DlibXML dlibXML(name,newPoints,rect,scalingFactorX,scalingFactorY);
dlibXML.generateXML();
}
void MainWindow::closeEvent (QCloseEvent *event)
{
on_saveQuit_clicked();
}