表格线识别通用库文档
载入中...
搜索中...
未找到
rects.hpp
浏览该文件的文档.
1/*
2 * @Description: 矩形列表类,头文件以及实现
3 * @Version:
4 * @Autor: LL
5 * @date: 2023-12-03
6 * @LastEditors: LL
7 * @LastEditTime: 2024-05-20
8 */
9#ifndef COMMON_RECTS_HPP
10#define COMMON_RECTS_HPP
11
12#include <iostream>
13#include <opencv2/core.hpp>
14
16#include "common/base/list.hpp"
17#include "common/base/rect.hpp"
18
19namespace cm {
20
28class Rects : public List<Rect> {
29 public:
30 using List<Rect>::List; // 复制构造函数
31
33 Rects() = default;
35 Rects(const Rects& rects) = default;
36 Rects(const std::vector<cv::Rect>& rects);
37 Rects(const std::vector<cv::Vec4i>& rects);
38 Rects(const std::vector<std::vector<int>>& rects);
40 ~Rects() = default;
41
43 Rects& operator=(const std::vector<cv::Rect>& rects);
44 Rects& operator=(const std::vector<cv::Vec4i>& rects);
45 Rects& operator=(const std::vector<std::vector<int>>& rects);
46
47 Rect MergeRects() const;
50};
51
66inline Rects::Rects(const std::vector<cv::Rect>& rects) {
67 for (auto& rect : rects)
68 this->emplace_back(rect.x, rect.y, rect.width, rect.height);
69}
70
92inline Rects::Rects(const std::vector<cv::Vec4i>& rects) {
93 for (auto& rect : rects)
94 this->emplace_back(rect[0], rect[1], rect[2], rect[3]);
95}
96
110inline Rects::Rects(const std::vector<std::vector<int>>& rects) {
111 for (auto& rect : rects)
112 this->emplace_back(rect[0], rect[1], rect[2], rect[3]);
113}
114
129inline Rects& Rects::operator=(const std::vector<cv::Rect>& rects) {
130 this->clear();
131 for(auto& rect:rects)
132 this->emplace_back(rect.x, rect.y, rect.width, rect.height);
133 return *this;
134}
135
157inline Rects& Rects::operator=(const std::vector<cv::Vec4i>& rects) {
158 this->clear();
159 for (auto& rect : rects)
160 this->emplace_back(rect[0], rect[1], rect[2], rect[3]);
161 return *this;
162}
163
177inline Rects& Rects::operator=(const std::vector<std::vector<int>>& rects) {
178 this->clear();
179 for (auto& rect : rects)
180 this->emplace_back(rect[0], rect[1], rect[2], rect[3]);
181 return *this;
182}
183
193inline Rect Rects::MergeRects() const {
195
196 // 计算矩形最小x坐标
197 new_rect.x = this->Min([](const Rect& rect1, const Rect rect2) { return rect1.x < rect2.x; })->x;
198
199 // 计算矩形最小y坐标
200 new_rect.y = this->Min([](const Rect& rect1, const Rect rect2) { return rect1.y < rect2.y; })->y;
201
202 // 计算矩形最大x坐标
203 auto rect = *this->Max([](const Rect& rect1, const Rect rect2) { return rect1.x + rect1.width < rect2.x + rect2.width; });
204 new_rect.width = rect.x + rect.width - new_rect.x;
205
206 // 计算矩形最大y坐标
207 rect = *this->Max([](const Rect& rect1, const Rect rect2) { return rect1.y + rect1.height < rect2.y + rect2.height; });
208 new_rect.height = rect.y + rect.height - new_rect.y;
209
210 return new_rect;
211}
212
226 auto& rects = *this; // 矩形列表
227
228 // 获取矩形数量
229 int num_rects = this->size();
230
231 // 如果矩形数量小于等于1
232 if (num_rects <= 1)
233 return rects;
234
235 // 建立一个矩形列表并查集
237
238 // 合并所有相邻矩形框
241 });
242
243 // 清空原来矩形列表
244 this->clear();
245
246 // 遍历所有可合并矩形的集合
247 for (auto& rects_collection : disjoint_set.GetAllCollections())
248 this->emplace_back(static_cast<const Rects&>(rects_collection).MergeRects());
249
250 return rects;
251}
252
265 *this = std::move(this->Filter([=](const Rect& rect) {
266 return rect.width >= min_width && rect.height >= min_height;
267 })); // 更新矩形列表类
268
269 return *this;
270}
271
272} // namespace cm
273
274#endif
列表类
Definition list.hpp:36
const_iterator Min() const
求列表中的最小值
Definition list.hpp:673
List< Rect > Filter(const FUNC &func) const
过滤列表项
Definition list.hpp:270
List()=default
列表类的默认构造函数
const_iterator Max() const
求列表中的最大值
Definition list.hpp:572
点类
Definition point.hpp:52
T y
点的 y 坐标
Definition point.hpp:57
T x
点的 x 坐标
Definition point.hpp:55
矩形类
Definition rect.hpp:31
矩形列表类
Definition rects.hpp:28
Rects()=default
矩形类的默认构造函数
Rect MergeRects() const
合并矩形框
Definition rects.hpp:193
~Rects()=default
矩形类的析构函数
Rects & FilterInvalidRects(int min_width=1, int min_height=1)
根据所给条件过滤无效矩形
Definition rects.hpp:264
Rects(const Rects &rects)=default
矩形类的拷贝构造函数
Rects & operator=(const std::vector< cv::Rect > &rects)
矩形类的赋值运算符
Definition rects.hpp:129
@ INTERSECT_XY
xy坐标上相交
Definition enum.h:62
矩形外边距结构体
Definition rect.hpp:72