首页 \ 问答 \ 获得矩形向量的边界矩形的最有效方法(Most efficient way to get bounding rectangle of vector of rectangles)

获得矩形向量的边界矩形的最有效方法(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 of cv::Rects (from the opencv framework) called rects, 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
更新时间:2023-11-18 17:11

相关问答

更多
  • 通常,您可以使用此算法。 我清楚地使用了其他数据。 特定于应用程序的代码将取代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 ...
  • 好的,我得到了感谢 - > 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 ...
  • 这是代码。 你的结果是输出变量: 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 ...
  • 这个答案基本上用{key: {subfields: value}}替换任何{key: value} ,递归地通过相同的算法发回value直到所有属性级别都被处理完毕。 var data = { "u1fsQExd1aZmnpL" : { "2fhjsnNchSJowl2" : {}, "2FNdVdkaefaD6xQ" : { "fZ0zn6d51TgVqID" : { "05E1JSFYVHJlGVP" : {}, "03jshviJSONDJla" : {} } } } }; // this is ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)