图像旋转及坐标点的对应

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;
}


猜你喜欢

转载自blog.csdn.net/x2017x/article/details/80092966