You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
563 lines
16 KiB
563 lines
16 KiB
#include "mainwindow.h" |
|
#include "./ui_mainwindow.h" |
|
|
|
#include <QDateTime> |
|
|
|
|
|
MainWindow::MainWindow(QWidget *parent) |
|
: QMainWindow(parent) |
|
, ui(new Ui::MainWindow) |
|
{ |
|
ui->setupUi(this); |
|
initIns(); |
|
initUI(); |
|
initConnect(); |
|
#if CLIENT==1 |
|
hideui(); |
|
#endif |
|
} |
|
|
|
MainWindow::~MainWindow() |
|
{ |
|
delete ui; |
|
} |
|
|
|
void MainWindow::initUI() |
|
{ |
|
b_stat = false; |
|
flip0 = false; |
|
flip1 = false; |
|
savevideo = false; |
|
savebin = false; |
|
savesbin = false; |
|
shotimg = false; |
|
shotbin = false; |
|
path = ""; |
|
fps = 0; |
|
fpscount = 0; |
|
path = QApplication::applicationDirPath()+"/"; |
|
|
|
setWindowTitle(QString(ProjectVersion)); |
|
|
|
|
|
ui->splitter_2->setCollapsible(1,false); |
|
|
|
// QHBoxLayout lout = QHBoxLayout(); |
|
|
|
ui->toolBar->addWidget(ui->pushButton); |
|
ui->toolBar->addWidget(ui->pushButton_2); |
|
ui->toolBar->addWidget(ui->label_2); |
|
ui->toolBar->addWidget(ui->spinBox); |
|
ui->toolBar->addWidget(ui->label_3); |
|
ui->toolBar->addWidget(ui->spinBox_2); |
|
|
|
|
|
m1 = new menu1(); |
|
m2 = new menu2(); |
|
m3 = new menu3(); |
|
m4 = new menu4(); |
|
|
|
ui->toolbox->addItem(m1,u8"相机设置"); |
|
ui->toolbox->addItem(m2,u8"算法设置"); |
|
ui->toolbox->addItem(m3,u8"显示设置"); |
|
ui->toolbox->addItem(m4,u8"保存设置"); |
|
|
|
type = gray8; |
|
rows = ui->spinBox->value(); |
|
cols = ui->spinBox_2->value(); |
|
|
|
ui->scrollAreaWidgetContents->setMinimumSize(cols+20,rows+20); |
|
ui->label->resize(cols,rows); |
|
|
|
ui->tableWidget->horizontalHeader()->hide(); |
|
ui->tableWidget->verticalHeader()->hide(); |
|
ui->tableWidget->setRowCount(5); |
|
ui->tableWidget->setColumnCount(5); |
|
ui->tableWidget->resizeColumnsToContents(); |
|
ui->tableWidget->resizeRowsToContents(); |
|
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); |
|
ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch); |
|
ui->label->installEventFilter(this); |
|
} |
|
|
|
void MainWindow::initConnect() |
|
{ |
|
connect(usb,SIGNAL(signalGetImage(cv::Mat)),this,SLOT(slotShowImage(cv::Mat))); |
|
|
|
connect(m1,SIGNAL(signalsavelow()),ucontrol,SLOT(savelow())); |
|
connect(m1,SIGNAL(signalsavehigh()),ucontrol,SLOT(savehigh())); |
|
connect(m1,SIGNAL(signalcalck()),ucontrol,SLOT(calck())); |
|
connect(m1,SIGNAL(signalsavek()),ucontrol,SLOT(savek())); |
|
connect(m1,SIGNAL(signalopenk(bool)),ucontrol,SLOT(openk(bool))); |
|
connect(m1,SIGNAL(signalonepoint()),ucontrol,SLOT(onepoint())); |
|
connect(m1,SIGNAL(signalsaveb()),ucontrol,SLOT(saveb())); |
|
connect(m1,SIGNAL(signalopenb(bool)),ucontrol,SLOT(openb(bool))); |
|
connect(m1,SIGNAL(signalsetGPOL(int)),ucontrol,SLOT(setGPOL(int))); |
|
connect(m1,SIGNAL(signalsetIT(double)),ucontrol,SLOT(setIT(double))); |
|
connect(m1,SIGNAL(signalopenTEC(bool)),ucontrol,SLOT(tec(bool))); |
|
connect(m1,SIGNAL(signalset14bit(bool)),ucontrol,SLOT(bit14(bool))); |
|
connect(m1,&menu1::signalset14bit,[=](bool b){ |
|
if(b) |
|
usb->SetImageInfo(gray16,rows,cols); |
|
else |
|
usb->SetImageInfo(gray8,rows,cols); |
|
}); |
|
connect(m2,SIGNAL(signalsag(bool)),ucontrol,SLOT(setsag(bool))); |
|
connect(m2,SIGNAL(signalsetDDE(int)),ucontrol,SLOT(setDDE(int))); |
|
connect(m2,SIGNAL(signalsetkf(int)),ucontrol,SLOT(setkf(int))); |
|
connect(m2,SIGNAL(signalsethist(int)),ucontrol,SLOT(sethist(int))); |
|
// connect(m2,SIGNAL(signalsetDE(bool)),) |
|
// connect(m2,SIGNAL(signalseterate(double)),) |
|
// connect(m2,SIGNAL(signalsetbright(int)),) |
|
///线性拉伸设置 |
|
/// |
|
/// |
|
connect(ucontrol,SIGNAL(signalSendMessage(QString)),this,SLOT(slotlogtext(QString))); |
|
|
|
connect(m3,&menu3::signalflip0,[=](bool b){ |
|
flip0 = b; |
|
}); |
|
connect(m3,&menu3::signalflip1,[=](bool b){ |
|
flip1 = b; |
|
}); |
|
connect(m3,&menu3::signalturnl,[=](){ |
|
turnl = true; |
|
turnf = false; |
|
turnr = false; |
|
}); |
|
connect(m3,&menu3::signalturnf,[=](){ |
|
turnl = false; |
|
turnf = true; |
|
turnr = false; |
|
}); |
|
connect(m3,&menu3::signalturnr,[=](){ |
|
turnl = false; |
|
turnf = false; |
|
turnr = true; |
|
}); |
|
|
|
connect(m3,&menu3::signalcolorgray,[=](){ |
|
colortype =0;//gray |
|
}); |
|
connect(m3,&menu3::signalcolorsub,[=](){ |
|
colortype = 1;//sub |
|
}); |
|
connect(m3,&menu3::signalcolorrgb,[=](){ |
|
colortype = 2; |
|
}); |
|
|
|
|
|
connect(m4,&menu4::signalsetdir,[=](QString dir){ |
|
if(dir == ""){ |
|
path = QApplication::applicationDirPath()+"/"; |
|
} |
|
else{ |
|
path = dir+"/"; |
|
} |
|
}); |
|
|
|
connect(m4,&menu4::signalshotimg,[=](){ |
|
shotimg = true; |
|
}); |
|
connect(m4,&menu4::signalshotbin,[=](){ |
|
shotbin = true; |
|
}); |
|
connect(m4,&menu4::signalsavevideo,[=](bool b){ |
|
if(b){ |
|
QString now = QDateTime::currentDateTime().toString("yyyy_MM_dd_hh_mm_ss"); |
|
std::string filename = path.toStdString()+now.toStdString()+".mp4"; |
|
writer = new cv::VideoWriter(filename,cv::VideoWriter::fourcc('M','P','4','V'),fps,cv::Size(cols,rows),false); |
|
QString msg = now +QString::fromLocal8Bit( "开始保存视频"); |
|
slotlogtext(msg); |
|
savevideo =true; |
|
} |
|
else{ |
|
savevideo = false; |
|
writer->release(); |
|
QDateTime t = QDateTime::currentDateTime(); |
|
QString msg = t.toString() +QString::fromLocal8Bit( "结束保存视频"); |
|
slotlogtext(msg); |
|
} |
|
}); |
|
connect(m4,&menu4::signalsavebin,[=](bool b){ |
|
if(b){ |
|
QString now = QDateTime::currentDateTime().toString("/yyyy-MM-dd-hh-mm-ss"); |
|
QString filename = path+now; |
|
if(type == gray8) |
|
filename += "8bit.bin"; |
|
else if(type == gray16) |
|
filename += "14bit.bin"; |
|
purefile = new QFile(filename); |
|
purefile->open(QIODevice::ReadWrite); |
|
savebin = true; |
|
QString msg = now +QString::fromLocal8Bit( "开始保存数据"); |
|
slotlogtext(msg); |
|
} |
|
else{ |
|
savebin = false; |
|
purefile->close(); |
|
QString now = QDateTime::currentDateTime().toString("/yyyy-MM-dd-hh-mm-ss"); |
|
QString msg = now +QString::fromLocal8Bit( "结束保存数据"); |
|
slotlogtext(msg); |
|
} |
|
}); |
|
|
|
connect(m4,&menu4::signalsavesbin,[=](int frames){ |
|
if(savesbin) |
|
return; |
|
QString now = QDateTime::currentDateTime().toString("/yyyy-MM-dd-hh-mm-ss"); |
|
QString filename = path+now; |
|
if(type == gray8) |
|
filename += "8bit.bin"; |
|
else if(type == gray16) |
|
filename += "14bit.bin"; |
|
spurefile = new QFile(filename); |
|
spurefile->open(QIODevice::ReadWrite); sbinnum = frames; |
|
savesbin = true; |
|
|
|
}); |
|
|
|
} |
|
|
|
void MainWindow::hideui() |
|
{ |
|
m1->hideui(); |
|
m2->hideui(); |
|
} |
|
|
|
|
|
void MainWindow::on_pushButton_clicked()// 开启传输 |
|
{ |
|
if(ui->pushButton->isChecked()){ |
|
usb->SetImageInfo(type,rows,cols); |
|
usb->setStatu(true); |
|
usb->start(); |
|
ui->pushButton->setText(u8"停止传输"); |
|
} |
|
else{ |
|
usb->setStatu(false); |
|
usb->stopProcessData(); |
|
ui->pushButton->setText(u8"开始传输"); |
|
} |
|
} |
|
|
|
|
|
void MainWindow::on_pushButton_2_clicked() |
|
{ |
|
//恢复默认设置 |
|
#ifndef CLIENT |
|
ucontrol->bit14(false); |
|
type = gray8; |
|
|
|
#elif CLIENT == 1 |
|
ucontrol->bit14(false); |
|
type = gray8; |
|
qDebug()<<"sim"; |
|
|
|
#elif CLIENT == 2 |
|
ucontrol->bit14(true); |
|
type = gray16; |
|
qDebug()<<"pc"; |
|
#endif |
|
|
|
ucontrol->setIT(5.0); |
|
ucontrol->setGPOL(800); |
|
ucontrol->sethist(200); |
|
ucontrol->openk(true); |
|
ucontrol->openb(true); |
|
} |
|
|
|
|
|
void MainWindow::on_pushButton_3_clicked() |
|
{ |
|
b_stat = ui->pushButton_3->isChecked(); |
|
if(b_stat){ |
|
ui->pushButton_3->setText(u8"暂停统计"); |
|
} |
|
else{ |
|
ui->pushButton_3->setText(u8"开始统计"); |
|
} |
|
|
|
} |
|
|
|
void MainWindow::slotShowImage(cv::Mat img) |
|
{ |
|
fpscount +=1; |
|
|
|
if(shotimg){ |
|
QString now = QDateTime::currentDateTime().toString("yyyy_MM_dd_hh_mm_ss"); |
|
QString file = path+ now+".png"; |
|
cv::imwrite(file.toLocal8Bit().toStdString(),img); |
|
shotimg = false; |
|
} |
|
if(shotbin){ |
|
QDateTime t = QDateTime::currentDateTime(); |
|
QString filename = path+ t.toString("yyyy-MM-dd-hh-mm-ss"); |
|
if(type == gray8) |
|
filename+= "-8bit.bin"; |
|
else if(type == gray16) |
|
filename+= "-14bit.bin"; |
|
QFile file(filename); |
|
file.open(QIODevice::NewOnly); |
|
file.write((char*)img.data,img.total()*img.elemSize()); |
|
file.flush(); |
|
file.close(); |
|
shotbin = false; |
|
} |
|
|
|
if(savevideo){ |
|
writer->write(img); |
|
} |
|
|
|
if(savebin){ |
|
purefile->write((char*)img.data,img.total()*img.elemSize()); |
|
purefile->flush(); |
|
} |
|
|
|
if(savesbin){ |
|
if(sbinnum>0){ |
|
spurefile->write((char*)img.data,img.total()*img.elemSize()); |
|
spurefile->flush(); |
|
} |
|
sbinnum -=1; |
|
// qDebug()<<sbinnum; |
|
|
|
if(sbinnum == 0){ |
|
spurefile->close(); |
|
savesbin = false; |
|
} |
|
} |
|
|
|
|
|
if(flip0){ |
|
cv::flip(img,img,0); |
|
} |
|
|
|
if(flip1){ |
|
cv::flip(img,img,1); |
|
} |
|
|
|
if(turnl){ |
|
|
|
} |
|
else if(turnr){ |
|
|
|
} |
|
|
|
|
|
if(b_stat ){ |
|
if(img.type() == CV_16UC1) |
|
lastimage = img/4; |
|
else |
|
lastimage = img; |
|
|
|
if( imageX>=0 && imageX<img.cols && imageY>=0 && imageY< img.rows){ |
|
int r = 2; |
|
int lmask[25] = {0}; |
|
int ldata[25] = {0}; |
|
for (int i = -r;i<=r;i++){ |
|
for (int j = -r;j<=r;j++){ |
|
int row = i+imageY; |
|
int col = j+imageX; |
|
if(row<0 || row>=lastimage.rows|| col<0 || col>=lastimage.cols){ |
|
ui->tableWidget->setItem(i+r,j+r,new QTableWidgetItem()); |
|
|
|
} |
|
else{ |
|
lmask[(i+r)*(2*r+1)+(j+r)] = 1; |
|
if(lastimage.type() == CV_16UC1){ |
|
QTableWidgetItem *item = new QTableWidgetItem(QString::number(lastimage.at<ushort>(row,col))); |
|
int gray = lastimage.at<ushort>(row,col)/64/2+64; |
|
item->setBackground(QColor(gray,gray,gray)); |
|
ui->tableWidget->setItem(i+r,j+r,item); |
|
ldata[(i+r)*(2*r+1)+(j+r)] = lastimage.at<ushort>(row,col); |
|
} |
|
else{ |
|
QTableWidgetItem *item = new QTableWidgetItem(QString::number(lastimage.at<uchar>(row,col))); |
|
int gray = lastimage.at<uchar>(row,col)/2+64; |
|
item->setBackground(QColor(gray,gray,gray)); |
|
ui->tableWidget->setItem(i+r,j+r,item); |
|
ldata[(i+r)*(2*r+1)+(j+r)] = lastimage.at<uchar>(row,col); |
|
} |
|
|
|
} |
|
} |
|
} |
|
double sum = 0; |
|
double msum = 0; |
|
double sum2 = 0; |
|
|
|
for(int i = 0; i< 25;i++){ |
|
sum+= lmask[i]*ldata[i]; |
|
msum += lmask[i]; |
|
sum2 += lmask[i]*ldata[i]*ldata[i]; |
|
} |
|
if(msum!=0){ |
|
double avg = sum/msum; |
|
double SIGMA = sum2/msum - avg*avg; |
|
double sigma = sqrt(SIGMA); |
|
ui->lineEdit->setText(QString::number(imageX)); |
|
ui->lineEdit_2->setText(QString::number(imageY)); |
|
ui->lineEdit_3->setText(QString::number(avg,'f',2)); |
|
ui->lineEdit_4->setText(QString::number(sigma,'f',2)); |
|
} |
|
} |
|
else{ |
|
ui->lineEdit->setText(""); |
|
ui->lineEdit_2->setText(""); |
|
ui->lineEdit_3->setText(""); |
|
ui->lineEdit_4->setText(""); |
|
} |
|
|
|
int lows = 0; |
|
int highs = 0; |
|
double sum = 0; |
|
double sum2 = 0; |
|
double sigma = 0; |
|
|
|
if(lastimage.type() == CV_16UC1){ |
|
for(int i = 0;i<lastimage.rows;i++){ |
|
for(int j = 0; j<lastimage.cols;j++){ |
|
int temp; |
|
temp =lastimage.at<ushort>(i,j); |
|
if(temp == 0){ |
|
lows+=1; |
|
} |
|
if(temp == 16383){ |
|
highs +=1; |
|
} |
|
sum += temp; |
|
sum2+=temp*temp; |
|
} |
|
} |
|
} |
|
else{ |
|
for(int i = 0;i<lastimage.rows;i++){ |
|
for(int j = 0; j<lastimage.cols;j++){ |
|
int temp; |
|
temp =lastimage.at<uchar>(i,j); |
|
sum += temp; |
|
sum2+=temp*temp; |
|
} |
|
} |
|
} |
|
int avg = sum/512/640; |
|
sigma = sqrt( sum2/512/640 -avg*avg); |
|
|
|
if(lastimage.type() == CV_16UC1){ |
|
ui->lineEdit_5->setText(QString::number(lows)); |
|
ui->lineEdit_6->setText(QString::number(highs)); |
|
} |
|
else{ |
|
ui->lineEdit_5->setText(""); |
|
ui->lineEdit_6->setText(""); |
|
} |
|
ui->lineEdit_7->setText(QString::number(avg,'f',2)); |
|
ui->lineEdit_8->setText(QString::number(sigma,'f',2)); |
|
} |
|
|
|
if(img.type() == CV_16UC1) |
|
img.convertTo(img,CV_8UC1,1.0/257); |
|
QImage image(img.data,img.cols,img.rows,img.cols,QImage::Format_Grayscale8); |
|
QPixmap pix = QPixmap::fromImage(image); |
|
|
|
|
|
// QPixmap pix = QPixmap::fromImage(img); |
|
if(b_stat){ |
|
QPainter p(&pix); |
|
QPen pen = p.pen(); |
|
QPen npen(QColor(0xad,0x14,0x53)); |
|
npen.setWidth(4); |
|
p.setPen(npen); |
|
if(imageX>=0 && imageX<img.cols && imageY>=0 && imageY< img.rows){ |
|
p.drawLine(imageX-10,imageY,imageX+10,imageY); |
|
p.drawLine(imageX,imageY-10,imageX,imageY+10); |
|
} |
|
p.setPen(pen); |
|
} |
|
|
|
ui->label->setPixmap(pix); |
|
|
|
} |
|
|
|
void MainWindow::slotlogtext(QString msg) |
|
{ |
|
ui->textBrowser->append(msg); |
|
} |
|
|
|
void MainWindow::slotlogstatus() |
|
{ |
|
QString msg = QString::fromLocal8Bit("当前图像深度为"); |
|
if(type==gray8){ |
|
msg+=QString::fromLocal8Bit( "8位"); |
|
} |
|
else{ |
|
msg+=QString::fromLocal8Bit("14位"); |
|
} |
|
msg+=QString::fromLocal8Bit(",帧率为")+QString::number(fps); |
|
ui->statusbar->showMessage(msg); |
|
} |
|
|
|
bool MainWindow::eventFilter(QObject *obj, QEvent *e) |
|
{ |
|
if(obj == ui->label && e->type() == QEvent::MouseButtonPress){ |
|
QMouseEvent* ev = static_cast<QMouseEvent*>(e); |
|
imageX = ev->x(); |
|
imageY =ev->y(); |
|
return true; |
|
} |
|
return false; |
|
} |
|
|
|
void MainWindow::keyPressEvent(QKeyEvent *ev) |
|
{ |
|
|
|
if(ev->key() == Qt::Key_A){ |
|
imageX-=1; |
|
} |
|
if(ev->key() == Qt::Key_D){ |
|
imageX+=1; |
|
} |
|
if(ev->key() == Qt::Key_W){ |
|
imageY-=1; |
|
} |
|
if(ev->key() == Qt::Key_S){ |
|
imageY+=1; |
|
} |
|
|
|
} |
|
|
|
void MainWindow::initIns() |
|
{ |
|
qRegisterMetaType<cv::Mat>("cv::Mat"); |
|
|
|
usb = new usbthread(); |
|
ucontrol = new usbcontrol(); |
|
|
|
|
|
QTimer* t = new QTimer(this); |
|
connect(t,&QTimer::timeout,[=](){ |
|
fps = fpscount; |
|
fpscount = 0; |
|
slotlogstatus(); |
|
}); |
|
t->start(1000); |
|
} |
|
|
|
|
|
void MainWindow::on_spinBox_valueChanged(int arg1) |
|
{ |
|
rows = arg1; |
|
ui->scrollAreaWidgetContents->setMinimumHeight(rows+20); |
|
ui->label->resize(cols,rows); |
|
|
|
} |
|
|
|
|
|
void MainWindow::on_spinBox_2_valueChanged(int arg1) |
|
{ |
|
cols = arg1; |
|
ui->scrollAreaWidgetContents->setMinimumWidth(cols+20); |
|
ui->label->resize(cols,rows); |
|
} |
|
|
|
|