Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions modules/dnn/include/opencv2/dnn/dnn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,22 @@ CV__DNN_INLINE_NS_BEGIN
*/
CV_EXPORTS_W void imagesFromBlob(const cv::Mat& blob_, OutputArrayOfArrays images_);

/** @brief return a specific Mat in a blob.
* A blob is 3 or 4D matrix function return a specific plane.
* if dims blob is less or equal to 2 (N rows x M columns) blob is return
* if dims blob is 3: blob is an H x N x M array and mat at (coord(0),0, 0) is returned.
* if dims blob is 4: blob is T x H x N x C array
* mat at (coord(0), coord(1), 0, 0) is returned as Mat with C channel if channelOrder = DNN_LAYOUT_NHWC.
* mat at (coord(0), coord(1), 0, 0) is returned as Mat with one channel if channelOrder = DNN_LAYOUT_NCHW.
* @param[in] blob a blob with dim size less or equal than 4
* @param[in] coord blob coordinate
* @param[in] channelOrder blob coordinate
* @returns Mat with dim size less or equal than 2
*
* @note return Mat is original data in blob with read write access to blob data
*/
CV_EXPORTS Mat getMatInBlob(Mat blob, std::vector<int> coord, int channelOrder = DNN_LAYOUT_NCHW);

/** @brief Convert all weights of Caffe network to half precision floating point.
* @param src Path to origin model from Caffe framework contains single
* precision floating point weights (usually has `.caffemodel` extension).
Expand Down
37 changes: 37 additions & 0 deletions modules/dnn/src/dnn_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,43 @@ void imagesFromBlob(const cv::Mat& blob_, OutputArrayOfArrays images_)
}
}

Mat getMatInBlob(Mat blob, std::vector<int> coord, int channelOrder)
{
CV_TRACE_FUNCTION();
CV_Assert(blob.dims <= 4);
CV_Assert(channelOrder == DNN_LAYOUT_NHWC || channelOrder == DNN_LAYOUT_NCHW);
Mat x, y, out;

switch (blob.dims)
{
case 0:
out = Mat();
break;
case 1:
case 2:
return blob;
break;
case 3:
CV_Assert(coord[0] < blob.size[0]);
x = blob.reshape(1, std::vector<int> {blob.size[0], blob.size[1] * blob.size[2]});
out = x.row(coord[0]).reshape(0, blob.size[1]);
break;
case 4:
CV_Assert(coord[0] < blob.size[0]);
x = blob.reshape(1, std::vector<int> {blob.size[0], blob.size[1] * blob.size[2] * blob.size[3]});
if (channelOrder == DNN_LAYOUT_NHWC)
out = x.row(coord[0]).reshape(blob.size[3], blob.size[1]);
else
{
CV_Assert(coord[1] < blob.size[1]);
out = x.row(coord[0]).reshape(1, std::vector<int> {blob.size[1], blob.size[2] * blob.size[3]});
out = out.row(coord[1]).reshape(1, blob.size[2]);
}
break;
}
return out;
}


CV__DNN_INLINE_NS_END
}} // namespace cv::dnn