-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
executable file
·168 lines (143 loc) · 5.35 KB
/
mainwindow.cpp
File metadata and controls
executable file
·168 lines (143 loc) · 5.35 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <stdio.h>
#include <stdlib.h>
//#define _NO_WEBCAM_
#define PERIOD 69400
//#define PERIOD 8700
#define ANGLE_CAMERA 45.0
//#define ANGLE_CAMERA 30.0
using namespace std;
using namespace cv;
//Scan scan;
int counter = 0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
highlight(false),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setupCamera();
cameraShow();
brightestPixls = new int[height];
rndr = new Renderer(&capWebCam, brightestPixls, this); //New Renderer
selection = new SelectionOverlay(ui->camera);
selection->installEventFilter(this);
connect(this, SIGNAL(startCapturing(int, int)), rndr, SLOT(captureFrames(int, int)));
connect(ui->commandLinkButton, SIGNAL(clicked(bool)), rndr, SLOT(turnOnLaser()));
connect(rndr, SIGNAL(finishedPixelCalculation()), this, SLOT(highlight_bright_pixels()));
connect(rndr, SIGNAL(finishedCapturingPoints()), this, SLOT(on_finished_scanning()));
rndr->setFrameSize(width, height);
//Setting up progress bar
progressBar = new QProgressBar(ui->statusBar);
progressBar->setAlignment(Qt::AlignRight);
progressBar->setMaximumSize(180, 19);
}
MainWindow::~MainWindow()
{
delete []brightestPixls;
delete timer;
delete ui;
}
void MainWindow::setupCamera()
{
capWebCam.open(1);
if(!capWebCam.isOpened()) {
capWebCam.open(0);
if(!capWebCam.isOpened()) {
cout << "Camera couldn't start" << endl;
return;
}
}
width = capWebCam.get(CV_CAP_PROP_FRAME_WIDTH);
height = capWebCam.get(CV_CAP_PROP_FRAME_HEIGHT);
v_guideline_pos = width / 2;
h_guideline_pos = height * 9 / 10;
}
void MainWindow::cameraShow()
{
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(showCameraFrame()));
timer->start(17);
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if(qobject_cast<QWidget*>(obj)==selection && event->type() == QEvent::MouseButtonPress) {
// qDebug() << "Cl" << static_cast<QMouseEvent*>(event)->x();
selection->setInitialPoint(static_cast<QMouseEvent*>(event)->x(),
static_cast<QMouseEvent*>(event)->y());
selection->movementPoint(static_cast<QMouseEvent*>(event)->x(),
static_cast<QMouseEvent*>(event)->y());
return true;
}
if(qobject_cast<QWidget*>(obj)==selection && event->type() == QEvent::MouseMove) {
// qDebug() << "Mv" << static_cast<QMouseEvent*>(event)->x();
selection->movementPoint(static_cast<QMouseEvent*>(event)->x(),
static_cast<QMouseEvent*>(event)->y());
return true;
}
if(qobject_cast<QWidget*>(obj)==selection && event->type() == QEvent::MouseButtonRelease) {
// qDebug() << "Rl" << static_cast<QMouseEvent*>(event)->x();
selection->setFinalPoint(static_cast<QMouseEvent*>(event)->x(),
static_cast<QMouseEvent*>(event)->y());
return true;
}
return false;
}
void MainWindow::showCameraFrame()
{
capWebCam >> matOriginal;
// cvtColor(matOriginal, matOriginal, CV_BGR2GRAY);
// threshold(matOriginal, matOriginal, 230, 255, 3);
// cvtColor(matOriginal, matOriginal, CV_GRAY2BGR);
cvtColor(matOriginal,matOriginal,CV_BGR2RGB);
camera_frame = QImage((uchar*) matOriginal.data,
matOriginal.cols,
matOriginal.rows,
matOriginal.step,
QImage::Format_RGB888);
//Inicializamos el Painter
QPainter painter;
painter.begin(&camera_frame);
painter.setCompositionMode(QPainter::RasterOp_SourceXorDestination);
painter.setPen(QColor(0x33, 0xff, 0x33));
//Dibujamos las lineas guia
painter.drawLine(v_guideline_pos, 0, v_guideline_pos, height);
painter.drawLine(0, h_guideline_pos, width, h_guideline_pos);
//Escribir sobre pixels mas brillantes
if(highlight) {
for (int i = selection->isSet() ? selection->top() : 0;
i < (selection->isSet() ? selection->bottom() : height);
i++)
camera_frame.setPixel(brightestPixls[i], i, qRgb(0, 0, 255));
}
//Mandar imagen a label: camera
ui->camera->setPixmap(QPixmap::fromImage(camera_frame));
}
void MainWindow::highlight_bright_pixels()
{
highlight = true;
//Updating status bar text
QString msg = QString::number(counter++) + "/" + QString::number(ui->frames_spinBox->value()) + " frames scanned";
ui->statusBar->showMessage(tr(msg.toStdString().c_str()), 1000);
progressBar->setValue(counter);
}
void MainWindow::on_pushButton_2_clicked() //Mesh rendering button
{
rndr->show();
}
void MainWindow::on_scan_bttn_clicked()
{
rndr->setAngles(ANGLE_CAMERA, 360.0 / ui->frames_spinBox->value());
if(selection->isSet())
rndr->setRange(selection->left(), selection->top(), selection->right(), selection->bottom());
ui->pushButton_2->setEnabled(true);
ui->statusBar->addPermanentWidget(progressBar);
progressBar->setMaximum(ui->frames_spinBox->value());
progressBar->setValue(0);
emit startCapturing(ui->frames_spinBox->value(), PERIOD / ui->frames_spinBox->value());
}
void MainWindow::on_finished_scanning()
{
ui->statusBar->showMessage(tr("Scanning finished, now creating mesh..."), 1000);
}