-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
661 lines (520 loc) · 17.3 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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"
#include "QColor"
#include "configdlg.h"
#include "Windows.h"
#include <QProcess>
#include "QDesktopServices"
#include "QUrl"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->pushButton->setVisible(0);
locator_offset = getConfigValue(13);
lastHeigth = getConfigValue(17);
calibrationValue = getConfigValue(19).toDouble();
ui->tableWidget->setColumnCount(3);
// ui->tableWidget->setFixedWidth(200);
ui->tableWidget->setRowCount(10);
// ui->tableWidget->setItem(1,1, new QTableWidgetItem(QString::number(5.13)));
// ui->tableWidget->item(1,1)->setBackgroundColor(QColor("red"));
/*connet to the liveFrame thread*/
frameGrabber = new CFrameGrabber;
qRegisterMetaType< Mat >("Mat");
connect(frameGrabber, SIGNAL(cameraErr()), this, SLOT(cameraWarningWdiget()));
connect(frameGrabber, SIGNAL(updataFrame(Mat)), this, SLOT(displayLiveFrame(Mat)));
connect(this, SIGNAL(grabCalibrationFrame()), frameGrabber, SLOT(grabCalibrateFrame()));
connect(frameGrabber, SIGNAL(calibrateFrame(Mat)), this, SLOT(grabCalibrationFrame(Mat)));
connect(frameGrabber, SIGNAL(sendDistDataToUI(QList<int>&)), this, SLOT(displayDistData(QList<int>&)));
connect(this, SIGNAL(signal_waveCheck()), frameGrabber, SLOT(slot_waveCheck2ProcThread()));
frameGrabber->start();
/*open the serial port connected to PC*/
serialPort = new QSerialPort;
serialPort->setPortName(getConfigValue(4));
serialPort->open(QIODevice::ReadWrite);
if (!serialPort->isOpen())
{
QWidget *comPortWarning = new QWidget;
QMessageBox::warning(comPortWarning,tr("错误"), tr("串口打开错误,请检查串口连接及串口号设置!"));
}
serialPort->setBaudRate(QSerialPort::Baud19200);
serialPort->setDataBits(QSerialPort::Data8);
serialPort->setParity(QSerialPort::NoParity);
serialPort->setStopBits(QSerialPort::OneStop);
/*Notify the subthread ger ready to grab frames now*/
connect(this, SIGNAL(grabFrameNow()), frameGrabber, SLOT(grabFrame()));
connect(this, SIGNAL(stopGrabFrame()), frameGrabber, SLOT(stopGrabFrameSlot()));
}
MainWindow::~MainWindow()
{
frameGrabber->terminate();
frameGrabber->wait();
delete frameGrabber;
delete serialPort;
delete ui;
}
void MainWindow::on_actionSettings_triggered()
{
//qDebug()<<"The action was triggered now!";
int res;
configDlg gD(this);
res = gD.exec();
if(res == QDialog::Rejected)
return;
}
QImage MainWindow::Mat2QImage(Mat& image)
{
QImage img;
if (image.channels()==3) {
cvtColor(image, image, CV_BGR2RGB);
img = QImage((const unsigned char *)(image.data), image.cols, image.rows,
image.cols*image.channels(), QImage::Format_RGB888);
} else if (image.channels()==1) {
img = QImage((const unsigned char *)(image.data), image.cols, image.rows,
image.cols*image.channels(), QImage::Format_ARGB32);
} else {
img = QImage((const unsigned char *)(image.data), image.cols, image.rows,
image.cols*image.channels(), QImage::Format_RGB888);
}
return img;
}
void MainWindow::cameraWarningWdiget()
{
QWidget *cameraWarning = new QWidget;
QMessageBox::warning(cameraWarning,tr("错误"), tr("相机连接错误,请确认相机连接状况"));
}
void MainWindow::displayLiveFrame(Mat liveFrame)
{
ui->liveFrame->setPixmap(QPixmap::fromImage(Mat2QImage(liveFrame)));
}
QString MainWindow::getConfigValue(int propId)
{
QSettings *configFile = new QSettings("gapCheck Configuration\\gabCheckConfig.ini", QSettings::IniFormat);
switch (propId)
{
case 1:
{
QString cameraID = configFile->value("basicInfo/cameraID").toString();
return cameraID;
break;
}
case 2:
{
QString FRAME_WIDTH = configFile->value("basicInfo/CV_CAP_PROP_FRAME_WIDTH").toString();
return FRAME_WIDTH;
break;
}
case 3:
{
QString FRAME_HEIGHT = configFile->value("basicInfo/CV_CAP_PROP_FRAME_HEIGHT").toString();
return FRAME_HEIGHT;
break;
}
case 4:
{
QString comPort = configFile->value("basicInfo/comPort").toString();
return comPort;
break;
}
case 5:
{
QString cylinderDelay = configFile->value("basicInfo/cylinderDelay").toString();
return cylinderDelay;
}
case 13:
{
QString locator_offset = configFile->value("ROIInfo/locator_offset").toString();
return locator_offset;
break;
}
case 14:
{
QString isShowDebugFrame = configFile->value("ROIInfo/isShowDebugFrame").toString();
return isShowDebugFrame;
break;
}
case 15:
{
QString yIndeForLocate = configFile->value("ROIInfo/yIndeForLocate").toString();
return yIndeForLocate;
break;
}
case 16:
{
QString isWriteROI = configFile->value("ROIInfo/isWriteROI").toString();
return isWriteROI;
break;
}
case 17:
{
QString lastHeigth = configFile->value("ROIInfo/lastHeigth").toString();
return lastHeigth;
break;
}
case 19:
{
QString calibrateValue = configFile->value("ROIInfo/calibrateValue").toString();
return calibrateValue;
break;
}
case 20:
{
QString max_distant = configFile->value("ROIInfo/max_distant").toString();
return max_distant;
break;
}
case 21:
{
QString min_distant = configFile->value("ROIInfo/min_distant").toString();
return min_distant;
break;
}
case 22:
{
QString calibrater_Lentgh = configFile->value("ROIInfo/calibrater_Lentgh").toString();
return calibrater_Lentgh;
break;
}
case 23:
{
QString baseCol4calib_upper = configFile->value("ROIInfo/baseCol4calib_upper").toString();
return baseCol4calib_upper;
break;
}
case 24:
{
QString baseCol4calib_lower = configFile->value("ROIInfo/baseCol4calib_lower").toString();
return baseCol4calib_lower;
break;
}
case 25:
{
QString calibThreshold = configFile->value("ROIInfo/calibThreshold").toString();
return calibThreshold;
break;
}
case 26:
{
QString indexRow_start4calib = configFile->value("ROIInfo/indexRow_start4calib").toString();
return indexRow_start4calib;
break;
}
default:
return 0;
break;
}
}
void MainWindow::on_originalPointBtn_clicked()
{
serialPort->write(QByteArray::number(9)); //go back to original position
char buffer[50];
while(serialPort->read(buffer, 50))
{
emit stopGrabFrame();
qDebug()<<"Receive DATA now!"<<buffer;
}
}
void MainWindow::on_lightBtn_clicked()
{
//emit stopGrabFrame();
QProcess::startDetached("explorer gapCheck Configuration");
}
void MainWindow::on_launchBtn_clicked()
{
ui->launchBtn->setEnabled(false);
luanchBtnStage = false;
ui->ng_items->setText("Loading");
emit grabFrameNow();
ui->tableWidget->clear();
pe.setColor(QPalette::WindowText, Qt::black);
ui->label->setPalette(pe);
ui->label->setText("LD");
//Sleep(getConfigValue(5).toInt());
//serialPort->write(QByteArray::number(7)); //闭合气缸
serialPort->write(QByteArray::number(0)); //推动拨杆
// char buffer[50];
// while(!serialPort->read(buffer, 50))
// {
// //serialPort->read(buffer, 50) == 6;
// //qDebug()<<"Receive DATA now!"<<buffer;
// }
//emit stopGrabFrame();
}
void MainWindow::displayDistData(QList<int>& listData)
{
qDebug()<<"UI thread receive data now!";
qDebug()<< "Dist Data is" << (getConfigValue(17).toInt() - listData[0])/calibrationValue - locator_offset.toDouble();
int ng_itme = 0;
int displayCount = 0;
bool ngOrOK = true;
double distData;
for(int rows = 0; rows < 10; rows++)
{
for(int cols = 0; cols < 3; cols++)
{
distData = (getConfigValue(17).toInt() -listData[displayCount])/calibrationValue - locator_offset.toDouble();
ui->tableWidget->setItem(rows, cols, new QTableWidgetItem(QString::number(distData)));
if((distData > max_distant)||(distData < min_distant))
{
if(distData >max_distant)
ng_itme = 1; // '1' means more than 0.5mm
else if(distData < min_distant)
ng_itme = 2; //'2' means less than 0.3mm
else
ng_itme = 0;
ui->tableWidget->item(rows,cols)->setBackgroundColor(QColor("red"));
ngOrOK = false;
}
displayCount++;
}
}
if(!ngOrOK)
{
ui->label->setText("NG");
pe.setColor(QPalette::WindowText, Qt::red);
ui->label->setPalette(pe);
if(ng_itme == 1) //more than 0.5mm
{
ui->ng_items->setText("0.5 NG");
}
else if(ng_itme ==2)
{
ui->ng_items->setText("0.3 NG");
}
}
else
{
ui->label->setText("OK");
pe.setColor(QPalette::WindowText, Qt::green);
ui->label->setPalette(pe);
ui->ng_items->setText("OK");
}
ui->launchBtn->setEnabled(true);
luanchBtnStage = true;
}
void MainWindow::keyPressEvent(QKeyEvent *e)
{
if((e->key() == Qt::Key_Enter) && luanchBtnStage)
{
on_launchBtn_clicked();
qDebug() << "Detect keyboard is pressed!";
}
}
void MainWindow::on_lightBtn_2_clicked()
{
}
void MainWindow::on_actionOpen_triggered()
{
on_lightBtn_clicked();
}
void MainWindow::on_actionCalibrate_triggered()
{
emit grabCalibrationFrame();
}
void MainWindow::grabCalibrationFrame(Mat frame4Calibrate)
{
imwrite("gapCheck Configuration\\source frames\\frame4Calibrate.bmp", frame4Calibrate);
on_pushButton_clicked(frame4Calibrate);
}
void MainWindow::on_pushButton_clicked(Mat calibFrame)
{
cvtColor(calibFrame, calibFrame, CV_BGR2GRAY);
// Mat calibFrame = imread("gapCheck Configuration\\source frames\\frame4Calibrate.bmp", 0);
// imshow("Original Frame", calibFrame);
// Mat imageEdged;
// Canny(calibFrame, imageEdged, 20, 40);
// imwrite("gapCheck Configuration\\source frames\\imageEdged.bmp", imageEdged);
// imshow("", imageEdged);
/*Find contours in binary image*/
// Mat dst = Mat::zeros(imageEdged.rows, imageEdged.cols, CV_8UC3);
// vector<vector<Point> > contours;
// vector<Vec4i> hierarchy;
// findContours( imageEdged, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
// // draw each connected component with its own random color
// int idx = 0;
// for( ; idx >= 0; idx = hierarchy[idx][0] )
// {
// Scalar color( rand()&255, rand()&255, rand()&255 );
// drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );
// }
// imshow( "Components", dst);
// 使用阈值化来判断标定值
Mat frameThreshed;
threshold(calibFrame, frameThreshed, getConfigValue(25).toInt(), 255, THRESH_BINARY);
// imshow("Frame Thresh", frameThreshed);
// HoughLines(imageEdged, );
// vector<Vec4i> lines;
// HoughLinesP(imageEdged, lines, 1, CV_PI/180, 80, 200, 10);
// drawDetectLines(calibFrame,lines,Scalar(0, 255, 0));
// imwrite("gapCheck Configuration\\source frames\\imageLined.jpg", calibFrame);
// imshow("Line Detect", calibFrame);
// double calibValue = calculateCalibValue(imageEdged);
double calibValue = calculateCalibValue(frameThreshed);
ui->label_2->setText(tr("标定值:"));
ui->ng_items->setText(QString::number((getConfigValue(22).toDouble()/calibValue)));
}
void MainWindow::drawDetectLines(Mat& image,const vector<Vec4i>& lines,Scalar & color)
{
// 将检测到的直线在图上画出来
vector<Vec4i>::const_iterator it=lines.begin();
while(it!=lines.end())
{
Point pt1((*it)[0],(*it)[1]);
Point pt2((*it)[2],(*it)[3]);
line(image,pt1,pt2,color,1); // 线条宽度设置为2
++it;
}
}
double MainWindow::calculateCalibValue(Mat frameEdged)
{
imwrite("gapCheck Configuration\\source frames\\frameInCal.bmp", frameEdged);
//The specified col of those index is 150, 250, 350
//260 320 380 index in threshold method
//Looking for upper X index for calibration
QList<int> rowIndex4Calib_upper;
// int colIndex_upper = 150;
// int colIndex_upper = 260;
int colIndex_upper;
int baseCol4calib_upper = getConfigValue(23).toInt();
colIndex_upper = baseCol4calib_upper;
// qDebug() << frameEdged.at(Point2d(260, 158));
for(int numIndex = 0; numIndex <3; numIndex++)
{
for (int j = 80; j < frameEdged.rows; j++)
{
uchar* data = frameEdged.ptr<uchar>(j);
{
// qDebug() << "The Value at Pos" << j << "is:" << data[colIndex_upper] ;
if(data[colIndex_upper] )
{
rowIndex4Calib_upper.push_back(j);
// colIndex_upper = colIndex_upper + 100;
qDebug() << "The upper Row index is:" << j;
colIndex_upper = colIndex_upper + 60;
break;
}
}
}
}
//Looking for lower X index for calibration
QList<int> rowIndex4Calib_lower;
// int colIndex_lower = 150;
// int colIndex_lower = 260;
int colIndex_lower;
int baseCol4calib_lower = getConfigValue(24).toInt();
colIndex_lower = baseCol4calib_lower;
for(int numIndex = 0; numIndex <3; numIndex++)
{
for (int j = frameEdged.rows + getConfigValue(26).toInt(); j > 0; j--)
{
uchar* data = frameEdged.ptr<uchar>(j);
{
if(data[colIndex_lower] == 255)
{
rowIndex4Calib_lower.push_back(j);
// colIndex_lower = colIndex_lower + 100;
qDebug() << "The lower Row index is:" << j;
colIndex_lower = colIndex_lower + 60;
break;
}
}
}
}
QList<double> theta;
double theta_upper;
//theta 1
if(rowIndex4Calib_upper[0] == rowIndex4Calib_upper[1])
{
theta_upper = 0;
theta.push_back(theta_upper);
}
else
{
theta_upper = atan((rowIndex4Calib_upper[1] - rowIndex4Calib_upper[0])/100.0) *180.0/CV_PI;
theta.push_back(theta_upper);
}
//theta 2
if(rowIndex4Calib_upper[0] == rowIndex4Calib_upper[2])
{
theta_upper = 0;
theta.push_back(theta_upper);
}
else
{
theta_upper = atan((rowIndex4Calib_upper[2] - rowIndex4Calib_upper[0])/200.0) *180.0/CV_PI;
theta.push_back(theta_upper);
}
//theta 3
if(rowIndex4Calib_upper[1] == rowIndex4Calib_upper[2])
{
theta_upper = 0;
theta.push_back(theta_upper);
}
else
{
theta_upper = atan((rowIndex4Calib_upper[2] - rowIndex4Calib_upper[1])/100.0) *180.0/CV_PI;
theta.push_back(theta_upper);
}
//theta 4
if(rowIndex4Calib_lower[0] == rowIndex4Calib_lower[1])
{
theta_upper = 0;
theta.push_back(theta_upper);
}
else
{
theta_upper = atan((rowIndex4Calib_lower[1] - rowIndex4Calib_lower[0])/100.0) *180.0/CV_PI;
theta.push_back(theta_upper);
}
//theta 6
if(rowIndex4Calib_lower[0] == rowIndex4Calib_lower[2])
{
theta_upper = 0;
theta.push_back(theta_upper);
}
else
{
qDebug() << rowIndex4Calib_lower[1];
theta_upper = atan((rowIndex4Calib_lower[2] - rowIndex4Calib_lower[0])/200.0) *180.0/CV_PI;
theta.push_back(theta_upper);
}
//theta 6
if(rowIndex4Calib_lower[1] == rowIndex4Calib_lower[2])
{
theta_upper = 0;
theta.push_back(theta_upper);
}
else
{
theta_upper = atan((rowIndex4Calib_lower[2] - rowIndex4Calib_lower[1])/100.0) *180.0/CV_PI;
theta.push_back(theta_upper);
}
double theta_sum = 0;
for(auto i = theta.begin(); i != theta.end(); i++)
{
theta_sum += *i;
}
double theta_Ave = theta_sum/(theta.length());
QList<int> vertical_LIST;
for(int i = 0; i < 3; i++)
{
int vertical_Dist = rowIndex4Calib_lower[i] - rowIndex4Calib_upper[i];
vertical_LIST.push_back(vertical_Dist);
}
double dist_sum = 0;
for(auto i = vertical_LIST.begin(); i != vertical_LIST.end(); i++)
{
dist_sum += *i;
}
double dist_Ave = dist_sum/(vertical_LIST.length());
double calibValue = dist_Ave * cos(theta_Ave/180*CV_PI);
qDebug() << "The Calibration Value is " << calibValue;
return calibValue;
}
//void MainWindow::on_pushButton_clicked()
//{
// emit signal_waveCheck();
//}