实现图像以任意角度旋转

//输入:原图像,角度
//输出:旋转后图像
void ImgRotate(cv::Mat imgIn, float theta, cv::Mat & imgOut)
    {
        int oldWidth = imgIn.cols;
        int oldHeight = imgIn.rows;
        //角度变换
        theta = (theta * 3.14159265) / (180 * 0.1);
        // 源图四个角的坐标(以图像中心为坐标系原点)
        float fSrcX1, fSrcY1, fSrcX2, fSrcY2, fSrcX3, fSrcY3, fSrcX4, fSrcY4;
        fSrcX1 = (float)(-(oldWidth - 1) / 2);
        fSrcY1 = (float)((oldHeight - 1) / 2);
        fSrcX2 = (float)((oldWidth - 1) / 2);
        fSrcY2 = (float)((oldHeight - 1) / 2);
        fSrcX3 = (float)(-(oldWidth - 1) / 2);
        fSrcY3 = (float)(-(oldHeight - 1) / 2);
        fSrcX4 = (float)((oldWidth - 1) / 2);
        fSrcY4 = (float)(-(oldHeight - 1) / 2);

        // 旋转后四个角的坐标(以图像中心为坐标系原点)
        float fDstX1, fDstY1, fDstX2, fDstY2, fDstX3, fDstY3, fDstX4, fDstY4;
        fDstX1 = cos(theta) * fSrcX1 + sin(theta) * fSrcY1;
        fDstY1 = -sin(theta) * fSrcX1 + cos(theta) * fSrcY1;
        fDstX2 = cos(theta) * fSrcX2 + sin(theta) * fSrcY2;
        fDstY2 = -sin(theta) * fSrcX2 + cos(theta) * fSrcY2;
        fDstX3 = cos(theta) * fSrcX3 + sin(theta) * fSrcY3;
        fDstY3 = -sin(theta) * fSrcX3 + cos(theta) * fSrcY3;
        fDstX4 = cos(theta) * fSrcX4 + sin(theta) * fSrcY4;
        fDstY4 = -sin(theta) * fSrcX4 + cos(theta) * fSrcY4;

        int newWidth = (max(fabs(fDstX4 - fDstX1), fabs(fDstX3 - fDstX2)) + 0.5);
        int newHeight = (max(fabs(fDstY4 - fDstY1), fabs(fDstY3 - fDstY2)) + 0.5);

        imgOut.create(newHeight, newWidth, imgIn.type());
        //
        float dx = -0.5 * newWidth * cos(theta) - 0.5 * newHeight * sin(theta) + 0.5 * oldWidth;
        float dy = 0.5 * newWidth * sin(theta) - 0.5 * newHeight * cos(theta) + 0.5 * oldHeight;

        int x, y;
        for (int i = 0; i < newHeight; i++)
        {
            for (int j = 0; j < newWidth; j++)
            {
                //旋转像素值变换
                x = float(j) * cos(theta) + float(i) * sin(theta) + dx;
                y = float(-j) * sin(theta) + float(i) * cos(theta) + dy;
                //超过原图像的坐标值置0
                //双线插值法
                if ((x < 0) || (x >= oldWidth) || (y < 0) || (y >= oldHeight))
                {
                    if (imgIn.channels() == 3)
                    {
                        imgOut.at<cv::Vec3b>(i, j) = cv::Vec3b(0, 0, 0);
                    }
                    else if (imgIn.channels() == 1)
                    {
                        imgOut.at<uchar>(i, j) = 0;
                    }
                }
                else
                {
                    if (imgIn.channels() == 3)
                    {
                        imgOut.at<cv::Vec3b>(i, j) = imgIn.at<cv::Vec3b>(y, x);
                    }
                    else if (imgIn.channels() == 1)
                    {
                        imgOut.at<uchar>(i, j) = imgIn.at<uchar>(y, x);
                    }
                }
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值