2020年8月5日21:35:43 图像左右上下翻转

 代码如下:功能比较简单,没有什么难度,基本思路是从数据中获取图像ID,读取进行旋转镜像操作。需要主要的是,imread(ImagePath[0].toLocal8Bit().toStdString()),因为路径有中文,会出现读取不到的现象。

  • The code is as follows: the function is relatively simple, there is no difficulty, the basic idea is to get the image ID from the data, read to rotate the mirror operation.The main thing is imread(ImagePath[0]. ToLocal8Bit (). ToStdString ())), because the path has Chinese, it can't be read.
  • 2020年8月5日21:35:43 图像左右上下翻转_第1张图片

#pragma execution_character_set("utf-8")//设置UI显示中文
#include "image_roate.h"
#include "ui_image_roate.h"

/*数据库头文件*/
#include 
#include 
#include 
#include 
#include 

#include 

image_roate::image_roate(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::image_roate)
{
    ui->setupUi(this);

    OnButtonSingalSlotInit(); //Singal --- Slot
    OnUIinitialization();
    OnImageinitialization();//从数据库中获取图像
}

void  image_roate::OnUIinitialization()
{
    #if 0
    ui->horizontalSlider_contrarotate->setMinimum(0);  // 最小值
    ui->horizontalSlider_contrarotate->setMaximum(180);  // 最大值
    ui->horizontalSlider_contrarotate->setSingleStep(1);  // 步长
    ui->horizontalSlider_contrarotate->setTickInterval(10);  // 设置刻度间隔
    ui->horizontalSlider_contrarotate->setTickPosition(QSlider::TicksAbove);  //刻度在上方
    #endif

    ui->spinBox_contrarotate->setMinimum(0.0);
    ui->spinBox_contrarotate->setMaximum(360.0);
    ui->spinBox_contrarotate->setSingleStep(0.1);

    ui->spinBox_clockwiserotate->setMinimum(0.0);
    ui->spinBox_clockwiserotate->setMaximum(360.0);
    ui->spinBox_clockwiserotate->setSingleStep(0.1);
}

