VisionFlowPro/NodeEditorPro/examples/opcv/CvImageViewWidget.cpp
2023-02-28 14:50:28 +08:00

86 lines
1.8 KiB
C++

#include "opcv/CvImageViewWidget.h"
#include <QDebug>
CvImageViewWidget::CvImageViewWidget(QWidget* parent)
{
if (parent != Q_NULLPTR)
this->setParent(parent);
this->setStyleSheet("background-color:black;");
curPixmap = new QPixmap();
}
void CvImageViewWidget::showImage(cv::Mat const& _cvimg)
{
if (_cvimg.empty())
{
curPixmap = new QPixmap();
this->setPixmap(*curPixmap);
this->update();
return;
}
int width;
int height;
double zoomRatio = 1.0;
width = _cvimg.cols;
height = _cvimg.rows;
if (width > this->width())
zoomRatio = 1.0 * this->width() / width;
cv::resize(_cvimg, curImage, cv::Size(width * zoomRatio, height * zoomRatio));
CvImageToQPixmap(curImage, *curPixmap);
this->update();
}
void CvImageViewWidget::CvImageToQPixmap(cv::Mat const& _img, QPixmap& tarImg)
{
const uchar* pSrc = (const uchar*)_img.data;
QImage image;
if (_img.type() == CV_8UC1)
{
qDebug() << "CV_8UC1";
image = QImage(pSrc, _img.cols, _img.rows, _img.step, QImage::Format_Grayscale8);
}
else if (_img.type() == CV_8UC3)
{
qDebug() << "CV_8UC3";
image = QImage(pSrc, _img.cols, _img.rows, _img.step, QImage::Format_RGB888);
}
else if (_img.type() == CV_8UC4)
{
qDebug() << "CV_8UC4";
image = QImage(pSrc, _img.cols, _img.rows, _img.step, QImage::Format_ARGB32);
}
else
{
qDebug() << "ERROR: Mat could not be converted to QImage.";
return;
}
tarImg = QPixmap::fromImage(image);
}
bool CvImageViewWidget::QImage2CvImage(QImage& from, cv::Mat& to)
{
return false;
}
void CvImageViewWidget::QPixmapToCvRegion(QPixmap const& _pix, cv::Rect2d& tarImg)
{
}
void CvImageViewWidget::paintEvent(QPaintEvent* event)
{
//QPainter painter(this);
if (!curPixmap->isNull())
{
this->setPixmap(curPixmap->scaled(this->width(), this->height(), Qt::KeepAspectRatio));
}
QLabel::paintEvent(event);
}