OpenCV自帶了一部分經常使用的GUI功能,可是更多的圖像處理功能須要其餘GUI框架來輔助實現,這裏經過QT來顯示OpenCV讀取的圖片。框架
在QtCreator中新建一個基於QMainWindow的應用:函數
其中QImageShowWidget就是用於顯示圖像的控件,它是繼承於QWidget實現的,能夠將其嵌入QMainWindow的centralwidget中:this
QImageShowWidget是自定義的顯示組件,能夠首先在QtCreator的設計師界面拖入一個QWidget,再經過「窗口部件提高」功能提高爲QImageShowWidget。spa
qimageshowwidget.h代碼以下:設計
#ifndef QIMAGESHOWWIDGET_H #define QIMAGESHOWWIDGET_H #include <QWidget> class QImageShowWidget : public QWidget { Q_OBJECT public: explicit QImageShowWidget(QWidget *parent = nullptr); ~QImageShowWidget(); bool LoadImage(const char* imagePath); signals: public slots: protected: void paintEvent(QPaintEvent *); //繪製 void Release(); private: uchar* winBuf; //窗口填充buf int winWidth; //窗口像素寬 int winHeight; //窗口像素高 int winBandNum; //波段數 }; #endif // QIMAGESHOWWIDGET_H
qimageshowwidget.cpp代碼以下:code
#include "qimageshowwidget.h" #include <opencv2\opencv.hpp> #include <QPainter> #include <QDebug> #include <iostream> using namespace cv; using namespace std; QImageShowWidget::QImageShowWidget(QWidget *parent) : QWidget(parent) { //填充背景色 setAutoFillBackground(true); setBackgroundRole(QPalette::Base); winBuf = nullptr; winWidth = rect().width(); winHeight = rect().height(); winBandNum = 3; } QImageShowWidget::~QImageShowWidget() { if(winBuf) { delete[] winBuf; winBuf = nullptr; } } bool QImageShowWidget::LoadImage(const char* imagePath) { //從文件中讀取成灰度圖像 Mat img = imread(imagePath); if (img.empty()) { fprintf(stderr, "Can not load image %s\n", imagePath); return false; } Release(); winWidth = rect().width(); winHeight = rect().height(); size_t winBufNum = (size_t) winWidth * winHeight * winBandNum; winBuf = new uchar[winBufNum]; memset(winBuf, 255, winBufNum*sizeof(uchar)); for (int ri = 0; ri < img.rows; ++ri) { for (int ci = 0; ci < img.cols; ++ci) { for(int bi = 0; bi < winBandNum; bi++) { size_t m = (size_t) winWidth * winBandNum * ri + winBandNum * ci + bi; size_t n = (size_t) img.cols * winBandNum * ri + winBandNum * ci + bi; winBuf[m] = img.data[n]; } } } update(); return true; } //從新實現paintEvent void QImageShowWidget::paintEvent(QPaintEvent *) { if(!winBuf) { return; } QImage::Format imgFomat = QImage::Format_RGB888; QPainter painter(this); QImage qImg(winBuf, winWidth, winHeight, winWidth*winBandNum, imgFomat); painter.drawPixmap(0, 0, QPixmap::fromImage(qImg)); } void QImageShowWidget::Release() { if(winBuf) { delete[] winBuf; winBuf = nullptr; } }
全部基於QWidget的類均可以從新實現界面重繪事件paintEvent(),它會在界面須要的時候(例如調用update())自動重繪。在這個事件函數中能夠經過圖形繪製接口QPainter繪製:orm
QImage::Format imgFomat = QImage::Format_RGB888; QPainter painter(this); QImage qImg(winBuf, winWidth, winHeight, winWidth*winBandNum, imgFomat); painter.drawPixmap(0, 0, QPixmap::fromImage(qImg));
能夠看到QPainter繪製的實際上是QImage對象,也就是重點是構造QImage這個對象。這個對象是由申請的內存winBuf來構建的。顯示的圖像是由寬、高以及波段組成的,須要將三維空間壓縮爲一維空間——簡單來說,內存的組成爲RGBRGBRGB...,而且起點位置爲左上角,由左至右,由上至下。對象
OpenCV讀取的圖像爲Mat對象:blog
//從文件中讀取成灰度圖像 Mat img = imread(imagePath); if (img.empty()) { fprintf(stderr, "Can not load image %s\n", imagePath); return false; }
Mat對象能夠經過data()方法直接訪問讀取的圖像內存。而這塊內存也是RGBRGBRGB...的結構組成,而且起點位置也是左上角,由左至右,由上至下。將其逐像素傳入到申請的內存winBuf:
winWidth = rect().width(); winHeight = rect().height(); size_t winBufNum = (size_t) winWidth * winHeight * winBandNum; winBuf = new uchar[winBufNum]; memset(winBuf, 255, winBufNum*sizeof(uchar)); for (int ri = 0; ri < img.rows; ++ri) { for (int ci = 0; ci < img.cols; ++ci) { for(int bi = 0; bi < winBandNum; bi++) { size_t m = (size_t) winWidth * winBandNum * ri + winBandNum * ci + bi; size_t n = (size_t) img.cols * winBandNum * ri + winBandNum * ci + bi; winBuf[m] = img.data[n]; } } }
經過界面加載一張圖像,顯示結果以下: