Opencv for Android(安卓)之透视变换
16lz
2021-01-24
/** * 透视变换 * @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}
效果图如下 这里只是对原图片进行了黑白处理 只是为了处理更快而已
原图片 变换后的图片更多相关文章
- Android剪裁图片简单的方法
- 使用bitmap缩略图,解决图片大小超过预算的VM的问题
- Android(安卓)特殊用法--来自中国移动开发社区
- Android(安卓)之 Bitmap 和 File 相互转换
- android camera Intent调用
- android中实现从一个图片中截取一部分,在翻转,成一个圆形显示
- Android(安卓)学习笔记-2011年7月
- android-------开发常用框架汇总
- Android(安卓)SwitchButton(滑动开关)