QT+opencv-讀取攝像頭和視頻

好久沒寫博客了,因爲準備實習了,沒什麼時間,後面抽空把平時的知識點複習補上。
因爲項目需要,大致上寫了個小的界面。
下面是效果圖:

在這裏插入圖片描述
注:QT顯示圖像需要把opencv的mat類型轉化爲QImage類型才能顯示
下面我拿了一個別人寫好的mat2image函數進行使用
(網上找的)

先把代碼貼上:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <opencv2/opencv.hpp>
#include <QTimer>
#include <QImage>
using namespace  cv;


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void dealpic();

private:
    Ui::MainWindow *ui;
    VideoCapture cap;
    QTimer *timer;
    Mat frame;
    int isCamera = 1;  //初始默認爲打開攝像頭
    int isflip = 0; //初始默認爲無    沒有選擇改變視頻翻轉

    int w,h;
    QImage image;
    //cut pic
    Mat gray,th,fushi,zhongzhi;
    Mat jietu,yuantu;

private slots:
    void importFrame();
//    QImage cvMat2QImage(const cv::Mat& mat);


    void on_comboBox_currentIndexChanged(int index);
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
    void on_comboBox_2_currentIndexChanged(int index);
    void on_pushButton_3_clicked();
};

#endif // MAINWINDOW_H

cpp:


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QImage>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    timer= new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(importFrame()));

}

MainWindow::~MainWindow()
{
    delete ui;
}
QImage cvMat2QImage(const cv::Mat& mat) //mat to image
{
    // 8-bits unsigned, NO. OF CHANNELS = 1
    if(mat.type() == CV_8UC1)
    {
        QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
        // Set the color table (used to translate colour indexes to qRgb values)
        image.setColorCount(256);
        for(int i = 0; i < 256; i++)
        {
            image.setColor(i, qRgb(i, i, i));
        }
        // Copy input Mat
        uchar *pSrc = mat.data;
        for(int row = 0; row < mat.rows; row ++)
        {
            uchar *pDest = image.scanLine(row);
            memcpy(pDest, pSrc, mat.cols);
            pSrc += mat.step;
        }
        return image;
    }
    // 8-bits unsigned, NO. OF CHANNELS = 3
    else if(mat.type() == CV_8UC3)
    {
        // Copy input Mat
        const uchar *pSrc = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
        return image.rgbSwapped();
    }
    else if(mat.type() == CV_8UC4)
    {
        qDebug() << "CV_8UC4";
        // Copy input Mat
        const uchar *pSrc = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
        return image.copy();
    }
    else
    {
        qDebug() << "ERROR: Mat could not be converted to QImage.";
        return QImage();
    }
}

void MainWindow::dealpic()
{
}

void MainWindow::importFrame()  //打開輸入畫面
{
    cap>>frame;
    int flipvalue=ui->comboBox_2->currentIndex();
    if (flipvalue != 0 )
    {
        flip(frame,frame,isflip);    //水平翻轉
    }

    dealpic(); //處理圖像

    QImage image = cvMat2QImage(frame);
    ui->label->resize(image.size());
//    ui->label->resize(w,h);
    ui->label->setPixmap(QPixmap::fromImage(image));
//    ui->label->setScaledContents(true); //自適應

    ui->label->show();

}

void MainWindow::on_comboBox_currentIndexChanged(int) //輸入流選擇
{

    if (ui->comboBox->currentIndex()==1)   // 1攝像頭
    {
        isCamera = 1;
    }
    else if (ui->comboBox->currentIndex()==2)   // 2視頻
    {
        isCamera = 2;
    }


}

void MainWindow::on_pushButton_clicked() //播放
{
    if(isCamera==1)        // =1 打開攝像頭
    {
        cap.open(0);
    }
    else if(isCamera==2)
    {
        cap.open("C:/Users/Administrator/Desktop/QT/cutpictools/99.mp4");
        qDebug()<<"打開視頻成功";
    }
    w=(int)cap.get(CV_CAP_PROP_FRAME_WIDTH);
    h=(int)cap.get(CV_CAP_PROP_FRAME_HEIGHT);
    qDebug()<<"width:"<<w;
    qDebug()<<"height:"<<h;
    timer->start(24);
    qDebug()<<"播放";
}

void MainWindow::on_pushButton_2_clicked() //暫停
{
    if(timer->isActive())
    {
        ui->pushButton_2->setText("繼續");
        timer->stop();
        qDebug()<<"已暫停!";
    }else
    {
        ui->pushButton_2->setText("暫停");
        timer->start(24);
        qDebug()<<"已繼續!";
    }


}

void MainWindow::on_comboBox_2_currentIndexChanged(int) //翻轉選擇
{
    if (ui->comboBox_2->currentIndex()==1)   // 垂直翻轉
    {
        isflip = 0;
    }
    else if (ui->comboBox_2->currentIndex()==2) //水平翻轉
    {
        isflip = 1;
    }
    else if (ui->comboBox_2->currentIndex()==3) //垂直水平翻轉
    {
        isflip = -1;
    }
}

void MainWindow::on_pushButton_3_clicked()  //退出
{
    timer->stop();
    cap.release();
    this->close();
    qDebug()<<"程序退出!";
}

代碼都比較簡單,都能看懂

再把配置opencv的過程貼一下:
把opencv的路徑,還有庫添加在pro即可

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章