-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.cpp
More file actions
238 lines (207 loc) · 6.94 KB
/
renderer.cpp
File metadata and controls
238 lines (207 loc) · 6.94 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
#include "renderer.h"
#include "ui_renderer.h"
//#define _WORKING_WITH_FILES_
Renderer::Renderer(VideoCapture *capturer, int *_brightestPixls, QWidget *parent) :
QDialog(parent),
ui(new Ui::Renderer),
capWebCam(capturer),
serial(NULL),
iterator(0),
brightestPixls(_brightestPixls),
timer(this)
{
ui->setupUi(this);
ui->openGLWidget->setPointCloud(&points);
initSerial(); //Inicializar la comunicacion serial
connect(&timer, SIGNAL(timeout()), this, SLOT(frameBrightestPixels()));
connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), ui->openGLWidget, SLOT(setRotX(int)));
ui->openGLWidget->setMinimumHeight(capWebCam->get(CV_CAP_PROP_FRAME_HEIGHT));
ui->openGLWidget->setMinimumWidth(capWebCam->get(CV_CAP_PROP_FRAME_WIDTH));
#ifndef _WORKING_WITH_FILES_
connect(this, SIGNAL(finishedPixelCalculation()), this, SLOT(processSlice()));
#endif
}
Renderer::~Renderer()
{
//Cerrar el puerto serial
if(serial->isOpen())
serial->close();
delete ui;
}
void Renderer::setFrameSize(int _w, int _h)
{
width = _w;
height = _h;
middle_x = _w / 2;
r_x0 = 0;
r_y0 = 0;
r_xf = width;
r_yf = height;
points.setHeight(height);
}
void Renderer::setAngles(float _laserAngle, float _stepAngle)
{
laserAngle = _laserAngle * 3.1416 / 180;
stepAngle = _stepAngle * 3.1416 / 180;
}
void Renderer::setRange(int x0, int y0, int xf, int yf)
{
qDebug() << "Setting: " << x0 << y0 << xf << yf;
r_x0 = x0;
r_y0 = y0;
r_xf = xf;
r_yf = yf;
}
void Renderer::initSerial()
{
serial = new QSerialPort;
// Initialize Serial
serial->setPortName("/dev/ttyUSB0");
if(serial->open(QIODevice::WriteOnly)) {
qDebug() << "SERIAL: port opened!";
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
}
else
qDebug() << "SERIAL: Couldn't find available serial port!" << endl;
}
void Renderer::captureFrames(int n, int rate) {
n_frames = n;
points.clean();
points.setFrames(n_frames);
iterator = 0;
qDebug() << "Will capture" << n_frames << "frames at" << rate << "ms";
qDebug() << "Range: (" << r_x0 << ", " << r_y0 << ") -> (" << r_xf << ", " << r_yf << ")";
#ifdef _WORKING_WITH_FILES_
frameBrightestPixels_();
#endif
timer.start(rate);
//Send command to arduino to start rotating
if (serial->isOpen() && serial->isWritable()) {
serial->write(QByteArray("s"));
// serial->flush();
qDebug() << "ROTATION: Data sent!" << endl;
}
else
qDebug() << "ROTATION: Couldn't send command!" << endl;
}
void Renderer::frameBrightestPixels()
{
//Obtener imagen desde OpenCV
(*capWebCam) >> transformationMat;
threshold(transformationMat, transformationMat, 235, 255, 3);
cvtColor(transformationMat, transformationMat, CV_BGR2GRAY);
cvtColor(transformationMat, transformationMat, CV_GRAY2BGR);
cvtColor(transformationMat, transformationMat, CV_BGR2HLS); //Imagen en HLS
// //Procesar pixel mas brillante para cada Y
// for (int i=r_y0; i < r_yf; i++) {
// brightestPixls[i] = middle_x;
// for(int j=middle_x; j < r_xf; j++) {
// if(transformationMat.at<cv::Vec3b>(i,j)[1] > transformationMat.at<cv::Vec3b>(i,brightestPixls[i])[1])
// brightestPixls[i] = j;
// }
// }
//Procesar pixel mas brillante para cada Y
int count;
bool continous;
for (int i=r_y0; i < r_yf; i++) {
count = 0; continous = false;
brightestPixls[i] = middle_x;
for(int j=middle_x; j < r_xf; j++) {
if(transformationMat.at<cv::Vec3b>(i,j)[1] == transformationMat.at<cv::Vec3b>(i,brightestPixls[i])[1]) {
count++;
continous = true;
}
else {
continous = false;
if(transformationMat.at<cv::Vec3b>(i,j)[1] > transformationMat.at<cv::Vec3b>(i,brightestPixls[i])[1]) {
count = 0;
brightestPixls[i] = j;
}
}
}
brightestPixls[i] = brightestPixls[i] + count / 2;
}
emit finishedPixelCalculation();
}
void Renderer::frameBrightestPixels_()
{
for(int it = 2; it < 72; ++it) {
//Obtener imagen desde OpenCV
Mat currentMat,
transformationMat;
QString path = "output/scan" + QString::number(it) + ".jpg"; /////////Debug
// QString path = "output/scan40.jpg"; /////////Debug
currentMat = imread(path.toStdString(), CV_LOAD_IMAGE_COLOR);
transformationMat = imread(path.toStdString(), CV_LOAD_IMAGE_COLOR);
threshold(transformationMat, transformationMat, 235, 255, 3);
cvtColor(transformationMat, transformationMat, CV_BGR2GRAY);
cvtColor(transformationMat, transformationMat, CV_GRAY2BGR);
cvtColor(transformationMat, transformationMat, CV_BGR2HLS); //Imagen en HLS
//Procesar pixel mas brillante para cada Y
int count;
bool continous;
for (int i=r_y0; i < r_yf; i++) {
count = 0; continous = false;
brightestPixls[i] = middle_x;
for(int j=middle_x; j < r_xf; j++) {
if(transformationMat.at<cv::Vec3b>(i,j)[1] == transformationMat.at<cv::Vec3b>(i,brightestPixls[i])[1]) {
count++;
continous = true;
}
else {
continous = false;
if(transformationMat.at<cv::Vec3b>(i,j)[1] > transformationMat.at<cv::Vec3b>(i,brightestPixls[i])[1]) {
count = 0;
brightestPixls[i] = j;
}
}
}
brightestPixls[i] = brightestPixls[i] + count / 2;
}
processSlice();
}
points.meshify();
points.saveToFile();
qDebug() << "File was written";
}
void Renderer::processSlice()
{
//Stop timer
if(iterator >= n_frames) {
timer.stop();
points.meshify();
qDebug() << "Timer was stopped";
points.saveToFile();
qDebug() << "File was written";
emit finishedCapturingPoints();
}
//Calculate position in space
float dAngle = stepAngle * iterator++,
dx, dz, x, z;
for (int i=r_y0; i < r_yf; i++) {
dx = brightestPixls[i] - middle_x;
dz = dx / sin(laserAngle);
x = dz * cos(dAngle);
z = dz * sin(dAngle);
// points.push_back(x, height - i * 1.0, -z);
points.push_back(x, height - i * 1.0, z); ////TEST
}
#ifndef _WORKING_WITH_FILES_
ui->openGLWidget->update();
#endif
emit finishedSliceProcessing();
}
void Renderer::turnOnLaser()
{
//Send command to arduino to turn on laser
if (serial->isOpen() && serial->isWritable()) {
serial->write(QByteArray("r%1"));
serial->flush();
}
else
qDebug() << "LASER: Couldn't turn on laser!" << endl;
}