#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;
}
Angus__liang
- 粉丝: 3
- 资源: 1
最新资源
- 没用333333333333333333333333333333
- 基于Vue和SpringBoot的企业员工管理系统2.0版本设计源码
- 【C++初级程序设计·配套源码】第2期-基本数据类型
- 基于Java和Vue的kopsoftKANBAN车间电子看板设计源码
- 影驰战将PS3111 东芝芯片TT18G23AIN开卡成功分享,图片里面画线的选项很重要
- 【C++初级程序设计·配套源码】第1期-语法基础
- 基于JavaScript、CSS、HTML的简易DOM版飞机游戏设计源码
- 基于Java开发的日程管理FlexTime应用设计源码
- SM2258XT-BGA144-4BGA180-6L-R1019 三星KLUCG4J1CB B0B1颗粒开盘工具 , EC, 3A, 94, 43, A4, CA 七彩虹SL300这个固件有用
- GJB 5236-2004 军用软件质量度量
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论5