在OpenCV中找到两个梯形的相交区域 [英] Find intersecting area of two trapezoids in OpenCV

查看:353
本文介绍了在OpenCV中找到两个梯形的相交区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个函数,用于评估两个非等腰梯形之间有多少像素重叠.在我的应用程序中,梯形始终定义为:

I'm writing a function that evaluates how many pixels overlap between two non-isosceles trapezoids. In my application, a trapezoid is always defined as :

typedef std::array<cv::Point, 4> Trapezoid;
//[0] - bottom left
//[1] - bottom right
//[2] - top right
//[3] - top left

此外,这些梯形将具有索引[0]和[1]始终具有与[2]和[3](始终平行于Y轴)相同的Y的特性.

Additionally, these trapezoids will have the properties that indexes [0] and [1] will always have the same Y as will [2] and [3] (Always parallel to Y axis).

所以,假设我有两个梯形:

So, let's say I have two trapezoids:

//300 high, 100 top, 200 bottom
Trapezoid trapezoid1{ cv::Point(100,400), cv::Point(300,400), cv::Point(250,100), cv::Point(150,100) };
//250 high, 50 top, 250 bottom
Trapezoid trapezoid2{ cv::Point(75,400), cv::Point(325,400), cv::Point(225,150), cv::Point(175,150) };

我可以遍历梯形之一的每一行,然后进行数学运算以找出该行中有多少像素重叠:

I could iterate over each row of one of the trapezoids and do the math to figure out how many pixels in that row overlap:

uint32_t TrapezoidOverlap( const Trapezoid& trapezoid1,
                           const Trapezoid& trapezoid2 )
{
    //Count number of overlapping pixels
    uint32_t overlappedpixels {0};

    for (int i = trapezoid1[3].y; i < trapezoid1[0].y; i++) {
        overlappedpixels += //Math for overlapping pixels in X;
    }

    return overlappedpixels;
}

第一行和最后一行将很简单,但是中间的所有行都需要使用三角函数来查找两个梯形的起点和终点.这看起来在计算上将是昂贵的,而性能在此应用程序中至关重要.我在中看到了这个问题,即Rect结构具有交集运算符,但是我不确定是否有任何函数可以在这种情况下提供帮助.

First and last row will be simple, but all rows in between will require trigonometry to find the start and end points of both trapezoids. This looks like it will be computationally expensive and performance is critical in this application. I have seen in this question that the Rect structures have a intersection operator, however I am not sure if there's any function that would help in this situation.

在这种情况下获得最佳性能的解决方案是什么?

What would be the solution for best performance in this situation?

推荐答案

使用CV_FILLED fillPoly 在两个矩阵中绘制梯形或多边形,并对其进行 AND 在逻辑上.相交区域为:

Draw the trapezoids or polygons using CV_FILLED or fillPoly in two matrix and AND them logically. Area of intersection is:

int area_int = countNonZero(bitwise_and(TrapeZoidMatA,TrapeZoidMatB));

我认为在这种情况下这将非常有效.

I think this would be much efficient in this case.

这篇关于在OpenCV中找到两个梯形的相交区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