Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Greenplum
Opencv
提交
9b7f40ce
O
Opencv
项目概览
Greenplum
/
Opencv
11 个月 前同步成功
通知
7
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
Opencv
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9b7f40ce
编写于
12月 10, 2021
作者:
A
Alexander Alekhin
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #21074 from vrabaud:3.4_rect
上级
62a010a2
6b4ea68b
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
59 addition
and
7 deletion
+59
-7
modules/core/include/opencv2/core/types.hpp
modules/core/include/opencv2/core/types.hpp
+27
-7
modules/core/test/test_misc.cpp
modules/core/test/test_misc.cpp
+32
-0
未找到文件。
modules/core/include/opencv2/core/types.hpp
浏览文件 @
9b7f40ce
...
@@ -1895,13 +1895,33 @@ Rect_<_Tp>& operator -= ( Rect_<_Tp>& a, const Size_<_Tp>& b )
...
@@ -1895,13 +1895,33 @@ Rect_<_Tp>& operator -= ( Rect_<_Tp>& a, const Size_<_Tp>& b )
template
<
typename
_Tp
>
static
inline
template
<
typename
_Tp
>
static
inline
Rect_
<
_Tp
>&
operator
&=
(
Rect_
<
_Tp
>&
a
,
const
Rect_
<
_Tp
>&
b
)
Rect_
<
_Tp
>&
operator
&=
(
Rect_
<
_Tp
>&
a
,
const
Rect_
<
_Tp
>&
b
)
{
{
_Tp
x1
=
std
::
max
(
a
.
x
,
b
.
x
);
if
(
a
.
empty
()
||
b
.
empty
())
{
_Tp
y1
=
std
::
max
(
a
.
y
,
b
.
y
);
a
=
Rect
();
a
.
width
=
std
::
min
(
a
.
x
+
a
.
width
,
b
.
x
+
b
.
width
)
-
x1
;
return
a
;
a
.
height
=
std
::
min
(
a
.
y
+
a
.
height
,
b
.
y
+
b
.
height
)
-
y1
;
}
a
.
x
=
x1
;
const
Rect_
<
_Tp
>&
Rx_min
=
(
a
.
x
<
b
.
x
)
?
a
:
b
;
a
.
y
=
y1
;
const
Rect_
<
_Tp
>&
Rx_max
=
(
a
.
x
<
b
.
x
)
?
b
:
a
;
if
(
a
.
width
<=
0
||
a
.
height
<=
0
)
const
Rect_
<
_Tp
>&
Ry_min
=
(
a
.
y
<
b
.
y
)
?
a
:
b
;
const
Rect_
<
_Tp
>&
Ry_max
=
(
a
.
y
<
b
.
y
)
?
b
:
a
;
// Looking at the formula below, we will compute Rx_min.width - (Rx_max.x - Rx_min.x)
// but we want to avoid overflows. Rx_min.width >= 0 and (Rx_max.x - Rx_min.x) >= 0
// by definition so the difference does not overflow. The only thing that can overflow
// is (Rx_max.x - Rx_min.x). And it can only overflow if Rx_min.x < 0.
// Let us first deal with the following case.
if
((
Rx_min
.
x
<
0
&&
Rx_min
.
x
+
Rx_min
.
width
<
Rx_max
.
x
)
||
(
Ry_min
.
y
<
0
&&
Ry_min
.
y
+
Ry_min
.
height
<
Ry_max
.
y
))
{
a
=
Rect
();
return
a
;
}
// We now know that either Rx_min.x >= 0, or
// Rx_min.x < 0 && Rx_min.x + Rx_min.width >= Rx_max.x and therefore
// Rx_min.width >= (Rx_max.x - Rx_min.x) which means (Rx_max.x - Rx_min.x)
// is inferior to a valid int and therefore does not overflow.
a
.
width
=
std
::
min
(
Rx_min
.
width
-
(
Rx_max
.
x
-
Rx_min
.
x
),
Rx_max
.
width
);
a
.
height
=
std
::
min
(
Ry_min
.
height
-
(
Ry_max
.
y
-
Ry_min
.
y
),
Ry_max
.
height
);
a
.
x
=
Rx_max
.
x
;
a
.
y
=
Ry_max
.
y
;
if
(
a
.
empty
())
a
=
Rect
();
a
=
Rect
();
return
a
;
return
a
;
}
}
...
...
modules/core/test/test_misc.cpp
浏览文件 @
9b7f40ce
...
@@ -784,4 +784,36 @@ TEST(Core_Check, testSize_1)
...
@@ -784,4 +784,36 @@ TEST(Core_Check, testSize_1)
}
}
template
<
typename
T
>
class
Rect_Test
:
public
testing
::
Test
{};
TYPED_TEST_CASE_P
(
Rect_Test
);
// Reimplement C++11 std::numeric_limits<>::lowest.
template
<
typename
T
>
T
cv_numeric_limits_lowest
();
template
<
>
int
cv_numeric_limits_lowest
<
int
>
()
{
return
INT_MIN
;
}
template
<
>
float
cv_numeric_limits_lowest
<
float
>
()
{
return
-
FLT_MAX
;
}
template
<
>
double
cv_numeric_limits_lowest
<
double
>
()
{
return
-
DBL_MAX
;
}
TYPED_TEST_P
(
Rect_Test
,
Overflows
)
{
typedef
Rect_
<
TypeParam
>
R
;
TypeParam
num_max
=
std
::
numeric_limits
<
TypeParam
>::
max
();
TypeParam
num_lowest
=
cv_numeric_limits_lowest
<
TypeParam
>
();
EXPECT_EQ
(
R
(
0
,
0
,
10
,
10
),
R
(
0
,
0
,
10
,
10
)
&
R
(
0
,
0
,
10
,
10
));
EXPECT_EQ
(
R
(
5
,
6
,
4
,
3
),
R
(
0
,
0
,
10
,
10
)
&
R
(
5
,
6
,
4
,
3
));
EXPECT_EQ
(
R
(
5
,
6
,
3
,
2
),
R
(
0
,
0
,
8
,
8
)
&
R
(
5
,
6
,
4
,
3
));
// Test with overflowing dimenions.
EXPECT_EQ
(
R
(
5
,
0
,
5
,
10
),
R
(
0
,
0
,
10
,
10
)
&
R
(
5
,
0
,
num_max
,
num_max
));
// Test with overflowing dimensions for floats/doubles.
EXPECT_EQ
(
R
(
num_max
,
0
,
num_max
/
4
,
10
),
R
(
num_max
,
0
,
num_max
/
2
,
10
)
&
R
(
num_max
,
0
,
num_max
/
4
,
10
));
// Test with overflowing coordinates.
EXPECT_EQ
(
R
(),
R
(
20
,
0
,
10
,
10
)
&
R
(
num_lowest
,
0
,
10
,
10
));
EXPECT_EQ
(
R
(),
R
(
20
,
0
,
10
,
10
)
&
R
(
0
,
num_lowest
,
10
,
10
));
EXPECT_EQ
(
R
(),
R
(
num_lowest
,
0
,
10
,
10
)
&
R
(
0
,
num_lowest
,
10
,
10
));
}
REGISTER_TYPED_TEST_CASE_P
(
Rect_Test
,
Overflows
);
typedef
::
testing
::
Types
<
int
,
float
,
double
>
RectTypes
;
INSTANTIATE_TYPED_TEST_CASE_P
(
Negative_Test
,
Rect_Test
,
RectTypes
);
}}
// namespace
}}
// namespace
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录