今天简单的实现一个通过按键控制图像旋转的功能。
- 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.