今晚将测试一晚上如果不显示是否还是会导致卡顿

This commit is contained in:
2024-05-21 01:26:00 +08:00
parent ed55e511ec
commit cc59a989b1
31 changed files with 287 additions and 245 deletions
@@ -17,53 +17,9 @@
#include <uiModule/components/fancyButton.h>
#include "qscrollbar.h"
#include "globalStruct.h"
static QImage cvMatToQImage(cv::Mat& src)
{
if (src.channels() == 1) { // if grayscale image
return QImage((uchar*)src.data, src.cols, src.rows, static_cast<int>(src.step), QImage::Format_Grayscale8).copy();
}
if (src.channels() == 3) { // if 3 channel color image
cv::Mat rgbMat;
cv::cvtColor(src, rgbMat, cv::COLOR_BGR2RGB); // invert BGR to RGB
return QImage((uchar*)rgbMat.data, src.cols, src.rows, static_cast<int>(src.step), QImage::Format_RGB888).copy();
}
return QImage();
}
static cv::Mat qImageToCvMat(const QImage& inImage)
{
switch (inImage.format())
{
case QImage::Format_RGB32:
case QImage::Format_RGB888:
{
QImage swapped = inImage;
if (inImage.format() == QImage::Format_RGB32)
{
swapped = swapped.convertToFormat(QImage::Format_RGB888);
}
cv::Mat matImg = cv::Mat(swapped.height(), swapped.width(),
CV_8UC3,
const_cast<uchar*>(swapped.bits()),
static_cast<size_t>(swapped.bytesPerLine())
);
cv::cvtColor(matImg, matImg, cv::COLOR_RGB2BGR);
return matImg.clone();
}
case QImage::Format_Indexed8:
{
cv::Mat mat(inImage.height(), inImage.width(),
CV_8UC1,
const_cast<uchar*>(inImage.bits()),
static_cast<size_t>(inImage.bytesPerLine())
);
return mat.clone();
}
default:
break;
}
return cv::Mat();
}
#include <qqueue.h>
#include <qthread.h>
class ImageWidgetController;
class SonGraphicsView :
public QGraphicsView
{
@@ -74,7 +30,7 @@ public:
{
//scene = new QGraphicsScene();
//this->setScene(scene);
mutex.lock();
setDragMode(QGraphicsView::DragMode::NoDrag);
setInteractive(false);
setEnabled(false);
@@ -85,53 +41,44 @@ public:
"background-position: center center;"
"background-size: contain;"
"}");
initShow();
//initShow();
scene = new QGraphicsScene(this);
this->setScene(scene);
//this->fitInView(pixmapItem, Qt::KeepAspectRatio);
this->setBackgroundBrush(QBrush(QColor(0, 0, 0)));
setEnabled(true);
setMouseTracking(true);
imgLabel = new QLabel();
scene->clear();
scene->addWidget(imgLabel);
imgLabel->setMaximumSize(9999, 9999);
mutex.unlock();
}
void setQImage(QImage image)
void setImg(QImage& image)
{
if (image.isNull())
return;
m_image = QPixmap::fromImage(image);
initShow();
showimg(QPixmap::fromImage(image));
}
void setMatImage(cv::Mat src)
void setImg(cv::Mat& src)
{
if (src.empty())
return;
m_image = QPixmap::fromImage(cvMatToQImage(src));
initShow();
showimg(QPixmap::fromImage(cvMatToQImage(src)));
}
void setPixmap(QPixmap pixmap)
void setImg(QPixmap& pixmap)
{
m_image = pixmap.copy();
initShow();
showimg(pixmap);
}
void resetImage()
{
if (m_image.isNull())
return;
scene->clear();
setEnabled(false);
}
QImage getQImage()
{
return m_image.toImage();
}
cv::Mat getMatImage()
{
return qImageToCvMat(m_image.toImage());
}
QPixmap getPixmap()
{
return m_image;
}
protected:
virtual void wheelEvent(QWheelEvent* event) override
{
if (m_image.isNull())
if (scene->children().size()==0)
return;
QPoint numDegrees = event->angleDelta() / 8;
if (!numDegrees.isNull()) {
@@ -142,11 +89,11 @@ protected:
}
virtual void keyPressEvent(QKeyEvent* event) override
{
if (event->key() == Qt::Key_O) {
this->resetTransform();
this->setSceneRect(m_image.rect());
this->fitInView(QRect(0, 0, m_image.width(), m_image.height()), Qt::KeepAspectRatio);
}
//if (event->key() == Qt::Key_O) {
// this->resetTransform();
// this->setSceneRect(m_image.rect());
// this->fitInView(QRect(0, 0, m_image.width(), m_image.height()), Qt::KeepAspectRatio);
//}
}
virtual void mousePressEvent(QMouseEvent* event) override
{
@@ -179,8 +126,8 @@ protected:
{
QRectF FOV = this->mapToScene(this->rect()).boundingRect();
QRectF FOVImage = QRectF(FOV.left(), FOV.top(), FOV.width(), FOV.height());
float scaleX = static_cast<float>(m_image.width()) / FOVImage.width();
float scaleY = static_cast<float>(m_image.height()) / FOVImage.height();
float scaleX = static_cast<float>(imageWidth) / FOVImage.width();
float scaleY = static_cast<float>(imageHeight) / FOVImage.height();
float minScale = scaleX > scaleY ? scaleY : scaleX;
float maxScale = scaleX > scaleY ? scaleX : scaleY;
if ((factor.y() > 0 && minScale > 100) || (factor.y() < 0 && maxScale < 1)) {
@@ -219,111 +166,28 @@ protected:
hBar->setValue(hBar->value() - delta.x());
vBar->setValue(vBar->value() - delta.y());
}
void initShow()
{
setEnabled(true);
setMouseTracking(true);
#if 0
scene->clear();
scene->addPixmap(m_image);
scene->update();
this->resetTransform();
this->setSceneRect(m_image.rect());
this->fitInView(QRect(0, 0, m_image.width(), m_image.height()), Qt::KeepAspectRatio);
#endif
if(scene==nullptr)
delete scene;
scene = new QGraphicsScene;
QImage image1 = getQImage();
QPixmap pixmap = QPixmap::fromImage(image1);
if (pixmap.isNull())
return;
QGraphicsPixmapItem* pixmapItem = scene->addPixmap(pixmap);
this->setScene(scene);
this->fitInView(pixmapItem, Qt::KeepAspectRatio);
this->show();
}
private:
void showimg(cv::Mat& img)
{
scene->clear();
// 加载图像
cv::Mat matForQimg;
cvtColor(img, matForQimg, cv::COLOR_BGR2RGB);
QImage disImage = QImage((const unsigned char*)(matForQimg.data), matForQimg.cols, matForQimg.rows, QImage::Format_RGB888);
QPixmap pixmap = QPixmap::fromImage(disImage);
if (pixmap.isNull()) {
return;
}
auto item = scene->addPixmap(pixmap);
setSceneRect(pixmap.rect());
// 自适应缩放
fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
// 设置视图
//setFixedSize(640, 480);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setMouseTracking(true);
if (rectGroup != nullptr)
delete rectGroup;
rectGroup = new QGraphicsItemGroup();
scene->addItem(rectGroup);
}
void showimg(QImage& img)
{
scene->clear();
// 加载图像
QPixmap pixmap = QPixmap::fromImage(img);
if (pixmap.isNull()) {
return;
}
auto item = scene->addPixmap(pixmap);
setSceneRect(pixmap.rect());
// 自适应缩放
fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
// 设置视图
//setFixedSize(640, 480);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setMouseTracking(true);
if (rectGroup != nullptr)
delete rectGroup;
rectGroup = new QGraphicsItemGroup();
scene->addItem(rectGroup);
}
void showimg(QPixmap& img)
{
scene->clear();
// 加载图像
if (img.isNull()) {
return;
}
auto item = scene->addPixmap(img);
setSceneRect(img.rect());
mutex.lock();
//scene->clear();
//scene->addPixmap(img);
imgLabel->setPixmap(img);
// 自适应缩放
QRect rect = img.rect();
imageWidth = rect.width();
imageHeight = rect.height();
imgLabel->setFixedSize(imageWidth, imageHeight);
setSceneRect(rect);
fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
// 设置视图
//setFixedSize(640, 480);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setMouseTracking(true);
if (rectGroup != nullptr)
delete rectGroup;
rectGroup = new QGraphicsItemGroup();
scene->addItem(rectGroup);
mutex.unlock();
}
QPixmap m_image;
bool m_isPan;
QPoint m_prevPan;
QLabel* imgLabel;
QGraphicsItemGroup* rectGroup = nullptr;
int imageWidth = 1; int imageHeight = 1;
QMutex mutex;
};
class ImageWidget : public QWidget {
public:
@@ -390,7 +254,7 @@ public:
label->setText(text);
}
//set不一定显示,要根据当前显示的类型决定
void setOriImg(cv::Mat oriImgInput)
void setOriImg(cv::Mat& oriImgInput)
{
if (oriImgInput.empty()) {
return;
@@ -404,7 +268,7 @@ public:
showImg(pixOriImg);
}
//set不一定显示,要根据当前显示的类型决定
void setResImg(cv::Mat resImgInput)
void setResImg(cv::Mat& resImgInput)
{
if (resImgInput.empty()) {
return;
@@ -418,7 +282,7 @@ public:
showImg(pixResImg);
}
//set不一定显示,要根据当前显示的类型决定
void setOriImg(QPixmap oriImgInput)
void setOriImg(QPixmap& oriImgInput)
{
pixOriImg = oriImgInput;
if (pixOriImg.isNull()) {
@@ -428,7 +292,7 @@ public:
showImg(pixOriImg);
}
//set不一定显示,要根据当前显示的类型决定
void setResImg(QPixmap resImgInput)
void setResImg(QPixmap& resImgInput)
{
pixResImg = resImgInput;
if (pixResImg.isNull()) {
@@ -452,10 +316,10 @@ public:
if (pixmap.isNull()) {
return;
}
view->setPixmap(pixmap);
view->setImg(pixmap);
}
void showImg(QPixmap& imageMat) {
view->setPixmap(imageMat);
view->setImg(imageMat);
}
void switchOriAndRes()//切换原图和结果图
{
@@ -536,6 +400,9 @@ public:
this->setLayout(Layout);
Layout->setContentsMargins(0, 0, 0, 0);
}
~ImageWidgetController()
{
}
ImageWidget* createViewer(QString nickName = "none")
{
ImageWidget* ImageView = new ImageWidget();
@@ -551,7 +418,7 @@ public:
QList< ImageWidget*>ImageWidgetList;
std::unordered_map<QString, ImageWidget*>map;
bool setImg(cv::Mat img, imgWidgetShowType _type , int index)
bool showImg(cv::Mat& img, imgWidgetShowType _type , int index)
{
if (index > ImageWidgetList.size())
return false;
@@ -566,7 +433,7 @@ public:
}
}
bool setImg(cv::Mat img, imgWidgetShowType _type, QString nickName)
bool showImg(cv::Mat& img, imgWidgetShowType _type, QString nickName)
{
if (map.find(nickName) == map.end())
return false;
@@ -580,7 +447,7 @@ public:
break;
}
}
bool setImg(QPixmap img, imgWidgetShowType _type, int index)
bool showImg(QPixmap& img, imgWidgetShowType _type, int index)
{
if (index > ImageWidgetList.size())
return false;
@@ -595,7 +462,7 @@ public:
}
}
bool setImg(QPixmap img, imgWidgetShowType _type, QString nickName)
bool showImg(QPixmap& img, imgWidgetShowType _type, QString nickName)
{
if (map.find(nickName) == map.end())
return false;