void image_roate::OnImageinitialization()
{
    //====================================================================================
    QSqlDatabase databaseBook = QSqlDatabase::addDatabase("QSQLITE");
    databaseBook.setDatabaseName("./DataDB.db");
    if( !databaseBook.open())
    {
       qDebug() << databaseBook.lastError();
       qFatal("Failed to connect.");
    }
    else
    {
        qDebug( "image roate Connected!" );
        QSqlQuery query;
        query.prepare("select * from scan_picture");
        if(!query.exec())
        {
            qDebug()< 0)
    {
        CurrentImage = imread(ImagePath[0].toLocal8Bit().toStdString());//读取原图像
        CurrentImagePath = ImagePath[0].toLocal8Bit().toStdString(); //当前图像具体地址
        QImage image = cvMat2QImage(imread(ImagePath[0].toLocal8Bit().toStdString()));//info_img表示输出图片
        ui->label_PageImage->setScaledContents(true);
        ui->label_PageImage->resize(ui->label_PageImage->size());
        ui->label_PageImage->setPixmap(QPixmap::fromImage(image));
    }
    else
    {
        QMessageBox::information(nullptr, "提醒", "扫描数据库中无该图片!");
    }

    #if 0//测试代码
    //qDebug() << ImagePath;//所有的图像路径
    qDebug() << ImagePath[0];
    src = imread(ImagePath[0].toLocal8Bit().toStdString());//读取原图像  String PerImagePath = bookVec[i][2].replace("\\","/").toLocal8Bit().toStdString();
    QImage image = cvMat2QImage(src);//info_img表示输出图片
    ui->label_PageImage->setScaledContents(true);
    ui->label_PageImage->resize(ui->label_PageImage->size());
    ui->label_PageImage->setPixmap(QPixmap::fromImage(image));
    #endif
}
image_roate::~image_roate()
{
    delete ui;
}

Mat Rotate(Mat src,double angle)
{
      Mat dst;
      // 旋转角度
      //double angle = 45.0;

      // 计算旋转后输出图形的尺寸
      int rotated_width = ceil(src.rows * fabs(sin(angle * CV_PI / 180)) + src.cols * fabs(cos(angle * CV_PI / 180)));
      int rotated_height = ceil(src.cols * fabs(sin(angle * CV_PI / 180)) + src.rows * fabs(cos(angle * CV_PI / 180)));

      // 计算仿射变换矩阵
      Point2f center(src.cols / 2, src.rows / 2);
      Mat rotate_matrix = getRotationMatrix2D(center, angle, 1.0);

      // 防止切边,对平移矩阵B进行修改
      rotate_matrix.at(0, 2) += (rotated_width - src.cols) / 2;
      rotate_matrix.at(1, 2) += (rotated_height - src.rows) / 2;

      // 应用仿射变换
      warpAffine(src, dst, rotate_matrix, Size(rotated_width, rotated_height), INTER_LINEAR, 0, Scalar(255, 255, 255));
      //imshow("result", dst);
      //cv::imwrite("right.jpg", dst);
      //waitKey();
      return dst;
}

// cv::Mat转换成QImage
QImage image_roate::cvMat2QImage(const Mat& mat)
{
    if (mat.type() == CV_8UC1)                          // 单通道
    {
        QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
        image.setColorCount(256);                       // 灰度级数256
        for (int i = 0; i < 256; i++)
        {
            image.setColor(i, qRgb(i, i, i));
        }
        uchar *pSrc = mat.data;                         // 复制mat数据
        for (int row = 0; row < mat.rows; row++)
        {
            uchar *pDest = image.scanLine(row);
            memcpy(pDest, pSrc, mat.cols);
            pSrc += mat.step;
        }
        return image;
    }
    else if (mat.type() == CV_8UC3)                     // 3通道
    {
        const uchar *pSrc = (const uchar*)mat.data;     // 复制像素
        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);    // R, G, B 对应 0,1,2
        return image.rgbSwapped();                      // rgbSwapped是为了显示效果色彩好一些。
    }
    else if (mat.type() == CV_8UC4)                     // 4通道
    {
        const uchar *pSrc = (const uchar*)mat.data;     // 复制像素
        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);        // B,G,R,A 对应 0,1,2,3
        return image.copy();
    }
    else
    {
        return QImage();
    }
}
/****************************************************************************
** file:
** brief: singal slot
** Copyright (C)
** Author:
** E-Mail:
** Version 2.0.1
** Last modified: 2020.07.20
** Modified By: 李伯爵的指间沙
****************************************************************************/
void image_roate::OnButtonSingalSlotInit()
{
    //--------------------------------------------------------------------
    connect(ui->Button_right_left, SIGNAL(clicked()), this, SLOT(OnRoateRL()));
    connect(ui->Button_up_down, SIGNAL(clicked()), this, SLOT(OnRoateUD()));
    connect(ui->Button_contrarotate_90, SIGNAL(clicked()), this, SLOT(OnRoateContrarotate_90()));
    connect(ui->Button_clockwiserotate_90, SIGNAL(clicked()), this, SLOT(OnRoateClockwiserotate_90()));
    connect(ui->Button_contrarotate_180, SIGNAL(clicked()), this, SLOT(OnRoateContrarotate_180()));
    connect(ui->Button_clockwiserotate_180, SIGNAL(clicked()), this, SLOT(OnRoateClockwiserotate_180()));
    connect(ui->Button_contrarotate_270, SIGNAL(clicked()), this, SLOT(OnRoateContrarotate_270()));
    connect(ui->Button_clockwiserotate_270, SIGNAL(clicked()), this, SLOT(OnRoateClockwiserotate_270()));
    //--------------------------------------------------------------------
    //connect(ui->horizontalSlider_contrarotate, SIGNAL(valueChanged(int)), this, SLOT(OnContrarotate(int)));
    //connect(ui->horizontalSlider_clockwiserotate, SIGNAL(clicked()), this, SLOT(OnRoateRL()));
    //--------------------------------------------------------------------
    connect(ui->Button_contrarotate, SIGNAL(clicked()), this, SLOT(OnContrarotate()));
    connect(ui->Button_clockwiserotate, SIGNAL(clicked()), this, SLOT(OnClockwiserotate()));
    //--------------------------------------------------------------------
    connect(ui->pushBtnLastPage, SIGNAL(clicked()), this, SLOT(OnLastPage()));
    connect(ui->pushBtnNextPage, SIGNAL(clicked()), this, SLOT(OnNextPage()));

}

