图像旋转及坐标点的对应

void ImgRotate(const Mat &srcImg, Mat &rotatedImg, double degree)
{
    //degree为逆时针旋转的角度
    int h = srcImg.rows;
    int w = srcImg.cols;
    //求对角线的长度,做一个以对角线为边长的正方形图像
    int diaLength = int(sqrt((h*h + w*w)));
    Mat tempImg = Mat::zeros(diaLength, diaLength, srcImg.type());
    int tx = diaLength / 2 - w / 2;//原图左上角在新图上的x坐标
    int ty = diaLength / 2 - h / 2;//原图左上角在新图上的y坐标
    srcImg.copyTo(tempImg(Range(ty, ty + h), Range(tx, tx + w)));//把原图先复制到新的临时图上。
    
    //以新的临时图的中心点为旋转点
    Point rotatepoint;
    rotatepoint.x = rotatepoint.y = diaLength / 2;
    Mat rotaMat = getRotationMatrix2D(rotatepoint, degree, 1);//获取二维旋转的仿射变换矩阵
    warpAffine(tempImg, rotatedImg, rotaMat, Size(diaLength, diaLength));//进行仿射变换。
    
    return;
}
// 获取指定像素点放射变换后的新的坐标位置
CvPoint getPointAffinedPos(CvPoint src, int h,int w, double degree)
{
    int diaLength = int(sqrt((h*h + w*w)));
    Point center;
    center.x = center.y = diaLength / 2;
    src.x+=diaLength / 2 - w / 2;
    src.y+=diaLength / 2 - h / 2;
    double angle = degree * CV_PI / 180.0;
    
    CvPoint dst;
    int x = src.x - center.x;
    int y = src.y - center.y;
    dst.x = cvRound(x * cos(angle) + y * sin(angle) + center.x);
    dst.y = cvRound(-x * sin(angle) + y * cos(angle) + center.y);
    return dst;
}

有关旋转后坐标点映射关系的计算原理可查看:https://blog.csdn.net/x2017x/article/details/84380966

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值