C++ Image roate

55 篇文章 0 订阅
50 篇文章 1 订阅

今天简单的实现一个通过按键控制图像旋转的功能。

  • Today's simple implementation of a button control image rotation function.。

通过拉动滑动条实现图像的旋转,但是直接拉动滑动条,可以实现图像旋转,但是比较卡顿。

  • The image rotation can be achieved by pulling the slider, while the image rotation can be achieved by pulling the slider directly, but the image rotation can be compared with the lag.

CODE:

init:

    OnButtonSingalSlotInit(); //Singal --- Slot

    #if 1//测试代码
    src = imread(".//01.jpg");//读取原图像
    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

    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);  //刻度在上方

singal and slot:

void MainWindow::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()));
    //--------------------------------------------------------------------

}

USE:

void MainWindow::OnContrarotate(int value)
{
    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));
}

roate:

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<double>(0, 2) += (rotated_width - src.cols) / 2;
      rotate_matrix.at<double>(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;
}

  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.   

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值