1. 程式人生 > >基礎學習筆記之opencv(7):ubuntu下opencv在Qt中的使用

基礎學習筆記之opencv(7):ubuntu下opencv在Qt中的使用

#include "mainwindow.h"#include "ui_mainwindow.h"#include <QtGui/QMainWindow>#include <QFileDialog>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/core/core.hpp>using namespace cv;MainWindow::MainWindow(QWidget *parent) :    QMainWindow(parent),    ui(
new Ui::MainWindow){ ui->setupUi(this);}MainWindow::~MainWindow(){ delete ui;}void MainWindow::on_openButton_clicked(){//第3個引數表示查詢檔案時從哪個目錄開始,如果為"."的話,表示從該工程目錄開始查詢,最後那個引數的過濾器的名字之間//要用空格,否則識別不出來 QString img_name = QFileDialog::getOpenFileName( this, tr("Open Image"), ".", tr(
"Image Files(*.png *.jpg *.jpeg *.bmp)"));//toAscii()返回8位描述的string,為QByteArray,data()表示返回QByteArray的指標,QByteArray為位元組指標 Mat src = imread( img_name.toAscii().data() ); cvtColor( src, src, CV_BGR2RGB ); QImage img = QImage( (const unsigned char*)(src.data), src.cols, src.rows, QImage::Format_RGB888 );
// namedWindow( "Image_Show", WINDOW_AUTOSIZE );// imshow( "Image_Show", src ); ui->label->setPixmap( QPixmap::fromImage(img) );// ui->label->resize( ui->label->pixmap()->size() );}void MainWindow::on_closeButton_clicked(){ close();}