获得矩形向量的边界矩形的最有效方法(Most efficient way to get bounding rectangle of vector of rectangles)
我有一个
cv::Rects
(来自opencv框架)的std::vector
,叫做rects
,但是我想同样的逻辑将应用于任何矩形对象,所以我不打算在这个问题中标记opencv。我试图找到包含矢量中所有矩形的最小矩形,这是我迄今为止的:
int t = min_element(rects.begin(), rects.end(), [](cv::Rect a, cv::Rect b){ a.tl().y < b.tl().y;})->tl().y; int l = min_element(rects.begin(), rects.end(), [](cv::Rect a, cv::Rect b){ a.tl().x < b.tl().x;})->tl().x; int b = max_element(rects.begin(), rects.end(), [](cv::Rect a, cv::Rect b){ a.br().y < b.br().y;})->br().y; int r = max_element(rects.begin(), rects.end(), [](cv::Rect a, cv::Rect b){ a.br().x < b.br().x;})->br().x; cv::Rect smallest_encompassing_rect = cv::Rect(cv::Point(t, l),cv::Point(b, r)))
如果不清楚,则
.tl()
函数将获取矩形的左上角点,而.br()
会在右下角。我知道这会起作用,但我想知道是否有更有效的方法来解决问题。 我正在处理图像处理,所以我的代码是时间敏感的。
任何帮助将不胜感激,谢谢
I have an
std::vector
ofcv::Rects
(from the opencv framework) calledrects
, but I suppose the same logic would be applied to any rectangle object, so I'm not going to tag opencv in this question.I am trying to find the smallest rectangle that encompasses all the rectangles in the vector, this is what I have so far:
int t = min_element(rects.begin(), rects.end(), [](cv::Rect a, cv::Rect b){ a.tl().y < b.tl().y;})->tl().y; int l = min_element(rects.begin(), rects.end(), [](cv::Rect a, cv::Rect b){ a.tl().x < b.tl().x;})->tl().x; int b = max_element(rects.begin(), rects.end(), [](cv::Rect a, cv::Rect b){ a.br().y < b.br().y;})->br().y; int r = max_element(rects.begin(), rects.end(), [](cv::Rect a, cv::Rect b){ a.br().x < b.br().x;})->br().x; cv::Rect smallest_encompassing_rect = cv::Rect(cv::Point(t, l),cv::Point(b, r)))
If it's not clear, the
.tl()
function gets the top left point of the rectangle, and.br()
gets the bottom right.I know this will work, but I am wondering if there is a more efficient way to solve the problem. I am working with image processing, so my code is time sensitive.
Any help would be appreciated, Thanks
原文:https://stackoverflow.com/questions/42685732
相关问答
更多-
如何逆转走树结构(How to reverse walk a tree structure)[2021-10-03]
通常,您可以使用此算法。 我清楚地使用了其他数据。 特定于应用程序的代码将取代console.log语句。 为了增强稳定性,我添加了一个测试是否存在子属性,并更改了数据以对其进行测试。 var data = { name: 'Parent', children: [{ name: 'Child 1', children: [{ name: 'Child 1a', ... -
这不是核心数据问题,而是图形问题。 你必须做同样的搜索。 但是,CoreData可以让您在对象中出错。 所以,你有几个选择,其中两个是 1)走树。 您应该能够使用与内存树一样的完全相同的算法。 CoreData应该只是对象的错误。 除非你有一棵非常深的树,你应该没事。 2)对每条记录中的父关系进行编码。 树更新时进行了大量更新,但提取速度会更快。 以下内容可以转换为NSCompoundPredicate并指定为fetch谓词... Docs说它适用于CoreData,但不适用于sqllite - 试试吧。 ...
-
您可以使用org.json库来执行此操作。 首先需要将json文件读入JSONObject ,它基本上由键/值对和数组组成。 然后,您可以遍历对象中的项目,查找pid并添加新子项。 以下是一些示例代码,可帮助您入门: //read file BufferedReader in = new BufferedReader(new FileReader("path/json.txt")); String line; StringBuilder sb = new StringBuilder(); w ...
-
Nestable List Plugin PHP MYSQlL Json Tree结构(Nestable List Plugin PHP MYSQlL Json Tree structure)[2023-03-04]
好的,我得到了感谢 - > Pierre de LESPINAY https://stackoverflow.com/a/22020668/1912618 if($productSQL[0]) { foreach($productSQL[1]['obj_ID'] as $iIDKey => $iIDValues) { if($productSQL[1]['objProductIDArr'][$iIDKey] == 0) { // ... -
我会克隆树(实际上不是一棵树)并删除parent引用: function cloneWithoutParent(n){ if (typeof n !== "object") return n; var c = {}; for (var k in n) { if (k!=="parent") c[k] = cloneWithoutParent(n[k]); } return c; } 之后,您可以对树进行字符串化。 当然,当您需要原始结构时,可以执行相反的操作来恢复par ...
-
尝试提取并获取Json对象信息的嵌套树结构对象(Nested Tree structure object trying to extract and get information Json object)[2022-02-07]
这是代码。 你的结果是输出变量: var input = [{ "Type": "grdparent1", "name": "grdparent1", "children": [{ "Type": "grdparent1", "Id": 45, "children": [] }, { "Type": "grdparent1", ... -
在这里,您可以通过所有航班进行实施。 我将$devices名称更改$devices $availableFlights以使示例更容易理解。 $data = json_decode($result, true); $availableFlights = $data['AvailabilityOutput']['AvailableFlights']; echo 'Flifht checking'; echo '
'; foreach ($availableFlights['OngoingFli ... -
value不是您在each()中寻找的值。 您需要使用value.Value和value.Text 。 var data = [{"Value":"???","Text":"??? - Unknown"}, {"Value":"AAA","Text":"AAA - A Company"}, {"Value":"BBB","Text":"BBB - B Company"}, {"Value":"CCC","Text":"CCC - C Company"}, {"Value":"DDD","Text": ...
-
使用$ .getJSON(url,data,callback); 它从给定的URL获取数据并检查它是否有效。 $.getJSON( 'http://www.example.com/getjson.php?catid=' + $('#valueContainer').val(), function (data) { // do stuff here }); Use $.getJSON(url, data, callback); It get data from ...
-
操纵JSON树结构(Manipulate JSON tree structure)[2021-12-15]
这个答案基本上用{key: {subfields: value}}替换任何{key: value} ,递归地通过相同的算法发回value直到所有属性级别都被处理完毕。 var data = { "u1fsQExd1aZmnpL" : { "2fhjsnNchSJowl2" : {}, "2FNdVdkaefaD6xQ" : { "fZ0zn6d51TgVqID" : { "05E1JSFYVHJlGVP" : {}, "03jshviJSONDJla" : {} } } } }; // this is ...