在openCV Java中旋转检测到的矩形

时间:2018-09-07 07:47:04

标签: android image opencv

我已经搜索互联网已有一段时间了。问题在于,我的问题的解决方案主要在python或C ++中提供。我试图复制代码,但没有运气。

我检测到一张卡片(矩形),如果矩形是笔直的,我可以对其进行裁剪,但是如果将矩形旋转一定角度,我会得到一张剪裁卡片的图像。

图片显示了我想要实现的目标...

What I want to achieve

我的直接图像处理代码。

Bitmap abc = null;
Point topleft, topright, bottomleft, bottomright;

float xRatio = (float) original.getWidth() / sourceImageView.getWidth();
float yRatio = (float) original.getHeight() / sourceImageView.getHeight();

float x1 = (points.get(0).x) * xRatio;
float x2 = (points.get(1).x) * xRatio;
float x3 = (points.get(2).x) * xRatio;
float x4 = (points.get(3).x) * xRatio;
float y1 = (points.get(0).y) * yRatio;
float y2 = (points.get(1).y) * yRatio;
float y3 = (points.get(2).y) * yRatio;
float y4 = (points.get(3).y) * yRatio;

Point p1 = new Point(x1, y1);
Point p2 = new Point(x2, y2);
Point p3 = new Point(x3, y3);
Point p4 = new Point(x4, y4);
List<Point> newpoints = new ArrayList<Point>();
newpoints.add(p1);
newpoints.add(p2);
newpoints.add(p3);
newpoints.add(p4);
Collections.sort(newpoints, new Comparator<Point>() {

    public int compare(Point o1, Point o2) {
        return Double.compare(o1.x, o2.x);
    }
});
if (newpoints.get(0).y > newpoints.get(1).y) {

    bottomleft = newpoints.get(0);
    topleft = newpoints.get(1);
} else {
    bottomleft = newpoints.get(1);
    topleft = newpoints.get(0);
}
if (newpoints.get(2).y > newpoints.get(3).y) {
    bottomright = newpoints.get(2);
    topright = newpoints.get(3);
} else {
    bottomright = newpoints.get(3);
    topright = newpoints.get(2);
}
final Mat newimage = new Mat();
Bitmap bmp32 = original.copy(Bitmap.Config.ARGB_8888, true);
org.opencv.android.Utils.bitmapToMat(bmp32, newimage);
final float dd = getAngle(bottomleft, bottomright);

Mat finalMat = new Mat(newimage, new org.opencv.core.Rect(topleft, bottomright));
abc = RotateBitmap(createBitmapfromMat(finalMat), (-dd));

矩形为直线时的当前代码:

enter image description here enter image description here

旋转矩形时的当前代码:

enter image description here enter image description here

链接到类似问题: Link 1 Link 2

0 个答案:

没有答案