QT+OPENCV摄像头的三种效果显示
opencv在window下的qt的mingw库需要自己编译,注意高版本的opencv可能不包含mingw,而本次采用的是OpenCV-2.4.3版,然后用cmake工具进行编译。参考链接:http://blog.csdn.net/qiurisuixiang/article/details/8665278/
QT_XCT.pro //工程文件QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QT_XCT
TEMPLATE = appDEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp\
mainwindow.cpp \ login.cppHEADERS += mainwindow.h \
login.hFORMS += mainwindow.ui \
login.uiRESOURCES += \
src/image.qrcINCLUDEPATH+=C:\OpenCV\include\opencv\
C:\OpenCV\include\opencv2\ C:\OpenCV\includeLIBS+=C:\OpenCV\lib\libopencv_calib3d243.dll.a\
C:\OpenCV\lib\libopencv_contrib243.dll.a\ C:\OpenCV\lib\libopencv_core243.dll.a\ C:\OpenCV\lib\libopencv_features2d243.dll.a\ C:\OpenCV\lib\libopencv_flann243.dll.a\ C:\OpenCV\lib\libopencv_gpu243.dll.a\ C:\OpenCV\lib\libopencv_highgui243.dll.a\ C:\OpenCV\lib\libopencv_imgproc243.dll.a\ C:\OpenCV\lib\libopencv_legacy243.dll.a\ C:\OpenCV\lib\libopencv_ml243.dll.a\ C:\OpenCV\lib\libopencv_objdetect243.dll.a\ C:\OpenCV\lib\libopencv_video243.dll.a mainwindow.h //头文件#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QPaintEvent> #include <QTimer> #include <QPainter> #include <QPixmap> #include <QLabel> #include <QImage> #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include "opencv2/opencv.hpp" #include <iostream> #include <QProcess> using namespace cv; using namespace std;namespace Ui {
class MainWindow; }class MainWindow : public QMainWindow
{ Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void delSignal(int); void getview1(); void getview2(); void getview3(); public slots: void on_btn_normal_clicked();void on_btn_border_clicked();
void on_btn_heibai_clicked();
signals:
void mySignal(int);private:
Ui::MainWindow *ui; Mat srcImage; VideoCapture videoCap; // QLabel *imageLabel; // QProcess m_process; QWindow *m_window; QWidget *m_widget; WId wid; char c;};
#endif // MAINWINDOW_H
mainwindow.cpp//源文件
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> #include <iostream> #include <windows.h> #include <QWindow> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); setWindowTitle("XCT"); setWindowIcon(QIcon("qrc:/new/prefix1/logo.jpg")); resize(587,455); m_window=NULL; m_widget=NULL; connect(this,&MainWindow::mySignal,this,&MainWindow::delSignal); }MainWindow::~MainWindow()
{ delete ui; }void MainWindow::delSignal(int sig)
{ switch(sig) { case 100: destroyWindow("normal");getview1();
break; case 101: destroyWindow("normal");getview2();
break; case 102: destroyWindow("normal");getview3();
break; defalut: break; } }void MainWindow::getview1()
{ //启动一个外部可执行程序 /* QString cmd = QString("C:/WINDOWS/system32/calc.exe"); m_process.start(cmd,QStringList()<<"");if(!m_process.waitForFinished(3000))
{ qDebug()<<"--------"; }Sleep(200);
*/// if(imageLabel!=NULL)
// { // ui->verticalLayout->removeWidget(imageLabel); // } // imageLabel = new QLabel(this); int i = 0; VideoCapture inputVideo(0); //0为外部摄像头的ID,1为笔记本内置摄像头的IDnamedWindow("normal");
wid=(WId)FindWindow(L"Main HighGUI class",NULL); m_window = QWindow::fromWinId(wid); m_widget = QWidget::createWindowContainer(m_window); ui->mdiArea->addSubWindow(m_widget); m_widget->setMaximumSize(423,323); m_widget->showMaximized();while(1)
{ inputVideo >> srcImage; if(srcImage.empty() & i > 0) return; if(i > 0) { imshow("normal",srcImage);// QImage image2 = QImage((uchar*)(srcImage.data), srcImage.cols, srcImage.rows, QImage::Format_RGB888);
// imageLabel->setPixmap(QPixmap::fromImage(image2)); // imageLabel->resize(361,291); // ui->verticalLayout->addWidget(imageLabel);}
i ++; c=(char)waitKey(10); if (c==27) { c=0; break; } } }void MainWindow::getview2()
{ Mat src; // if(imageLabel!=NULL) // { // ui->verticalLayout->removeWidget(imageLabel); // } // imageLabel = new QLabel(this); char c; int i = 0; VideoCapture inputVideo(0); //0为外部摄像头的ID,1为笔记本内置摄像头的IDnamedWindow("normal");
wid=(WId)FindWindow(L"Main HighGUI class",NULL); m_window = QWindow::fromWinId(wid); m_widget = QWidget::createWindowContainer(m_window); ui->mdiArea->addSubWindow(m_widget); m_widget->setMaximumSize(423,323); m_widget->showMaximized();while(1)
{ inputVideo >>srcImage; if(srcImage.empty() & i > 0) return; if(i > 0) { cvtColor(srcImage,src, CV_BGR2GRAY); GaussianBlur(src, src, Size(7,7), 1.5, 1.5); Canny(src, src, 0, 20, 3); imshow("normal",src); // QImage image2= QImage((uchar*)(srcImage.data), srcImage.cols, srcImage.rows, QImage::Format_RGB888); // imageLabel->setPixmap(QPixmap::fromImage(image2)); // imageLabel->resize(361,291); // ui->verticalLayout->addWidget(imageLabel); } i ++; c=(char)waitKey(10); if (c==27) { c=0; break; } } }void MainWindow::getview3()
{// if(imageLabel!=NULL)
// { // ui->verticalLayout->removeWidget(imageLabel); // } // imageLabel = new QLabel(this);Mat src;
char d; int i = 0; VideoCapture inputVideo(0); //0为外部摄像头的ID,1为笔记本内置摄像头的IDnamedWindow("normal");
wid=(WId)FindWindow(L"Main HighGUI class",NULL); m_window = QWindow::fromWinId(wid); m_widget = QWidget::createWindowContainer(m_window); ui->mdiArea->addSubWindow(m_widget); m_widget->setMaximumSize(423,323); m_widget->showMaximized();while(1)
{ inputVideo >> srcImage; if(srcImage.empty() & i > 0) return; if(i > 0) { cvtColor(srcImage,src, CV_BGR2GRAY);imshow("normal",src);
// QImage image2 = QImage((uchar*)(srcImage.data), srcImage.cols, srcImage.rows, QImage::Format_RGB888); // imageLabel->setPixmap(QPixmap::fromImage(image2)); // imageLabel->resize(361,291); // ui->verticalLayout->addWidget(imageLabel); } i ++;c=(char)waitKey(10);
if (c==27) { c=0; break; } } }//正常显示
void MainWindow::on_btn_normal_clicked() { emit mySignal(100);}
//轮廓显示
void MainWindow::on_btn_border_clicked() { emit mySignal(101);}
//黑白显示 void MainWindow::on_btn_heibai_clicked() { emit mySignal(102); }