From e644b5a358a6282cf65f50b8b0ad9a2a2b0a7df9 Mon Sep 17 00:00:00 2001 From: Vitaliy Lyudvichenko Date: Thu, 20 Aug 2015 21:52:41 +0300 Subject: [PATCH] Changed Blob::ptr and Blob::offset methods --- modules/dnn/include/opencv2/dnn/blob.hpp | 14 ++++++---- modules/dnn/include/opencv2/dnn/blob.inl.hpp | 27 ++++++++++++++----- modules/dnn/include/opencv2/dnn/dnn.hpp | 2 +- modules/dnn/src/blob.cpp | 2 +- modules/dnn/src/layers/convolution_layer.cpp | 12 ++++----- modules/dnn/src/layers/elementwise_layers.hpp | 2 +- modules/dnn/src/layers/lrn_layer.cpp | 2 +- 7 files changed, 40 insertions(+), 21 deletions(-) diff --git a/modules/dnn/include/opencv2/dnn/blob.hpp b/modules/dnn/include/opencv2/dnn/blob.hpp index 3a69351f4..29a6c64d7 100644 --- a/modules/dnn/include/opencv2/dnn/blob.hpp +++ b/modules/dnn/include/opencv2/dnn/blob.hpp @@ -67,6 +67,7 @@ namespace dnn Mat& getMatRef(); const Mat& getMatRef() const; + //TODO: add UMat get methods Mat getMat(int n, int cn); //shape getters @@ -98,13 +99,13 @@ namespace dnn */ int canonicalAxis(int axis) const; - /** @brief returns real shape of the blob + /** @brief returns shape of the blob */ BlobShape shape() const; bool equalShape(const Blob &other) const; - //shape getters, oriented for 4-dim Blobs processing + //shape getters for 4-dim Blobs processing int cols() const; int rows() const; int channels() const; @@ -113,11 +114,14 @@ namespace dnn Vec4i shape4() const; //CPU data pointer functions - int offset(int n = 0, int cn = 0, int row = 0, int col = 0) const; - uchar *ptrRaw(int n = 0, int cn = 0, int row = 0, int col = 0); - float *ptrf(int n = 0, int cn = 0, int row = 0, int col = 0); + template + size_t offset(const Vec &pos) const; + size_t offset(int n = 0, int cn = 0, int row = 0, int col = 0) const; + uchar *ptr(int n = 0, int cn = 0, int row = 0, int col = 0); template TFloat *ptr(int n = 0, int cn = 0, int row = 0, int col = 0); + float *ptrf(int n = 0, int cn = 0, int row = 0, int col = 0); + //TODO: add const ptr methods /** @brief share data with other blob and returns *this @returns *this diff --git a/modules/dnn/include/opencv2/dnn/blob.inl.hpp b/modules/dnn/include/opencv2/dnn/blob.inl.hpp index 7cae032de..e74f67a3e 100644 --- a/modules/dnn/include/opencv2/dnn/blob.inl.hpp +++ b/modules/dnn/include/opencv2/dnn/blob.inl.hpp @@ -158,10 +158,25 @@ inline size_t Blob::total(int startAxis, int endAxis) const return size; } -inline int Blob::offset(int n, int cn, int row, int col) const + +template +inline size_t Blob::offset(const Vec &pos) const +{ + size_t ofs = 0; + int i; + for (i = 0; i < std::min(n, dims()); i++) + { + CV_DbgAssert(pos[i] >= 0 && pos[i] < size(i)); + ofs = ofs * (size_t)size(i) + pos[i]; + } + for (; i < dims(); i++) + ofs *= (size_t)size(i); + return ofs; +} + +inline size_t Blob::offset(int n, int cn, int row, int col) const { - CV_DbgAssert(0 <= n && n < num() && 0 <= cn && cn < channels() && 0 <= row && row < rows() && 0 <= col && col < cols()); - return ((n*channels() + cn)*rows() + row)*cols() + col; + return offset(Vec4i(n, cn, row, col)); } inline float *Blob::ptrf(int n, int cn, int row, int col) @@ -170,7 +185,7 @@ inline float *Blob::ptrf(int n, int cn, int row, int col) return (float*)m.data + offset(n, cn, row, col); } -inline uchar *Blob::ptrRaw(int n, int cn, int row, int col) +inline uchar *Blob::ptr(int n, int cn, int row, int col) { return m.data + m.elemSize() * offset(n, cn, row, col); } @@ -179,7 +194,7 @@ template inline TFloat* Blob::ptr(int n, int cn, int row, int col) { CV_Assert(type() == cv::DataDepth::value); - return (TFloat*) ptrRaw(n, cn, row, col); + return (TFloat*) ptr(n, cn, row, col); } inline BlobShape Blob::shape() const @@ -212,7 +227,7 @@ inline const Mat& Blob::getMatRef() const inline Mat Blob::getMat(int n, int cn) { - return Mat(rows(), cols(), m.type(), this->ptrRaw(n, cn)); + return Mat(rows(), cols(), m.type(), this->ptr(n, cn)); } inline int Blob::cols() const diff --git a/modules/dnn/include/opencv2/dnn/dnn.hpp b/modules/dnn/include/opencv2/dnn/dnn.hpp index 16cacb85e..bea4f8717 100644 --- a/modules/dnn/include/opencv2/dnn/dnn.hpp +++ b/modules/dnn/include/opencv2/dnn/dnn.hpp @@ -79,7 +79,7 @@ namespace dnn Ptr impl; }; - class CV_EXPORTS Importer + class Importer { public: diff --git a/modules/dnn/src/blob.cpp b/modules/dnn/src/blob.cpp index d64e7fa30..9ac8d011b 100644 --- a/modules/dnn/src/blob.cpp +++ b/modules/dnn/src/blob.cpp @@ -88,7 +88,7 @@ namespace dnn std::vector wrapBuf(dstShape[-3]); int elemSize = (int)m.elemSize(); - uchar *ptr = this->ptrRaw(); + uchar *ptr = this->ptr(); for (size_t i = 0; i < inMats.size(); i++) { Mat inMat = inMats[i]; diff --git a/modules/dnn/src/layers/convolution_layer.cpp b/modules/dnn/src/layers/convolution_layer.cpp index b3de8e00d..8ce196da5 100644 --- a/modules/dnn/src/layers/convolution_layer.cpp +++ b/modules/dnn/src/layers/convolution_layer.cpp @@ -86,8 +86,8 @@ namespace dnn { im2col(inpBlob, n, g); - Mat kerMat(outGroupCn, ksize, wgtBlob.type(), wgtBlob.ptrRaw(g*outGroupCn)); - Mat dstMat(outGroupCn, outH*outW, outBlob.type(), outBlob.ptrRaw(n, g*outGroupCn)); + Mat kerMat(outGroupCn, ksize, wgtBlob.type(), wgtBlob.ptr(g*outGroupCn)); + Mat dstMat(outGroupCn, outH*outW, outBlob.type(), outBlob.ptr(n, g*outGroupCn)); cv::gemm(kerMat, colMat, 1, noArray(), 0, dstMat); @@ -104,7 +104,7 @@ namespace dnn void ConvolutionLayer::im2col(Blob &inpBlob, int imNum, int cnGroup) { - uchar *srcPtr = inpBlob.ptrRaw(imNum, cnGroup*inpGroupCn); + uchar *srcPtr = inpBlob.ptr(imNum, cnGroup*inpGroupCn); if (is1x1()) { @@ -170,13 +170,13 @@ namespace dnn { for (int g = 0; g < group; g++) { - Mat dstMat(inpGroupCn, inpH*inpW, decnBlob.type(), decnBlob.ptrRaw(n, g*inpGroupCn)); + Mat dstMat(inpGroupCn, inpH*inpW, decnBlob.type(), decnBlob.ptr(n, g*inpGroupCn)); if (is1x1()) colMat = dstMat; - Mat convMat(outGroupCn, outH*outW, convBlob.type(), convBlob.ptrRaw(n, g*outGroupCn)); - Mat wghtMat(outGroupCn, ksize, wghtBlob.type(), wghtBlob.ptrRaw(g*outGroupCn)); + Mat convMat(outGroupCn, outH*outW, convBlob.type(), convBlob.ptr(n, g*outGroupCn)); + Mat wghtMat(outGroupCn, ksize, wghtBlob.type(), wghtBlob.ptr(g*outGroupCn)); cv::gemm(wghtMat, convMat, 1, noArray(), 0, colMat, GEMM_1_T); col2im(dstMat); diff --git a/modules/dnn/src/layers/elementwise_layers.hpp b/modules/dnn/src/layers/elementwise_layers.hpp index edbdb87be..014959115 100644 --- a/modules/dnn/src/layers/elementwise_layers.hpp +++ b/modules/dnn/src/layers/elementwise_layers.hpp @@ -33,7 +33,7 @@ using std::pow; { for (size_t i = 0; i < inputs.size(); i++) { - CV_Assert(inputs[i]->ptrRaw() == outputs[i].ptrRaw() && inputs[i]->type() == outputs[i].type()); + CV_Assert(inputs[i]->ptr() == outputs[i].ptr() && inputs[i]->type() == outputs[i].type()); size_t size = outputs[i].total(); diff --git a/modules/dnn/src/layers/lrn_layer.cpp b/modules/dnn/src/layers/lrn_layer.cpp index bf3a394da..2d8b23d9e 100644 --- a/modules/dnn/src/layers/lrn_layer.cpp +++ b/modules/dnn/src/layers/lrn_layer.cpp @@ -59,7 +59,7 @@ namespace dnn void LRNLayer::channelNoramlization(Blob &srcBlob, Blob &dstBlob) { - CV_DbgAssert(srcBlob.ptrRaw() != dstBlob.ptrRaw()); + CV_DbgAssert(srcBlob.ptr() != dstBlob.ptr()); int num = srcBlob.num(); int channels = srcBlob.channels();