表格线识别通用库文档
载入中...
搜索中...
未找到
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
15
#include "
common/base/disjoint_set.hpp
"
16
#include "
common/base/list.hpp
"
17
#include "
common/base/rect.hpp
"
18
19
namespace
cm
{
20
28
class
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
;
48
Rects
&
MergeRects
(
int
horizontal_min_distance
= 0,
int
vertical_min_distance
= 0);
49
Rects
&
FilterInvalidRects
(
int
min_width
= 1,
int
min_height
= 1);
50
};
51
66
inline
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
92
inline
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
110
inline
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
129
inline
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
157
inline
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
177
inline
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
193
inline
Rect
Rects::MergeRects
()
const
{
194
Rect
new_rect
;
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
225
inline
Rects
&
Rects::MergeRects
(
int
horizontal_min_distance
,
int
vertical_min_distance
) {
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
// 建立一个矩形列表并查集
236
DisjointSet<Rect>
disjoint_set
(rects);
237
238
// 合并所有相邻矩形框
239
disjoint_set
.UnionAll([&
horizontal_min_distance
, &
vertical_min_distance
](
const
Rect
&
rect1
,
const
Rect
&
rect2
) {
240
return
rect1
.IsIntersect(
rect2
,
INTERSECT_XY
,
Rect::Margin
(
vertical_min_distance
,
horizontal_min_distance
));
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
264
inline
Rects
&
Rects::FilterInvalidRects
(
int
min_width
,
int
min_height
) {
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
cm::List
列表类
Definition
list.hpp:36
cm::List< Rect >::Min
const_iterator Min() const
求列表中的最小值
Definition
list.hpp:673
cm::List< Rect >::Filter
List< Rect > Filter(const FUNC &func) const
过滤列表项
Definition
list.hpp:270
cm::List< Rect >::List
List()=default
列表类的默认构造函数
cm::List< Rect >::Max
const_iterator Max() const
求列表中的最大值
Definition
list.hpp:572
cm::Point
点类
Definition
point.hpp:52
cm::Point::y
T y
点的 y 坐标
Definition
point.hpp:57
cm::Point::x
T x
点的 x 坐标
Definition
point.hpp:55
cm::Rect
矩形类
Definition
rect.hpp:31
cm::Rects
矩形列表类
Definition
rects.hpp:28
cm::Rects::Rects
Rects()=default
矩形类的默认构造函数
cm::Rects::MergeRects
Rect MergeRects() const
合并矩形框
Definition
rects.hpp:193
cm::Rects::~Rects
~Rects()=default
矩形类的析构函数
cm::Rects::FilterInvalidRects
Rects & FilterInvalidRects(int min_width=1, int min_height=1)
根据所给条件过滤无效矩形
Definition
rects.hpp:264
cm::Rects::Rects
Rects(const Rects &rects)=default
矩形类的拷贝构造函数
cm::Rects::operator=
Rects & operator=(const std::vector< cv::Rect > &rects)
矩形类的赋值运算符
Definition
rects.hpp:129
disjoint_set.hpp
list.hpp
cm
Definition
disjoint_set.hpp:22
cm::INTERSECT_XY
@ INTERSECT_XY
xy坐标上相交
Definition
enum.h:62
rect.hpp
cm::Rect::Margin
矩形外边距结构体
Definition
rect.hpp:72
common
include
common
list
rects.hpp
制作者
1.10.0