Qt 用Opencv导入图像后,重新缩放图片,储存图片

继第一篇文章后,对这个软件进行新的操作,重新缩放图片

第一篇蚊帐传送门:http://blog.csdn.net/andromeda_wck/article/details/79173728

图片导入之后,我们对界面上的图像缩放按键进行填写;

首先,在构造函数中增加一句该按键的使能(红色),没有导入图片的情况下,该按键不可用,并在导入图片之后设置可用:

ImageProcess::ImageProcess(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ImageProcess)
{
    ui->setupUi(this);
    ui->graphicsView->setMouseTracking(true);//when this is open the graphics view widget can traking mouse
    setMouseTracking(true);
    ImageProcess::centralWidget()->setMouseTracking(true);
    ui->ResizeImage_Button->setEnabled(false);
}
我期望的状况是,按下这个按钮,启动一个小的对话框,然后输入需要缩放到什么尺寸,所以此处引入一个对话框类:

SizeInputDialog.h 如下,构造函数我们增加两个int参数,用于提示输入的默认数据(改变前的原始尺寸)

#ifndef SIZEINPUTDIALOG_H
#define SIZEINPUTDIALOG_H
#include"imageprocess.h"
#include <QDialog>
namespace Ui {
class SizeInputDialog;
}
class SizeInputDialog : public QDialog
{
    Q_OBJECT
public:
    explicit SizeInputDialog(int w,int h,QWidget *parent = 0);
    ~SizeInputDialog();
private slots:
    void on_buttonBox_accepted();
signals:
    void  SendData(int Width,int Height);
private:
    Ui::SizeInputDialog *ui;
    int Width;
    int Height;
};

#endif // SIZEINPUTDIALOG_H
SizeInputDialog.cpp如下:

#include "sizeinputdialog.h"
#include "ui_sizeinputdialog.h"
#include<QMessageBox>
SizeInputDialog::SizeInputDialog(int w,int h, QWidget *parent) :
    QDialog(parent),//修改构造函数
    ui(new Ui::SizeInputDialog)
{
    ui->setupUi(this);
    ui->HeightInput->setText(QString::number(h));
    ui->Width_input->setText(QString::number(w));
    Width=w;
    Height=h;
}

SizeInputDialog::~SizeInputDialog()
{
    delete ui;
}

void SizeInputDialog::on_buttonBox_accepted()
{
    bool ok=true;

   QString inputStr=ui->Width_input->text();
   inputStr.push_back(ui->HeightInput->text());
   foreach (QChar ss, inputStr) {
       if(ss>'9'||ss<'0')
       {
           ok=false;
           break;
       }
   }
   if(ok)
   {
       Width=ui->Width_input->text().toInt();
       Height=ui->HeightInput->text().toInt();
   }
   else
   {
       QMessageBox::warning(this,"Input Error","Illegal input!");
   }
   emit SendData(Width,Height);
}
加入了检测是否输入合法,如果不合法,就按照原尺寸返回数据,此处使用了信号和槽进行数据通信。
在主窗口,完善ResizeButton的操作:

void ImageProcess::on_ResizeImage_Button_clicked()
{
    b_ResizeButton=true;
    SizeInputDialog *inpudialog=new SizeInputDialog(LoadImage.cols,LoadImage.rows);
    inpudialog->show();
    connect(inpudialog,SIGNAL(SendData(int,int)),this,SLOT(ReceiveFromInputDialog(int,int)));
}
第一个bool参数在ImageProcess中进行了声明,默认为false,因为后期可能用到这个输入长宽的函数,此处用bool数据来验证是哪个按键的操作。

此处进行了信号和槽的连接,用于接收返回数据。

返回数据接收函数如下设置:

 void ImageProcess::ReceiveFromInputDialog(int r_Width,int r_Height)
 {
     this->show();
     if(b_ResizeButton)
     {
         cv::resize(LoadImage,LoadImage,cv::Size(r_Width,r_Height),0,0);
         ImageShow();
     }

 }
添加了刷新显示图像的函数,Imageshow(),其构成如下

void ImageProcess::ImageShow()
{ // to update the showing image;
    QPixmap ImageP=QPixmap::fromImage(cvMat2QImage(LoadImage));
    QGraphicsScene *scene = new QGraphicsScene;
    scene->addPixmap(ImageP);
    ui->graphicsView->setScene(scene);
    ui->graphicsView->show();
    ui->Width_Image_LineEdit->setText(QString::number(LoadImage.cols));
    ui->Height_Image_LineEdit->setText(QString::number(LoadImage.rows));
}
效果展示:


缩小为原尺寸的一半:


下一篇博客准备写储存和裁剪。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值