博客
关于我
使用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万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>
mysql丢失更新问题
查看>>
MySQL两千万数据优化&迁移
查看>>
MySql中 delimiter 详解
查看>>
MYSQL中 find_in_set() 函数用法详解
查看>>
MySQL中auto_increment有什么作用?(IT枫斗者)
查看>>
MySQL中B+Tree索引原理
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
MySQL中DQL语言的执行顺序
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>