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
}

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

Opencv for Android 之透视变换_第1张图片 原图片 Opencv for Android 之透视变换_第2张图片 变换后的图片

你可能感兴趣的:(opencv,for,Android)