AndroidStudio opencv(二)perspective transform

本文详细介绍了如何在Android Studio项目中使用OpenCV库进行透视变换操作,通过PolygonView展示了具体实现过程,揭示了在Android开发中集成计算机视觉技术的方法。
摘要由CSDN通过智能技术生成

这里写图片描述

 public void transform(View view) {
        //最开始的图片        
        Mat sampledImage = ImageUtils.bitmapToMat(mBitmap);
        //变换后图片
        Mat correctedImage = new Mat(sampledImage.rows(), sampledImage.cols(), sampledImage.type());

        Map<Integer, PointF> points = mPolygonView.getPoints();
        //屏幕选中点的坐标转换为图片坐标
        List<Point> srccorners = getSrcPoint(mBitmap, points);

        List<Point> descorners = new ArrayList<>();
        descorners.add(new Point(0, 0));
        descorners.add(new Point(correctedImage.cols(), 0));
        descorners.add(new Point(0, correctedImage.rows()));
        descorners.add(new Point(correctedImage.cols(), correctedImage.rows()));

        Mat srcPoints = Converters.vector_Point2f_to_Mat(srccorners);
        Mat desPoints = Converters.vector_Point2f_to_Mat(descorners);
        //求出变换矩阵
        Mat transformation = Imgproc.getPerspectiveTransform(srcPoints, desPoints);
        //进行仿射变换
        Imgproc.warpPerspective(sampledImage, correctedImage, transformation, correctedImage.size());
        displayImage(correctedImage);
    }
//ImageUtil的bitmapToMat()方法
  public static Mat bitmapToMat(Bitmap bitmap) {
        Mat mat = new Mat(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8U, new Scalar(4));
        Bitmap bitmap32 = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        Utils.bitmapToMat(bitmap32, mat);
        return mat;
    }
    //ImageUtil的matToBitmap()方法
    public static Bitmap matToBitmap(Mat mat) {
        Bitmap bitmap = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(mat, bitmap);
        return bitmap;
    }
 //屏幕坐标与图片坐标转换
    private List<Point> getSrcPoint(Bitmap original, Map<Integer, PointF> points) {
        List<Point> src
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值