Opencv学习记录【六】——图像透视变换

  1. 接上一篇,现在需要绕两中轴旋转,并且给定角度旋转。
  2. 思路:运用几何关系通过角度算出四点变换;再运用opencv四点得出3*3矩阵;最后运用透视变换函数进行变换。

下面视图为手稿:
在这里插入图片描述
代码如下:


cv::Mat angleTOmatbyMID(cv::Mat src,int angle1,int angle2)
{
    float anx,any;
    cout << "anx="<<angle1<<"         any="<<angle2<<endl;
    anx = 3.14*angle1/180;   //x边动,y中轴不变
    any = 3.14*angle2/180;

    vector<cv::Point2f> corners(4),corners_trans(4);
    corners[0] = cv::Point2f(0,0);
    corners[1] = cv::Point2f(src.cols-1,0);
    corners[2] = cv::Point2f(src.cols-1,src.rows-1);
    corners[3] = cv::Point2f(0,src.rows-1);

    int dis_x,dis_y,dis_xx1,dis_yy1,dis_xx2,dis_yy2;
    dis_x = 0.5*src.rows*cos(any)*sin(any);
    dis_y = 0.5*src.rows*cos(any)*cos(any);
    dis_xx1 = 0.5*(src.cols + 2*dis_x)*cos(anx)*cos(anx);
    dis_yy1 = 0.5*(src.cols + 2*dis_x)*cos(anx)*sin(anx);
    dis_xx2 = 0.5*(src.cols - 2*dis_x)*cos(anx)*cos(anx);
    dis_yy2 = 0.5*(src.cols - 2*dis_x)*cos(anx)*sin(anx);

    corners_trans[0].x = 0.5*src.cols - dis_xx2;
    corners_trans[0].y = (0.5*src.rows - dis_y) - dis_yy2;

    corners_trans[1].x =0.5*src.cols + dis_xx2;
    corners_trans[1].y = (0.5*src.rows - dis_y) + dis_yy2;

    corners_trans[2].x = 0.5*src.cols + dis_xx1;;
    corners_trans[2].y = src.rows - (dis_yy1 +  (0.5*src.rows - dis_y));

    corners_trans[3].x = 0.5*src.cols - dis_xx1;
    corners_trans[3].y = src.rows + (dis_yy1 -  (0.5*src.rows - dis_y));

    //求外切矩形 左x比小、右x比大;上y比小,下y比大。

    cv::Point2f change_point = cv::Point2f(0,0);

    if(corners_trans[0].y == corners_trans[1].y)   // anx = 0   上下平行
    {
        out_height = corners_trans[3].y - corners_trans[0].y;
        if(corners_trans[0].x > corners_trans[3].x){
            cout << "下大"<<endl;
            out_width = corners_trans[2].x - corners_trans[3].x;
            change_point.x = 0 - corners_trans[3].x;
            change_point.y = out_height  - corners_trans[3].y;
        }

        else{
            cout << "上大"<<endl;
            out_width = corners_trans[1].x - corners_trans[0].x;
            change_point.x = 0 - corners_trans[0].x;
            change_point.y = 0 - corners_trans[0].y;
        }
        corners_trans[0] += change_point;
        corners_trans[1] += change_point;
        corners_trans[2] +=change_point;
        corners_trans[3] += change_point;

    }
    else if(corners_trans[0].x 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值