|
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html. |
| 4 | + |
| 5 | +// Copyright (C) 2017, Intel Corporation, all rights reserved. |
| 6 | +// Third party copyrights are property of their respective owners. |
| 7 | +#include "../precomp.hpp" |
| 8 | +#include "layers_common.hpp" |
| 9 | +#include <opencv2/imgproc.hpp> |
| 10 | + |
| 11 | +namespace cv { namespace dnn { |
| 12 | + |
| 13 | +class ResizeNearestNeighborLayerImpl : public ResizeNearestNeighborLayer |
| 14 | +{ |
| 15 | +public: |
| 16 | + ResizeNearestNeighborLayerImpl(const LayerParams& params) |
| 17 | + { |
| 18 | + setParamsFrom(params); |
| 19 | + CV_Assert(params.has("width"), params.has("height")); |
| 20 | + outWidth = params.get<float>("width"); |
| 21 | + outHeight = params.get<float>("height"); |
| 22 | + alignCorners = params.get<bool>("align_corners", false); |
| 23 | + if (alignCorners) |
| 24 | + CV_Error(Error::StsNotImplemented, "Nearest neighborhood resize with align_corners=true is not implemented"); |
| 25 | + } |
| 26 | + |
| 27 | + bool getMemoryShapes(const std::vector<MatShape> &inputs, |
| 28 | + const int requiredOutputs, |
| 29 | + std::vector<MatShape> &outputs, |
| 30 | + std::vector<MatShape> &internals) const |
| 31 | + { |
| 32 | + CV_Assert(inputs.size() == 1, inputs[0].size() == 4); |
| 33 | + outputs.resize(1, inputs[0]); |
| 34 | + outputs[0][2] = outHeight; |
| 35 | + outputs[0][3] = outWidth; |
| 36 | + // We can work in-place (do nothing) if input shape == output shape. |
| 37 | + return (outputs[0][2] == inputs[0][2]) && (outputs[0][3] == inputs[0][3]); |
| 38 | + } |
| 39 | + |
| 40 | + void forward(std::vector<Mat*> &inputs, std::vector<Mat> &outputs, std::vector<Mat> &internals) |
| 41 | + { |
| 42 | + CV_TRACE_FUNCTION(); |
| 43 | + CV_TRACE_ARG_VALUE(name, "name", name.c_str()); |
| 44 | + |
| 45 | + if (outHeight == inputs[0]->size[2] && outWidth == inputs[0]->size[3]) |
| 46 | + return; |
| 47 | + |
| 48 | + Mat& inp = *inputs[0]; |
| 49 | + Mat& out = outputs[0]; |
| 50 | + for (size_t n = 0; n < inputs[0]->size[0]; ++n) |
| 51 | + { |
| 52 | + for (size_t ch = 0; ch < inputs[0]->size[1]; ++ch) |
| 53 | + { |
| 54 | + resize(getPlane(inp, n, ch), getPlane(out, n, ch), |
| 55 | + Size(outWidth, outHeight), 0, 0, INTER_NEAREST); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +private: |
| 60 | + int outWidth, outHeight; |
| 61 | + bool alignCorners; |
| 62 | +}; |
| 63 | + |
| 64 | + |
| 65 | +Ptr<ResizeNearestNeighborLayer> ResizeNearestNeighborLayer::create(const LayerParams& params) |
| 66 | +{ |
| 67 | + return Ptr<ResizeNearestNeighborLayer>(new ResizeNearestNeighborLayerImpl(params)); |
| 68 | +} |
| 69 | + |
| 70 | +} // namespace dnn |
| 71 | +} // namespace cv |
0 commit comments