//水平变换
Mat  Horizontal_transformation(Mat srcImage,Mat map_x,Mat map_y)
{
    Mat dstImage;
    for (int i = 0; i < srcImage.rows; i++)
    {
        for (int j = 0; j < srcImage.cols; j++)
        {
            map_x.at(i, j) = static_cast(i);
            map_y.at(i, j) = static_cast(srcImage.cols - j);
        }
    }
    remap(srcImage, dstImage, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar::all(0));
    return dstImage;
}

//垂直变换
Mat  Vertical_transformation(Mat srcImage,Mat map_x,Mat map_y)
{
    Mat dstImage;
    for (int i = 0; i < srcImage.rows; i++)
    {
        for (int j = 0; j < srcImage.cols; j++)
        {
            map_x.at(i, j) = static_cast(j);//列不变
            map_y.at(i, j) = static_cast(srcImage.rows - i);//行相反
        }
    }
    remap(srcImage, dstImage, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar::all(0));
    return dstImage;
}

void image_roate::OnRoateRL()
{
    //Mat mirrorImage;
    flip(CurrentImage, CurrentImage, 1);
    imwrite(CurrentImagePath,CurrentImage);

    QImage image = cvMat2QImage(CurrentImage);//info_img表示输出图片
    ui->label_PageImage->setScaledContents(true);
    ui->label_PageImage->resize(ui->label_PageImage->size());
    ui->label_PageImage->setPixmap(QPixmap::fromImage(image));
}

void image_roate::OnRoateUD()
{

    //Mat mirrorImage;
    flip(CurrentImage, CurrentImage, 0);
    imwrite(CurrentImagePath,CurrentImage);

    QImage image = cvMat2QImage(CurrentImage);//info_img表示输出图片
    ui->label_PageImage->setScaledContents(true);
    ui->label_PageImage->resize(ui->label_PageImage->size());
    ui->label_PageImage->setPixmap(QPixmap::fromImage(image));

}

//逆时针
void image_roate::OnRoateContrarotate_90()
{
    Mat dst = Rotate(CurrentImage,90.0);
    CurrentImage = dst.clone();
    imwrite(CurrentImagePath,CurrentImage);

    QImage image = cvMat2QImage(CurrentImage);//info_img表示输出图片
    ui->label_PageImage->setScaledContents(true);
    ui->label_PageImage->resize(ui->label_PageImage->size());
    ui->label_PageImage->setPixmap(QPixmap::fromImage(image));

    ui->textEdit_output->append("逆时针旋转90度");
}
//顺时针
void image_roate::OnRoateClockwiserotate_90()
{
    Mat dst = Rotate(CurrentImage,-90.0);
    CurrentImage = dst.clone();
    imwrite(CurrentImagePath,CurrentImage);

    QImage image = cvMat2QImage(CurrentImage);//info_img表示输出图片
    ui->label_PageImage->setScaledContents(true);
    ui->label_PageImage->resize(ui->label_PageImage->size());
    ui->label_PageImage->setPixmap(QPixmap::fromImage(image));

    ui->textEdit_output->append("顺时针旋转90度");
}
//逆时针
void image_roate::OnRoateContrarotate_180()
{
    Mat dst = Rotate(CurrentImage,180.0);
    CurrentImage = dst.clone();
    imwrite(CurrentImagePath,CurrentImage);

    QImage image = cvMat2QImage(CurrentImage);//info_img表示输出图片
    ui->label_PageImage->setScaledContents(true);
    ui->label_PageImage->resize(ui->label_PageImage->size());
    ui->label_PageImage->setPixmap(QPixmap::fromImage(image));

    ui->textEdit_output->append("逆时针旋转180度");
}
//顺时针
void image_roate::OnRoateClockwiserotate_180()
{
    Mat dst = Rotate(CurrentImage,-180.0);
    CurrentImage = dst.clone();
    imwrite(CurrentImagePath,CurrentImage);

    QImage image = cvMat2QImage(CurrentImage);//info_img表示输出图片
    ui->label_PageImage->setScaledContents(true);
    ui->label_PageImage->resize(ui->label_PageImage->size());
    ui->label_PageImage->setPixmap(QPixmap::fromImage(image));

    ui->textEdit_output->append("顺时针旋转180度");
}
//逆时针
void image_roate::OnRoateContrarotate_270()
{
    Mat dst = Rotate(CurrentImage,270.0);
    CurrentImage = dst.clone();
    imwrite(CurrentImagePath,CurrentImage);

    QImage image = cvMat2QImage(CurrentImage);//info_img表示输出图片
    ui->label_PageImage->setScaledContents(true);
    ui->label_PageImage->resize(ui->label_PageImage->size());
    ui->label_PageImage->setPixmap(QPixmap::fromImage(image));

    ui->textEdit_output->append("逆时针旋转270度");
}
//顺时针
void image_roate::OnRoateClockwiserotate_270()
{
    Mat dst = Rotate(CurrentImage,-270.0);
    CurrentImage = dst.clone();
    imwrite(CurrentImagePath,CurrentImage);

    QImage image = cvMat2QImage(CurrentImage);//info_img表示输出图片
    ui->label_PageImage->setScaledContents(true);
    ui->label_PageImage->resize(ui->label_PageImage->size());
    ui->label_PageImage->setPixmap(QPixmap::fromImage(image));

    ui->textEdit_output->append("顺时针旋转270度");
}

