mirror of
https://github.com/opencv/opencv.git
synced 2026-07-31 00:03:03 +04:00
Merge remote-tracking branch 'upstream/3.4' into merge-3.4
This commit is contained in:
@@ -39,6 +39,26 @@ class solvepnp_test(NewOpenCVTests):
|
||||
obj_points, img_points, cameraMatrix, distCoeffs, reprojectionError=r
|
||||
)
|
||||
|
||||
def test_regression_16049(self):
|
||||
obj_points = np.array([[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0]], dtype=np.float32)
|
||||
img_points = np.array(
|
||||
[[[700, 400], [700, 600], [900, 600], [900, 400]]], dtype=np.float32
|
||||
)
|
||||
|
||||
cameraMatrix = np.array(
|
||||
[[712.0634, 0, 800], [0, 712.540, 500], [0, 0, 1]], dtype=np.float32
|
||||
)
|
||||
distCoeffs = np.array([[0, 0, 0, 0]], dtype=np.float32)
|
||||
x, r, t, e = cv.solvePnPGeneric(
|
||||
obj_points, img_points, cameraMatrix, distCoeffs
|
||||
)
|
||||
if e is None:
|
||||
# noArray() is supported, see https://github.com/opencv/opencv/issues/16049
|
||||
pass
|
||||
else:
|
||||
eDump = cv.utils.dumpInputArray(e)
|
||||
self.assertEqual(eDump, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=1 dims(-1)=2 size(-1)=1x1 type(-1)=CV_32FC1")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
NewOpenCVTests.bootstrap()
|
||||
|
||||
@@ -1009,7 +1009,10 @@ int solvePnPGeneric( InputArray _opoints, InputArray _ipoints,
|
||||
|
||||
if (reprojectionError.needed())
|
||||
{
|
||||
int type = reprojectionError.type();
|
||||
int type = (reprojectionError.fixedType() || !reprojectionError.empty())
|
||||
? reprojectionError.type()
|
||||
: (max(_ipoints.depth(), _opoints.depth()) == CV_64F ? CV_64F : CV_32F);
|
||||
|
||||
reprojectionError.create(solutions, 1, type);
|
||||
CV_CheckType(reprojectionError.type(), type == CV_32FC1 || type == CV_64FC1,
|
||||
"Type of reprojectionError must be CV_32FC1 or CV_64FC1!");
|
||||
|
||||
@@ -2078,6 +2078,10 @@ MulTransposedR(const Mat& srcmat, const Mat& dstmat, const Mat& deltamat, double
|
||||
deltastep = deltastep ? 4 : 0;
|
||||
}
|
||||
|
||||
#if CV_SIMD_64F
|
||||
v_float64x2 v_scale = v_setall_f64(scale);
|
||||
#endif
|
||||
|
||||
if( !delta )
|
||||
for( i = 0; i < size.width; i++, tdst += dststep )
|
||||
{
|
||||
@@ -2086,22 +2090,41 @@ MulTransposedR(const Mat& srcmat, const Mat& dstmat, const Mat& deltamat, double
|
||||
|
||||
for( j = i; j <= size.width - 4; j += 4 )
|
||||
{
|
||||
double s0 = 0, s1 = 0, s2 = 0, s3 = 0;
|
||||
const sT *tsrc = src + j;
|
||||
|
||||
for( k = 0; k < size.height; k++, tsrc += srcstep )
|
||||
#if CV_SIMD_64F
|
||||
if (DataType<sT>::depth == CV_64F && DataType<dT>::depth == CV_64F)
|
||||
{
|
||||
double a = col_buf[k];
|
||||
s0 += a * tsrc[0];
|
||||
s1 += a * tsrc[1];
|
||||
s2 += a * tsrc[2];
|
||||
s3 += a * tsrc[3];
|
||||
}
|
||||
v_float64x2 s0 = v_setzero_f64(), s1 = v_setzero_f64();
|
||||
const double *tsrc = (double*)(src + j);
|
||||
|
||||
tdst[j] = (dT)(s0*scale);
|
||||
tdst[j+1] = (dT)(s1*scale);
|
||||
tdst[j+2] = (dT)(s2*scale);
|
||||
tdst[j+3] = (dT)(s3*scale);
|
||||
for( k = 0; k < size.height; k++, tsrc += srcstep )
|
||||
{
|
||||
v_float64x2 a = v_setall_f64((double)col_buf[k]);
|
||||
s0 += a * v_load(tsrc+0);
|
||||
s1 += a * v_load(tsrc+2);
|
||||
}
|
||||
|
||||
v_store((double*)(tdst+j), s0*v_scale);
|
||||
v_store((double*)(tdst+j+2), s1*v_scale);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
double s0 = 0, s1 = 0, s2 = 0, s3 = 0;
|
||||
const sT *tsrc = src + j;
|
||||
|
||||
for( k = 0; k < size.height; k++, tsrc += srcstep )
|
||||
{
|
||||
double a = col_buf[k];
|
||||
s0 += a * tsrc[0];
|
||||
s1 += a * tsrc[1];
|
||||
s2 += a * tsrc[2];
|
||||
s3 += a * tsrc[3];
|
||||
}
|
||||
|
||||
tdst[j] = (dT)(s0*scale);
|
||||
tdst[j+1] = (dT)(s1*scale);
|
||||
tdst[j+2] = (dT)(s2*scale);
|
||||
tdst[j+3] = (dT)(s3*scale);
|
||||
}
|
||||
}
|
||||
|
||||
for( ; j < size.width; j++ )
|
||||
@@ -2127,23 +2150,45 @@ MulTransposedR(const Mat& srcmat, const Mat& dstmat, const Mat& deltamat, double
|
||||
|
||||
for( j = i; j <= size.width - 4; j += 4 )
|
||||
{
|
||||
double s0 = 0, s1 = 0, s2 = 0, s3 = 0;
|
||||
const sT *tsrc = src + j;
|
||||
const dT *d = delta_buf ? delta_buf : delta + j;
|
||||
|
||||
for( k = 0; k < size.height; k++, tsrc+=srcstep, d+=deltastep )
|
||||
#if CV_SIMD_64F
|
||||
if (DataType<sT>::depth == CV_64F && DataType<dT>::depth == CV_64F)
|
||||
{
|
||||
double a = col_buf[k];
|
||||
s0 += a * (tsrc[0] - d[0]);
|
||||
s1 += a * (tsrc[1] - d[1]);
|
||||
s2 += a * (tsrc[2] - d[2]);
|
||||
s3 += a * (tsrc[3] - d[3]);
|
||||
}
|
||||
v_float64x2 s0 = v_setzero_f64(), s1 = v_setzero_f64();
|
||||
const double *tsrc = (double*)(src + j);
|
||||
const double *d = (double*)(delta_buf ? delta_buf : delta + j);
|
||||
|
||||
tdst[j] = (dT)(s0*scale);
|
||||
tdst[j+1] = (dT)(s1*scale);
|
||||
tdst[j+2] = (dT)(s2*scale);
|
||||
tdst[j+3] = (dT)(s3*scale);
|
||||
for( k = 0; k < size.height; k++, tsrc+=srcstep, d+=deltastep )
|
||||
{
|
||||
v_float64x2 a = v_setall_f64((double)col_buf[k]);
|
||||
s0 += a * (v_load(tsrc+0) - v_load(d+0));
|
||||
s1 += a * (v_load(tsrc+2) - v_load(d+2));
|
||||
}
|
||||
|
||||
v_store((double*)(tdst+j), s0*v_scale);
|
||||
v_store((double*)(tdst+j+2), s1*v_scale);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
|
||||
{
|
||||
double s0 = 0, s1 = 0, s2 = 0, s3 = 0;
|
||||
const sT *tsrc = src + j;
|
||||
const dT *d = delta_buf ? delta_buf : delta + j;
|
||||
|
||||
for( k = 0; k < size.height; k++, tsrc+=srcstep, d+=deltastep )
|
||||
{
|
||||
double a = col_buf[k];
|
||||
s0 += a * (tsrc[0] - d[0]);
|
||||
s1 += a * (tsrc[1] - d[1]);
|
||||
s2 += a * (tsrc[2] - d[2]);
|
||||
s3 += a * (tsrc[3] - d[3]);
|
||||
}
|
||||
|
||||
tdst[j] = (dT)(s0*scale);
|
||||
tdst[j+1] = (dT)(s1*scale);
|
||||
tdst[j+2] = (dT)(s2*scale);
|
||||
tdst[j+3] = (dT)(s3*scale);
|
||||
}
|
||||
}
|
||||
|
||||
for( ; j < size.width; j++ )
|
||||
@@ -2182,10 +2227,25 @@ MulTransposedL(const Mat& srcmat, const Mat& dstmat, const Mat& deltamat, double
|
||||
double s = 0;
|
||||
const sT *tsrc1 = src + i*srcstep;
|
||||
const sT *tsrc2 = src + j*srcstep;
|
||||
#if CV_SIMD_64F
|
||||
if (DataType<sT>::depth == CV_64F && DataType<dT>::depth == CV_64F)
|
||||
{
|
||||
const double *v_tsrc1 = (double *)(tsrc1);
|
||||
const double *v_tsrc2 = (double *)(tsrc2);
|
||||
v_float64x2 v_s = v_setzero_f64();
|
||||
|
||||
for( k = 0; k <= size.width - 4; k += 4 )
|
||||
s += (double)tsrc1[k]*tsrc2[k] + (double)tsrc1[k+1]*tsrc2[k+1] +
|
||||
(double)tsrc1[k+2]*tsrc2[k+2] + (double)tsrc1[k+3]*tsrc2[k+3];
|
||||
for( k = 0; k <= size.width - 4; k += 4 )
|
||||
v_s += (v_load(v_tsrc1+k) * v_load(v_tsrc2+k)) +
|
||||
(v_load(v_tsrc1+k+2) * v_load(v_tsrc2+k+2));
|
||||
s += v_reduce_sum(v_s);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
for( k = 0; k <= size.width - 4; k += 4 )
|
||||
s += (double)tsrc1[k]*tsrc2[k] + (double)tsrc1[k+1]*tsrc2[k+1] +
|
||||
(double)tsrc1[k+2]*tsrc2[k+2] + (double)tsrc1[k+3]*tsrc2[k+3];
|
||||
}
|
||||
for( ; k < size.width; k++ )
|
||||
s += (double)tsrc1[k] * tsrc2[k];
|
||||
tdst[j] = (dT)(s*scale);
|
||||
@@ -2220,11 +2280,30 @@ MulTransposedL(const Mat& srcmat, const Mat& dstmat, const Mat& deltamat, double
|
||||
delta_buf[2] = delta_buf[3] = tdelta2[0];
|
||||
tdelta2 = delta_buf;
|
||||
}
|
||||
for( k = 0; k <= size.width-4; k += 4, tdelta2 += delta_shift )
|
||||
s += (double)row_buf[k]*(tsrc2[k] - tdelta2[0]) +
|
||||
(double)row_buf[k+1]*(tsrc2[k+1] - tdelta2[1]) +
|
||||
(double)row_buf[k+2]*(tsrc2[k+2] - tdelta2[2]) +
|
||||
(double)row_buf[k+3]*(tsrc2[k+3] - tdelta2[3]);
|
||||
#if CV_SIMD_64F
|
||||
if (DataType<sT>::depth == CV_64F && DataType<dT>::depth == CV_64F)
|
||||
{
|
||||
const double *v_tsrc2 = (double *)(tsrc2);
|
||||
const double *v_tdelta2 = (double *)(tdelta2);
|
||||
const double *v_row_buf = (double *)(row_buf);
|
||||
v_float64x2 v_s = v_setzero_f64();
|
||||
|
||||
for( k = 0; k <= size.width - 4; k += 4, v_tdelta2 += delta_shift )
|
||||
v_s += ((v_load(v_tsrc2+k) - v_load(v_tdelta2)) * v_load(v_row_buf+k)) +
|
||||
((v_load(v_tsrc2+k+2) - v_load(v_tdelta2+2)) * v_load(v_row_buf+k+2));
|
||||
s += v_reduce_sum(v_s);
|
||||
|
||||
tdelta2 = (const dT *)(v_tdelta2);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
for( k = 0; k <= size.width-4; k += 4, tdelta2 += delta_shift )
|
||||
s += (double)row_buf[k]*(tsrc2[k] - tdelta2[0]) +
|
||||
(double)row_buf[k+1]*(tsrc2[k+1] - tdelta2[1]) +
|
||||
(double)row_buf[k+2]*(tsrc2[k+2] - tdelta2[2]) +
|
||||
(double)row_buf[k+3]*(tsrc2[k+3] - tdelta2[3]);
|
||||
}
|
||||
for( ; k < size.width; k++, tdelta2++ )
|
||||
s += (double)row_buf[k]*(tsrc2[k] - tdelta2[0]);
|
||||
tdst[j] = (dT)(s*scale);
|
||||
|
||||
@@ -40,7 +40,7 @@ DECLARE_CV_PAUSE
|
||||
#endif
|
||||
#ifndef CV_PAUSE
|
||||
# if defined __GNUC__ && (defined __i386__ || defined __x86_64__)
|
||||
# if !defined(__SSE__)
|
||||
# if !defined(__SSE2__)
|
||||
static inline void cv_non_sse_mm_pause() { __asm__ __volatile__ ("rep; nop"); }
|
||||
# define _mm_pause cv_non_sse_mm_pause
|
||||
# endif
|
||||
|
||||
@@ -250,7 +250,8 @@ CV__DNN_INLINE_NS_BEGIN
|
||||
std::vector<size_t> pads_begin, pads_end;
|
||||
CV_DEPRECATED_EXTERNAL Size kernel, stride, pad;
|
||||
CV_DEPRECATED_EXTERNAL int pad_l, pad_t, pad_r, pad_b;
|
||||
bool globalPooling;
|
||||
bool globalPooling; //!< Flag is true if at least one of the axes is global pooled.
|
||||
std::vector<bool> isGlobalPooling;
|
||||
bool computeMaxIdx;
|
||||
String padMode;
|
||||
bool ceilMode;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#define OPENCV_DNN_VERSION_HPP
|
||||
|
||||
/// Use with major OpenCV version only.
|
||||
#define OPENCV_DNN_API_VERSION 20191202
|
||||
#define OPENCV_DNN_API_VERSION 20200128
|
||||
|
||||
#if !defined CV_DOXYGEN && !defined CV_STATIC_ANALYSIS && !defined CV_DNN_DONT_ADD_INLINE_NS
|
||||
#define CV__DNN_INLINE_NS __CV_CAT(dnn4_v, OPENCV_DNN_API_VERSION)
|
||||
|
||||
@@ -1555,19 +1555,6 @@ public:
|
||||
const int group = numOutput / outGroupCn;
|
||||
|
||||
if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) {
|
||||
if (padMode.empty()) {
|
||||
for (int i = 0; i < adjust_pads.size(); i++) {
|
||||
if (pads_end[i] < adjust_pads[i])
|
||||
return false;
|
||||
}
|
||||
} else if (padMode == "SAME") {
|
||||
for (int i = 0; i < adjust_pads.size(); i++) {
|
||||
if (kernel_size[i] < pads_begin[i] + 1 + adjust_pads[i])
|
||||
return false;
|
||||
}
|
||||
} else if (padMode == "VALID")
|
||||
return false;
|
||||
|
||||
return group == 1;
|
||||
}
|
||||
|
||||
@@ -2334,20 +2321,16 @@ public:
|
||||
ieWeights = std::make_shared<ngraph::op::Constant>(ngraph::element::f32, kernel_shape, newWeights.data);
|
||||
}
|
||||
std::vector<size_t> paddings_end;
|
||||
if (padMode.empty())
|
||||
{
|
||||
for (int i = 0; i < pads_end.size(); i++) {
|
||||
paddings_end.push_back(pads_end[i] - adjust_pads[i]);
|
||||
}
|
||||
}
|
||||
else if (padMode == "SAME")
|
||||
if (padMode == "SAME")
|
||||
{
|
||||
for (int i = 0; i < pads_begin.size(); i++) {
|
||||
paddings_end.push_back(kernel_size[i] - pads_begin[i] - 1 - adjust_pads[i]);
|
||||
}
|
||||
adjust_pads = std::vector<size_t>(pads_begin.size(), 0);
|
||||
} else {
|
||||
paddings_end = pads_end;
|
||||
}
|
||||
ngraph::op::PadType pad_type = padMode == "VALID" ? ngraph::op::PadType::VALID : ngraph::op::PadType::EXPLICIT;
|
||||
|
||||
auto deconv = std::make_shared<ngraph::op::v1::ConvolutionBackpropData>(
|
||||
ieInpNode,
|
||||
@@ -2355,7 +2338,10 @@ public:
|
||||
ngraph::Strides(strides),
|
||||
ngraph::CoordinateDiff(std::vector<std::ptrdiff_t>(pads_begin.begin(), pads_begin.end())),
|
||||
ngraph::CoordinateDiff(std::vector<std::ptrdiff_t>(paddings_end.begin(), paddings_end.end())),
|
||||
ngraph::Strides(dilations));
|
||||
ngraph::Strides(dilations),
|
||||
pad_type,
|
||||
ngraph::CoordinateDiff(std::vector<std::ptrdiff_t>(adjust_pads.begin(), adjust_pads.end())));
|
||||
|
||||
if (hasBias() || fusedBias)
|
||||
{
|
||||
std::vector<size_t> shape(deconv->get_shape().size(), 1);
|
||||
|
||||
@@ -144,26 +144,37 @@ void getStrideAndPadding(const LayerParams ¶ms, std::vector<size_t>& pads_be
|
||||
}
|
||||
}
|
||||
|
||||
void getPoolingKernelParams(const LayerParams ¶ms, std::vector<size_t>& kernel, bool &globalPooling,
|
||||
void getPoolingKernelParams(const LayerParams ¶ms, std::vector<size_t>& kernel, std::vector<bool>& globalPooling,
|
||||
std::vector<size_t>& pads_begin, std::vector<size_t>& pads_end,
|
||||
std::vector<size_t>& strides, cv::String &padMode)
|
||||
{
|
||||
globalPooling = params.has("global_pooling") &&
|
||||
params.get<bool>("global_pooling");
|
||||
bool is_global = params.get<bool>("global_pooling", false);
|
||||
globalPooling.resize(3);
|
||||
globalPooling[0] = params.get<bool>("global_pooling_d", is_global);
|
||||
globalPooling[1] = params.get<bool>("global_pooling_h", is_global);
|
||||
globalPooling[2] = params.get<bool>("global_pooling_w", is_global);
|
||||
|
||||
if (globalPooling)
|
||||
if (globalPooling[0] || globalPooling[1] || globalPooling[2])
|
||||
{
|
||||
util::getStrideAndPadding(params, pads_begin, pads_end, strides, padMode);
|
||||
if(params.has("kernel_h") || params.has("kernel_w") || params.has("kernel_size"))
|
||||
{
|
||||
if ((globalPooling[0] && params.has("kernel_d")) ||
|
||||
(globalPooling[1] && params.has("kernel_h")) ||
|
||||
(globalPooling[2] && params.has("kernel_w")) ||
|
||||
params.has("kernel_size")) {
|
||||
CV_Error(cv::Error::StsBadArg, "In global_pooling mode, kernel_size (or kernel_h and kernel_w) cannot be specified");
|
||||
}
|
||||
for (int i = 0; i < pads_begin.size(); i++) {
|
||||
if (pads_begin[i] != 0 || pads_end[i] != 0)
|
||||
|
||||
kernel.resize(3);
|
||||
kernel[0] = params.get<int>("kernel_d", 1);
|
||||
kernel[1] = params.get<int>("kernel_h", 1);
|
||||
kernel[2] = params.get<int>("kernel_w", 1);
|
||||
|
||||
for (int i = 0, j = globalPooling.size() - pads_begin.size(); i < pads_begin.size(); i++, j++) {
|
||||
if ((pads_begin[i] != 0 || pads_end[i] != 0) && globalPooling[j])
|
||||
CV_Error(cv::Error::StsBadArg, "In global_pooling mode, pads must be = 0");
|
||||
}
|
||||
for (int i = 0; i < strides.size(); i++) {
|
||||
if (strides[i] != 1)
|
||||
for (int i = 0, j = globalPooling.size() - strides.size(); i < strides.size(); i++, j++) {
|
||||
if (strides[i] != 1 && globalPooling[j])
|
||||
CV_Error(cv::Error::StsBadArg, "In global_pooling mode, strides must be = 1");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ void getConvolutionKernelParams(const LayerParams ¶ms, std::vector<size_t>&
|
||||
std::vector<size_t>& pads_end, std::vector<size_t>& strides, std::vector<size_t>& dilations,
|
||||
cv::String &padMode, std::vector<size_t>& adjust_pads);
|
||||
|
||||
void getPoolingKernelParams(const LayerParams ¶ms, std::vector<size_t>& kernel, bool &globalPooling,
|
||||
void getPoolingKernelParams(const LayerParams ¶ms, std::vector<size_t>& kernel, std::vector<bool>& globalPooling,
|
||||
std::vector<size_t>& pads_begin, std::vector<size_t>& pads_end, std::vector<size_t>& strides, cv::String &padMode);
|
||||
|
||||
void getConvPoolOutParams(const std::vector<int>& inp, const std::vector<size_t>& kernel,
|
||||
|
||||
@@ -89,6 +89,7 @@ public:
|
||||
{
|
||||
computeMaxIdx = true;
|
||||
globalPooling = false;
|
||||
isGlobalPooling = std::vector<bool>(3, false);
|
||||
stride = Size(1, 1);
|
||||
pad_t = pad_l = pad_b = pad_r = 0;
|
||||
|
||||
@@ -105,7 +106,8 @@ public:
|
||||
else
|
||||
CV_Error(Error::StsBadArg, "Unknown pooling type \"" + pool + "\"");
|
||||
|
||||
getPoolingKernelParams(params, kernel_size, globalPooling, pads_begin, pads_end, strides, padMode);
|
||||
getPoolingKernelParams(params, kernel_size, isGlobalPooling, pads_begin, pads_end, strides, padMode);
|
||||
globalPooling = isGlobalPooling[0] || isGlobalPooling[1] || isGlobalPooling[2];
|
||||
if (kernel_size.size() == 2) {
|
||||
kernel = Size(kernel_size[1], kernel_size[0]);
|
||||
stride = Size(strides[1], strides[0]);
|
||||
@@ -157,9 +159,14 @@ public:
|
||||
out.push_back(outputs[0].size[i]);
|
||||
}
|
||||
if (globalPooling) {
|
||||
kernel = Size(inp[1], inp[0]);
|
||||
kernel_size = std::vector<size_t>(inp.begin(), inp.end());
|
||||
}
|
||||
std::vector<size_t> finalKernel;
|
||||
for (int i = 0; i < inp.size(); i++) {
|
||||
int idx = isGlobalPooling.size() - inp.size() + i;
|
||||
finalKernel.push_back(isGlobalPooling[idx] ? inp[i] : kernel_size[idx]);
|
||||
}
|
||||
kernel_size = finalKernel;
|
||||
kernel = Size(kernel_size[1], kernel_size[0]);
|
||||
}
|
||||
|
||||
getConvPoolPaddings(inp, kernel_size, strides, padMode, pads_begin, pads_end);
|
||||
if (pads_begin.size() == 2) {
|
||||
@@ -1149,20 +1156,25 @@ virtual Ptr<BackendNode> initNgraph(const std::vector<Ptr<BackendWrapper> >& inp
|
||||
std::vector<int> inpShape(inputs[0].begin() + 2, inputs[0].end());
|
||||
std::vector<int> outShape(inputs[0].begin(), inputs[0].begin() + 2);
|
||||
|
||||
if (globalPooling)
|
||||
{
|
||||
outShape.push_back(1);
|
||||
outShape.push_back(1);
|
||||
std::vector<size_t> local_kernel;
|
||||
if (globalPooling) {
|
||||
for (int i = 0; i < inpShape.size(); i++) {
|
||||
int idx = isGlobalPooling.size() - inpShape.size() + i;
|
||||
local_kernel.push_back(isGlobalPooling[idx] ? inpShape[i] : kernel_size[idx]);
|
||||
}
|
||||
} else {
|
||||
local_kernel = kernel_size;
|
||||
}
|
||||
else if (type == ROI || type == PSROI)
|
||||
|
||||
if (type == ROI || type == PSROI)
|
||||
{
|
||||
outShape.push_back(pooledSize.height);
|
||||
outShape.push_back(pooledSize.width);
|
||||
}
|
||||
else if (padMode.empty())
|
||||
{
|
||||
for (int i = 0; i < kernel_size.size(); i++) {
|
||||
float dst = (float)(inpShape[i] + pads_begin[i] + pads_end[i] - kernel_size[i]) / strides[i];
|
||||
for (int i = 0; i < local_kernel.size(); i++) {
|
||||
float dst = (float)(inpShape[i] + pads_begin[i] + pads_end[i] - local_kernel[i]) / strides[i];
|
||||
outShape.push_back(1 + (ceilMode ? ceil(dst) : floor(dst)));
|
||||
}
|
||||
|
||||
@@ -1177,7 +1189,7 @@ virtual Ptr<BackendNode> initNgraph(const std::vector<Ptr<BackendWrapper> >& inp
|
||||
}
|
||||
else
|
||||
{
|
||||
getConvPoolOutParams(inpShape, kernel_size, strides, padMode, std::vector<size_t>(kernel_size.size(), 1), outShape);
|
||||
getConvPoolOutParams(inpShape, local_kernel, strides, padMode, std::vector<size_t>(local_kernel.size(), 1), outShape);
|
||||
}
|
||||
if (type == ROI)
|
||||
{
|
||||
|
||||
@@ -121,7 +121,8 @@ public:
|
||||
{
|
||||
return backendId == DNN_BACKEND_OPENCV ||
|
||||
backendId == DNN_BACKEND_CUDA ||
|
||||
((backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 || backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) &&
|
||||
(backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && sliceRanges.size() == 1) ||
|
||||
(backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 &&
|
||||
#ifdef HAVE_INF_ENGINE
|
||||
INF_ENGINE_VER_MAJOR_GE(INF_ENGINE_RELEASE_2019R1) &&
|
||||
#endif
|
||||
|
||||
@@ -100,7 +100,8 @@ public:
|
||||
return backendId == DNN_BACKEND_OPENCV ||
|
||||
backendId == DNN_BACKEND_CUDA ||
|
||||
(backendId == DNN_BACKEND_HALIDE && haveHalide() && axisRaw == 1) ||
|
||||
((backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 || backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) && haveInfEngine() && !logSoftMax) ||
|
||||
backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH ||
|
||||
(backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && haveInfEngine() && !logSoftMax) ||
|
||||
(backendId == DNN_BACKEND_VKCOM && haveVulkan());
|
||||
}
|
||||
|
||||
@@ -366,6 +367,9 @@ public:
|
||||
auto& ieInpNode = nodes[0].dynamicCast<InfEngineNgraphNode>()->node;
|
||||
int axis = clamp(axisRaw, ieInpNode->get_shape().size());
|
||||
auto softmax = std::make_shared<ngraph::op::v1::Softmax>(ieInpNode, axis);
|
||||
if (logSoftMax)
|
||||
return Ptr<BackendNode>(new InfEngineNgraphNode(std::make_shared<ngraph::op::v0::Log>(softmax)));
|
||||
|
||||
return Ptr<BackendNode>(new InfEngineNgraphNode(softmax));
|
||||
}
|
||||
#endif // HAVE_DNN_NGRAPH
|
||||
|
||||
@@ -564,11 +564,45 @@ static std::map<std::string, InferenceEngine::InferenceEnginePluginPtr>& getShar
|
||||
return sharedPlugins;
|
||||
}
|
||||
#else
|
||||
InferenceEngine::Core& getCore()
|
||||
static bool init_IE_plugins()
|
||||
{
|
||||
// load and hold IE plugins
|
||||
static InferenceEngine::Core* init_core = new InferenceEngine::Core(); // 'delete' is never called
|
||||
(void)init_core->GetAvailableDevices();
|
||||
return true;
|
||||
}
|
||||
static InferenceEngine::Core& create_IE_Core_instance()
|
||||
{
|
||||
static InferenceEngine::Core core;
|
||||
return core;
|
||||
}
|
||||
static InferenceEngine::Core& create_IE_Core_pointer()
|
||||
{
|
||||
// load and hold IE plugins
|
||||
static InferenceEngine::Core* core = new InferenceEngine::Core(); // 'delete' is never called
|
||||
return *core;
|
||||
}
|
||||
InferenceEngine::Core& getCore()
|
||||
{
|
||||
// to make happy memory leak tools use:
|
||||
// - OPENCV_DNN_INFERENCE_ENGINE_HOLD_PLUGINS=0
|
||||
// - OPENCV_DNN_INFERENCE_ENGINE_CORE_LIFETIME_WORKAROUND=0
|
||||
static bool param_DNN_INFERENCE_ENGINE_HOLD_PLUGINS = utils::getConfigurationParameterBool("OPENCV_DNN_INFERENCE_ENGINE_HOLD_PLUGINS", true);
|
||||
static bool init_IE_plugins_ = param_DNN_INFERENCE_ENGINE_HOLD_PLUGINS && init_IE_plugins(); CV_UNUSED(init_IE_plugins_);
|
||||
|
||||
static bool param_DNN_INFERENCE_ENGINE_CORE_LIFETIME_WORKAROUND =
|
||||
utils::getConfigurationParameterBool("OPENCV_DNN_INFERENCE_ENGINE_CORE_LIFETIME_WORKAROUND",
|
||||
#ifdef _WIN32
|
||||
true
|
||||
#else
|
||||
false
|
||||
#endif
|
||||
);
|
||||
static InferenceEngine::Core& core = param_DNN_INFERENCE_ENGINE_CORE_LIFETIME_WORKAROUND
|
||||
? create_IE_Core_pointer()
|
||||
: create_IE_Core_instance();
|
||||
return core;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(OPENCV_DNN_IE_VPU_TYPE_DEFAULT)
|
||||
|
||||
@@ -1936,20 +1936,22 @@ void TFImporter::populateNet(Net dstNet)
|
||||
}
|
||||
else if (type == "Mean")
|
||||
{
|
||||
// Computes the mean of elements across dimensions of a tensor.
|
||||
// If keepdims is false (default) reduces input_tensor along the dimensions given in axis,
|
||||
// else the reduced dimensions are retained with length 1.
|
||||
// if indices = [1, 2] in NHWC layout we use global pooling: NxCxHxW --Pooling--> NxCx1x1
|
||||
// if keepdims is false we use Flatten after Pooling: out_shape = NxC
|
||||
// if indices = [0] we use a global pooling by indices.
|
||||
// To return correct shape, we use Reshape after Pooling. To determine input shape use Slice for input,
|
||||
// if keepdims is false we use Flatten after Slice.
|
||||
// Example: input_shape = NxCxHxW
|
||||
// determine out shape: NxCxHxW --Slice--> 1xCxHxW
|
||||
// out_shape = 1xCxHxW if keepDims else (1xCxHxW --Flatten--> CxHxW)
|
||||
// global pool: NxCxHxW --Flatten--> Nx(C*H*W) --Reshape--> 1x1xNx(C*H*W) --Pooling--> 1x1x1x(C*H*W) --Reshape--> out_shape
|
||||
|
||||
Mat indices = getTensorContent(getConstBlob(layer, value_id, 1));
|
||||
CV_Assert(indices.type() == CV_32SC1);
|
||||
|
||||
if (indices.total() != 2 || indices.at<int>(0) != 1 || indices.at<int>(1) != 2)
|
||||
CV_Error(Error::StsNotImplemented, "Unsupported mode of reduce_mean operation.");
|
||||
|
||||
layerParams.set("pool", "ave");
|
||||
layerParams.set("global_pooling", true);
|
||||
|
||||
int id = dstNet.addLayer(name, "Pooling", layerParams);
|
||||
layer_id[name] = id;
|
||||
|
||||
connect(layer_id, dstNet, parsePin(layer.input(0)), id, 0);
|
||||
|
||||
// There are two attributes, "keepdims" and a deprecated "keep_dims".
|
||||
bool keepDims = false;
|
||||
if (hasLayerAttr(layer, "keepdims"))
|
||||
@@ -1957,16 +1959,128 @@ void TFImporter::populateNet(Net dstNet)
|
||||
else if (hasLayerAttr(layer, "keep_dims"))
|
||||
keepDims = getLayerAttr(layer, "keep_dims").b();
|
||||
|
||||
if (!keepDims)
|
||||
if (indices.total() == 1 && indices.at<int>(0) == 0)
|
||||
{
|
||||
LayerParams flattenLp;
|
||||
std::string flattenName = name + "/flatten";
|
||||
CV_Assert(layer_id.find(flattenName) == layer_id.end());
|
||||
int flattenId = dstNet.addLayer(flattenName, "Flatten", flattenLp);
|
||||
layer_id[flattenName] = flattenId;
|
||||
connect(layer_id, dstNet, Pin(name), flattenId, 0);
|
||||
connect(layer_id, dstNet, parsePin(layer.input(0)), flattenId, 0);
|
||||
|
||||
LayerParams reshapeLp;
|
||||
std::string reshapeName = name + "/reshape";
|
||||
CV_Assert(layer_id.find(reshapeName) == layer_id.end());
|
||||
reshapeLp.set("axis", 0);
|
||||
reshapeLp.set("num_axes", 1);
|
||||
int newShape[] = {1, 1, -1};
|
||||
reshapeLp.set("dim", DictValue::arrayInt(&newShape[0], 3));
|
||||
|
||||
int reshapeId = dstNet.addLayer(reshapeName, "Reshape", reshapeLp);
|
||||
layer_id[reshapeName] = reshapeId;
|
||||
connect(layer_id, dstNet, Pin(flattenName), reshapeId, 0);
|
||||
|
||||
LayerParams avgLp;
|
||||
std::string avgName = name + "/avg";
|
||||
CV_Assert(layer_id.find(avgName) == layer_id.end());
|
||||
avgLp.set("pool", "ave");
|
||||
// pooling kernel H x 1
|
||||
avgLp.set("global_pooling_h", true);
|
||||
avgLp.set("kernel_w", 1);
|
||||
int avgId = dstNet.addLayer(avgName, "Pooling", avgLp);
|
||||
layer_id[avgName] = avgId;
|
||||
connect(layer_id, dstNet, Pin(reshapeName), avgId, 0);
|
||||
|
||||
LayerParams sliceLp;
|
||||
std::string layerShapeName = name + "/slice";
|
||||
CV_Assert(layer_id.find(layerShapeName) == layer_id.end());
|
||||
sliceLp.set("axis", 0);
|
||||
int begin[] = {0};
|
||||
int size[] = {1};
|
||||
sliceLp.set("begin", DictValue::arrayInt(&begin[0], 1));
|
||||
sliceLp.set("size", DictValue::arrayInt(&size[0], 1));
|
||||
int sliceId = dstNet.addLayer(layerShapeName, "Slice", sliceLp);
|
||||
layer_id[layerShapeName] = sliceId;
|
||||
connect(layer_id, dstNet, Pin(layer.input(0)), sliceId, 0);
|
||||
|
||||
if (!keepDims)
|
||||
{
|
||||
LayerParams squeezeLp;
|
||||
std::string squeezeName = name + "/squeeze";
|
||||
CV_Assert(layer_id.find(squeezeName) == layer_id.end());
|
||||
squeezeLp.set("axis", 0);
|
||||
squeezeLp.set("end_axis", 1);
|
||||
int squeezeId = dstNet.addLayer(squeezeName, "Flatten", squeezeLp);
|
||||
layer_id[squeezeName] = squeezeId;
|
||||
connect(layer_id, dstNet, Pin(layerShapeName), squeezeId, 0);
|
||||
layerShapeName = squeezeName;
|
||||
}
|
||||
|
||||
int id = dstNet.addLayer(name, "Reshape", layerParams);
|
||||
layer_id[name] = id;
|
||||
connect(layer_id, dstNet, Pin(avgName), id, 0);
|
||||
connect(layer_id, dstNet, Pin(layerShapeName), id, 1);
|
||||
} else {
|
||||
if (indices.total() != 2 || indices.at<int>(0) != 1 || indices.at<int>(1) != 2)
|
||||
CV_Error(Error::StsNotImplemented, "Unsupported mode of reduce_mean operation.");
|
||||
|
||||
layerParams.set("pool", "ave");
|
||||
layerParams.set("global_pooling", true);
|
||||
int id = dstNet.addLayer(name, "Pooling", layerParams);
|
||||
layer_id[name] = id;
|
||||
connect(layer_id, dstNet, parsePin(layer.input(0)), id, 0);
|
||||
|
||||
if (!keepDims)
|
||||
{
|
||||
LayerParams flattenLp;
|
||||
std::string flattenName = name + "/flatten";
|
||||
CV_Assert(layer_id.find(flattenName) == layer_id.end());
|
||||
int flattenId = dstNet.addLayer(flattenName, "Flatten", flattenLp);
|
||||
layer_id[flattenName] = flattenId;
|
||||
connect(layer_id, dstNet, Pin(name), flattenId, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (type == "Pack")
|
||||
{
|
||||
// op: tf.stack(list of tensors, axis=0)
|
||||
// Join a list of inputs along a new axis.
|
||||
// The "axis" specifies the index of the new axis in the dimensions of the output.
|
||||
// Example: given a list with "N" tensors of shape (C, H, W):
|
||||
// if axis == 0 then the output tensor will have the shape (N, C, H, W),
|
||||
// if axis == 1 then the output tensor will have the shape (C, N, H, W).
|
||||
CV_Assert(hasLayerAttr(layer, "axis"));
|
||||
int dim = (int)getLayerAttr(layer, "axis").i();
|
||||
if (dim != 0)
|
||||
CV_Error(Error::StsNotImplemented, "Unsupported mode of pack operation.");
|
||||
|
||||
CV_Assert(hasLayerAttr(layer, "N"));
|
||||
int num = (int)getLayerAttr(layer, "N").i();
|
||||
CV_Assert(layer.input_size() == num);
|
||||
std::string base_name = name + "/reshape_";
|
||||
std::vector<int> reshape_ids;
|
||||
for (int i = 0; i < num; i++) {
|
||||
std::ostringstream ss;
|
||||
ss << i;
|
||||
std::string reshape_name = base_name + ss.str();
|
||||
LayerParams reshapeLP;
|
||||
reshapeLP.set("axis", dim);
|
||||
reshapeLP.set("num_axes", 1);
|
||||
int outShape[] = {1, -1};
|
||||
reshapeLP.set("dim", DictValue::arrayInt(&outShape[0], 2));
|
||||
int id = dstNet.addLayer(reshape_name, "Reshape", reshapeLP);
|
||||
layer_id[reshape_name] = id;
|
||||
reshape_ids.push_back(id);
|
||||
connect(layer_id, dstNet, parsePin(layer.input(i)), id, 0);
|
||||
}
|
||||
|
||||
layerParams.set("axis", dim);
|
||||
int id = dstNet.addLayer(name, "Concat", layerParams);
|
||||
layer_id[name] = id;
|
||||
|
||||
for (int li = 0; li < num; li++)
|
||||
dstNet.connect(reshape_ids[li], 0, id, li);
|
||||
}
|
||||
else if (type == "ClipByValue")
|
||||
{
|
||||
// op: "ClipByValue"
|
||||
|
||||
@@ -121,6 +121,13 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
TEST_P(Test_TensorFlow_layers, reduce_mean)
|
||||
{
|
||||
if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
|
||||
applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
|
||||
runTensorFlowNet("global_pool_by_axis");
|
||||
}
|
||||
|
||||
TEST_P(Test_TensorFlow_layers, conv)
|
||||
{
|
||||
runTensorFlowNet("single_conv");
|
||||
|
||||
@@ -9,4 +9,5 @@ ocv_add_dispatched_file(color_yuv SSE2 SSE4_1 AVX2)
|
||||
ocv_add_dispatched_file(median_blur SSE2 SSE4_1 AVX2)
|
||||
ocv_add_dispatched_file(morph SSE2 SSE4_1 AVX2)
|
||||
ocv_add_dispatched_file(smooth SSE2 SSE4_1 AVX2)
|
||||
ocv_add_dispatched_file(sumpixels SSE2 AVX2 AVX512_SKX)
|
||||
ocv_define_module(imgproc opencv_core WRAP java python js)
|
||||
|
||||
+8
-11
@@ -2,14 +2,13 @@
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2019, Intel Corporation, all rights reserved.
|
||||
#include "precomp.hpp"
|
||||
#include "sumpixels.hpp"
|
||||
// Copyright (C) 2019-2020, Intel Corporation, all rights reserved.
|
||||
|
||||
#include "opencv2/core/hal/intrin.hpp"
|
||||
|
||||
namespace cv { namespace hal {
|
||||
CV_CPU_OPTIMIZATION_NAMESPACE_BEGIN
|
||||
|
||||
namespace cv {
|
||||
namespace { // Anonymous namespace to avoid exposing the implementation classes
|
||||
|
||||
//
|
||||
@@ -432,16 +431,14 @@ __m512d IntegralCalculator < 4 > ::calculate_integral(const __m512i src_longs, c
|
||||
|
||||
} // end of anonymous namespace
|
||||
|
||||
namespace opt_AVX512_SKX {
|
||||
|
||||
// This is the implementation for the external callers interface entry point.
|
||||
// It should be the only function called into this file from outside
|
||||
// Any new implementations should be directed from here
|
||||
static
|
||||
void calculate_integral_avx512(const uchar *src, size_t _srcstep,
|
||||
double *sum, size_t _sumstep,
|
||||
double *sqsum, size_t _sqsumstep,
|
||||
int width, int height, int cn)
|
||||
{
|
||||
CV_INSTRUMENT_REGION();
|
||||
|
||||
switch(cn){
|
||||
case 1: {
|
||||
IntegralCalculator< 1 > calculator;
|
||||
@@ -466,5 +463,5 @@ void calculate_integral_avx512(const uchar *src, size_t _srcstep,
|
||||
}
|
||||
|
||||
|
||||
} // end namespace opt_AVX512_SXK
|
||||
} // end namespace cv
|
||||
CV_CPU_OPTIMIZATION_NAMESPACE_END
|
||||
}} // end namespace cv::hal
|
||||
@@ -10,7 +10,7 @@
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008,2019 Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2000-2020 Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Copyright (C) 2014, Itseez Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
@@ -44,210 +44,157 @@
|
||||
#include "precomp.hpp"
|
||||
#include "opencl_kernels_imgproc.hpp"
|
||||
#include "opencv2/core/hal/intrin.hpp"
|
||||
#include "sumpixels.hpp"
|
||||
|
||||
namespace cv
|
||||
{
|
||||
#include "sumpixels.simd.hpp"
|
||||
#include "sumpixels.simd_declarations.hpp" // defines CV_CPU_DISPATCH_MODES_ALL=AVX2,...,BASELINE based on CMakeLists.txt content
|
||||
|
||||
template <typename T, typename ST, typename QT>
|
||||
struct Integral_SIMD
|
||||
|
||||
namespace cv {
|
||||
|
||||
#ifdef HAVE_OPENCL
|
||||
|
||||
static bool ocl_integral( InputArray _src, OutputArray _sum, int sdepth )
|
||||
{
|
||||
bool operator()(const T *, size_t,
|
||||
ST *, size_t,
|
||||
QT *, size_t,
|
||||
ST *, size_t,
|
||||
int, int, int) const
|
||||
{
|
||||
bool doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0;
|
||||
|
||||
if ( (_src.type() != CV_8UC1) ||
|
||||
!(sdepth == CV_32S || sdepth == CV_32F || (doubleSupport && sdepth == CV_64F)))
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
static const int tileSize = 16;
|
||||
|
||||
template <>
|
||||
struct Integral_SIMD<uchar, double, double> {
|
||||
Integral_SIMD() {};
|
||||
String build_opt = format("-D sumT=%s -D LOCAL_SUM_SIZE=%d%s",
|
||||
ocl::typeToStr(sdepth), tileSize,
|
||||
doubleSupport ? " -D DOUBLE_SUPPORT" : "");
|
||||
|
||||
ocl::Kernel kcols("integral_sum_cols", ocl::imgproc::integral_sum_oclsrc, build_opt);
|
||||
if (kcols.empty())
|
||||
return false;
|
||||
|
||||
bool operator()(const uchar *src, size_t _srcstep,
|
||||
double *sum, size_t _sumstep,
|
||||
double *sqsum, size_t _sqsumstep,
|
||||
double *tilted, size_t _tiltedstep,
|
||||
int width, int height, int cn) const
|
||||
UMat src = _src.getUMat();
|
||||
Size src_size = src.size();
|
||||
Size bufsize(((src_size.height + tileSize - 1) / tileSize) * tileSize, ((src_size.width + tileSize - 1) / tileSize) * tileSize);
|
||||
UMat buf(bufsize, sdepth);
|
||||
kcols.args(ocl::KernelArg::ReadOnly(src), ocl::KernelArg::WriteOnlyNoSize(buf));
|
||||
size_t gt = src.cols, lt = tileSize;
|
||||
if (!kcols.run(1, >, <, false))
|
||||
return false;
|
||||
|
||||
ocl::Kernel krows("integral_sum_rows", ocl::imgproc::integral_sum_oclsrc, build_opt);
|
||||
if (krows.empty())
|
||||
return false;
|
||||
|
||||
Size sumsize(src_size.width + 1, src_size.height + 1);
|
||||
_sum.create(sumsize, sdepth);
|
||||
UMat sum = _sum.getUMat();
|
||||
|
||||
krows.args(ocl::KernelArg::ReadOnlyNoSize(buf), ocl::KernelArg::WriteOnly(sum));
|
||||
gt = src.rows;
|
||||
return krows.run(1, >, <, false);
|
||||
}
|
||||
|
||||
static bool ocl_integral( InputArray _src, OutputArray _sum, OutputArray _sqsum, int sdepth, int sqdepth )
|
||||
{
|
||||
bool doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0;
|
||||
|
||||
if ( _src.type() != CV_8UC1 || (!doubleSupport && (sdepth == CV_64F || sqdepth == CV_64F)) )
|
||||
return false;
|
||||
|
||||
static const int tileSize = 16;
|
||||
|
||||
String build_opt = format("-D SUM_SQUARE -D sumT=%s -D sumSQT=%s -D LOCAL_SUM_SIZE=%d%s",
|
||||
ocl::typeToStr(sdepth), ocl::typeToStr(sqdepth),
|
||||
tileSize,
|
||||
doubleSupport ? " -D DOUBLE_SUPPORT" : "");
|
||||
|
||||
ocl::Kernel kcols("integral_sum_cols", ocl::imgproc::integral_sum_oclsrc, build_opt);
|
||||
if (kcols.empty())
|
||||
return false;
|
||||
|
||||
UMat src = _src.getUMat();
|
||||
Size src_size = src.size();
|
||||
Size bufsize(((src_size.height + tileSize - 1) / tileSize) * tileSize, ((src_size.width + tileSize - 1) / tileSize) * tileSize);
|
||||
UMat buf(bufsize, sdepth);
|
||||
UMat buf_sq(bufsize, sqdepth);
|
||||
kcols.args(ocl::KernelArg::ReadOnly(src), ocl::KernelArg::WriteOnlyNoSize(buf), ocl::KernelArg::WriteOnlyNoSize(buf_sq));
|
||||
size_t gt = src.cols, lt = tileSize;
|
||||
if (!kcols.run(1, >, <, false))
|
||||
return false;
|
||||
|
||||
ocl::Kernel krows("integral_sum_rows", ocl::imgproc::integral_sum_oclsrc, build_opt);
|
||||
if (krows.empty())
|
||||
return false;
|
||||
|
||||
Size sumsize(src_size.width + 1, src_size.height + 1);
|
||||
_sum.create(sumsize, sdepth);
|
||||
UMat sum = _sum.getUMat();
|
||||
_sqsum.create(sumsize, sqdepth);
|
||||
UMat sum_sq = _sqsum.getUMat();
|
||||
|
||||
krows.args(ocl::KernelArg::ReadOnlyNoSize(buf), ocl::KernelArg::ReadOnlyNoSize(buf_sq), ocl::KernelArg::WriteOnly(sum), ocl::KernelArg::WriteOnlyNoSize(sum_sq));
|
||||
gt = src.rows;
|
||||
return krows.run(1, >, <, false);
|
||||
}
|
||||
|
||||
#endif // HAVE_OPENCL
|
||||
|
||||
#ifdef HAVE_IPP
|
||||
|
||||
static bool ipp_integral(
|
||||
int depth, int sdepth, int sqdepth,
|
||||
const uchar* src, size_t srcstep,
|
||||
uchar* sum, size_t sumstep,
|
||||
uchar* sqsum, size_t sqsumstep,
|
||||
uchar* tilted, size_t tstep,
|
||||
int width, int height, int cn)
|
||||
{
|
||||
CV_INSTRUMENT_REGION_IPP();
|
||||
|
||||
IppiSize size = {width, height};
|
||||
|
||||
if(cn > 1)
|
||||
return false;
|
||||
if(tilted)
|
||||
{
|
||||
#if CV_TRY_AVX512_SKX
|
||||
CV_UNUSED(_tiltedstep);
|
||||
// TODO: Add support for 1 channel input (WIP)
|
||||
if (CV_CPU_HAS_SUPPORT_AVX512_SKX && !tilted && (cn <= 4)){
|
||||
opt_AVX512_SKX::calculate_integral_avx512(src, _srcstep, sum, _sumstep,
|
||||
sqsum, _sqsumstep, width, height, cn);
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
// Avoid warnings in some builds
|
||||
CV_UNUSED(src); CV_UNUSED(_srcstep); CV_UNUSED(sum); CV_UNUSED(_sumstep);
|
||||
CV_UNUSED(sqsum); CV_UNUSED(_sqsumstep); CV_UNUSED(tilted); CV_UNUSED(_tiltedstep);
|
||||
CV_UNUSED(width); CV_UNUSED(height); CV_UNUSED(cn);
|
||||
#endif
|
||||
CV_UNUSED(tstep);
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#if CV_SIMD && CV_SIMD_WIDTH <= 64
|
||||
|
||||
template <>
|
||||
struct Integral_SIMD<uchar, int, double>
|
||||
{
|
||||
Integral_SIMD() {}
|
||||
|
||||
bool operator()(const uchar * src, size_t _srcstep,
|
||||
int * sum, size_t _sumstep,
|
||||
double * sqsum, size_t,
|
||||
int * tilted, size_t,
|
||||
int width, int height, int cn) const
|
||||
if(!sqsum)
|
||||
{
|
||||
if (sqsum || tilted || cn != 1)
|
||||
if(depth == CV_8U && sdepth == CV_32S)
|
||||
return CV_INSTRUMENT_FUN_IPP(ippiIntegral_8u32s_C1R, (const Ipp8u*)src, (int)srcstep, (Ipp32s*)sum, (int)sumstep, size, 0) >= 0;
|
||||
else if(depth == CV_8UC1 && sdepth == CV_32F)
|
||||
return CV_INSTRUMENT_FUN_IPP(ippiIntegral_8u32f_C1R, (const Ipp8u*)src, (int)srcstep, (Ipp32f*)sum, (int)sumstep, size, 0) >= 0;
|
||||
else if(depth == CV_32FC1 && sdepth == CV_32F)
|
||||
return CV_INSTRUMENT_FUN_IPP(ippiIntegral_32f_C1R, (const Ipp32f*)src, (int)srcstep, (Ipp32f*)sum, (int)sumstep, size) >= 0;
|
||||
else
|
||||
return false;
|
||||
|
||||
// the first iteration
|
||||
memset(sum, 0, (width + 1) * sizeof(int));
|
||||
|
||||
// the others
|
||||
for (int i = 0; i < height; ++i)
|
||||
{
|
||||
const uchar * src_row = src + _srcstep * i;
|
||||
int * prev_sum_row = (int *)((uchar *)sum + _sumstep * i) + 1;
|
||||
int * sum_row = (int *)((uchar *)sum + _sumstep * (i + 1)) + 1;
|
||||
|
||||
sum_row[-1] = 0;
|
||||
|
||||
v_int32 prev = vx_setzero_s32();
|
||||
int j = 0;
|
||||
for ( ; j + v_uint16::nlanes <= width; j += v_uint16::nlanes)
|
||||
{
|
||||
v_int16 el8 = v_reinterpret_as_s16(vx_load_expand(src_row + j));
|
||||
v_int32 el4l, el4h;
|
||||
#if CV_AVX2 && CV_SIMD_WIDTH == 32
|
||||
__m256i vsum = _mm256_add_epi16(el8.val, _mm256_slli_si256(el8.val, 2));
|
||||
vsum = _mm256_add_epi16(vsum, _mm256_slli_si256(vsum, 4));
|
||||
vsum = _mm256_add_epi16(vsum, _mm256_slli_si256(vsum, 8));
|
||||
__m256i shmask = _mm256_set1_epi32(7);
|
||||
el4l.val = _mm256_add_epi32(_mm256_cvtepi16_epi32(_v256_extract_low(vsum)), prev.val);
|
||||
el4h.val = _mm256_add_epi32(_mm256_cvtepi16_epi32(_v256_extract_high(vsum)), _mm256_permutevar8x32_epi32(el4l.val, shmask));
|
||||
prev.val = _mm256_permutevar8x32_epi32(el4h.val, shmask);
|
||||
#else
|
||||
el8 += v_rotate_left<1>(el8);
|
||||
el8 += v_rotate_left<2>(el8);
|
||||
#if CV_SIMD_WIDTH >= 32
|
||||
el8 += v_rotate_left<4>(el8);
|
||||
#if CV_SIMD_WIDTH == 64
|
||||
el8 += v_rotate_left<8>(el8);
|
||||
#endif
|
||||
#endif
|
||||
v_expand(el8, el4l, el4h);
|
||||
el4l += prev;
|
||||
el4h += el4l;
|
||||
|
||||
prev = v_broadcast_element<v_int32::nlanes - 1>(el4h);
|
||||
#endif
|
||||
v_store(sum_row + j , el4l + vx_load(prev_sum_row + j ));
|
||||
v_store(sum_row + j + v_int32::nlanes, el4h + vx_load(prev_sum_row + j + v_int32::nlanes));
|
||||
}
|
||||
|
||||
for (int v = sum_row[j - 1] - prev_sum_row[j - 1]; j < width; ++j)
|
||||
sum_row[j] = (v += src_row[j]) + prev_sum_row[j];
|
||||
}
|
||||
vx_cleanup();
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Integral_SIMD<uchar, float, double>
|
||||
{
|
||||
Integral_SIMD() {}
|
||||
|
||||
bool operator()(const uchar * src, size_t _srcstep,
|
||||
float * sum, size_t _sumstep,
|
||||
double * sqsum, size_t,
|
||||
float * tilted, size_t,
|
||||
int width, int height, int cn) const
|
||||
else
|
||||
{
|
||||
if (sqsum || tilted || cn != 1)
|
||||
if(depth == CV_8U && sdepth == CV_32S && sqdepth == CV_32S)
|
||||
return CV_INSTRUMENT_FUN_IPP(ippiSqrIntegral_8u32s_C1R, (const Ipp8u*)src, (int)srcstep, (Ipp32s*)sum, (int)sumstep, (Ipp32s*)sqsum, (int)sqsumstep, size, 0, 0) >= 0;
|
||||
else if(depth == CV_8U && sdepth == CV_32S && sqdepth == CV_64F)
|
||||
return CV_INSTRUMENT_FUN_IPP(ippiSqrIntegral_8u32s64f_C1R, (const Ipp8u*)src, (int)srcstep, (Ipp32s*)sum, (int)sumstep, (Ipp64f*)sqsum, (int)sqsumstep, size, 0, 0) >= 0;
|
||||
else if(depth == CV_8U && sdepth == CV_32F && sqdepth == CV_64F)
|
||||
return CV_INSTRUMENT_FUN_IPP(ippiSqrIntegral_8u32f64f_C1R, (const Ipp8u*)src, (int)srcstep, (Ipp32f*)sum, (int)sumstep, (Ipp64f*)sqsum, (int)sqsumstep, size, 0, 0) >= 0;
|
||||
else
|
||||
return false;
|
||||
|
||||
// the first iteration
|
||||
memset(sum, 0, (width + 1) * sizeof(int));
|
||||
|
||||
// the others
|
||||
for (int i = 0; i < height; ++i)
|
||||
{
|
||||
const uchar * src_row = src + _srcstep * i;
|
||||
float * prev_sum_row = (float *)((uchar *)sum + _sumstep * i) + 1;
|
||||
float * sum_row = (float *)((uchar *)sum + _sumstep * (i + 1)) + 1;
|
||||
|
||||
sum_row[-1] = 0;
|
||||
|
||||
v_float32 prev = vx_setzero_f32();
|
||||
int j = 0;
|
||||
for (; j + v_uint16::nlanes <= width; j += v_uint16::nlanes)
|
||||
{
|
||||
v_int16 el8 = v_reinterpret_as_s16(vx_load_expand(src_row + j));
|
||||
v_float32 el4l, el4h;
|
||||
#if CV_AVX2 && CV_SIMD_WIDTH == 32
|
||||
__m256i vsum = _mm256_add_epi16(el8.val, _mm256_slli_si256(el8.val, 2));
|
||||
vsum = _mm256_add_epi16(vsum, _mm256_slli_si256(vsum, 4));
|
||||
vsum = _mm256_add_epi16(vsum, _mm256_slli_si256(vsum, 8));
|
||||
__m256i shmask = _mm256_set1_epi32(7);
|
||||
el4l.val = _mm256_add_ps(_mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_v256_extract_low(vsum))), prev.val);
|
||||
el4h.val = _mm256_add_ps(_mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_v256_extract_high(vsum))), _mm256_permutevar8x32_ps(el4l.val, shmask));
|
||||
prev.val = _mm256_permutevar8x32_ps(el4h.val, shmask);
|
||||
#else
|
||||
el8 += v_rotate_left<1>(el8);
|
||||
el8 += v_rotate_left<2>(el8);
|
||||
#if CV_SIMD_WIDTH >= 32
|
||||
el8 += v_rotate_left<4>(el8);
|
||||
#if CV_SIMD_WIDTH == 64
|
||||
el8 += v_rotate_left<8>(el8);
|
||||
#endif
|
||||
#endif
|
||||
v_int32 el4li, el4hi;
|
||||
v_expand(el8, el4li, el4hi);
|
||||
el4l = v_cvt_f32(el4li) + prev;
|
||||
el4h = v_cvt_f32(el4hi) + el4l;
|
||||
|
||||
prev = v_broadcast_element<v_float32::nlanes - 1>(el4h);
|
||||
#endif
|
||||
v_store(sum_row + j , el4l + vx_load(prev_sum_row + j ));
|
||||
v_store(sum_row + j + v_float32::nlanes, el4h + vx_load(prev_sum_row + j + v_float32::nlanes));
|
||||
}
|
||||
|
||||
for (float v = sum_row[j - 1] - prev_sum_row[j - 1]; j < width; ++j)
|
||||
sum_row[j] = (v += src_row[j]) + prev_sum_row[j];
|
||||
}
|
||||
vx_cleanup();
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif // HAVE_IPP
|
||||
|
||||
template<typename T, typename ST, typename QT>
|
||||
namespace hal {
|
||||
|
||||
template<typename T, typename ST, typename QT> static
|
||||
void integral_( const T* src, size_t _srcstep, ST* sum, size_t _sumstep,
|
||||
QT* sqsum, size_t _sqsumstep, ST* tilted, size_t _tiltedstep,
|
||||
int width, int height, int cn )
|
||||
{
|
||||
int x, y, k;
|
||||
|
||||
if (Integral_SIMD<T, ST, QT>()(src, _srcstep,
|
||||
sum, _sumstep,
|
||||
sqsum, _sqsumstep,
|
||||
tilted, _tiltedstep,
|
||||
width, height, cn))
|
||||
return;
|
||||
|
||||
int srcstep = (int)(_srcstep/sizeof(T));
|
||||
int sumstep = (int)(_sumstep/sizeof(ST));
|
||||
int tiltedstep = (int)(_tiltedstep/sizeof(ST));
|
||||
@@ -401,157 +348,36 @@ void integral_( const T* src, size_t _srcstep, ST* sum, size_t _sumstep,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#ifdef HAVE_OPENCL
|
||||
|
||||
static bool ocl_integral( InputArray _src, OutputArray _sum, int sdepth )
|
||||
static bool integral_SIMD(
|
||||
int depth, int sdepth, int sqdepth,
|
||||
const uchar* src, size_t srcstep,
|
||||
uchar* sum, size_t sumstep,
|
||||
uchar* sqsum, size_t sqsumstep,
|
||||
uchar* tilted, size_t tstep,
|
||||
int width, int height, int cn)
|
||||
{
|
||||
bool doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0;
|
||||
CV_INSTRUMENT_REGION();
|
||||
|
||||
if ( (_src.type() != CV_8UC1) ||
|
||||
!(sdepth == CV_32S || sdepth == CV_32F || (doubleSupport && sdepth == CV_64F)))
|
||||
return false;
|
||||
|
||||
static const int tileSize = 16;
|
||||
|
||||
String build_opt = format("-D sumT=%s -D LOCAL_SUM_SIZE=%d%s",
|
||||
ocl::typeToStr(sdepth), tileSize,
|
||||
doubleSupport ? " -D DOUBLE_SUPPORT" : "");
|
||||
|
||||
ocl::Kernel kcols("integral_sum_cols", ocl::imgproc::integral_sum_oclsrc, build_opt);
|
||||
if (kcols.empty())
|
||||
return false;
|
||||
|
||||
UMat src = _src.getUMat();
|
||||
Size src_size = src.size();
|
||||
Size bufsize(((src_size.height + tileSize - 1) / tileSize) * tileSize, ((src_size.width + tileSize - 1) / tileSize) * tileSize);
|
||||
UMat buf(bufsize, sdepth);
|
||||
kcols.args(ocl::KernelArg::ReadOnly(src), ocl::KernelArg::WriteOnlyNoSize(buf));
|
||||
size_t gt = src.cols, lt = tileSize;
|
||||
if (!kcols.run(1, >, <, false))
|
||||
return false;
|
||||
|
||||
ocl::Kernel krows("integral_sum_rows", ocl::imgproc::integral_sum_oclsrc, build_opt);
|
||||
if (krows.empty())
|
||||
return false;
|
||||
|
||||
Size sumsize(src_size.width + 1, src_size.height + 1);
|
||||
_sum.create(sumsize, sdepth);
|
||||
UMat sum = _sum.getUMat();
|
||||
|
||||
krows.args(ocl::KernelArg::ReadOnlyNoSize(buf), ocl::KernelArg::WriteOnly(sum));
|
||||
gt = src.rows;
|
||||
return krows.run(1, >, <, false);
|
||||
CV_CPU_DISPATCH(integral_SIMD, (depth, sdepth, sqdepth, src, srcstep, sum, sumstep, sqsum, sqsumstep, tilted, tstep, width, height, cn),
|
||||
CV_CPU_DISPATCH_MODES_ALL);
|
||||
}
|
||||
|
||||
static bool ocl_integral( InputArray _src, OutputArray _sum, OutputArray _sqsum, int sdepth, int sqdepth )
|
||||
void integral(
|
||||
int depth, int sdepth, int sqdepth,
|
||||
const uchar* src, size_t srcstep,
|
||||
uchar* sum, size_t sumstep,
|
||||
uchar* sqsum, size_t sqsumstep,
|
||||
uchar* tilted, size_t tstep,
|
||||
int width, int height, int cn)
|
||||
{
|
||||
bool doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0;
|
||||
CV_INSTRUMENT_REGION();
|
||||
|
||||
if ( _src.type() != CV_8UC1 || (!doubleSupport && (sdepth == CV_64F || sqdepth == CV_64F)) )
|
||||
return false;
|
||||
|
||||
static const int tileSize = 16;
|
||||
|
||||
String build_opt = format("-D SUM_SQUARE -D sumT=%s -D sumSQT=%s -D LOCAL_SUM_SIZE=%d%s",
|
||||
ocl::typeToStr(sdepth), ocl::typeToStr(sqdepth),
|
||||
tileSize,
|
||||
doubleSupport ? " -D DOUBLE_SUPPORT" : "");
|
||||
|
||||
ocl::Kernel kcols("integral_sum_cols", ocl::imgproc::integral_sum_oclsrc, build_opt);
|
||||
if (kcols.empty())
|
||||
return false;
|
||||
|
||||
UMat src = _src.getUMat();
|
||||
Size src_size = src.size();
|
||||
Size bufsize(((src_size.height + tileSize - 1) / tileSize) * tileSize, ((src_size.width + tileSize - 1) / tileSize) * tileSize);
|
||||
UMat buf(bufsize, sdepth);
|
||||
UMat buf_sq(bufsize, sqdepth);
|
||||
kcols.args(ocl::KernelArg::ReadOnly(src), ocl::KernelArg::WriteOnlyNoSize(buf), ocl::KernelArg::WriteOnlyNoSize(buf_sq));
|
||||
size_t gt = src.cols, lt = tileSize;
|
||||
if (!kcols.run(1, >, <, false))
|
||||
return false;
|
||||
|
||||
ocl::Kernel krows("integral_sum_rows", ocl::imgproc::integral_sum_oclsrc, build_opt);
|
||||
if (krows.empty())
|
||||
return false;
|
||||
|
||||
Size sumsize(src_size.width + 1, src_size.height + 1);
|
||||
_sum.create(sumsize, sdepth);
|
||||
UMat sum = _sum.getUMat();
|
||||
_sqsum.create(sumsize, sqdepth);
|
||||
UMat sum_sq = _sqsum.getUMat();
|
||||
|
||||
krows.args(ocl::KernelArg::ReadOnlyNoSize(buf), ocl::KernelArg::ReadOnlyNoSize(buf_sq), ocl::KernelArg::WriteOnly(sum), ocl::KernelArg::WriteOnlyNoSize(sum_sq));
|
||||
gt = src.rows;
|
||||
return krows.run(1, >, <, false);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
#if defined(HAVE_IPP)
|
||||
namespace cv
|
||||
{
|
||||
static bool ipp_integral(
|
||||
int depth, int sdepth, int sqdepth,
|
||||
const uchar* src, size_t srcstep,
|
||||
uchar* sum, size_t sumstep,
|
||||
uchar* sqsum, size_t sqsumstep,
|
||||
uchar* tilted, size_t tstep,
|
||||
int width, int height, int cn)
|
||||
{
|
||||
CV_INSTRUMENT_REGION_IPP();
|
||||
|
||||
IppiSize size = {width, height};
|
||||
|
||||
if(cn > 1)
|
||||
return false;
|
||||
if(tilted)
|
||||
{
|
||||
CV_UNUSED(tstep);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!sqsum)
|
||||
{
|
||||
if(depth == CV_8U && sdepth == CV_32S)
|
||||
return CV_INSTRUMENT_FUN_IPP(ippiIntegral_8u32s_C1R, (const Ipp8u*)src, (int)srcstep, (Ipp32s*)sum, (int)sumstep, size, 0) >= 0;
|
||||
else if(depth == CV_8UC1 && sdepth == CV_32F)
|
||||
return CV_INSTRUMENT_FUN_IPP(ippiIntegral_8u32f_C1R, (const Ipp8u*)src, (int)srcstep, (Ipp32f*)sum, (int)sumstep, size, 0) >= 0;
|
||||
else if(depth == CV_32FC1 && sdepth == CV_32F)
|
||||
return CV_INSTRUMENT_FUN_IPP(ippiIntegral_32f_C1R, (const Ipp32f*)src, (int)srcstep, (Ipp32f*)sum, (int)sumstep, size) >= 0;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(depth == CV_8U && sdepth == CV_32S && sqdepth == CV_32S)
|
||||
return CV_INSTRUMENT_FUN_IPP(ippiSqrIntegral_8u32s_C1R, (const Ipp8u*)src, (int)srcstep, (Ipp32s*)sum, (int)sumstep, (Ipp32s*)sqsum, (int)sqsumstep, size, 0, 0) >= 0;
|
||||
else if(depth == CV_8U && sdepth == CV_32S && sqdepth == CV_64F)
|
||||
return CV_INSTRUMENT_FUN_IPP(ippiSqrIntegral_8u32s64f_C1R, (const Ipp8u*)src, (int)srcstep, (Ipp32s*)sum, (int)sumstep, (Ipp64f*)sqsum, (int)sqsumstep, size, 0, 0) >= 0;
|
||||
else if(depth == CV_8U && sdepth == CV_32F && sqdepth == CV_64F)
|
||||
return CV_INSTRUMENT_FUN_IPP(ippiSqrIntegral_8u32f64f_C1R, (const Ipp8u*)src, (int)srcstep, (Ipp32f*)sum, (int)sumstep, (Ipp64f*)sqsum, (int)sqsumstep, size, 0, 0) >= 0;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace cv { namespace hal {
|
||||
|
||||
void integral(int depth, int sdepth, int sqdepth,
|
||||
const uchar* src, size_t srcstep,
|
||||
uchar* sum, size_t sumstep,
|
||||
uchar* sqsum, size_t sqsumstep,
|
||||
uchar* tilted, size_t tstep,
|
||||
int width, int height, int cn)
|
||||
{
|
||||
CALL_HAL(integral, cv_hal_integral, depth, sdepth, sqdepth, src, srcstep, sum, sumstep, sqsum, sqsumstep, tilted, tstep, width, height, cn);
|
||||
CV_IPP_RUN_FAST(ipp_integral(depth, sdepth, sqdepth, src, srcstep, sum, sumstep, sqsum, sqsumstep, tilted, tstep, width, height, cn));
|
||||
|
||||
if (integral_SIMD(depth, sdepth, sqdepth, src, srcstep, sum, sumstep, sqsum, sqsumstep, tilted, tstep, width, height, cn))
|
||||
return;
|
||||
|
||||
#define ONE_CALL(A, B, C) integral_<A, B, C>((const A*)src, srcstep, (B*)sum, sumstep, (C*)sqsum, sqsumstep, (B*)tilted, tstep, width, height, cn)
|
||||
|
||||
if( depth == CV_8U && sdepth == CV_32S && sqdepth == CV_64F )
|
||||
@@ -579,14 +405,14 @@ void integral(int depth, int sdepth, int sqdepth,
|
||||
else if( depth == CV_64F && sdepth == CV_64F && sqdepth == CV_64F )
|
||||
ONE_CALL(double, double, double);
|
||||
else
|
||||
CV_Error( CV_StsUnsupportedFormat, "" );
|
||||
CV_Error(Error::StsUnsupportedFormat, "");
|
||||
|
||||
#undef ONE_CALL
|
||||
}
|
||||
|
||||
}} // cv::hal::
|
||||
} // namespace hal
|
||||
|
||||
void cv::integral( InputArray _src, OutputArray _sum, OutputArray _sqsum, OutputArray _tilted, int sdepth, int sqdepth )
|
||||
void integral(InputArray _src, OutputArray _sum, OutputArray _sqsum, OutputArray _tilted, int sdepth, int sqdepth )
|
||||
{
|
||||
CV_INSTRUMENT_REGION();
|
||||
|
||||
@@ -624,20 +450,21 @@ void cv::integral( InputArray _src, OutputArray _sum, OutputArray _sqsum, Output
|
||||
src.cols, src.rows, cn);
|
||||
}
|
||||
|
||||
void cv::integral( InputArray src, OutputArray sum, int sdepth )
|
||||
void integral( InputArray src, OutputArray sum, int sdepth )
|
||||
{
|
||||
CV_INSTRUMENT_REGION();
|
||||
|
||||
integral( src, sum, noArray(), noArray(), sdepth );
|
||||
}
|
||||
|
||||
void cv::integral( InputArray src, OutputArray sum, OutputArray sqsum, int sdepth, int sqdepth )
|
||||
void integral( InputArray src, OutputArray sum, OutputArray sqsum, int sdepth, int sqdepth )
|
||||
{
|
||||
CV_INSTRUMENT_REGION();
|
||||
|
||||
integral( src, sum, sqsum, noArray(), sdepth, sqdepth );
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
CV_IMPL void
|
||||
cvIntegral( const CvArr* image, CvArr* sumImage,
|
||||
@@ -1,25 +0,0 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2019, Intel Corporation, all rights reserved.
|
||||
#ifndef OPENCV_IMGPROC_SUM_PIXELS_HPP
|
||||
#define OPENCV_IMGPROC_SUM_PIXELS_HPP
|
||||
|
||||
namespace cv
|
||||
{
|
||||
|
||||
namespace opt_AVX512_SKX
|
||||
{
|
||||
#if CV_TRY_AVX512_SKX
|
||||
void calculate_integral_avx512(
|
||||
const uchar *src, size_t _srcstep,
|
||||
double *sum, size_t _sumstep,
|
||||
double *sqsum, size_t _sqsumstep,
|
||||
int width, int height, int cn);
|
||||
|
||||
#endif
|
||||
} // end namespace opt_AVX512_SKX
|
||||
} // end namespace cv
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,288 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2020 Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Copyright (C) 2014, Itseez Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "opencv2/core/hal/intrin.hpp"
|
||||
|
||||
#if CV_AVX512_SKX
|
||||
#include "sumpixels.avx512_skx.hpp"
|
||||
#endif
|
||||
|
||||
namespace cv { namespace hal {
|
||||
CV_CPU_OPTIMIZATION_NAMESPACE_BEGIN
|
||||
|
||||
// forward declarations
|
||||
bool integral_SIMD(
|
||||
int depth, int sdepth, int sqdepth,
|
||||
const uchar* src, size_t srcstep,
|
||||
uchar* sum, size_t sumstep,
|
||||
uchar* sqsum, size_t sqsumstep,
|
||||
uchar* tilted, size_t tstep,
|
||||
int width, int height, int cn);
|
||||
|
||||
#ifndef CV_CPU_OPTIMIZATION_DECLARATIONS_ONLY
|
||||
namespace {
|
||||
|
||||
template <typename T, typename ST, typename QT>
|
||||
struct Integral_SIMD
|
||||
{
|
||||
bool operator()(const T *, size_t,
|
||||
ST *, size_t,
|
||||
QT *, size_t,
|
||||
ST *, size_t,
|
||||
int, int, int) const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
#if CV_AVX512_SKX
|
||||
template <>
|
||||
struct Integral_SIMD<uchar, double, double> {
|
||||
Integral_SIMD() {};
|
||||
|
||||
|
||||
bool operator()(const uchar *src, size_t _srcstep,
|
||||
double *sum, size_t _sumstep,
|
||||
double *sqsum, size_t _sqsumstep,
|
||||
double *tilted, size_t _tiltedstep,
|
||||
int width, int height, int cn) const
|
||||
{
|
||||
CV_UNUSED(_tiltedstep);
|
||||
// TODO: Add support for 1 channel input (WIP)
|
||||
if (!tilted && (cn <= 4))
|
||||
{
|
||||
calculate_integral_avx512(src, _srcstep, sum, _sumstep,
|
||||
sqsum, _sqsumstep, width, height, cn);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
#endif
|
||||
|
||||
#if CV_SIMD && CV_SIMD_WIDTH <= 64
|
||||
|
||||
template <>
|
||||
struct Integral_SIMD<uchar, int, double>
|
||||
{
|
||||
Integral_SIMD() {}
|
||||
|
||||
bool operator()(const uchar * src, size_t _srcstep,
|
||||
int * sum, size_t _sumstep,
|
||||
double * sqsum, size_t,
|
||||
int * tilted, size_t,
|
||||
int width, int height, int cn) const
|
||||
{
|
||||
if (sqsum || tilted || cn != 1)
|
||||
return false;
|
||||
|
||||
// the first iteration
|
||||
memset(sum, 0, (width + 1) * sizeof(int));
|
||||
|
||||
// the others
|
||||
for (int i = 0; i < height; ++i)
|
||||
{
|
||||
const uchar * src_row = src + _srcstep * i;
|
||||
int * prev_sum_row = (int *)((uchar *)sum + _sumstep * i) + 1;
|
||||
int * sum_row = (int *)((uchar *)sum + _sumstep * (i + 1)) + 1;
|
||||
|
||||
sum_row[-1] = 0;
|
||||
|
||||
v_int32 prev = vx_setzero_s32();
|
||||
int j = 0;
|
||||
for ( ; j + v_uint16::nlanes <= width; j += v_uint16::nlanes)
|
||||
{
|
||||
v_int16 el8 = v_reinterpret_as_s16(vx_load_expand(src_row + j));
|
||||
v_int32 el4l, el4h;
|
||||
#if CV_AVX2 && CV_SIMD_WIDTH == 32
|
||||
__m256i vsum = _mm256_add_epi16(el8.val, _mm256_slli_si256(el8.val, 2));
|
||||
vsum = _mm256_add_epi16(vsum, _mm256_slli_si256(vsum, 4));
|
||||
vsum = _mm256_add_epi16(vsum, _mm256_slli_si256(vsum, 8));
|
||||
__m256i shmask = _mm256_set1_epi32(7);
|
||||
el4l.val = _mm256_add_epi32(_mm256_cvtepi16_epi32(_v256_extract_low(vsum)), prev.val);
|
||||
el4h.val = _mm256_add_epi32(_mm256_cvtepi16_epi32(_v256_extract_high(vsum)), _mm256_permutevar8x32_epi32(el4l.val, shmask));
|
||||
prev.val = _mm256_permutevar8x32_epi32(el4h.val, shmask);
|
||||
#else
|
||||
el8 += v_rotate_left<1>(el8);
|
||||
el8 += v_rotate_left<2>(el8);
|
||||
#if CV_SIMD_WIDTH >= 32
|
||||
el8 += v_rotate_left<4>(el8);
|
||||
#if CV_SIMD_WIDTH == 64
|
||||
el8 += v_rotate_left<8>(el8);
|
||||
#endif
|
||||
#endif
|
||||
v_expand(el8, el4l, el4h);
|
||||
el4l += prev;
|
||||
el4h += el4l;
|
||||
|
||||
prev = v_broadcast_element<v_int32::nlanes - 1>(el4h);
|
||||
#endif
|
||||
v_store(sum_row + j , el4l + vx_load(prev_sum_row + j ));
|
||||
v_store(sum_row + j + v_int32::nlanes, el4h + vx_load(prev_sum_row + j + v_int32::nlanes));
|
||||
}
|
||||
|
||||
for (int v = sum_row[j - 1] - prev_sum_row[j - 1]; j < width; ++j)
|
||||
sum_row[j] = (v += src_row[j]) + prev_sum_row[j];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Integral_SIMD<uchar, float, double>
|
||||
{
|
||||
Integral_SIMD() {}
|
||||
|
||||
bool operator()(const uchar * src, size_t _srcstep,
|
||||
float * sum, size_t _sumstep,
|
||||
double * sqsum, size_t,
|
||||
float * tilted, size_t,
|
||||
int width, int height, int cn) const
|
||||
{
|
||||
if (sqsum || tilted || cn != 1)
|
||||
return false;
|
||||
|
||||
// the first iteration
|
||||
memset(sum, 0, (width + 1) * sizeof(int));
|
||||
|
||||
// the others
|
||||
for (int i = 0; i < height; ++i)
|
||||
{
|
||||
const uchar * src_row = src + _srcstep * i;
|
||||
float * prev_sum_row = (float *)((uchar *)sum + _sumstep * i) + 1;
|
||||
float * sum_row = (float *)((uchar *)sum + _sumstep * (i + 1)) + 1;
|
||||
|
||||
sum_row[-1] = 0;
|
||||
|
||||
v_float32 prev = vx_setzero_f32();
|
||||
int j = 0;
|
||||
for (; j + v_uint16::nlanes <= width; j += v_uint16::nlanes)
|
||||
{
|
||||
v_int16 el8 = v_reinterpret_as_s16(vx_load_expand(src_row + j));
|
||||
v_float32 el4l, el4h;
|
||||
#if CV_AVX2 && CV_SIMD_WIDTH == 32
|
||||
__m256i vsum = _mm256_add_epi16(el8.val, _mm256_slli_si256(el8.val, 2));
|
||||
vsum = _mm256_add_epi16(vsum, _mm256_slli_si256(vsum, 4));
|
||||
vsum = _mm256_add_epi16(vsum, _mm256_slli_si256(vsum, 8));
|
||||
__m256i shmask = _mm256_set1_epi32(7);
|
||||
el4l.val = _mm256_add_ps(_mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_v256_extract_low(vsum))), prev.val);
|
||||
el4h.val = _mm256_add_ps(_mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_v256_extract_high(vsum))), _mm256_permutevar8x32_ps(el4l.val, shmask));
|
||||
prev.val = _mm256_permutevar8x32_ps(el4h.val, shmask);
|
||||
#else
|
||||
el8 += v_rotate_left<1>(el8);
|
||||
el8 += v_rotate_left<2>(el8);
|
||||
#if CV_SIMD_WIDTH >= 32
|
||||
el8 += v_rotate_left<4>(el8);
|
||||
#if CV_SIMD_WIDTH == 64
|
||||
el8 += v_rotate_left<8>(el8);
|
||||
#endif
|
||||
#endif
|
||||
v_int32 el4li, el4hi;
|
||||
v_expand(el8, el4li, el4hi);
|
||||
el4l = v_cvt_f32(el4li) + prev;
|
||||
el4h = v_cvt_f32(el4hi) + el4l;
|
||||
|
||||
prev = v_broadcast_element<v_float32::nlanes - 1>(el4h);
|
||||
#endif
|
||||
v_store(sum_row + j , el4l + vx_load(prev_sum_row + j ));
|
||||
v_store(sum_row + j + v_float32::nlanes, el4h + vx_load(prev_sum_row + j + v_float32::nlanes));
|
||||
}
|
||||
|
||||
for (float v = sum_row[j - 1] - prev_sum_row[j - 1]; j < width; ++j)
|
||||
sum_row[j] = (v += src_row[j]) + prev_sum_row[j];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace anon
|
||||
|
||||
bool integral_SIMD(
|
||||
int depth, int sdepth, int sqdepth,
|
||||
const uchar* src, size_t srcstep,
|
||||
uchar* sum, size_t sumstep,
|
||||
uchar* sqsum, size_t sqsumstep,
|
||||
uchar* tilted, size_t tstep,
|
||||
int width, int height, int cn)
|
||||
{
|
||||
CV_INSTRUMENT_REGION();
|
||||
|
||||
#define ONE_CALL(T, ST, QT) \
|
||||
return Integral_SIMD<T, ST, QT>()((const T*)src, srcstep, (ST*)sum, sumstep, (QT*)sqsum, sqsumstep, (ST*)tilted, tstep, width, height, cn)
|
||||
|
||||
if( depth == CV_8U && sdepth == CV_32S && sqdepth == CV_64F )
|
||||
ONE_CALL(uchar, int, double);
|
||||
else if( depth == CV_8U && sdepth == CV_32S && sqdepth == CV_32F )
|
||||
ONE_CALL(uchar, int, float);
|
||||
else if( depth == CV_8U && sdepth == CV_32S && sqdepth == CV_32S )
|
||||
ONE_CALL(uchar, int, int);
|
||||
else if( depth == CV_8U && sdepth == CV_32F && sqdepth == CV_64F )
|
||||
ONE_CALL(uchar, float, double);
|
||||
else if( depth == CV_8U && sdepth == CV_32F && sqdepth == CV_32F )
|
||||
ONE_CALL(uchar, float, float);
|
||||
else if( depth == CV_8U && sdepth == CV_64F && sqdepth == CV_64F )
|
||||
ONE_CALL(uchar, double, double);
|
||||
else if( depth == CV_16U && sdepth == CV_64F && sqdepth == CV_64F )
|
||||
ONE_CALL(ushort, double, double);
|
||||
else if( depth == CV_16S && sdepth == CV_64F && sqdepth == CV_64F )
|
||||
ONE_CALL(short, double, double);
|
||||
else if( depth == CV_32F && sdepth == CV_32F && sqdepth == CV_64F )
|
||||
ONE_CALL(float, float, double);
|
||||
else if( depth == CV_32F && sdepth == CV_32F && sqdepth == CV_32F )
|
||||
ONE_CALL(float, float, float);
|
||||
else if( depth == CV_32F && sdepth == CV_64F && sqdepth == CV_64F )
|
||||
ONE_CALL(float, double, double);
|
||||
else if( depth == CV_64F && sdepth == CV_64F && sqdepth == CV_64F )
|
||||
ONE_CALL(double, double, double);
|
||||
else
|
||||
return false;
|
||||
|
||||
#undef ONE_CALL
|
||||
}
|
||||
|
||||
#endif
|
||||
CV_CPU_OPTIMIZATION_NAMESPACE_END
|
||||
}} // cv::hal::
|
||||
@@ -80,6 +80,15 @@ type_dict = {
|
||||
'suffix': 'Ljava_lang_String_2',
|
||||
'j_import': 'java.lang.String'
|
||||
},
|
||||
'vector_string': { # std::vector<std::string>, see "vector_String" in modules/core/misc/java/gen_dict.json
|
||||
'j_type': 'List<String>',
|
||||
'jn_type': 'List<String>',
|
||||
'jni_type': 'jobject',
|
||||
'jni_var': 'std::vector< std::string > %(n)s',
|
||||
'suffix': 'Ljava_util_List',
|
||||
'v_type': 'string',
|
||||
'j_import': 'java.lang.String'
|
||||
},
|
||||
}
|
||||
|
||||
# Defines a rule to add extra prefixes for names from specific namespaces.
|
||||
@@ -925,7 +934,10 @@ class JavaWrapperGenerator(object):
|
||||
c_epilogue.append("Mat* _retval_ = new Mat();")
|
||||
c_epilogue.append(fi.ctype+"_to_Mat(_ret_val_vector_, *_retval_);")
|
||||
else:
|
||||
c_epilogue.append("return " + fi.ctype + "_to_List(env, _ret_val_vector_);")
|
||||
if ret:
|
||||
c_epilogue.append("jobject _retval_ = " + fi.ctype + "_to_List(env, _ret_val_vector_);")
|
||||
else:
|
||||
c_epilogue.append("return " + fi.ctype + "_to_List(env, _ret_val_vector_);")
|
||||
if fi.classname:
|
||||
if not fi.ctype: # c-tor
|
||||
retval = fi.fullClass(isCPP=True) + "* _retval_ = "
|
||||
|
||||
@@ -57,3 +57,54 @@ void Copy_vector_String_to_List(JNIEnv* env, std::vector<cv::String>& vs, jobjec
|
||||
env->DeleteLocalRef(element);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
jobject vector_string_to_List(JNIEnv* env, std::vector<std::string>& vs) {
|
||||
|
||||
static jclass juArrayList = ARRAYLIST(env);
|
||||
static jmethodID m_create = CONSTRUCTOR(env, juArrayList);
|
||||
jmethodID m_add = LIST_ADD(env, juArrayList);
|
||||
|
||||
jobject result = env->NewObject(juArrayList, m_create, vs.size());
|
||||
for (std::vector<std::string>::iterator it = vs.begin(); it != vs.end(); ++it) {
|
||||
jstring element = env->NewStringUTF((*it).c_str());
|
||||
env->CallBooleanMethod(result, m_add, element);
|
||||
env->DeleteLocalRef(element);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::string> List_to_vector_string(JNIEnv* env, jobject list)
|
||||
{
|
||||
static jclass juArrayList = ARRAYLIST(env);
|
||||
jmethodID m_size = LIST_SIZE(env,juArrayList);
|
||||
jmethodID m_get = LIST_GET(env, juArrayList);
|
||||
|
||||
jint len = env->CallIntMethod(list, m_size);
|
||||
std::vector<std::string> result;
|
||||
result.reserve(len);
|
||||
for (jint i=0; i<len; i++)
|
||||
{
|
||||
jstring element = static_cast<jstring>(env->CallObjectMethod(list, m_get, i));
|
||||
const char* pchars = env->GetStringUTFChars(element, NULL);
|
||||
result.push_back(pchars);
|
||||
env->ReleaseStringUTFChars(element, pchars);
|
||||
env->DeleteLocalRef(element);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void Copy_vector_string_to_List(JNIEnv* env, std::vector<std::string>& vs, jobject list)
|
||||
{
|
||||
static jclass juArrayList = ARRAYLIST(env);
|
||||
jmethodID m_clear = LIST_CLEAR(env, juArrayList);
|
||||
jmethodID m_add = LIST_ADD(env, juArrayList);
|
||||
|
||||
env->CallVoidMethod(list, m_clear);
|
||||
for (std::vector<std::string>::iterator it = vs.begin(); it != vs.end(); ++it)
|
||||
{
|
||||
jstring element = env->NewStringUTF((*it).c_str());
|
||||
env->CallBooleanMethod(list, m_add, element);
|
||||
env->DeleteLocalRef(element);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,4 +16,11 @@ std::vector<cv::String> List_to_vector_String(JNIEnv* env, jobject list);
|
||||
|
||||
void Copy_vector_String_to_List(JNIEnv* env, std::vector<cv::String>& vs, jobject list);
|
||||
|
||||
|
||||
jobject vector_string_to_List(JNIEnv* env, std::vector<std::string>& vs);
|
||||
|
||||
std::vector<std::string> List_to_vector_string(JNIEnv* env, jobject list);
|
||||
|
||||
void Copy_vector_string_to_List(JNIEnv* env, std::vector<std::string>& vs, jobject list);
|
||||
|
||||
#endif /* LISTCONVERTERS_HPP */
|
||||
|
||||
@@ -691,8 +691,8 @@ public:
|
||||
CV_WRAP bool detect(InputArray img, OutputArray points) const;
|
||||
|
||||
/** @brief Decodes QR code in image once it's found by the detect() method.
|
||||
Returns UTF8-encoded output string or empty string if the code cannot be decoded.
|
||||
|
||||
Returns UTF8-encoded output string or empty string if the code cannot be decoded.
|
||||
@param img grayscale or color (BGR) image containing QR code.
|
||||
@param points Quadrangle vertices found by detect() method (or some other algorithm).
|
||||
@param straight_qrcode The optional output image containing rectified and binarized QR code
|
||||
@@ -702,11 +702,44 @@ public:
|
||||
/** @brief Both detects and decodes QR code
|
||||
|
||||
@param img grayscale or color (BGR) image containing QR code.
|
||||
@param points opiotnal output array of vertices of the found QR code quadrangle. Will be empty if not found.
|
||||
@param points optional output array of vertices of the found QR code quadrangle. Will be empty if not found.
|
||||
@param straight_qrcode The optional output image containing rectified and binarized QR code
|
||||
*/
|
||||
CV_WRAP std::string detectAndDecode(InputArray img, OutputArray points=noArray(),
|
||||
OutputArray straight_qrcode = noArray());
|
||||
/** @brief Detects QR codes in image and returns the vector of the quadrangles containing the codes.
|
||||
@param img grayscale or color (BGR) image containing (or not) QR codes.
|
||||
@param points Output vector of vector of vertices of the minimum-area quadrangle containing the codes.
|
||||
*/
|
||||
CV_WRAP
|
||||
bool detectMulti(InputArray img, OutputArray points) const;
|
||||
|
||||
/** @brief Decodes QR codes in image once it's found by the detect() method.
|
||||
@param img grayscale or color (BGR) image containing QR codes.
|
||||
@param decoded_info UTF8-encoded output vector of string or empty vector of string if the codes cannot be decoded.
|
||||
@param points vector of Quadrangle vertices found by detect() method (or some other algorithm).
|
||||
@param straight_qrcode The optional output vector of images containing rectified and binarized QR codes
|
||||
*/
|
||||
CV_WRAP
|
||||
bool decodeMulti(
|
||||
InputArray img, InputArray points,
|
||||
CV_OUT std::vector<std::string>& decoded_info,
|
||||
OutputArrayOfArrays straight_qrcode = noArray()
|
||||
) const;
|
||||
|
||||
/** @brief Both detects and decodes QR codes
|
||||
@param img grayscale or color (BGR) image containing QR codes.
|
||||
@param decoded_info UTF8-encoded output vector of string or empty vector of string if the codes cannot be decoded.
|
||||
@param points optional output vector of vertices of the found QR code quadrangles. Will be empty if not found.
|
||||
@param straight_qrcode The optional output vector of images containing rectified and binarized QR codes
|
||||
*/
|
||||
CV_WRAP
|
||||
bool detectAndDecodeMulti(
|
||||
InputArray img, CV_OUT std::vector<std::string>& decoded_info,
|
||||
OutputArray points = noArray(),
|
||||
OutputArrayOfArrays straight_qrcode = noArray()
|
||||
) const;
|
||||
|
||||
protected:
|
||||
struct Impl;
|
||||
Ptr<Impl> p;
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package org.opencv.test.objdetect;
|
||||
|
||||
import java.util.List;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.objdetect.QRCodeDetector;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.test.OpenCVTestCase;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class QRCodeDetectorTest extends OpenCVTestCase {
|
||||
|
||||
@@ -21,9 +23,27 @@ public class QRCodeDetectorTest extends OpenCVTestCase {
|
||||
|
||||
public void testDetectAndDecode() {
|
||||
Mat img = Imgcodecs.imread(testDataPath + "/cv/qrcode/link_ocv.jpg");
|
||||
assertFalse(img.empty());
|
||||
QRCodeDetector detector = new QRCodeDetector();
|
||||
assertNotNull(detector);
|
||||
String output = detector.detectAndDecode(img);
|
||||
assertEquals(output, "https://opencv.org/");
|
||||
}
|
||||
|
||||
public void testDetectAndDecodeMulti() {
|
||||
Mat img = Imgcodecs.imread(testDataPath + "/cv/qrcode/multiple/6_qrcodes.png");
|
||||
assertFalse(img.empty());
|
||||
QRCodeDetector detector = new QRCodeDetector();
|
||||
assertNotNull(detector);
|
||||
List < String > output = new ArrayList< String >();
|
||||
boolean result = detector.detectAndDecodeMulti(img, output);
|
||||
assertTrue(result);
|
||||
assertEquals(output.size(), 6);
|
||||
assertEquals(output.get(0), "SKIP");
|
||||
assertEquals(output.get(1), "EXTRA");
|
||||
assertEquals(output.get(2), "TWO STEPS FORWARD");
|
||||
assertEquals(output.get(3), "STEP BACK");
|
||||
assertEquals(output.get(4), "QUESTION");
|
||||
assertEquals(output.get(5), "STEP FORWARD");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,42 @@ import cv2 as cv
|
||||
from tests_common import NewOpenCVTests
|
||||
|
||||
class qrcode_detector_test(NewOpenCVTests):
|
||||
|
||||
def test_detect(self):
|
||||
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/qrcode/link_ocv.jpg'))
|
||||
self.assertFalse(img is None)
|
||||
detector = cv.QRCodeDetector()
|
||||
retval, points = detector.detect(img)
|
||||
self.assertTrue(retval)
|
||||
self.assertEqual(points.shape, (1, 4, 2))
|
||||
|
||||
def test_detect_and_decode(self):
|
||||
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/qrcode/link_ocv.jpg'))
|
||||
self.assertFalse(img is None)
|
||||
detector = cv.QRCodeDetector()
|
||||
retval, points, straight_qrcode = detector.detectAndDecode(img)
|
||||
self.assertEqual(retval, "https://opencv.org/");
|
||||
self.assertEqual(retval, "https://opencv.org/")
|
||||
self.assertEqual(points.shape, (1, 4, 2))
|
||||
|
||||
def test_detect_multi(self):
|
||||
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/qrcode/multiple/6_qrcodes.png'))
|
||||
self.assertFalse(img is None)
|
||||
detector = cv.QRCodeDetector()
|
||||
retval, points = detector.detectMulti(img)
|
||||
self.assertTrue(retval)
|
||||
self.assertEqual(points.shape, (6, 4, 2))
|
||||
|
||||
def test_detect_and_decode_multi(self):
|
||||
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/qrcode/multiple/6_qrcodes.png'))
|
||||
self.assertFalse(img is None)
|
||||
detector = cv.QRCodeDetector()
|
||||
retval, decoded_data, points, straight_qrcode = detector.detectAndDecodeMulti(img)
|
||||
self.assertTrue(retval)
|
||||
self.assertEqual(len(decoded_data), 6)
|
||||
self.assertEqual(decoded_data[0], "TWO STEPS FORWARD")
|
||||
self.assertEqual(decoded_data[1], "EXTRA")
|
||||
self.assertEqual(decoded_data[2], "SKIP")
|
||||
self.assertEqual(decoded_data[3], "STEP FORWARD")
|
||||
self.assertEqual(decoded_data[4], "STEP BACK")
|
||||
self.assertEqual(decoded_data[5], "QUESTION")
|
||||
self.assertEqual(points.shape, (6, 4, 2))
|
||||
|
||||
@@ -53,6 +53,56 @@ PERF_TEST_P_(Perf_Objdetect_QRCode, decode)
|
||||
}
|
||||
#endif
|
||||
|
||||
typedef ::perf::TestBaseWithParam< std::string > Perf_Objdetect_QRCode_Multi;
|
||||
|
||||
PERF_TEST_P_(Perf_Objdetect_QRCode_Multi, detectMulti)
|
||||
{
|
||||
const std::string name_current_image = GetParam();
|
||||
const std::string root = "cv/qrcode/multiple/";
|
||||
|
||||
std::string image_path = findDataFile(root + name_current_image);
|
||||
Mat src = imread(image_path);
|
||||
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
|
||||
std::vector<Point2f> corners;
|
||||
QRCodeDetector qrcode;
|
||||
TEST_CYCLE() ASSERT_TRUE(qrcode.detectMulti(src, corners));
|
||||
SANITY_CHECK(corners);
|
||||
}
|
||||
|
||||
#ifdef HAVE_QUIRC
|
||||
PERF_TEST_P_(Perf_Objdetect_QRCode_Multi, decodeMulti)
|
||||
{
|
||||
const std::string name_current_image = GetParam();
|
||||
const std::string root = "cv/qrcode/multiple/";
|
||||
|
||||
std::string image_path = findDataFile(root + name_current_image);
|
||||
Mat src = imread(image_path);
|
||||
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
|
||||
QRCodeDetector qrcode;
|
||||
std::vector<Point2f> corners;
|
||||
ASSERT_TRUE(qrcode.detectMulti(src, corners));
|
||||
std::vector<Mat> straight_barcode;
|
||||
std::vector< cv::String > decoded_info;
|
||||
TEST_CYCLE()
|
||||
{
|
||||
ASSERT_TRUE(qrcode.decodeMulti(src, corners, decoded_info, straight_barcode));
|
||||
for(size_t i = 0; i < decoded_info.size(); i++)
|
||||
{
|
||||
ASSERT_FALSE(decoded_info[i].empty());
|
||||
}
|
||||
}
|
||||
std::vector < std::vector< uint8_t > > decoded_info_uint8_t;
|
||||
for(size_t i = 0; i < decoded_info.size(); i++)
|
||||
{
|
||||
std::vector< uint8_t > tmp(decoded_info[i].begin(), decoded_info[i].end());
|
||||
decoded_info_uint8_t.push_back(tmp);
|
||||
}
|
||||
SANITY_CHECK(decoded_info_uint8_t);
|
||||
SANITY_CHECK(straight_barcode);
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_QRCode,
|
||||
::testing::Values(
|
||||
"version_1_down.jpg", "version_1_left.jpg", "version_1_right.jpg", "version_1_up.jpg", "version_1_top.jpg",
|
||||
@@ -61,6 +111,13 @@ INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_QRCode,
|
||||
)
|
||||
);
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_QRCode_Multi,
|
||||
::testing::Values(
|
||||
"2_qrcodes.png", "3_close_qrcodes.png", "3_qrcodes.png", "4_qrcodes.png",
|
||||
"5_qrcodes.png", "6_qrcodes.png", "7_qrcodes.png", "8_close_qrcodes.png"
|
||||
)
|
||||
);
|
||||
|
||||
typedef ::perf::TestBaseWithParam< tuple< std::string, Size > > Perf_Objdetect_Not_QRCode;
|
||||
|
||||
PERF_TEST_P_(Perf_Objdetect_Not_QRCode, detect)
|
||||
|
||||
+1297
-164
File diff suppressed because it is too large
Load Diff
@@ -21,7 +21,11 @@ std::string qrcode_images_close[] = {
|
||||
std::string qrcode_images_monitor[] = {
|
||||
"monitor_1.png", "monitor_2.png", "monitor_3.png", "monitor_4.png", "monitor_5.png"
|
||||
};
|
||||
// #define UPDATE_QRCODE_TEST_DATA
|
||||
std::string qrcode_images_multiple[] = {
|
||||
"2_qrcodes.png", "3_close_qrcodes.png", "3_qrcodes.png", "4_qrcodes.png",
|
||||
"5_qrcodes.png", "6_qrcodes.png", "7_qrcodes.png", "8_close_qrcodes.png"
|
||||
};
|
||||
//#define UPDATE_QRCODE_TEST_DATA
|
||||
#ifdef UPDATE_QRCODE_TEST_DATA
|
||||
|
||||
TEST(Objdetect_QRCode, generate_test_data)
|
||||
@@ -134,6 +138,66 @@ TEST(Objdetect_QRCode_Monitor, generate_test_data)
|
||||
file_config.release();
|
||||
}
|
||||
|
||||
|
||||
TEST(Objdetect_QRCode_Multi, generate_test_data)
|
||||
{
|
||||
const std::string root = "qrcode/multiple/";
|
||||
const std::string dataset_config = findDataFile(root + "dataset_config.json");
|
||||
FileStorage file_config(dataset_config, FileStorage::WRITE);
|
||||
|
||||
file_config << "multiple_images" << "[:";
|
||||
size_t multiple_count = sizeof(qrcode_images_multiple) / sizeof(qrcode_images_multiple[0]);
|
||||
for (size_t i = 0; i < multiple_count; i++)
|
||||
{
|
||||
file_config << "{:" << "image_name" << qrcode_images_multiple[i];
|
||||
std::string image_path = findDataFile(root + qrcode_images_multiple[i]);
|
||||
Mat src = imread(image_path);
|
||||
|
||||
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
|
||||
std::vector<Point> corners;
|
||||
EXPECT_TRUE(detectQRCodeMulti(src, corners));
|
||||
#ifdef HAVE_QUIRC
|
||||
std::vector<cv::String> decoded_info;
|
||||
std::vector<Mat> straight_barcode;
|
||||
EXPECT_TRUE(decodeQRCodeMulti(src, corners, decoded_info, straight_barcode));
|
||||
#endif
|
||||
file_config << "x" << "[:";
|
||||
for(size_t j = 0; j < corners.size(); j += 4)
|
||||
{
|
||||
file_config << "[:";
|
||||
for (size_t k = 0; k < 4; k++)
|
||||
{
|
||||
file_config << corners[j + k].x;
|
||||
}
|
||||
file_config << "]";
|
||||
}
|
||||
file_config << "]";
|
||||
file_config << "y" << "[:";
|
||||
for(size_t j = 0; j < corners.size(); j += 4)
|
||||
{
|
||||
file_config << "[:";
|
||||
for (size_t k = 0; k < 4; k++)
|
||||
{
|
||||
file_config << corners[j + k].y;
|
||||
}
|
||||
file_config << "]";
|
||||
}
|
||||
file_config << "]";
|
||||
file_config << "info";
|
||||
file_config << "[:";
|
||||
|
||||
for(size_t j = 0; j < decoded_info.size(); j++)
|
||||
{
|
||||
file_config << decoded_info[j];
|
||||
}
|
||||
file_config << "]";
|
||||
file_config << "}";
|
||||
}
|
||||
|
||||
file_config << "]";
|
||||
file_config.release();
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
typedef testing::TestWithParam< std::string > Objdetect_QRCode;
|
||||
@@ -326,9 +390,96 @@ TEST_P(Objdetect_QRCode_Monitor, regression)
|
||||
}
|
||||
}
|
||||
|
||||
typedef testing::TestWithParam < std::string > Objdetect_QRCode_Multi;
|
||||
TEST_P(Objdetect_QRCode_Multi, regression)
|
||||
{
|
||||
const std::string name_current_image = GetParam();
|
||||
const std::string root = "qrcode/multiple/";
|
||||
const int pixels_error = 3;
|
||||
|
||||
std::string image_path = findDataFile(root + name_current_image);
|
||||
Mat src = imread(image_path);
|
||||
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
|
||||
QRCodeDetector qrcode;
|
||||
std::vector<Point> corners;
|
||||
#ifdef HAVE_QUIRC
|
||||
std::vector<cv::String> decoded_info;
|
||||
std::vector<Mat> straight_barcode;
|
||||
EXPECT_TRUE(qrcode.detectAndDecodeMulti(src, decoded_info, corners, straight_barcode));
|
||||
ASSERT_FALSE(corners.empty());
|
||||
ASSERT_FALSE(decoded_info.empty());
|
||||
#else
|
||||
ASSERT_TRUE(qrcode.detectMulti(src, corners));
|
||||
#endif
|
||||
|
||||
const std::string dataset_config = findDataFile(root + "dataset_config.json");
|
||||
FileStorage file_config(dataset_config, FileStorage::READ);
|
||||
ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;
|
||||
{
|
||||
FileNode images_list = file_config["multiple_images"];
|
||||
size_t images_count = static_cast<size_t>(images_list.size());
|
||||
ASSERT_GT(images_count, 0u) << "Can't find validation data entries in 'test_images': " << dataset_config;
|
||||
for (size_t index = 0; index < images_count; index++)
|
||||
{
|
||||
FileNode config = images_list[(int)index];
|
||||
std::string name_test_image = config["image_name"];
|
||||
if (name_test_image == name_current_image)
|
||||
{
|
||||
for(int j = 0; j < int(corners.size()); j += 4)
|
||||
{
|
||||
bool ok = false;
|
||||
for (int k = 0; k < int(corners.size() / 4); k++)
|
||||
{
|
||||
int count_eq_points = 0;
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
int x = config["x"][k][i];
|
||||
int y = config["y"][k][i];
|
||||
if(((abs(corners[j + i].x - x)) <= pixels_error) && ((abs(corners[j + i].y - y)) <= pixels_error))
|
||||
count_eq_points++;
|
||||
}
|
||||
if (count_eq_points == 4)
|
||||
{
|
||||
ok = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
EXPECT_TRUE(ok);
|
||||
}
|
||||
|
||||
#ifdef HAVE_QUIRC
|
||||
size_t count_eq_info = 0;
|
||||
for(int i = 0; i < int(decoded_info.size()); i++)
|
||||
{
|
||||
for(int j = 0; j < int(decoded_info.size()); j++)
|
||||
{
|
||||
std::string original_info = config["info"][j];
|
||||
if(original_info == decoded_info[i])
|
||||
{
|
||||
count_eq_info++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
EXPECT_EQ(decoded_info.size(), count_eq_info);
|
||||
#endif
|
||||
|
||||
return; // done
|
||||
}
|
||||
}
|
||||
std::cerr
|
||||
<< "Not found results for '" << name_current_image
|
||||
<< "' image in config file:" << dataset_config << std::endl
|
||||
<< "Re-run tests with enabled UPDATE_QRCODE_TEST_DATA macro to update test data."
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode, testing::ValuesIn(qrcode_images_name));
|
||||
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Close, testing::ValuesIn(qrcode_images_close));
|
||||
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Monitor, testing::ValuesIn(qrcode_images_monitor));
|
||||
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Multi, testing::ValuesIn(qrcode_images_multiple));
|
||||
|
||||
TEST(Objdetect_QRCode_basic, not_found_qrcode)
|
||||
{
|
||||
|
||||
@@ -319,6 +319,7 @@ typedef std::vector<std::vector<Mat> > vector_vector_Mat;
|
||||
typedef std::vector<UMat> vector_UMat;
|
||||
typedef std::vector<DMatch> vector_DMatch;
|
||||
typedef std::vector<String> vector_String;
|
||||
typedef std::vector<std::string> vector_string;
|
||||
typedef std::vector<Scalar> vector_Scalar;
|
||||
|
||||
typedef std::vector<std::vector<char> > vector_vector_char;
|
||||
|
||||
@@ -967,6 +967,29 @@ bool CvCapture_FFMPEG::open( const char* _filename )
|
||||
enc->thread_count = get_number_of_cpus();
|
||||
//#endif
|
||||
|
||||
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(52, 123, 0)
|
||||
AVDictionaryEntry* avdiscard_entry = av_dict_get(dict, "avdiscard", NULL, 0);
|
||||
|
||||
if (avdiscard_entry != 0) {
|
||||
if(strcmp(avdiscard_entry->value, "all") == 0)
|
||||
enc->skip_frame = AVDISCARD_ALL;
|
||||
else if (strcmp(avdiscard_entry->value, "bidir") == 0)
|
||||
enc->skip_frame = AVDISCARD_BIDIR;
|
||||
else if (strcmp(avdiscard_entry->value, "default") == 0)
|
||||
enc->skip_frame = AVDISCARD_DEFAULT;
|
||||
else if (strcmp(avdiscard_entry->value, "none") == 0)
|
||||
enc->skip_frame = AVDISCARD_NONE;
|
||||
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 59, 100)
|
||||
else if (strcmp(avdiscard_entry->value, "nonintra") == 0)
|
||||
enc->skip_frame = AVDISCARD_NONINTRA;
|
||||
#endif
|
||||
else if (strcmp(avdiscard_entry->value, "nonkey") == 0)
|
||||
enc->skip_frame = AVDISCARD_NONKEY;
|
||||
else if (strcmp(avdiscard_entry->value, "nonref") == 0)
|
||||
enc->skip_frame = AVDISCARD_NONREF;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
|
||||
#define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
|
||||
#endif
|
||||
|
||||
@@ -180,6 +180,54 @@ const videoio_container_params_t videoio_container_params[] =
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(/**/, videoio_container, testing::ValuesIn(videoio_container_params));
|
||||
|
||||
typedef tuple<string, string, int> videoio_skip_params_t;
|
||||
typedef testing::TestWithParam< videoio_skip_params_t > videoio_skip;
|
||||
|
||||
TEST_P(videoio_skip, DISABLED_read) // optional test, may fail in some configurations
|
||||
{
|
||||
#if CV_VERSION_MAJOR >= 4
|
||||
if (!videoio_registry::hasBackend(CAP_FFMPEG))
|
||||
throw SkipTestException("Backend was not found");
|
||||
#endif
|
||||
|
||||
const string path = get<0>(GetParam());
|
||||
const string env = get<1>(GetParam());
|
||||
const int expectedFrameNumber = get<2>(GetParam());
|
||||
|
||||
#ifdef _WIN32
|
||||
_putenv_s("OPENCV_FFMPEG_CAPTURE_OPTIONS", env.c_str());
|
||||
#else
|
||||
setenv("OPENCV_FFMPEG_CAPTURE_OPTIONS", env.c_str(), 1);
|
||||
#endif
|
||||
VideoCapture container(findDataFile(path), CAP_FFMPEG);
|
||||
#ifdef _WIN32
|
||||
_putenv_s("OPENCV_FFMPEG_CAPTURE_OPTIONS", "");
|
||||
#else
|
||||
setenv("OPENCV_FFMPEG_CAPTURE_OPTIONS", "", 1);
|
||||
#endif
|
||||
|
||||
ASSERT_TRUE(container.isOpened());
|
||||
|
||||
Mat reference;
|
||||
int nframes = 0, n_err = 0;
|
||||
while (container.isOpened())
|
||||
{
|
||||
if (container.read(reference))
|
||||
nframes++;
|
||||
else if (++n_err > 3)
|
||||
break;
|
||||
}
|
||||
EXPECT_EQ(expectedFrameNumber, nframes);
|
||||
}
|
||||
|
||||
const videoio_skip_params_t videoio_skip_params[] =
|
||||
{
|
||||
videoio_skip_params_t("video/big_buck_bunny.mp4", "", 125),
|
||||
videoio_skip_params_t("video/big_buck_bunny.mp4", "avdiscard;nonkey", 11)
|
||||
};
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(/**/, videoio_skip, testing::ValuesIn(videoio_skip_params));
|
||||
|
||||
//==========================================================================
|
||||
|
||||
static void generateFrame(Mat &frame, unsigned int i, const Point ¢er, const Scalar &color)
|
||||
|
||||
Reference in New Issue
Block a user