#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QGraphicsItem>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ImageViewscene =new QGraphicsScene(this);
ImagePix =new QGraphicsPixmapItem();
Rectang =new QGraphicsRectItem(); //矩形
ImageViewscene->addItem(ImagePix);
ImageViewscene->addItem(Rectang);
ui->graphicsView->setScene(ImageViewscene); //待检测图片以及检测图片绑定场景
QObject::connect(ui->graphicsView,SIGNAL(mouseClickedPoint(QPoint)), this, SLOT(on_mouseClickedPoint(QPoint)));
QObject::connect(ui->graphicsView,SIGNAL(mouseMovePoint(QPoint)), this, SLOT(on_mouseMovePoint(QPoint)));
QObject::connect(ui->graphicsView,SIGNAL(mouseReleasePoint(QPoint)), this, SLOT(on_mouseReleasePoint(QPoint)));
}
MainWindow::~MainWindow()
{
delete ui;
}
//void MainWindow::on_pushButton_clicked()
//{
// GlobalImg= imread("D:/123.png");
// Rect rect(2000,2000, 200, 200);
// Mat ROI = GlobalImg(rect);
// imshow("ROI_WIN",ROI);
//}
void MainWindow::on_mouseClickedPoint(QPoint point) //鼠标移动
{
QPointF pointScene=ui->graphicsView->mapToScene(point); //转换到Scene坐标
StartPoint = pointScene;
}
void MainWindow::on_mouseMovePoint(QPoint point)
{
QPointF pointScene=ui->graphicsView->mapToScene(point); //转换到Scene坐标
EndPoint=pointScene;
QPen RedPen(Qt::red);
ImageViewscene->removeItem(Rectang);
Rectang->setPen(RedPen);
Rectang->setRect(StartPoint.x(), StartPoint.y(),EndPoint.x()-StartPoint.x(), EndPoint.y()-StartPoint.y());
ImageViewscene->addItem(Rectang);
qDebug()<<StartPoint<<EndPoint;
}
void MainWindow::on_mouseReleasePoint(QPoint point)
{
QPointF pointScene=ui->graphicsView->mapToScene(point); //转换到Scene坐标
EndPoint=pointScene;
QPen RedPen(Qt::red);
ImageViewscene->removeItem(Rectang);
ImageViewscene->addRect(StartPoint.x(), StartPoint.y(),EndPoint.x()-StartPoint.x(), EndPoint.y()-StartPoint.y(),RedPen);
Rect rect(StartPoint.x(), StartPoint.y(),EndPoint.x()-StartPoint.x(), EndPoint.y()-StartPoint.y());
Mat ROI = GlobalImg(rect); //截取感兴趣区域
QImage imgROI =Mat2QImage(ROI);
ui->label->setPixmap(QPixmap::fromImage(imgROI));
ui->label->setScaledContents(true);
}
void MainWindow::on_OpenImage_clicked()
{
QString filename1=QFileDialog::getOpenFileName(this,QStringLiteral("打开图片"),"../");
if(filename1.isEmpty()==false)
{
cv::String cvfilename1=filename1.toLocal8Bit().toStdString();//转换为OpenCV路径
GlobalImg=imread(cvfilename1);
cvtColor(GlobalImg,GlobalImg,CV_BGR2RGB);
Rect rect(2000,2000, 200, 200);
Mat ROI = GlobalImg(rect); //截取感兴趣区域
QImage imgROI =Mat2QImage(ROI);
QImage img = QImage((const unsigned char*)(GlobalImg.data),GlobalImg.cols,GlobalImg.rows, GlobalImg.cols*GlobalImg.channels(), QImage::Format_RGB888);
ImageViewscene->addPixmap(QPixmap::fromImage(img));
ui->label->setPixmap(QPixmap::fromImage(imgROI));
ui->label->setScaledContents(true);
QPen GreenPen(Qt::red);
ImageViewscene->addLine(0,ImageViewscene->height()/2.0,ImageViewscene->width(),ImageViewscene->height()/2.0,GreenPen);
ImageViewscene->addLine(ImageViewscene->width()/2.0,0,ImageViewscene->width()/2.0,ImageViewscene->height(),GreenPen); //设置0点标识
}
qDebug()<<ImageViewscene->width()<<ImageViewscene->height();
}
//Mat图像转Qimage图像
QImage MainWindow::Mat2QImage(const cv::Mat& InputMat)
{
cv::Mat TmpMat;
QImage Result;
if (InputMat.channels() == 1)
{
cv::cvtColor(InputMat, TmpMat, CV_GRAY2RGB);
Result = QImage((const uchar*)(TmpMat.data), TmpMat.cols, TmpMat.rows, TmpMat.cols*TmpMat.channels(),
QImage::Format_Indexed8);
}
else
{
cv::cvtColor(InputMat, TmpMat, CV_BGR2RGB);
Result = QImage((const uchar*)(TmpMat.data), TmpMat.cols, TmpMat.rows, TmpMat.cols*TmpMat.channels(),
QImage::Format_RGB888);
}
Result.bits();
return Result;
}
![avatar](https://profile-avatar.csdnimg.cn/4c6fd3abcfe5413f8fbd43559dd86b60_liangyuhc.jpg!1)
Angus__liang
- 粉丝: 3
- 资源: 1
最新资源
- 【JCR一区级】鸽群算法PIO-Transformer-GRU负荷数据回归预测【含Matlab源码 6315期】.zip
- 【LSTM回归预测】粒子群优化注意力机制的长短时记忆神经网络PSO-attention-LSTM数据回归预测【含Matlab源码 3196期】.zip
- 【独家首发】麻雀搜索算法SSA优化Transformer-BiLSTM负荷数据回归预测【含Matlab源码 6564期】.zip
- 【独家首发】凌日算法TSOA优化Transformer-BiLSTM负荷数据回归预测【含Matlab源码 6562期】.zip
- 【独家首发】能量谷算法EVO优化Transformer-BiLSTM负荷数据回归预测【含Matlab源码 6565期】.zip
- 【JCR一区级】豪猪算法CPO-Transformer-GRU负荷数据回归预测【含Matlab源码 6319期】.zip
- 【JCR一区级】雪融算法SAO-Transformer-GRU负荷数据回归预测【含Matlab源码 6351期】.zip
- 【JCR一区级】黑猩猩算法Chimp-Transformer-GRU负荷数据回归预测【含Matlab源码 6320期】.zip
- 【独家首发】粒子群算法PSO优化Transformer-BiLSTM负荷数据回归预测【含Matlab源码 6561期】.zip
- 【JCR1区】阿基米德算法AOA-CNN-SVM故障诊断分类预测【含Matlab源码 5772期】.zip
- 【JCR一区级】蝗虫算法GOA-Transformer-GRU负荷数据回归预测【含Matlab源码 6322期】.zip
- 【JCR一区级】星雀算法NOA-Transformer-GRU负荷数据回归预测【含Matlab源码 6350期】.zip
- 【JCR一区级】花朵授粉算法FPA-Transformer-GRU负荷数据回归预测【含Matlab源码 6321期】.zip
- 【JCR1区】蝗虫算法GOA-CNN-SVM故障诊断分类预测【含Matlab源码 5794期】.zip
- 【JCR一区级】混沌博弈算法CGO-Transformer-GRU负荷数据回归预测【含Matlab源码 6324期】.zip
- 【JCR一区级】减法平均算法SABO-Transformer-GRU负荷数据回归预测【含Matlab源码 6325期】.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
评论5