Opencv for Android 之透视变换

 /**
     * 透视变换
     * @param in 原图像
     * @param point 定位到的四个点
     * @return
     */
    private  Mat getWarpPersPective(Mat in,Point [] point){
        MatOfPoint2f reusltPoint2f=null,srcPoint2f=null;
        Mat out=new Mat();
        Point []targetPoints=new Point[4];
        for (int i=0;i<4;i++){
            targetPoints[i]=new Point();
        }
  //这里拿到倾斜的长度作为宽高 结果可能比真正矫正的图片略小点 但是矫正效果还是很不错的
    double rect_width = Math.sqrt(Math.abs(point[0].x - point[1].x)*Math.abs(point[0].x - point[1].x) +
            Math.abs(point[0].y - point[1].y)*Math.abs(point[0].y - point[1].y));
    double rect_height =  Math.sqrt(Math.abs(point[0].x - point[2].x)*Math.abs(point[0].x - point[2].x) +
            Math.abs(point[0].y - point[2].y)*Math.abs(point[0].y - point[2].y));

    double moveValueX = 0.0;
    double moveValueY = 0.0;

    targetPoints[0].x = 0.0 + moveValueX; targetPoints[0].y = 0 + moveValueY;// top_left
    targetPoints[2].x = 0.0 + moveValueX; targetPoints[2].y = rect_height + moveValueY;// bottom_Left
    targetPoints[1].x = rect_width + moveValueX; targetPoints[1].y = 0.0 + moveValueY;// top_Right
    targetPoints[3].x = rect_width + moveValueX; targetPoints[3].y = rect_height + moveValueY;// bottom_Right
    reusltPoint2f=new MatOfPoint2f(targetPoints);//这里需要将四个点转换成Mat
    srcPoint2f=new MatOfPoint2f(point);

    Mat tranform=getPerspectiveTransform(reusltPoint2f,srcPoint2f); // 透视变换
    warpPerspective(in,out,tranform,new Size(rect_width,rect_height),INTER_LINEAR | WARP_INVERSE_MAP);
    showImage(out); 
    return out;//变换后的Mat
}

 效果图如下 这里只是对原图片进行了黑白处理 只是为了处理更快而已 

原图片
变换后的图片
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值