forked from thohel/LaserStripFilterApplication
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
421 lines (329 loc) · 14.1 KB
/
mainwindow.cpp
File metadata and controls
421 lines (329 loc) · 14.1 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <math.h>
#include <list>
#include <iostream>
#include <fstream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(ui->blueSliderMin, SIGNAL(valueChanged(int)), this, SLOT(updateView(int)));
QObject::connect(ui->blueSliderMax, SIGNAL(valueChanged(int)), this, SLOT(updateView(int)));
QObject::connect(ui->greenSliderMin, SIGNAL(valueChanged(int)), this, SLOT(updateView(int)));
QObject::connect(ui->greenSliderMax, SIGNAL(valueChanged(int)), this, SLOT(updateView(int)));
QObject::connect(ui->redSliderMin, SIGNAL(valueChanged(int)), this, SLOT(updateView(int)));
QObject::connect(ui->redSliderMax, SIGNAL(valueChanged(int)), this, SLOT(updateView(int)));
QObject::connect(ui->medianSlider, SIGNAL(valueChanged(int)), this, SLOT(updateView(int)));
QObject::connect(ui->erosionSlider, SIGNAL(valueChanged(int)), this, SLOT(updateView(int)));
QObject::connect(ui->checkBox, SIGNAL(toggled(bool)), this, SLOT(toggleFilter(bool)));
QObject::connect(ui->diffCheckBox, SIGNAL(toggled(bool)), this, SLOT(toggleFilter(bool)));
QObject::connect(ui->diffReferenceCheckBox, SIGNAL(toggled(bool)), this, SLOT(toggleFilter(bool)));
QObject::connect(ui->medianCheckBox, SIGNAL(toggled(bool)), this, SLOT(toggleFilter(bool)));
QObject::connect(ui->erosionCheckBox, SIGNAL(toggled(bool)), this, SLOT(toggleFilter(bool)));
QObject::connect(ui->mainCheckBox, SIGNAL(toggled(bool)), this, SLOT(toggleFilter(bool)));
ui->greenSliderMax->setValue(130);
ui->blueSliderMax->setValue(130);
ui->redSliderMax->setValue(255);
ui->redSliderMin->setValue(30);
ui->medianSlider->setValue(5);
ui->erosionSlider->setValue(1);
ui->diffCheckBox->toggle();
ui->checkBox->toggle();
ui->diffReferenceCheckBox->toggle();
ui->erosionCheckBox->toggle();
ui->medianCheckBox->toggle();
}
QImage MainWindow::mat2qimage(cv::Mat& mat) {
switch ( mat.type() ) {
// 8-bit, 4 channel
case CV_8UC4: {
QImage image( mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB32 );
return image;
}
// 8-bit, 3 channel
case CV_8UC3: {
QImage image( mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888 );
return image.rgbSwapped();
}
// 8-bit, 1 channel
case CV_8UC1: {
static QVector<QRgb> sColorTable;
// only create our color table once
if ( sColorTable.isEmpty() ) {
for ( int i = 0; i < 256; ++i )
sColorTable.push_back( qRgb( i, i, i ) );
}
QImage image( mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8 );
image.setColorTable( sColorTable );
return image;
}
default:
std::cout << "ASM::cvMatToQImage() - cv::Mat image type not handled in switch:" << mat.type();
break;
}
return QImage();
}
MainWindow::~MainWindow()
{
QString path = QString("/home/minions/pikk.ply");
std::ofstream myfile;
myfile.open (path.toUtf8(), std::ofstream::out);
myfile << "ply" << std::endl;
myfile << "format ascii 1.0" << std::endl;
myfile << "comment Made with spinscan!" << std::endl;
myfile << "element vertex " << std::endl;
myfile << "property double x" << std::endl;
myfile << "property double y" << std::endl;
myfile << "property double z" << std::endl;
myfile << "property float nx" << std::endl;
myfile << "property float ny" << std::endl;
myfile << "property float nz" << std::endl;
// myfile << "property uchar red" << std::endl;
// myfile << "property uchar green" << std::endl;
// myfile << "property uchar blue" << std::endl;
myfile << "end_header" << std::endl;
myfile.close();
std::cout << "Wrote to ply file" << std::endl;
processImage(this->filtered, path);
delete ui;
}
void MainWindow::toggleFilter(bool filter)
{
updateView(0);
}
void MainWindow::updateView(int i)
{
cv::Mat before = cv::imread("/home/minions/pictureBefore_1.png");
cv::Mat after = cv::imread("/home/minions/pictureAfter_1.png");
cv::Mat referenceBefore = cv::imread("/home/minions/pictureReferenceBefore_1.png");
cv::Mat referenceAfter = cv::imread("/home/minions/pictureReferenceAfter_1.png");
cv::Mat final = after;
cv::Mat temp;
if (ui->diffCheckBox->isChecked() && ui->mainCheckBox->isChecked()) {
cv::absdiff(before, after, temp);
final = temp;
}
if (ui->diffReferenceCheckBox->isChecked() && ui->mainCheckBox->isChecked()) {
cv::Mat referenceDiff;
cv::absdiff(referenceBefore, referenceAfter, referenceDiff);
cv::absdiff(final, referenceDiff, temp);
final = temp;
}
cv::Mat temp2;
if (ui->sharpeningCheckBox->isChecked() && ui->mainCheckBox->isChecked()) {
cv::GaussianBlur(final, temp2, cv::Size(0, 0), 5);
cv::addWeighted(final, 3, temp2, -1, 0, temp);
final = temp;
}
if (ui->medianCheckBox->isChecked() && ui->mainCheckBox->isChecked()) {
// Post-processing to remove noise
int kernelSize = ui->medianSlider->value();
if (kernelSize % 2 != 1)
kernelSize = kernelSize - 1;
if (kernelSize < 1)
kernelSize = 1;
cv::medianBlur(final, temp, kernelSize);
final = temp;
}
if (ui->erosionCheckBox->isChecked() && ui->mainCheckBox->isChecked()) {
int erosion_size = ui->erosionSlider->value();
cv::Mat element = getStructuringElement(cv::MORPH_RECT,
cv::Size( 2*erosion_size + 1, 2*erosion_size+1 ),
cv::Point( erosion_size, erosion_size ) );
// Apply the erosion operation
erode(final, temp, element );
final = temp;
}
if (ui->checkBox->isChecked() && ui->mainCheckBox->isChecked()) {
cv::inRange(final, cv::Scalar(ui->blueSliderMin->value(), ui->greenSliderMin->value(), ui->redSliderMin->value()),
cv::Scalar(ui->blueSliderMax->value(), ui->greenSliderMax->value(), ui->redSliderMax->value()), temp);
final = temp;
}
QImage qImg = mat2qimage(final);
QPixmap pixMap = QPixmap::fromImage(qImg);
ui->imageLabel->setPixmap(pixMap);
this->filtered = final;
}
/*
void processImages()
{
double angle_step = 1.0;
int steps = round(180/angle_step);
for (int i = 0; i < steps; i++) {
processImage();
}
}
*/
void MainWindow::processImage(cv::Mat img, QString output_file)
{
int cam_res_vert = img.rows;
int cam_res_hor = img.cols;
double radiansToDegrees = 180.0 / 3.14159265359;
double degreesToRadians = 3.14159265359 / 180.0;
// FIXME! Set the correct angle here
double laser_angle = 30.0;
// These values are not perfect, but the variance between cameras is good enough for now
// Assumes hd resolution is used
double cam_focal_distance_pixels = 1081.37;
// double cam_focal_distance_mm = 3.291;
// double cam_cx = 959.5;
// double cam_cy = 539.5;
// double cam_fov_hor = 84.1; // Degrees
// double cam_fov_vert = 53.8;
// double cam_tilt_angle = 0.0; // Degrees
double cam_center_dist = 200.0; // mm
double laser_cam_dist = cam_center_dist*tan(laser_angle*degreesToRadians);
double laser_center_dist = cam_center_dist / cos(laser_angle*degreesToRadians);
/* This assmumes you have brightness information
* currently we only have a binary image, and so this does not work.
for (int y = 0; y < cam_res_vert; y++) {
// find the brightest pixel
brightestValue = 0;
brightestX = -1;
for (int x = 0; x < cam_res_hor; x++) {
int pixelValue = laserImage.pixels[index];
float pixelBrightness = pixelValue >> 16 & 0xFF;
if (pixelBrightness > brightestValue && pixelBrightness > threshold) {
brightestValue = pixelBrightness;
brightestX = x;
}
index++;
}
*/
for (int y = 0; y < cam_res_vert; y++) {
bool found_first_x = false;
int first_x = -1;
int last_x = -1;
for (int x = 0; x < cam_res_hor; x++) {
cv::Scalar intensity = img.at<uchar>(y, x);
if (intensity.val[0] > 200) {
if (!found_first_x) {
found_first_x = true;
std::cout << "Found a point" << std::endl;
std::cout << " X-location is " << x << std::endl;
first_x = x;
} else {
last_x = x;
}
} else if (found_first_x) {
// End of continuous segment of the line. Just break out of the loop and calculate the point.
break;
}
}
if (found_first_x) {
double average_x = double (first_x + last_x)/2;
double pixel_angle_x = atan2(average_x - (cam_res_hor/2), cam_focal_distance_pixels)*radiansToDegrees;
double laser_hor_angle = (180 - 90 - laser_angle);
double laser_cam_angle = 180 - (90 - pixel_angle_x) - laser_hor_angle;
double laser_cam_plane_dist = laser_cam_dist/sin(laser_cam_angle*degreesToRadians)*sin((90 - pixel_angle_x)*degreesToRadians);
double radius = laser_center_dist - laser_cam_plane_dist;
double x_pos = sin(laser_angle*degreesToRadians)*radius;
double y_pos = cos(laser_angle*degreesToRadians)*radius;
double cam_cam_plane_dist = cam_center_dist - y_pos;
double z_pos = y / cam_focal_distance_pixels * cam_cam_plane_dist;
std::ofstream myfile;
myfile.open(output_file.toUtf8(), std::ofstream::out | std::ofstream::app);
myfile << x_pos << " ";
myfile << y_pos << " ";
myfile << z_pos << " ";
myfile << "0.0 0.0 0.0" << std::endl;
myfile.close();
// float pointZ = -atan((camVFOV * degreesToRadians / 2.0)) * 2.0 * camDistance * float(y) / float(videoHeight);
// println("line: " + y + " point: " + pointX + "," + pointY + "," + pointZ);
// println("brightestX: " + brightestX + " camAngle: " + camAngle + " radius: " + radius);
// thisPoint[0] = pointX;
// thisPoint[1] = pointY;
// thisPoint[2] = pointZ;
// println(thisPoint);
// pointList.add(thisPoint);
// framePointList.add(thisPoint);
// FIXME: these normals are bad
// assume normals are all pointing outwards from 0,0,z = pointX,pointY,0 (should be point to camera...)
// normalize it
// float normalLength = sqrt((pointX * pointX) + (pointY * pointY) + (0.0 * 0.0));
// thisNormal[0] = pointX/normalLength;
// thisNormal[1] = pointY/normalLength;
// thisNormal[0] = pointX;
// thisNormal[1] = pointY;
// thisNormal[2] = 0.0;
// normalList.add(thisNormal);
}
}
}
/*
// code based on http://www.sjbaker.org/wiki/index.php?title=A_Simple_3D_Scanner
laserOffset = Float.parseFloat(laserOffsetField.getText());
// all the points in this frame ie. this spline
ArrayList framePointList = new ArrayList();
// println("Processing frame: " + frame + "/" + laserMovie.getFrameCount());
laserMovie.gotoFrameNumber(frame);
laserMovie.read();
laserImage = laserMovie.get();
textureMovie.gotoFrameNumber(frame);
textureMovie.read();
textureImage = textureMovie.get();
int brightestX = 0;
float brightestValue = 0;
laserImage.loadPixels();
textureImage.loadPixels();
int index = 0;
float frameAngle = float(frame) * (360.0 / float(laserMovie.getFrameCount()));
for (int y = 0; y < videoHeight; y++) {
// find the brightest pixel
brightestValue = 0;
brightestX = -1;
for (int x = 0; x < videoWidth; x++) {
int pixelValue = laserImage.pixels[index];
float pixelBrightness = pixelValue >> 16 & 0xFF;
if (pixelBrightness > brightestValue && pixelBrightness > threshold) {
brightestValue = pixelBrightness;
brightestX = x;
}
index++;
}
int[] thisColor = new int[3];
float[] thisPoint = new float[3];
float[] thisNormal = new float[3];
if (brightestX > 0) {
laserImage.pixels[y*videoWidth+brightestX] = color(0, 255, 0);
float r = red(textureImage.pixels[y*videoWidth+brightestX]);
float g = green(textureImage.pixels[y*videoWidth+brightestX]);
float b = blue(textureImage.pixels[y*videoWidth+brightestX]);
thisColor[0] = int(r);
thisColor[1] = int(g);
thisColor[2] = int(b);
colorList.add(thisColor);
float radius;
float camAngle = camHFOV * (0.5 - float(brightestX) / float(videoWidth));
float pointAngle = 180.0 - camAngle + laserOffset;
radius = camDistance * sin(camAngle * degreesToRadians) / sin(pointAngle * degreesToRadians);
float pointX = radius * sin(frameAngle * degreesToRadians);
float pointY = radius * cos(frameAngle * degreesToRadians);
float pointZ = -atan((camVFOV * degreesToRadians / 2.0)) * 2.0 * camDistance * float(y) / float(videoHeight);
// println("line: " + y + " point: " + pointX + "," + pointY + "," + pointZ);
// println("brightestX: " + brightestX + " camAngle: " + camAngle + " radius: " + radius);
thisPoint[0] = pointX;
thisPoint[1] = pointY;
thisPoint[2] = pointZ;
// println(thisPoint);
pointList.add(thisPoint);
framePointList.add(thisPoint);
// FIXME: these normals are bad
// assume normals are all pointing outwards from 0,0,z = pointX,pointY,0 (should be point to camera...)
// normalize it
// float normalLength = sqrt((pointX * pointX) + (pointY * pointY) + (0.0 * 0.0));
// thisNormal[0] = pointX/normalLength;
// thisNormal[1] = pointY/normalLength;
thisNormal[0] = pointX;
thisNormal[1] = pointY;
thisNormal[2] = 0.0;
normalList.add(thisNormal);
p.addPoint(thisPoint[0], -thisPoint[2], -thisPoint[1], r/255.0, g/255.0, b/255.0, 1);
}
}
splineList.add(framePointList);
laserImage.updatePixels();
}
*/