#include "mainwindow.h"
#include "ui_mainwindow.h"
using namespace cv;
//????
Mat src;
Mat gray;
Mat tmp;
Mat dst;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
timer = new QTimer(this);
imag = new QImage(); // ???
connect(timer, SIGNAL(timeout()), this, SLOT(readFarme())); // ?????????????
bMethod = false;//??????
on_pushButton_clicked();
clickLabel = new ClickedLabel(this);
clickLabel->setGeometry(0,0,400,400);
connect(clickLabel,SIGNAL(clicked(ClickedLabel*)),this,SLOT(on_pushButton_3_clicked()));
clickLabel2 = new ClickedLabel(this);
clickLabel2->setGeometry(400,0,400,400);
connect(clickLabel2,SIGNAL(clicked(ClickedLabel*)),this,SLOT(on_pushButton_2_clicked()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_MainWindow_iconSizeChanged(const QSize &iconSize)
{
}
//Open camera
//????
//?????
void MainWindow::on_pushButton_clicked()
{
//???????????????
videocapture = new VideoCapture(0);
// ??????????timeout()??
timer->start(33);
}
//????Frame??
void MainWindow::readFarme()
{
// ?????????????
videocapture->read(matFrame);
//????
if(bMethod)
{
cvtColor(matFrame,tmp,COLOR_BGR2GRAY);
Canny(tmp,dst,30,255);
}
else
{
dst = matFrame.clone();
}
// ????
QPixmap qpixmap = Mat2QImage(dst);
// ??????label?
//ui->label->setPixmap(qpixmap);
clickLabel->setPixmap(qpixmap);
}
//capture
void MainWindow::on_pushButton_2_clicked()
{
// ????
QPixmap qpixmap = Mat2QImage(dst);
// ??????label?
//ui->label_2->setPixmap(qpixmap);
clickLabel2->setPixmap(qpixmap);
}
//action
void MainWindow::on_pushButton_3_clicked()
{
bMethod = !bMethod;
}
//exit
void MainWindow::on_pushButton_4_clicked()
{
timer->stop(); // ???????
videocapture->release();
}
//////////////////////////helper??//////////////////////////////////////////////////
//????
QPixmap Mat2QImage(Mat src)
{
QImage img;
//??QT?????????
if(src.channels() == 3)
{
cvtColor( src, tmp, CV_BGR2RGB );
img = QImage( (const unsigned char*)(tmp.data), tmp.cols, tmp.rows, QImage::Format_RGB888 );
}
else
{
img = QImage( (const unsigned char*)(src.data), src.cols, src.rows, QImage::Format_Indexed8 );
}
QPixmap qimg = QPixmap::fromImage(img) ;
return qimg;
}