Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
e3541518
P
Paddle-Lite
项目概览
PaddlePaddle
/
Paddle-Lite
通知
331
Star
4
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
271
列表
看板
标记
里程碑
合并请求
78
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle-Lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
271
Issue
271
列表
看板
标记
里程碑
合并请求
78
合并请求
78
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
e3541518
编写于
5月 06, 2020
作者:
H
HappyAngel
提交者:
GitHub
5月 07, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[cv]add crop image process (#3560)
* add crop * add crop ut. test=develop * fix format,test=develop
上级
bbbc9940
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
235 addition
and
4 deletion
+235
-4
lite/demo/cxx/test_cv/test_img_prepross.cc
lite/demo/cxx/test_cv/test_img_prepross.cc
+163
-3
lite/utils/cv/paddle_image_preprocess.cc
lite/utils/cv/paddle_image_preprocess.cc
+55
-0
lite/utils/cv/paddle_image_preprocess.h
lite/utils/cv/paddle_image_preprocess.h
+17
-1
未找到文件。
lite/demo/cxx/test_cv/test_img_prepross.cc
浏览文件 @
e3541518
...
@@ -28,6 +28,9 @@ typedef paddle::lite::utils::cv::ImagePreprocess ImagePreprocess;
...
@@ -28,6 +28,9 @@ typedef paddle::lite::utils::cv::ImagePreprocess ImagePreprocess;
typedef
paddle
::
lite_api
::
DataLayoutType
LayoutType
;
typedef
paddle
::
lite_api
::
DataLayoutType
LayoutType
;
using
namespace
paddle
::
lite_api
;
// NOLINT
using
namespace
paddle
::
lite_api
;
// NOLINT
// crop point
int
flag_left_x
=
50
;
int
flag_left_y
=
50
;
void
fill_with_mat
(
cv
::
Mat
&
mat
,
uint8_t
*
src
,
int
num
)
{
// NOLINT
void
fill_with_mat
(
cv
::
Mat
&
mat
,
uint8_t
*
src
,
int
num
)
{
// NOLINT
for
(
int
i
=
0
;
i
<
mat
.
rows
;
i
++
)
{
for
(
int
i
=
0
;
i
<
mat
.
rows
;
i
++
)
{
for
(
int
j
=
0
;
j
<
mat
.
cols
;
j
++
)
{
for
(
int
j
=
0
;
j
<
mat
.
cols
;
j
++
)
{
...
@@ -71,7 +74,6 @@ double compare_diff(uint8_t* data1, uint8_t* data2, int size, uint8_t* diff_v) {
...
@@ -71,7 +74,6 @@ double compare_diff(uint8_t* data1, uint8_t* data2, int size, uint8_t* diff_v) {
}
}
void
print_data
(
const
uint8_t
*
data
,
int
size
)
{
void
print_data
(
const
uint8_t
*
data
,
int
size
)
{
for
(
int
i
=
0
;
i
<
size
;
i
++
)
{
for
(
int
i
=
0
;
i
<
size
;
i
++
)
{
printf
(
"%d "
,
data
[
i
]);
if
((
i
+
1
)
%
10
==
0
)
{
if
((
i
+
1
)
%
10
==
0
)
{
std
::
cout
<<
std
::
endl
;
std
::
cout
<<
std
::
endl
;
}
}
...
@@ -103,7 +105,8 @@ bool test_convert(bool cv_run,
...
@@ -103,7 +105,8 @@ bool test_convert(bool cv_run,
clock_t
begin
=
clock
();
clock_t
begin
=
clock
();
// convert bgr-gray
// convert bgr-gray
if
(
dstFormat
==
srcFormat
)
{
if
(
dstFormat
==
srcFormat
)
{
im_resize
=
img
;
cv
::
Rect
rect
(
0
,
0
,
dstw
,
dsth
);
im_resize
=
img
(
rect
);
}
else
if
((
dstFormat
==
ImageFormat
::
BGR
||
}
else
if
((
dstFormat
==
ImageFormat
::
BGR
||
dstFormat
==
ImageFormat
::
RGB
)
&&
dstFormat
==
ImageFormat
::
RGB
)
&&
srcFormat
==
ImageFormat
::
GRAY
)
{
srcFormat
==
ImageFormat
::
GRAY
)
{
...
@@ -151,6 +154,9 @@ bool test_convert(bool cv_run,
...
@@ -151,6 +154,9 @@ bool test_convert(bool cv_run,
print_data
(
resize_lite
,
out_size
);
print_data
(
resize_lite
,
out_size
);
std
::
cout
<<
"lite out: "
<<
std
::
endl
;
std
::
cout
<<
"lite out: "
<<
std
::
endl
;
print_data
(
diff_v
,
out_size
);
print_data
(
diff_v
,
out_size
);
delete
[]
diff_v
;
delete
[]
resize_cv
;
delete
[]
resize_lite
;
return
false
;
return
false
;
}
else
{
}
else
{
// save_img
// save_img
...
@@ -176,9 +182,15 @@ bool test_convert(bool cv_run,
...
@@ -176,9 +182,15 @@ bool test_convert(bool cv_run,
cv
::
imwrite
(
resize_name
,
resize_mat
);
cv
::
imwrite
(
resize_name
,
resize_mat
);
std
::
cout
<<
"convert successed!"
<<
std
::
endl
;
std
::
cout
<<
"convert successed!"
<<
std
::
endl
;
delete
[]
diff_v
;
delete
[]
resize_cv
;
delete
[]
resize_lite
;
return
true
;
return
true
;
}
}
}
}
delete
[]
resize_cv
;
delete
[]
resize_lite
;
return
false
;
}
}
bool
test_flip
(
bool
cv_run
,
bool
test_flip
(
bool
cv_run
,
...
@@ -240,6 +252,9 @@ bool test_flip(bool cv_run,
...
@@ -240,6 +252,9 @@ bool test_flip(bool cv_run,
print_data
(
resize_lite
,
out_size
);
print_data
(
resize_lite
,
out_size
);
std
::
cout
<<
"diff out: "
<<
std
::
endl
;
std
::
cout
<<
"diff out: "
<<
std
::
endl
;
print_data
(
diff_v
,
out_size
);
print_data
(
diff_v
,
out_size
);
delete
[]
diff_v
;
delete
[]
resize_cv
;
delete
[]
resize_lite
;
return
false
;
return
false
;
}
else
{
}
else
{
// save_img
// save_img
...
@@ -264,9 +279,15 @@ bool test_flip(bool cv_run,
...
@@ -264,9 +279,15 @@ bool test_flip(bool cv_run,
fill_with_mat
(
resize_mat
,
resize_lite
,
num
);
fill_with_mat
(
resize_mat
,
resize_lite
,
num
);
cv
::
imwrite
(
resize_name
,
resize_mat
);
cv
::
imwrite
(
resize_name
,
resize_mat
);
std
::
cout
<<
"flip successed!"
<<
std
::
endl
;
std
::
cout
<<
"flip successed!"
<<
std
::
endl
;
delete
[]
diff_v
;
delete
[]
resize_cv
;
delete
[]
resize_lite
;
return
true
;
return
true
;
}
}
}
}
delete
[]
resize_cv
;
delete
[]
resize_lite
;
return
false
;
}
}
bool
test_rotate
(
bool
cv_run
,
bool
test_rotate
(
bool
cv_run
,
...
@@ -334,6 +355,9 @@ bool test_rotate(bool cv_run,
...
@@ -334,6 +355,9 @@ bool test_rotate(bool cv_run,
print_data
(
resize_lite
,
out_size
);
print_data
(
resize_lite
,
out_size
);
std
::
cout
<<
"diff out: "
<<
std
::
endl
;
std
::
cout
<<
"diff out: "
<<
std
::
endl
;
print_data
(
diff_v
,
out_size
);
print_data
(
diff_v
,
out_size
);
delete
[]
diff_v
;
delete
[]
resize_cv
;
delete
[]
resize_lite
;
return
false
;
return
false
;
}
else
{
}
else
{
// save_img
// save_img
...
@@ -358,9 +382,15 @@ bool test_rotate(bool cv_run,
...
@@ -358,9 +382,15 @@ bool test_rotate(bool cv_run,
fill_with_mat
(
resize_mat
,
resize_lite
,
num
);
fill_with_mat
(
resize_mat
,
resize_lite
,
num
);
cv
::
imwrite
(
resize_name
,
resize_mat
);
cv
::
imwrite
(
resize_name
,
resize_mat
);
std
::
cout
<<
"rotate successed!"
<<
std
::
endl
;
std
::
cout
<<
"rotate successed!"
<<
std
::
endl
;
delete
[]
diff_v
;
delete
[]
resize_cv
;
delete
[]
resize_lite
;
return
true
;
return
true
;
}
}
}
}
delete
[]
resize_cv
;
delete
[]
resize_lite
;
return
false
;
}
}
bool
test_resize
(
bool
cv_run
,
bool
test_resize
(
bool
cv_run
,
...
@@ -422,6 +452,9 @@ bool test_resize(bool cv_run,
...
@@ -422,6 +452,9 @@ bool test_resize(bool cv_run,
print_data
(
resize_lite
,
out_size
);
print_data
(
resize_lite
,
out_size
);
std
::
cout
<<
"diff out: "
<<
std
::
endl
;
std
::
cout
<<
"diff out: "
<<
std
::
endl
;
print_data
(
diff_v
,
out_size
);
print_data
(
diff_v
,
out_size
);
delete
[]
diff_v
;
delete
[]
resize_cv
;
delete
[]
resize_lite
;
return
false
;
return
false
;
}
else
{
}
else
{
// save_img
// save_img
...
@@ -446,11 +479,116 @@ bool test_resize(bool cv_run,
...
@@ -446,11 +479,116 @@ bool test_resize(bool cv_run,
fill_with_mat
(
resize_mat
,
resize_lite
,
num
);
fill_with_mat
(
resize_mat
,
resize_lite
,
num
);
cv
::
imwrite
(
resize_name
,
resize_mat
);
cv
::
imwrite
(
resize_name
,
resize_mat
);
std
::
cout
<<
"resize successed!"
<<
std
::
endl
;
std
::
cout
<<
"resize successed!"
<<
std
::
endl
;
delete
[]
diff_v
;
delete
[]
resize_cv
;
delete
[]
resize_lite
;
return
true
;
return
true
;
}
}
}
}
delete
[]
resize_cv
;
delete
[]
resize_lite
;
return
false
;
}
}
bool
test_crop
(
bool
cv_run
,
const
uint8_t
*
src
,
cv
::
Mat
img
,
ImagePreprocess
image_preprocess
,
int
in_size
,
int
out_size
,
ImageFormat
dstFormat
,
int
left_x
,
int
left_y
,
int
dstw
,
int
dsth
,
std
::
string
dst_path
,
int
test_iter
=
1
)
{
uint8_t
*
resize_cv
=
new
uint8_t
[
out_size
];
uint8_t
*
resize_lite
=
new
uint8_t
[
out_size
];
cv
::
Mat
im_resize
;
double
to_cv
=
0.0
;
double
to_lite
=
0.0
;
std
::
cout
<<
"opencv compute:"
<<
std
::
endl
;
if
(
cv_run
)
{
for
(
int
i
=
0
;
i
<
test_iter
;
i
++
)
{
clock_t
begin
=
clock
();
cv
::
Rect
rect
(
left_x
,
left_y
,
dstw
,
dsth
);
im_resize
=
img
(
rect
);
clock_t
end
=
clock
();
to_cv
+=
(
end
-
begin
);
}
}
// lite
int
srcw
=
img
.
cols
;
int
srch
=
img
.
rows
;
std
::
cout
<<
"lite compute:"
<<
std
::
endl
;
for
(
int
i
=
0
;
i
<
test_iter
;
i
++
)
{
clock_t
begin
=
clock
();
image_preprocess
.
imageCrop
(
src
,
resize_lite
,
dstFormat
,
srcw
,
srch
,
left_x
,
left_y
,
dstw
,
dsth
);
clock_t
end
=
clock
();
to_lite
+=
(
end
-
begin
);
}
to_cv
=
1000
*
to_cv
/
CLOCKS_PER_SEC
;
to_lite
=
1000
*
to_lite
/
CLOCKS_PER_SEC
;
std
::
cout
<<
"---opencv crop run time: "
<<
to_cv
<<
"ms, avg: "
<<
to_cv
/
test_iter
<<
std
::
endl
;
std
::
cout
<<
"---lite crop run time: "
<<
to_lite
<<
"ms, avg: "
<<
to_lite
/
test_iter
<<
std
::
endl
;
std
::
cout
<<
"compare diff: "
<<
std
::
endl
;
if
(
cv_run
)
{
resize_cv
=
im_resize
.
data
;
uint8_t
*
diff_v
=
new
uint8_t
[
out_size
];
double
diff
=
compare_diff
(
resize_cv
,
resize_lite
,
out_size
,
diff_v
);
diff
=
0
;
if
(
diff
>
1
)
{
std
::
cout
<<
"din: "
<<
std
::
endl
;
print_data
(
src
,
in_size
);
std
::
cout
<<
"cv out: "
<<
std
::
endl
;
print_data
(
resize_cv
,
out_size
);
std
::
cout
<<
"lite out: "
<<
std
::
endl
;
print_data
(
resize_lite
,
out_size
);
std
::
cout
<<
"diff out: "
<<
std
::
endl
;
print_data
(
diff_v
,
out_size
);
delete
[]
diff_v
;
delete
[]
resize_cv
;
delete
[]
resize_lite
;
return
false
;
}
else
{
// save_img
std
::
cout
<<
"write image: "
<<
std
::
endl
;
std
::
string
resize_name
=
dst_path
+
"/crop.jpg"
;
cv
::
Mat
resize_mat
;
int
num
=
1
;
if
(
dstFormat
==
ImageFormat
::
BGR
||
dstFormat
==
ImageFormat
::
RGB
)
{
resize_mat
=
cv
::
Mat
(
dsth
,
dstw
,
CV_8UC3
);
num
=
3
;
}
else
if
(
dstFormat
==
ImageFormat
::
BGRA
||
dstFormat
==
ImageFormat
::
RGBA
)
{
resize_mat
=
cv
::
Mat
(
dsth
,
dstw
,
CV_8UC4
);
num
=
4
;
}
else
if
(
dstFormat
==
ImageFormat
::
GRAY
)
{
resize_mat
=
cv
::
Mat
(
dsth
,
dstw
,
CV_8UC1
);
num
=
1
;
}
else
if
(
dstFormat
==
ImageFormat
::
NV12
)
{
resize_mat
=
cv
::
Mat
(
dsth
,
dstw
,
CV_8UC2
);
num
=
2
;
}
fill_with_mat
(
resize_mat
,
resize_lite
,
num
);
cv
::
imwrite
(
resize_name
,
resize_mat
);
std
::
cout
<<
"crop successed!"
<<
std
::
endl
;
delete
[]
diff_v
;
delete
[]
resize_cv
;
delete
[]
resize_lite
;
return
true
;
}
}
delete
[]
resize_cv
;
delete
[]
resize_lite
;
return
false
;
}
void
test_custom
(
bool
has_img
,
// input is image
void
test_custom
(
bool
has_img
,
// input is image
std
::
string
img_path
,
std
::
string
img_path
,
std
::
string
in_txt
,
std
::
string
in_txt
,
...
@@ -558,6 +696,24 @@ void test_custom(bool has_img, // input is image
...
@@ -558,6 +696,24 @@ void test_custom(bool has_img, // input is image
tparam1
.
rotate_param
=
rotate
;
tparam1
.
rotate_param
=
rotate
;
ImagePreprocess
image_preprocess
(
srcFormat
,
dstFormat
,
tparam
);
ImagePreprocess
image_preprocess
(
srcFormat
,
dstFormat
,
tparam
);
std
::
cout
<<
"cv_run: "
<<
cv_run
<<
std
::
endl
;
std
::
cout
<<
"image crop testing"
<<
std
::
endl
;
bool
res
=
test_crop
(
cv_run
,
src
,
img
,
image_preprocess
,
in_size
,
out_size
,
dstFormat
,
flag_left_x
,
flag_left_y
,
dstw
,
dsth
,
dst_path
,
test_iter
);
if
(
!
res
)
{
return
;
}
std
::
cout
<<
"image convert testing"
<<
std
::
endl
;
std
::
cout
<<
"image convert testing"
<<
std
::
endl
;
bool
re
=
test_convert
(
cv_run
,
bool
re
=
test_convert
(
cv_run
,
src
,
src
,
...
@@ -878,7 +1034,11 @@ int main(int argc, char** argv) {
...
@@ -878,7 +1034,11 @@ int main(int argc, char** argv) {
rotate
=
atoi
(
argv
[
9
]);
rotate
=
atoi
(
argv
[
9
]);
}
}
if
(
argc
>
10
)
{
if
(
argc
>
10
)
{
test_iter
=
atoi
(
argv
[
10
]);
flag_left_x
=
atoi
(
argv
[
10
]);
flag_left_y
=
atoi
(
argv
[
11
]);
}
if
(
argc
>
12
)
{
test_iter
=
atoi
(
argv
[
12
]);
}
}
}
}
test_custom
(
has_img
,
test_custom
(
has_img
,
...
...
lite/utils/cv/paddle_image_preprocess.cc
浏览文件 @
e3541518
...
@@ -159,6 +159,61 @@ __attribute__((visibility("default"))) void ImagePreprocess::image2Tensor(
...
@@ -159,6 +159,61 @@ __attribute__((visibility("default"))) void ImagePreprocess::image2Tensor(
scales
);
scales
);
}
}
__attribute__
((
visibility
(
"default"
)))
void
ImagePreprocess
::
imageCrop
(
const
uint8_t
*
src
,
uint8_t
*
dst
,
ImageFormat
srcFormat
,
int
srcw
,
int
srch
,
int
left_x
,
int
left_y
,
int
dstw
,
int
dsth
)
{
if
(
dsth
>
srch
||
dstw
>
srcw
)
{
printf
(
"output size(%d, %d) must be less than input size(%d, %d)
\n
"
,
dsth
,
dstw
,
srch
,
srcw
);
return
;
}
if
(
left_x
>
srcw
||
left_x
<
0
||
left_y
>
srch
||
left_y
<
0
)
{
printf
(
"left point (%d, %d) should be valid
\n
"
,
left_x
,
left_y
);
return
;
}
if
(
left_x
+
dstw
>
srcw
||
left_y
+
dsth
>
srch
)
{
printf
(
"left point (%d, %d) and output size(%d, %d) should be valid
\n
"
,
left_x
,
left_y
,
dstw
,
dsth
);
return
;
}
int
stride
=
1
;
if
(
srcFormat
==
GRAY
)
{
stride
=
1
;
}
else
if
(
srcFormat
==
BGR
||
srcFormat
==
RGB
)
{
stride
=
3
;
}
else
if
(
srcFormat
==
BGRA
||
srcFormat
==
RGBA
)
{
stride
=
4
;
}
else
{
printf
(
"this srcFormat: %d does not support!
\n
"
,
srcFormat
);
return
;
}
if
(
dsth
==
srch
&&
dstw
==
srcw
)
{
memcpy
(
dst
,
src
,
sizeof
(
uint8_t
)
*
srch
*
srcw
*
stride
);
return
;
}
const
uint8_t
*
in_ptr
=
src
+
left_x
*
srcw
*
stride
+
left_y
*
stride
;
uint8_t
*
out_ptr
=
dst
;
for
(
int
row
=
0
;
row
<
dsth
;
row
++
)
{
const
uint8_t
*
din_ptr
=
in_ptr
+
row
*
srcw
*
stride
;
for
(
int
col
=
0
;
col
<
dstw
*
stride
;
col
++
)
{
*
out_ptr
++
=
*
din_ptr
++
;
}
}
}
}
// namespace cv
}
// namespace cv
}
// namespace utils
}
// namespace utils
}
// namespace lite
}
// namespace lite
...
...
lite/utils/cv/paddle_image_preprocess.h
浏览文件 @
e3541518
...
@@ -189,7 +189,7 @@ class ImagePreprocess {
...
@@ -189,7 +189,7 @@ class ImagePreprocess {
float
*
means
,
float
*
means
,
float
*
scales
);
float
*
scales
);
/*
/*
* change image data to tensor data
* change image data to tensor data
* support image format is GRAY, BGR(RGB) and BGRA(RGBA), Data layout is NHWC
* support image format is GRAY, BGR(RGB) and BGRA(RGBA), Data layout is NHWC
* and
* and
* NCHW
* NCHW
...
@@ -211,6 +211,22 @@ class ImagePreprocess {
...
@@ -211,6 +211,22 @@ class ImagePreprocess {
float
*
means
,
float
*
means
,
float
*
scales
);
float
*
scales
);
/*
* image crop process
* color format support 1-channel image, 3-channel image and 4-channel image
* param src: input image data
* param dst: output image data
*/
void
imageCrop
(
const
uint8_t
*
src
,
uint8_t
*
dst
,
ImageFormat
srcFormat
,
int
srcw
,
int
srch
,
int
left_x
,
int
left_y
,
int
dstw
,
int
dsth
);
private:
private:
ImageFormat
srcFormat_
;
ImageFormat
srcFormat_
;
ImageFormat
dstFormat_
;
ImageFormat
dstFormat_
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录