//逆时针
void image_roate::OnContrarotate()
{
    #if 0
    int pos = ui->horizontalSlider_contrarotate->value();
    QString str = QString("%1").arg(pos);
    ui->textEdit_output->append(str);

    Mat dst = Rotate(src,double(pos));
    QImage image = cvMat2QImage(dst);//info_img表示输出图片
    ui->label_PageImage->setScaledContents(true);
    ui->label_PageImage->resize(ui->label_PageImage->size());
    ui->label_PageImage->setPixmap(QPixmap::fromImage(image));
    #endif

    float num = ui->spinBox_contrarotate->value();            //获取角度

    Mat dst = Rotate(CurrentImage,num);
    CurrentImage = dst.clone();
    imwrite(CurrentImagePath,CurrentImage);

    QImage image = cvMat2QImage(CurrentImage);//info_img表示输出图片

    ui->label_PageImage->setScaledContents(true);
    ui->label_PageImage->resize(ui->label_PageImage->size());
    ui->label_PageImage->setPixmap(QPixmap::fromImage(image));

    ui->textEdit_output->append("逆时针旋转"+QString("%1").arg(num)+"度");

}

//顺时针
void image_roate::OnClockwiserotate()
{

    float num = ui->spinBox_clockwiserotate->value();            //获取角度
    Mat dst = Rotate(CurrentImage,-num);
    CurrentImage = dst.clone();
    imwrite(CurrentImagePath,CurrentImage);

    QImage image = cvMat2QImage(CurrentImage);//info_img表示输出图片

    ui->label_PageImage->setScaledContents(true);
    ui->label_PageImage->resize(ui->label_PageImage->size());
    ui->label_PageImage->setPixmap(QPixmap::fromImage(image));

    ui->textEdit_output->append("顺时针旋转"+QString("%1").arg(num)+"度");

}

//上一页
void image_roate::OnLastPage()
{

}

//下一页
void image_roate::OnNextPage()
{
    qDebug()<<"下一页";
    if(ImagePath.size()==0)
    {
        QMessageBox::warning(nullptr, "提醒:","暂无数据,\n请先打开图像进行识别!");
    }
    NextNumberPage++;
    qDebug()<label_PageImage->clear();
        qDebug()<label_PageImage->setScaledContents(true);
        ui->label_PageImage->resize(ui->label_PageImage->size());
        ui->label_PageImage->setPixmap(QPixmap::fromImage(image));
    }
    else
    {
        NextNumberPage--;
        if(NextNumberPage==0)
        {
            NextNumberPage = 0;
        }
        QMessageBox::warning(nullptr, "提醒:","已经是最后一张图像了");
    }
}

 I hope I can help you,If you have any questions, please  comment on this blog or send me a private message. I will reply in my free time.   

你可能感兴趣的:(C++,English,blog,QT)