OpenCV Warp透视和欧几里德距离的问题

问题描述 投票:1回答:1

我的程序使用OpenCV的C#端口来查找场景中最大的矩形并扭曲透视图。它几乎可以工作,但有一个非常烦人的错误,我无法弄清楚。

在正方形上使用它可以很好地工作,但它只能在一个方向的矩形上正常工作。

例如,如果我给它一张便条卡的图像,我得到:

但如果我将相同的记录卡旋转90度,我会得到:

我知道找到矩形的代码是正确的,所以它必须在warp透视过程中看起来像这样:

public Mat GetPerspective (List<Point> corners, Mat sub)
{
    //Pretty sure these four lines are the problem
    double top = Math.Sqrt(Math.Pow(corners[0].x - corners[1].x, 2) + Math.Pow(corners[0].y - corners[1].y, 2));
    double right = Math.Sqrt(Math.Pow(corners[1].x - corners[2].x, 2) + Math.Pow(corners[1].y - corners[2].y, 2));
    double bottom = Math.Sqrt(Math.Pow(corners[2].x - corners[3].x, 2) + Math.Pow(corners[2].y - corners[3].y, 2));
    double left = Math.Sqrt(Math.Pow(corners[3].x - corners[1].x, 2) + Math.Pow(corners[3].y - corners[1].y, 2));

    Mat quad = Mat.zeros(new Size(Math.Max(top, bottom), Math.Max(left, right)), CvType.CV_8UC3);
    List<Point> result_points = new List<Point>();

    result_points.Add(new Point(0,0));
    result_points.Add(new Point(quad.cols(), 0));
    result_points.Add(new Point(quad.cols(), quad.rows()));
    result_points.Add(new Point(0, quad.rows()));

    Mat cornerPts = Converters.vector_Point2f_to_Mat(corners);
    Mat resultPts = Converters.vector_Point2f_to_Mat(result_points);

    Mat transformation = Imgproc.getPerspectiveTransform(cornerPts, resultPts);
    Imgproc.warpPerspective(sub, quad, transformation, quad.size());
    return quad;
}
java c# android opencv euclidean-distance
1个回答
0
投票

试试这个

long[] tl = corners[0];

long[] tr = corners[1];

long[] br = corners[2];

long[] bl = corners[3];

float widthA = (float) Math.sqrt((Math.pow((br[0] - bl[0]), 2)) + Math.pow((br[1] - bl[1]), 2));

float widthB = (float) Math.sqrt((Math.pow((tr[0] - tl[0]), 2)) + Math.pow((tr[1] - tl[1]), 2));

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

float heightA = (float) Math.sqrt((Math.pow((tr[0] - br[0]), 2)) + Math.pow((tr[1] - br[1]), 2));

float heightB = (float) Math.sqrt((Math.pow((tl[0] - bl[0]), 2)) + Math.pow((tl[1] - bl[1]), 2));

int maxHeight = (int) Math.max(heightA, heightB);
© www.soinside.com 2019 - 2024. All rights reserved.