博客
关于我
使用QT显示OpenCV读取的图片
阅读量:261 次
发布时间:2019-03-01

本文共 2729 字,大约阅读时间需要 9 分钟。

OpenCV?QT?????

??

OpenCV?????????GUI?????????????????????????????GUI????????????????Qt?????OpenCV????????

????

?QtCreator???????QMainWindow?????????????QImageShowWidget?????????????QMainWindow?centralwidget??

????

header??

#ifndef QIMAGESHOWWIDGET_H#define QIMAGESHOWWIDGET_H#include 
#include
class QImageShowWidget : public QWidget{ Q_OBJECTpublic: explicit QImageShowWidget(QWidget *parent = nullptr); ~QImageShowWidget(); bool LoadImage(const char *imagePath);protected: void paintEvent(QPaintEvent *event); void Release(); private: uchar *winBuf; int winWidth; int winHeight; int winBandNum;};#endif // QIMAGESHOWWIDGET_H

source??

#include "qimageshowwidget.h"#include 
#include
#include
#include
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;}void QImageShowWidget::paintEvent(QPaintEvent *event){ if (!winBuf) { return; } QImage::Format imgFormat = QImage::Format_RGB888; QPainter painter(this); QImage qImg(winBuf, winWidth, winHeight, winWidth * winBandNum, imgFormat); painter.drawPixmap(0, 0, QPixmap::fromImage(qImg));}void QImageShowWidget::Release(){ if (winBuf) { delete[] winBuf; winBuf = nullptr; }}

????

?????QImageShowWidget

QImageShowWidget???QWidget?????????????????LoadImage??????????paintEvent?????????

??????

??QPainter??QImage?????winBuf??????????OpenCV?Mat???winBuf????????????????????

????

???????????????????????????

转载地址:http://qawx.baihongyu.com/

你可能感兴趣的文章
MySQL-【4】基本操作
查看>>
Mysql-丢失更新
查看>>
Mysql-事务阻塞
查看>>
Mysql-存储引擎
查看>>
mysql-开启慢查询&所有操作记录日志
查看>>
MySQL-数据目录
查看>>
MySQL-数据页的结构
查看>>
MySQL-架构篇
查看>>
MySQL-索引的分类(聚簇索引、二级索引、联合索引)
查看>>
Mysql-触发器及创建触发器失败原因
查看>>
MySQL-连接
查看>>
mysql-递归查询(二)
查看>>
MySQL5.1安装
查看>>
mysql5.5和5.6版本间的坑
查看>>
mysql5.5最简安装教程
查看>>
mysql5.6 TIME,DATETIME,TIMESTAMP
查看>>
mysql5.6.21重置数据库的root密码
查看>>
Mysql5.6主从复制-基于binlog
查看>>
MySQL5.6忘记root密码(win平台)
查看>>
MySQL5.6的Linux安装shell脚本之二进制安装(一)
查看>>