java opencv 写入中文_Opencv Java图像转换

本文档描述了作者在Android应用中使用Java OpenCV库进行图像转换时遇到的问题。作者试图实现一个图像透视转换功能,但结果不理想。代码包括从Bitmap到Mat的转换、灰度化、高斯模糊以及获取用户标记的四个角点来构建透视变换矩阵。目前的问题在于透视转换后的图像不符合预期,寻求改善转换效果的方法。
摘要由CSDN通过智能技术生成

我跟随this tutorial为我的Android应用程序创建一个图像转换器。我已经将代码转换为Java,但是结果并不是我所期望的那样。我想知道是否有任何方法可以修改代码,以便将图像转换为教程中的图像。在

目前我的代码:// This method is called when a button is pressed.

//Unlike the tutorial post, the 4 corner coordinates are manually marked by the user.

// Bitmap is the image taken by the user

private void thresholdPhoto(Bitmap bitmap) {

// I am using the OpenCV library so I need to convert it to a matrix

Mat matrixImage = new Mat();

Utils.bitmapToMat(bitmap, matrixImage);

// Convert to gray

Imgproc.cvtColor(matrixImage, matrixImage, Imgproc.COLOR_BGR2GRAY);

// Gaussian blur

Imgproc.GaussianBlur(matrixImage, matrixImage, new Size(5,5), 0);

// The conversion to gray and blurring is for my app later on but I don't think it should affect the warping process.

// Get the coordinates marked by the user

PointF bottomRight = new PointF(polygonView.pointer4.getX(), polygonView.pointer4.getY());

PointF bottomLeft = new PointF(polygonView.pointer3.getX(), polygonView.pointer3.getY());

PointF topRight = new PointF(polygonView.pointer2.getX(), polygonView.pointer2.getY());

PointF topLeft = new PointF(polygonView.pointer1.getX(), polygonView.pointer1.getY());

// Code following the tutorial post above

double widthA = Math.sqrt(Math.pow(bottomRight.x-bottomLeft.x, 2)+Math.pow(bottomRight.y-bottomLeft.y, 2));

double widthB = Math.sqrt(Math.pow(topRight.x-topLeft.x, 2)+Math.pow(topRight.y-topLeft.y, 2));

double maxWidth = Math.max(widthA, widthB);

double heightA = Math.sqrt(Math.pow(topRight.x-bottomRight.x, 2)+Math.pow(topRight.y-bottomRight.y, 2));

double heightB = Math.sqrt(Math.pow(topLeft.x-bottomLeft.x, 2)+Math.pow(topLeft.y-bottomLeft.y, 2));

double maxHeight = Math.max(heightA, heightB);

// To apply the final perspective transform I had to create new Matrices

Point tl = new Point(topLeft.x, topLeft.y);

Point tr = new Point(topRight.x, topLeft.y);

Point bl = new Point(bottomLeft.x, bottomLeft.y);

Point br = new Point(bottomRight.x, bottomRight.y);

MatOfPoint2f src = new MatOfPoint2f(tl, tr, br, bl);

MatOfPoint2f dst = new MatOfPoint2f(

new Point(0,0), // top left

new Point(maxWidth-1, 0), // top right

new Point(maxWidth-1, maxHeight-1), // bottom right

new Point(0, maxHeight-1) // bottom left

);

Mat perspectiveTransform = Imgproc.getPerspectiveTransform(src, dst);

Imgproc.warpPerspective(matrixImage, matrixImage, perspectiveTransform, new Size(maxWidth, maxHeight));

// Convert matrix back to bitmap to display inside imageview. This final part is just for my visualization purposes.

Bitmap bm = Bitmap.createBitmap(matrixImage.cols(), matrixImage.rows(),Bitmap.Config.ARGB_8888);

Utils.matToBitmap(matrixImage, bm);

polygonView.setVisibility(View.GONE);

scannedImage.setImageBitmap(bm);

}

使用的图像:

{1美元^

翘曲的结果不是我想要的,所以我想知道是否有其他参数/设置可以修改以使其更好。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值