Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
e844c082
Commit
e844c082
authored
Feb 01, 2014
by
Ilya Lavrenov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
some fixes and improvements in cv::matchTemplate
parent
02692f9a
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
124 additions
and
169 deletions
+124
-169
ocl_defs.hpp
modules/core/include/opencv2/core/opencl/ocl_defs.hpp
+0
-2
perf_matchTemplate.cpp
modules/imgproc/perf/opencl/perf_matchTemplate.cpp
+1
-2
match_template.cl
modules/imgproc/src/opencl/match_template.cl
+3
-4
templmatch.cpp
modules/imgproc/src/templmatch.cpp
+108
-148
test_match_template.cpp
modules/imgproc/test/ocl/test_match_template.cpp
+12
-13
No files found.
modules/core/include/opencv2/core/opencl/ocl_defs.hpp
View file @
e844c082
...
@@ -5,8 +5,6 @@
...
@@ -5,8 +5,6 @@
// Copyright (C) 2014, Advanced Micro Devices, Inc., all rights reserved.
// Copyright (C) 2014, Advanced Micro Devices, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
// Third party copyrights are property of their respective owners.
#define CV_OPENCL_RUN_ASSERT
#ifdef HAVE_OPENCL
#ifdef HAVE_OPENCL
#ifdef CV_OPENCL_RUN_VERBOSE
#ifdef CV_OPENCL_RUN_VERBOSE
...
...
modules/imgproc/perf/opencl/perf_matchTemplate.cpp
View file @
e844c082
...
@@ -54,4 +54,4 @@ namespace ocl {
...
@@ -54,4 +54,4 @@ namespace ocl {
}
}
}
}
#endif // HAVE_OPENCL
#endif // HAVE_OPENCL
\ No newline at end of file
modules/imgproc/src/opencl/match_template.cl
View file @
e844c082
...
@@ -98,7 +98,7 @@ __kernel void matchTemplate_Naive_CCORR (__global const uchar * img,int img_step
...
@@ -98,7 +98,7 @@ __kernel void matchTemplate_Naive_CCORR (__global const uchar * img,int img_step
__kernel
void
matchTemplate_CCORR_NORMED
(
__global
const
uchar
*
img_sqsums,
int
img_sqsums_step,
int
img_sqsums_offset,
__kernel
void
matchTemplate_CCORR_NORMED
(
__global
const
uchar
*
img_sqsums,
int
img_sqsums_step,
int
img_sqsums_offset,
__global
uchar
*
res,
int
res_step,
int
res_offset,
int
res_rows,
int
res_cols,
__global
uchar
*
res,
int
res_step,
int
res_offset,
int
res_rows,
int
res_cols,
int
tpl_rows,
int
tpl_cols,
ulong
tpl_sqsum
)
int
tpl_rows,
int
tpl_cols,
float
tpl_sqsum
)
{
{
int
gidx
=
get_global_id
(
0
)
;
int
gidx
=
get_global_id
(
0
)
;
int
gidy
=
get_global_id
(
1
)
;
int
gidy
=
get_global_id
(
1
)
;
...
@@ -157,7 +157,7 @@ __kernel void matchTemplate_Naive_SQDIFF(__global const uchar * img,int img_step
...
@@ -157,7 +157,7 @@ __kernel void matchTemplate_Naive_SQDIFF(__global const uchar * img,int img_step
__kernel
void
matchTemplate_SQDIFF_NORMED
(
__global
const
uchar
*
img_sqsums,
int
img_sqsums_step,
int
img_sqsums_offset,
__kernel
void
matchTemplate_SQDIFF_NORMED
(
__global
const
uchar
*
img_sqsums,
int
img_sqsums_step,
int
img_sqsums_offset,
__global
uchar
*
res,
int
res_step,
int
res_offset,
int
res_rows,
int
res_cols,
__global
uchar
*
res,
int
res_step,
int
res_offset,
int
res_rows,
int
res_cols,
int
tpl_rows,
int
tpl_cols,
ulong
tpl_sqsum
)
int
tpl_rows,
int
tpl_cols,
float
tpl_sqsum
)
{
{
int
gidx
=
get_global_id
(
0
)
;
int
gidx
=
get_global_id
(
0
)
;
int
gidy
=
get_global_id
(
1
)
;
int
gidy
=
get_global_id
(
1
)
;
...
@@ -394,4 +394,4 @@ __kernel void matchTemplate_CCOEFF_NORMED_C4 (__global const uchar * img_sums, i
...
@@ -394,4 +394,4 @@ __kernel void matchTemplate_CCOEFF_NORMED_C4 (__global const uchar * img_sums, i
__global
float
*
result
=
(
__global
float
*
)(
res+res_idx
)
;
__global
float
*
result
=
(
__global
float
*
)(
res+res_idx
)
;
*result
=
normAcc
((
*result
)
-
num,
denum
)
;
*result
=
normAcc
((
*result
)
-
num,
denum
)
;
}
}
}
}
\ No newline at end of file
modules/imgproc/src/templmatch.cpp
View file @
e844c082
...
@@ -49,7 +49,7 @@ namespace cv
...
@@ -49,7 +49,7 @@ namespace cv
#ifdef HAVE_OPENCL
#ifdef HAVE_OPENCL
static
bool
useNaive
(
int
method
,
int
depth
,
Size
size
)
static
bool
useNaive
(
int
method
,
int
depth
,
const
Size
&
size
)
{
{
#ifdef HAVE_CLAMDFFT
#ifdef HAVE_CLAMDFFT
if
(
method
==
TM_SQDIFF
&&
depth
==
CV_32F
)
if
(
method
==
TM_SQDIFF
&&
depth
==
CV_32F
)
...
@@ -59,135 +59,128 @@ static bool useNaive(int method, int depth, Size size)
...
@@ -59,135 +59,128 @@ static bool useNaive(int method, int depth, Size size)
else
else
return
false
;
return
false
;
#else
#else
#define UNUSED(x) (void)(x
);
(
void
)(
method
);
UNUSED
(
method
)
UNUSED
(
depth
)
UNUSED
(
size
)
(
void
)(
depth
);
#undef UNUSED
(
void
)(
size
);
return
true
;
return
true
;
#endif
#endif
}
}
/////////////////////////////////////////////////// CCORR //////////////////////////////////////////////////////////////
/////////////////////////////////////////////////// CCORR //////////////////////////////////////////////////////////////
static
bool
matchTemplateNaive_CCORR
(
InputArray
_image
,
InputArray
_templ
,
OutputArray
_result
,
int
cn
)
static
bool
matchTemplateNaive_CCORR
(
InputArray
_image
,
InputArray
_templ
,
OutputArray
_result
)
{
{
int
type
=
_image
.
type
();
int
type
=
_image
.
type
(),
depth
=
CV_MAT_DEPTH
(
type
),
cn
=
CV_MAT_CN
(
type
);
int
depth
=
CV_MAT_DEPTH
(
type
);
const
char
*
kernelName
=
"matchTemplate_Naive_CCORR"
;
ocl
::
Kernel
k
(
kernelName
,
ocl
::
imgproc
::
match_template_oclsrc
,
format
(
"-D type=%s -D elem_type=%s -D cn=%d"
,
ocl
::
typeToStr
(
type
),
ocl
::
typeToStr
(
depth
),
cn
));
ocl
::
Kernel
k
(
"matchTemplate_Naive_CCORR"
,
ocl
::
imgproc
::
match_template_oclsrc
,
format
(
"-D type=%s -D elem_type=%s -D cn=%d"
,
ocl
::
typeToStr
(
type
),
ocl
::
typeToStr
(
depth
),
cn
));
if
(
k
.
empty
())
if
(
k
.
empty
())
return
false
;
return
false
;
UMat
image
=
_image
.
getUMat
();
UMat
image
=
_image
.
getUMat
(),
templ
=
_templ
.
getUMat
();
UMat
templ
=
_templ
.
getUMat
(),
result
;
_result
.
create
(
image
.
rows
-
templ
.
rows
+
1
,
image
.
cols
-
templ
.
cols
+
1
,
CV_32F
);
_result
.
create
(
image
.
rows
-
templ
.
rows
+
1
,
image
.
cols
-
templ
.
cols
+
1
,
CV_32F
);
result
=
_result
.
getUMat
();
UMat
result
=
_result
.
getUMat
();
size_t
globalsize
[
2
]
=
{
result
.
cols
,
result
.
rows
};
size_t
globalsize
[
2
]
=
{
result
.
cols
,
result
.
rows
};
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image
),
ocl
::
KernelArg
::
ReadOnly
(
templ
),
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image
),
ocl
::
KernelArg
::
ReadOnly
(
templ
),
ocl
::
KernelArg
::
WriteOnly
(
result
)).
run
(
2
,
globalsize
,
NULL
,
false
);
ocl
::
KernelArg
::
WriteOnly
(
result
)).
run
(
2
,
globalsize
,
NULL
,
false
);
}
}
static
bool
matchTemplate_CCORR_NORMED
(
InputArray
_image
,
InputArray
_templ
,
OutputArray
_result
)
static
bool
matchTemplate_CCORR_NORMED
(
InputArray
_image
,
InputArray
_templ
,
OutputArray
_result
)
{
{
matchTemplate
(
_image
,
_templ
,
_result
,
CV_TM_CCORR
);
matchTemplate
(
_image
,
_templ
,
_result
,
CV_TM_CCORR
);
int
type
=
_image
.
type
();
int
type
=
_image
.
type
(),
depth
=
CV_MAT_DEPTH
(
type
),
cn
=
CV_MAT_CN
(
type
);
int
depth
=
CV_MAT_DEPTH
(
type
),
cn
=
CV_MAT_CN
(
type
);
const
char
*
kernelName
=
"matchTemplate_CCORR_NORMED"
;
ocl
::
Kernel
k
(
kernelName
,
ocl
::
imgproc
::
match_template_oclsrc
,
format
(
"-D type=%s -D elem_type=%s -D cn=%d"
,
ocl
::
typeToStr
(
type
),
ocl
::
typeToStr
(
depth
),
cn
));
ocl
::
Kernel
k
(
"matchTemplate_CCORR_NORMED"
,
ocl
::
imgproc
::
match_template_oclsrc
,
format
(
"-D type=%s -D elem_type=%s -D cn=%d"
,
ocl
::
typeToStr
(
type
),
ocl
::
typeToStr
(
depth
),
cn
));
if
(
k
.
empty
())
if
(
k
.
empty
())
return
false
;
return
false
;
UMat
image
=
_image
.
getUMat
();
UMat
image
=
_image
.
getUMat
(),
templ
=
_templ
.
getUMat
();
UMat
templ
=
_templ
.
getUMat
(),
result
;
_result
.
create
(
image
.
rows
-
templ
.
rows
+
1
,
image
.
cols
-
templ
.
cols
+
1
,
CV_32F
);
_result
.
create
(
image
.
rows
-
templ
.
rows
+
1
,
image
.
cols
-
templ
.
cols
+
1
,
CV_32F
);
result
=
_result
.
getUMat
();
UMat
result
=
_result
.
getUMat
();
UMat
image_sums
,
image_sqsums
;
UMat
image_sums
,
image_sqsums
;
integral
(
image
.
reshape
(
1
),
image_sums
,
image_sqsums
,
CV_32F
,
CV_32F
);
integral
(
image
.
reshape
(
1
),
image_sums
,
image_sqsums
,
CV_32F
,
CV_32F
);
UMat
templ_resh
,
temp
;
UMat
temp
;
templ
.
reshape
(
1
).
convertTo
(
templ_resh
,
CV_32F
);
multiply
(
templ
,
templ
,
temp
,
1
,
CV_32F
);
Scalar
s
=
sum
(
temp
);
float
templ_sqsum
=
0
;
for
(
int
i
=
0
;
i
<
cn
;
++
i
)
templ_sqsum
+=
static_cast
<
float
>
(
s
[
i
]);
multiply
(
templ_resh
,
templ_resh
,
temp
);
size_t
globalsize
[
2
]
=
{
result
.
cols
,
result
.
rows
};
unsigned
long
long
templ_sqsum
=
(
unsigned
long
long
)
sum
(
temp
)[
0
];
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sqsums
),
ocl
::
KernelArg
::
ReadWrite
(
result
),
templ
.
rows
,
templ
.
cols
,
templ_sqsum
).
run
(
2
,
globalsize
,
NULL
,
false
);
size_t
globalsize
[
2
]
=
{
result
.
cols
,
result
.
rows
};
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sqsums
),
ocl
::
KernelArg
::
WriteOnly
(
result
),
templ
.
rows
,
templ
.
cols
,
templ_sqsum
).
run
(
2
,
globalsize
,
NULL
,
false
);
}
}
static
bool
matchTemplate_CCORR
(
InputArray
_image
,
InputArray
_templ
,
OutputArray
_result
)
static
bool
matchTemplate_CCORR
(
InputArray
_image
,
InputArray
_templ
,
OutputArray
_result
)
{
{
if
(
useNaive
(
TM_CCORR
,
_image
.
depth
(),
_templ
.
size
())
)
if
(
useNaive
(
TM_CCORR
,
_image
.
depth
(),
_templ
.
size
())
)
return
matchTemplateNaive_CCORR
(
_image
,
_templ
,
_result
,
_image
.
channels
()
);
return
matchTemplateNaive_CCORR
(
_image
,
_templ
,
_result
);
else
else
return
false
;
return
false
;
}
}
////////////////////////////////////// SQDIFF //////////////////////////////////////////////////////////////
////////////////////////////////////// SQDIFF //////////////////////////////////////////////////////////////
static
bool
matchTemplateNaive_SQDIFF
(
InputArray
_image
,
InputArray
_templ
,
OutputArray
_result
,
int
cn
)
static
bool
matchTemplateNaive_SQDIFF
(
InputArray
_image
,
InputArray
_templ
,
OutputArray
_result
)
{
{
int
type
=
_image
.
type
();
int
type
=
_image
.
type
(),
depth
=
CV_MAT_DEPTH
(
type
),
cn
=
CV_MAT_CN
(
type
);
int
depth
=
CV_MAT_DEPTH
(
type
);
const
char
*
kernelName
=
"matchTemplate_Naive_SQDIFF"
;
ocl
::
Kernel
k
(
"matchTemplate_Naive_SQDIFF"
,
ocl
::
imgproc
::
match_template_oclsrc
,
format
(
"-D type=%s -D elem_type=%s -D cn=%d"
,
ocl
::
typeToStr
(
type
),
ocl
::
Kernel
k
(
kernelName
,
ocl
::
imgproc
::
match_template_oclsrc
,
format
(
"-D type=%s -D elem_type=%s -D cn=%d"
,
ocl
::
typeToStr
(
type
),
ocl
::
typeToStr
(
depth
),
cn
));
ocl
::
typeToStr
(
depth
),
cn
));
if
(
k
.
empty
())
if
(
k
.
empty
())
return
false
;
return
false
;
UMat
image
=
_image
.
getUMat
();
UMat
image
=
_image
.
getUMat
(),
templ
=
_templ
.
getUMat
();
UMat
templ
=
_templ
.
getUMat
(),
result
;
_result
.
create
(
image
.
rows
-
templ
.
rows
+
1
,
image
.
cols
-
templ
.
cols
+
1
,
CV_32F
);
_result
.
create
(
image
.
rows
-
templ
.
rows
+
1
,
image
.
cols
-
templ
.
cols
+
1
,
CV_32F
);
result
=
_result
.
getUMat
();
UMat
result
=
_result
.
getUMat
();
size_t
globalsize
[
2
]
=
{
result
.
cols
,
result
.
rows
};
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image
),
ocl
::
KernelArg
::
ReadOnly
(
templ
),
ocl
::
KernelArg
::
WriteOnly
(
result
)).
run
(
2
,
globalsize
,
NULL
,
false
);
size_t
globalsize
[
2
]
=
{
result
.
cols
,
result
.
rows
};
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image
),
ocl
::
KernelArg
::
ReadOnly
(
templ
),
ocl
::
KernelArg
::
WriteOnly
(
result
)).
run
(
2
,
globalsize
,
NULL
,
false
);
}
}
static
bool
matchTemplate_SQDIFF_NORMED
(
InputArray
_image
,
InputArray
_templ
,
OutputArray
_result
)
static
bool
matchTemplate_SQDIFF_NORMED
(
InputArray
_image
,
InputArray
_templ
,
OutputArray
_result
)
{
{
matchTemplate
(
_image
,
_templ
,
_result
,
CV_TM_CCORR
);
matchTemplate
(
_image
,
_templ
,
_result
,
CV_TM_CCORR
);
int
type
=
_image
.
type
();
int
type
=
_image
.
type
(),
depth
=
CV_MAT_DEPTH
(
type
),
cn
=
CV_MAT_CN
(
type
);
int
depth
=
CV_MAT_DEPTH
(
type
),
cn
=
CV_MAT_CN
(
type
);
const
char
*
kernelName
=
"matchTemplate_SQDIFF_NORMED"
;
ocl
::
Kernel
k
(
"matchTemplate_SQDIFF_NORMED"
,
ocl
::
imgproc
::
match_template_oclsrc
,
format
(
"-D type=%s -D elem_type=%s -D cn=%d"
,
ocl
::
Kernel
k
(
kernelName
,
ocl
::
imgproc
::
match_template_oclsrc
,
format
(
"-D type=%s -D elem_type=%s -D cn=%d"
,
ocl
::
typeToStr
(
type
),
ocl
::
typeToStr
(
depth
),
cn
));
ocl
::
typeToStr
(
type
),
ocl
::
typeToStr
(
depth
),
cn
));
if
(
k
.
empty
())
if
(
k
.
empty
())
return
false
;
return
false
;
UMat
image
=
_image
.
getUMat
();
UMat
image
=
_image
.
getUMat
(),
templ
=
_templ
.
getUMat
();
UMat
templ
=
_templ
.
getUMat
(),
result
;
_result
.
create
(
image
.
rows
-
templ
.
rows
+
1
,
image
.
cols
-
templ
.
cols
+
1
,
CV_32F
);
_result
.
create
(
image
.
rows
-
templ
.
rows
+
1
,
image
.
cols
-
templ
.
cols
+
1
,
CV_32F
);
result
=
_result
.
getUMat
();
UMat
result
=
_result
.
getUMat
();
UMat
image_sums
,
image_sqsums
;
UMat
image_sums
,
image_sqsums
;
integral
(
image
.
reshape
(
1
),
image_sums
,
image_sqsums
,
CV_32F
,
CV_32F
);
integral
(
image
.
reshape
(
1
),
image_sums
,
image_sqsums
,
CV_32F
,
CV_32F
);
UMat
temp
,
templ_resh
;
UMat
temp
;
templ
.
reshape
(
1
).
convertTo
(
templ_resh
,
CV_32F
);
multiply
(
templ
,
templ
,
temp
,
1
,
CV_32F
);
Scalar
s
=
sum
(
temp
);
multiply
(
templ_resh
,
templ_resh
,
temp
);
float
templ_sqsum
=
0
;
unsigned
long
long
templ_sqsum
=
(
unsigned
long
long
)
sum
(
temp
)[
0
];
for
(
int
i
=
0
;
i
<
cn
;
++
i
)
templ_sqsum
+=
(
float
)
s
[
i
];
size_t
globalsize
[
2
]
=
{
result
.
cols
,
result
.
rows
};
size_t
globalsize
[
2
]
=
{
result
.
cols
,
result
.
rows
};
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sqsums
),
ocl
::
KernelArg
::
ReadWrite
(
result
),
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sqsums
),
ocl
::
KernelArg
::
WriteOnly
(
result
),
templ
.
rows
,
templ
.
cols
,
templ_sqsum
).
run
(
2
,
globalsize
,
NULL
,
false
);
templ
.
rows
,
templ
.
cols
,
templ_sqsum
).
run
(
2
,
globalsize
,
NULL
,
false
);
}
}
static
bool
matchTemplate_SQDIFF
(
InputArray
_image
,
InputArray
_templ
,
OutputArray
_result
)
static
bool
matchTemplate_SQDIFF
(
InputArray
_image
,
InputArray
_templ
,
OutputArray
_result
)
{
{
if
(
useNaive
(
TM_SQDIFF
,
_image
.
depth
(),
_templ
.
size
()))
if
(
useNaive
(
TM_SQDIFF
,
_image
.
depth
(),
_templ
.
size
()))
return
matchTemplateNaive_SQDIFF
(
_image
,
_templ
,
_result
,
_image
.
channels
()
);
return
matchTemplateNaive_SQDIFF
(
_image
,
_templ
,
_result
);
else
else
return
false
;
return
false
;
}
}
...
@@ -201,91 +194,66 @@ static bool matchTemplate_CCOEFF(InputArray _image, InputArray _templ, OutputArr
...
@@ -201,91 +194,66 @@ static bool matchTemplate_CCOEFF(InputArray _image, InputArray _templ, OutputArr
UMat
image_sums
;
UMat
image_sums
;
integral
(
_image
,
image_sums
);
integral
(
_image
,
image_sums
);
int
type
=
image_sums
.
type
();
int
type
=
image_sums
.
type
(),
depth
=
CV_MAT_DEPTH
(
type
),
cn
=
CV_MAT_CN
(
type
);
int
depth
=
CV_MAT_DEPTH
(
type
),
cn
=
CV_MAT_CN
(
type
);
const
char
*
kernelName
;
if
(
cn
==
1
)
ocl
::
Kernel
k
(
cv
::
format
(
"matchTemplate_Prepared_CCOEFF_C%d"
,
cn
).
c_str
(),
ocl
::
imgproc
::
match_template_oclsrc
,
kernelName
=
"matchTemplate_Prepared_CCOEFF_C1"
;
format
(
"-D type=%s -D elem_type=%s -D cn=%d"
,
ocl
::
typeToStr
(
type
),
ocl
::
typeToStr
(
depth
),
cn
));
else
if
(
cn
==
2
)
kernelName
=
"matchTemplate_Prepared_CCOEFF_C2"
;
else
kernelName
=
"matchTemplate_Prepared_CCOEFF_C4"
;
ocl
::
Kernel
k
(
kernelName
,
ocl
::
imgproc
::
match_template_oclsrc
,
format
(
"-D type=%s -D elem_type=%s -D cn=%d"
,
ocl
::
typeToStr
(
type
),
ocl
::
typeToStr
(
depth
),
cn
));
if
(
k
.
empty
())
if
(
k
.
empty
())
return
false
;
return
false
;
UMat
templ
=
_templ
.
getUMat
()
,
result
;
UMat
templ
=
_templ
.
getUMat
();
Size
size
=
_image
.
size
();
Size
size
=
_image
.
size
()
,
tsize
=
templ
.
size
()
;
_result
.
create
(
size
.
height
-
templ
.
rows
+
1
,
size
.
width
-
templ
.
cols
+
1
,
CV_32F
);
_result
.
create
(
size
.
height
-
templ
.
rows
+
1
,
size
.
width
-
templ
.
cols
+
1
,
CV_32F
);
result
=
_result
.
getUMat
();
UMat
result
=
_result
.
getUMat
();
size_t
globalsize
[
2
]
=
{
result
.
cols
,
result
.
rows
};
size_t
globalsize
[
2
]
=
{
result
.
cols
,
result
.
rows
};
if
(
cn
==
1
)
if
(
cn
==
1
)
{
{
float
templ_sum
=
(
float
)
sum
(
_templ
)[
0
]
/
_templ
.
size
().
area
();
float
templ_sum
=
static_cast
<
float
>
(
sum
(
_templ
)[
0
])
/
tsize
.
area
();
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sums
),
ocl
::
KernelArg
::
WriteOnly
(
result
),
templ
.
rows
,
templ
.
cols
,
templ_sum
).
run
(
2
,
globalsize
,
NULL
,
false
);
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sums
),
ocl
::
KernelArg
::
ReadWrite
(
result
),
templ
.
rows
,
templ
.
cols
,
templ_sum
).
run
(
2
,
globalsize
,
NULL
,
false
);
}
}
else
else
{
{
Vec4f
templ_sum
=
Vec4f
::
all
(
0
);
Vec4f
templ_sum
=
Vec4f
::
all
(
0
);
templ_sum
=
sum
(
templ
)
/
templ
.
size
()
.
area
();
templ_sum
=
sum
(
templ
)
/
tsize
.
area
();
if
(
cn
==
2
)
if
(
cn
==
2
)
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sums
),
ocl
::
KernelArg
::
WriteOnly
(
result
),
templ
.
rows
,
templ
.
cols
,
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sums
),
ocl
::
KernelArg
::
ReadWrite
(
result
),
templ
.
rows
,
templ
.
cols
,
templ_sum
[
0
],
templ_sum
[
1
]).
run
(
2
,
globalsize
,
NULL
,
false
);
templ_sum
[
0
],
templ_sum
[
1
]).
run
(
2
,
globalsize
,
NULL
,
false
);
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sums
),
ocl
::
KernelArg
::
WriteOnly
(
result
),
templ
.
rows
,
templ
.
cols
,
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sums
),
ocl
::
KernelArg
::
ReadWrite
(
result
),
templ
.
rows
,
templ
.
cols
,
templ_sum
[
0
],
templ_sum
[
1
],
templ_sum
[
2
],
templ_sum
[
3
]).
run
(
2
,
globalsize
,
NULL
,
false
);
templ_sum
[
0
],
templ_sum
[
1
],
templ_sum
[
2
],
templ_sum
[
3
]).
run
(
2
,
globalsize
,
NULL
,
false
);
}
}
}
}
static
bool
matchTemplate_CCOEFF_NORMED
(
InputArray
_image
,
InputArray
_templ
,
OutputArray
_result
)
static
bool
matchTemplate_CCOEFF_NORMED
(
InputArray
_image
,
InputArray
_templ
,
OutputArray
_result
)
{
{
UMat
imagef
,
templf
;
matchTemplate
(
_image
,
_templ
,
_result
,
CV_TM_CCORR
);
_image
.
getUMat
().
convertTo
(
imagef
,
CV_32F
);
_templ
.
getUMat
().
convertTo
(
templf
,
CV_32F
);
matchTemplate
(
imagef
,
templf
,
_result
,
CV_TM_CCORR
);
const
char
*
kernelName
;
UMat
temp
,
image_sums
,
image_sqsums
;
UMat
temp
,
image_sums
,
image_sqsums
;
integral
(
_image
,
image_sums
,
image_sqsums
,
CV_32F
,
CV_32F
);
integral
(
_image
,
image_sums
,
image_sqsums
,
CV_32F
,
CV_32F
);
int
type
=
image_sums
.
type
();
int
type
=
image_sums
.
type
(),
depth
=
CV_MAT_DEPTH
(
type
),
cn
=
CV_MAT_CN
(
type
);
int
depth
=
CV_MAT_DEPTH
(
type
),
cn
=
CV_MAT_CN
(
type
);
if
(
cn
==
1
)
kernelName
=
"matchTemplate_CCOEFF_NORMED_C1"
;
else
if
(
cn
==
2
)
kernelName
=
"matchTemplate_CCOEFF_NORMED_C2"
;
else
kernelName
=
"matchTemplate_CCOEFF_NORMED_C4"
;
ocl
::
Kernel
k
(
kernelName
,
ocl
::
imgproc
::
match_template_oclsrc
,
ocl
::
Kernel
k
(
format
(
"matchTemplate_CCOEFF_NORMED_C%d"
,
cn
).
c_str
()
,
ocl
::
imgproc
::
match_template_oclsrc
,
format
(
"-D type=%s -D elem_type=%s -D cn=%d"
,
ocl
::
typeToStr
(
type
),
ocl
::
typeToStr
(
depth
),
cn
));
format
(
"-D type=%s -D elem_type=%s -D cn=%d"
,
ocl
::
typeToStr
(
type
),
ocl
::
typeToStr
(
depth
),
cn
));
if
(
k
.
empty
())
if
(
k
.
empty
())
return
false
;
return
false
;
UMat
image
=
_image
.
getUMat
();
UMat
templ
=
_templ
.
getUMat
();
UMat
templ
=
_templ
.
getUMat
(),
result
;
Size
size
=
_image
.
size
(),
tsize
=
templ
.
size
();
int
image_rows
=
_image
.
size
().
height
,
image_cols
=
_image
.
size
().
width
;
_result
.
create
(
size
.
height
-
templ
.
rows
+
1
,
size
.
width
-
templ
.
cols
+
1
,
CV_32F
);
_result
.
create
(
image_rows
-
templ
.
rows
+
1
,
image_cols
-
templ
.
cols
+
1
,
CV_32F
);
UMat
result
=
_result
.
getUMat
();
result
=
_result
.
getUMat
();
size_t
globalsize
[
2
]
=
{
result
.
cols
,
result
.
rows
};
float
scale
=
1.
f
/
templ
.
size
().
area
();
size_t
globalsize
[
2
]
=
{
result
.
cols
,
result
.
rows
};
float
scale
=
1.
f
/
tsize
.
area
();
if
(
cn
==
1
)
if
(
cn
==
1
)
{
{
float
templ_sum
=
(
float
)
sum
(
templ
)[
0
];
float
templ_sum
=
(
float
)
sum
(
templ
)[
0
];
multiply
(
templ
f
,
templf
,
temp
);
multiply
(
templ
,
templ
,
temp
,
1
,
CV_32F
);
float
templ_sqsum
=
(
float
)
sum
(
temp
)[
0
];
float
templ_sqsum
=
(
float
)
sum
(
temp
)[
0
];
templ_sqsum
-=
scale
*
templ_sum
*
templ_sum
;
templ_sqsum
-=
scale
*
templ_sum
*
templ_sum
;
...
@@ -297,27 +265,23 @@ static bool matchTemplate_CCOEFF_NORMED(InputArray _image, InputArray _templ, Ou
...
@@ -297,27 +265,23 @@ static bool matchTemplate_CCOEFF_NORMED(InputArray _image, InputArray _templ, Ou
return
true
;
return
true
;
}
}
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sums
),
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sqsums
),
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sums
),
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sqsums
),
ocl
::
KernelArg
::
WriteOnly
(
result
),
templ
.
rows
,
templ
.
cols
,
scale
,
templ_sum
,
templ_sqsum
)
ocl
::
KernelArg
::
ReadWrite
(
result
),
templ
.
rows
,
templ
.
cols
,
scale
,
templ_sum
,
templ_sqsum
)
.
run
(
2
,
globalsize
,
NULL
,
false
);
.
run
(
2
,
globalsize
,
NULL
,
false
);
}
}
else
else
{
{
Vec4f
templ_sum
=
Vec4f
::
all
(
0
);
Vec4f
templ_sum
=
Vec4f
::
all
(
0
),
templ_sqsum
=
Vec4f
::
all
(
0
);
Vec4f
templ_sqsum
=
Vec4f
::
all
(
0
);
templ_sum
=
sum
(
templ
);
templ_sum
=
sum
(
templ
);
multiply
(
templ
f
,
templf
,
temp
);
multiply
(
templ
,
templ
,
temp
,
1
,
CV_32F
);
templ_sqsum
=
sum
(
temp
);
templ_sqsum
=
sum
(
temp
);
float
templ_sqsum_sum
=
0
;
float
templ_sqsum_sum
=
0
;
for
(
int
i
=
0
;
i
<
cn
;
i
++
)
for
(
int
i
=
0
;
i
<
cn
;
i
++
)
{
templ_sqsum_sum
+=
templ_sqsum
[
i
]
-
scale
*
templ_sum
[
i
]
*
templ_sum
[
i
];
templ_sqsum_sum
+=
templ_sqsum
[
i
]
-
scale
*
templ_sum
[
i
]
*
templ_sum
[
i
];
}
templ_sum
*=
scale
;
templ_sum
*=
scale
;
if
(
templ_sqsum_sum
<
DBL_EPSILON
)
if
(
templ_sqsum_sum
<
DBL_EPSILON
)
{
{
...
@@ -325,38 +289,35 @@ static bool matchTemplate_CCOEFF_NORMED(InputArray _image, InputArray _templ, Ou
...
@@ -325,38 +289,35 @@ static bool matchTemplate_CCOEFF_NORMED(InputArray _image, InputArray _templ, Ou
return
true
;
return
true
;
}
}
if
(
cn
==
2
)
if
(
cn
==
2
)
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sums
),
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sqsums
),
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sums
),
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sqsums
),
ocl
::
KernelArg
::
WriteOnly
(
result
),
templ
.
rows
,
templ
.
cols
,
scale
,
ocl
::
KernelArg
::
ReadWrite
(
result
),
templ
.
rows
,
templ
.
cols
,
scale
,
templ_sum
[
0
],
templ_sum
[
1
],
templ_sqsum_sum
)
templ_sum
[
0
],
templ_sum
[
1
],
templ_sqsum_sum
).
run
(
2
,
globalsize
,
NULL
,
false
);
.
run
(
2
,
globalsize
,
NULL
,
false
);
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sums
),
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sqsums
),
return
k
.
args
(
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sums
),
ocl
::
KernelArg
::
ReadOnlyNoSize
(
image_sqsums
),
ocl
::
KernelArg
::
WriteOnly
(
result
),
templ
.
rows
,
templ
.
cols
,
scale
,
ocl
::
KernelArg
::
ReadWrite
(
result
),
templ
.
rows
,
templ
.
cols
,
scale
,
templ_sum
[
0
],
templ_sum
[
1
],
templ_sum
[
2
],
templ_sum
[
3
],
templ_sqsum_sum
)
templ_sum
[
0
],
templ_sum
[
1
],
templ_sum
[
2
],
templ_sum
[
3
],
.
run
(
2
,
globalsize
,
NULL
,
false
);
templ_sqsum_sum
).
run
(
2
,
globalsize
,
NULL
,
false
);
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
static
bool
ocl_matchTemplate
(
InputArray
_img
,
InputArray
_templ
,
OutputArray
_result
,
int
method
)
static
bool
ocl_matchTemplate
(
InputArray
_img
,
InputArray
_templ
,
OutputArray
_result
,
int
method
)
{
{
int
cn
=
CV_MAT_CN
(
_img
.
type
()
);
int
cn
=
_img
.
channels
(
);
if
(
cn
==
3
||
cn
>
4
)
if
(
cn
==
3
||
cn
>
4
)
return
false
;
return
false
;
typedef
bool
(
*
Caller
)(
InputArray
_img
,
InputArray
_templ
,
OutputArray
_result
);
typedef
bool
(
*
Caller
)(
InputArray
_img
,
InputArray
_templ
,
OutputArray
_result
);
const
Caller
callers
[]
=
static
const
Caller
callers
[]
=
{
{
matchTemplate_SQDIFF
,
matchTemplate_SQDIFF_NORMED
,
matchTemplate_CCORR
,
matchTemplate_SQDIFF
,
matchTemplate_SQDIFF_NORMED
,
matchTemplate_CCORR
,
matchTemplate_CCORR_NORMED
,
matchTemplate_CCOEFF
,
matchTemplate_CCOEFF_NORMED
matchTemplate_CCORR_NORMED
,
matchTemplate_CCOEFF
,
matchTemplate_CCOEFF_NORMED
};
};
const
Caller
caller
=
callers
[
method
];
Caller
caller
=
callers
[
method
];
return
caller
(
_img
,
_templ
,
_result
);
return
caller
(
_img
,
_templ
,
_result
);
}
}
...
@@ -552,17 +513,16 @@ void crossCorr( const Mat& img, const Mat& _templ, Mat& corr,
...
@@ -552,17 +513,16 @@ void crossCorr( const Mat& img, const Mat& _templ, Mat& corr,
void
cv
::
matchTemplate
(
InputArray
_img
,
InputArray
_templ
,
OutputArray
_result
,
int
method
)
void
cv
::
matchTemplate
(
InputArray
_img
,
InputArray
_templ
,
OutputArray
_result
,
int
method
)
{
{
CV_Assert
(
CV_TM_SQDIFF
<=
method
&&
method
<=
CV_TM_CCOEFF_NORMED
);
CV_Assert
(
CV_TM_SQDIFF
<=
method
&&
method
<=
CV_TM_CCOEFF_NORMED
);
CV_Assert
(
(
_img
.
depth
()
==
CV_8U
||
_img
.
depth
()
==
CV_32F
)
&&
_img
.
type
()
==
_templ
.
type
()
);
CV_Assert
(
(
_img
.
depth
()
==
CV_8U
||
_img
.
depth
()
==
CV_32F
)
&&
_img
.
type
()
==
_templ
.
type
()
&&
_img
.
dims
()
<=
2
);
CV_Assert
(
_img
.
dims
()
<=
2
);
bool
swapNotNeed
=
(
_img
.
size
().
height
>=
_templ
.
size
().
height
&&
_img
.
size
().
width
>=
_templ
.
size
().
width
)
;
bool
needswap
=
_img
.
size
().
height
<
_templ
.
size
().
height
||
_img
.
size
().
width
<
_templ
.
size
().
width
;
if
(
!
swapNotNeed
)
if
(
needswap
)
{
{
CV_Assert
(
_img
.
size
().
height
<=
_templ
.
size
().
height
&&
_img
.
size
().
width
<=
_templ
.
size
().
width
);
CV_Assert
(
_img
.
size
().
height
<=
_templ
.
size
().
height
&&
_img
.
size
().
width
<=
_templ
.
size
().
width
);
}
}
CV_OCL_RUN
(
_img
.
dims
()
<=
2
&&
_result
.
isUMat
(),
CV_OCL_RUN
(
_img
.
dims
()
<=
2
&&
_result
.
isUMat
(),
(
swapNotNeed
?
ocl_matchTemplate
(
_img
,
_templ
,
_result
,
method
)
:
ocl_matchTemplate
(
_templ
,
_img
,
_result
,
method
)))
(
!
needswap
?
ocl_matchTemplate
(
_img
,
_templ
,
_result
,
method
)
:
ocl_matchTemplate
(
_templ
,
_img
,
_result
,
method
)))
int
numType
=
method
==
CV_TM_CCORR
||
method
==
CV_TM_CCORR_NORMED
?
0
:
int
numType
=
method
==
CV_TM_CCORR
||
method
==
CV_TM_CCORR_NORMED
?
0
:
method
==
CV_TM_CCOEFF
||
method
==
CV_TM_CCOEFF_NORMED
?
1
:
2
;
method
==
CV_TM_CCOEFF
||
method
==
CV_TM_CCOEFF_NORMED
?
1
:
2
;
...
@@ -571,7 +531,7 @@ void cv::matchTemplate( InputArray _img, InputArray _templ, OutputArray _result,
...
@@ -571,7 +531,7 @@ void cv::matchTemplate( InputArray _img, InputArray _templ, OutputArray _result,
method
==
CV_TM_CCOEFF_NORMED
;
method
==
CV_TM_CCOEFF_NORMED
;
Mat
img
=
_img
.
getMat
(),
templ
=
_templ
.
getMat
();
Mat
img
=
_img
.
getMat
(),
templ
=
_templ
.
getMat
();
if
(
!
swapNotNeed
)
if
(
needswap
)
std
::
swap
(
img
,
templ
);
std
::
swap
(
img
,
templ
);
Size
corrSize
(
img
.
cols
-
templ
.
cols
+
1
,
img
.
rows
-
templ
.
rows
+
1
);
Size
corrSize
(
img
.
cols
-
templ
.
cols
+
1
,
img
.
rows
-
templ
.
rows
+
1
);
...
...
modules/imgproc/test/ocl/test_match_template.cpp
View file @
e844c082
...
@@ -51,9 +51,12 @@
...
@@ -51,9 +51,12 @@
namespace
cvtest
{
namespace
cvtest
{
namespace
ocl
{
namespace
ocl
{
/////////////////////////////////////////////
matchTemplate
//////////////////////////////////////////////////////////
/////////////////////////////////////////////
matchTemplate
//////////////////////////////////////////////////////////
PARAM_TEST_CASE
(
MatchTemplate
,
MatDepth
,
Channels
,
int
,
bool
)
CV_ENUM
(
MatchTemplType
,
CV_TM_SQDIFF
,
CV_TM_SQDIFF_NORMED
,
CV_TM_CCORR
,
CV_TM_CCORR_NORMED
,
CV_TM_CCOEFF
,
CV_TM_CCOEFF_NORMED
)
PARAM_TEST_CASE
(
MatchTemplate
,
MatDepth
,
Channels
,
MatchTemplType
,
bool
)
{
{
int
type
;
int
type
;
int
depth
;
int
depth
;
...
@@ -88,7 +91,7 @@ PARAM_TEST_CASE(MatchTemplate, MatDepth, Channels, int, bool)
...
@@ -88,7 +91,7 @@ PARAM_TEST_CASE(MatchTemplate, MatDepth, Channels, int, bool)
randomSubMat
(
templ
,
templ_roi
,
templ_roiSize
,
templBorder
,
type
,
-
upValue
,
upValue
);
randomSubMat
(
templ
,
templ_roi
,
templ_roiSize
,
templBorder
,
type
,
-
upValue
,
upValue
);
Border
resultBorder
=
randomBorder
(
0
,
use_roi
?
MAX_VALUE
:
0
);
Border
resultBorder
=
randomBorder
(
0
,
use_roi
?
MAX_VALUE
:
0
);
randomSubMat
(
result
,
result_roi
,
result_roiSize
,
resultBorder
,
CV_32F
,
-
upValue
,
upValue
);
randomSubMat
(
result
,
result_roi
,
result_roiSize
,
resultBorder
,
CV_32F
C1
,
-
upValue
,
upValue
);
UMAT_UPLOAD_INPUT_PARAMETER
(
image
)
UMAT_UPLOAD_INPUT_PARAMETER
(
image
)
UMAT_UPLOAD_INPUT_PARAMETER
(
templ
)
UMAT_UPLOAD_INPUT_PARAMETER
(
templ
)
...
@@ -97,7 +100,7 @@ PARAM_TEST_CASE(MatchTemplate, MatDepth, Channels, int, bool)
...
@@ -97,7 +100,7 @@ PARAM_TEST_CASE(MatchTemplate, MatDepth, Channels, int, bool)
void
Near
(
double
threshold
=
0.0
)
void
Near
(
double
threshold
=
0.0
)
{
{
OCL_EXPECT_MATS_NEAR
(
result
,
threshold
);
OCL_EXPECT_MATS_NEAR
_RELATIVE
(
result
,
threshold
);
}
}
};
};
...
@@ -107,22 +110,19 @@ OCL_TEST_P(MatchTemplate, Mat)
...
@@ -107,22 +110,19 @@ OCL_TEST_P(MatchTemplate, Mat)
{
{
generateTestData
();
generateTestData
();
OCL_OFF
(
cv
::
matchTemplate
(
image_roi
,
templ_roi
,
result_roi
,
method
));
OCL_OFF
(
cv
::
matchTemplate
(
image_roi
,
templ_roi
,
result_roi
,
method
));
OCL_ON
(
cv
::
matchTemplate
(
uimage_roi
,
utempl_roi
,
uresult_roi
,
method
));
OCL_ON
(
cv
::
matchTemplate
(
uimage_roi
,
utempl_roi
,
uresult_roi
,
method
));
if
(
method
==
0
)
Near
(
1.5e-4
);
Near
(
10.0
f
);
else
Near
(
method
%
2
==
1
?
0.001
f
:
1.0
f
);
}
}
}
}
OCL_INSTANTIATE_TEST_CASE_P
(
ImageProc
,
MatchTemplate
,
Combine
(
OCL_INSTANTIATE_TEST_CASE_P
(
ImageProc
,
MatchTemplate
,
Combine
(
Values
(
CV_8U
,
CV_32F
),
Values
(
CV_8U
,
CV_32F
),
Values
(
1
,
2
,
4
),
Values
(
1
,
2
,
4
),
Values
(
0
,
1
,
2
,
3
,
4
,
5
),
MatchTemplType
::
all
(
),
Bool
())
Bool
())
);
);
}
}
// namespace cvtest::ocl
}
}
// namespace cvtest::ocl
#endif
#endif
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment