/*
功能:对一张图片进行旋转
参数:
image:输入图像
imgOut:旋转后的图像
angle:旋转角度
*/
void rotateImage(cv::Mat& image, cv::Mat& imgOut, int angle)
{
/*
对旋转的进行改进,由于图形是一个矩形,旋转后的新图像的形状是一个原图像的外接矩形
因此需要重新计算出旋转后的图形的宽和高
*/
int width = image.cols;
int height = image.rows;
double radian = angle * CV_PI / 180.;//角度转换为弧度
double width_rotate = fabs(width*cos(radian)) + fabs(height*sin(radian));
double height_rotate = fabs(width*sin(radian)) + fabs(height*cos(radian));
//旋转中心 原图像中心点
cv::Point2f center((float)width / 2.0, (float)height / 2.0);
//旋转矩阵
cv::Mat m1 = cv::getRotationMatrix2D(center, angle, 1.0);
//m1为2行3列通道数为1的矩阵
//变换矩阵的中心点相当于平移一样 原图像的中心点与新图像的中心点的相对位置
m1.at<double>(0, 2) += (width_rotate - width) / 2.;
m1.at<double>(1, 2) += (height_rotate - height) / 2.;
if (image.channels() == 1)
{
cv::warpAffine(image, imgOut, m1, cv::Size(width_rotate, height_rotate), cv::INTER_LINEAR, 0, cv::Scalar(0));
}
else if (image.channels() == 3)
{
cv::warpAffine(image, imgOut, m1, cv::Size(width_rotate, height_rotate), cv::INTER_LINEAR, 0, cv::Scalar(0,0,0));
}
}
opencv C++图像旋转无缺失
最新推荐文章于 2024-05-06 13:24:45 发布