27#include "mycroplayer.hpp"
29MyCropLayer::MyCropLayer(
const cv::dnn::LayerParams ¶ms)
30 : cv::dnn::Layer(params)
33cv::Ptr<cv::dnn::Layer> MyCropLayer::create(cv::dnn::LayerParams ¶ms)
35 return cv::Ptr<Layer>(
new MyCropLayer(params));
38bool MyCropLayer::getMemoryShapes(
const std::vector<std::vector<int>> &inputs,
39 const int requiredOutputs,
40 std::vector<std::vector<int>> &outputs,
41 std::vector<std::vector<int>> &internals)
const
43 CV_UNUSED(requiredOutputs);
45 std::vector<int> outShape(4);
46 outShape[0] = inputs[0][0];
47 outShape[1] = inputs[0][1];
48 outShape[2] = inputs[1][2];
49 outShape[3] = inputs[1][3];
50 outputs.assign(1, outShape);
54void MyCropLayer::forward(std::vector<cv::Mat *> &input,
55 std::vector<cv::Mat> &output,
56 std::vector<cv::Mat> &internals)
58 cv::Mat *inp = input[0];
59 cv::Mat out = output[0];
60 int ystart = (inp->size[2] - out.size[2]) / 2;
61 int xstart = (inp->size[3] - out.size[3]) / 2;
62 int yend = ystart + out.size[2];
63 int xend = xstart + out.size[3];
65 const int batchSize = inp->size[0];
66 const int numChannels = inp->size[1];
67 const int height = out.size[2];
68 const int width = out.size[3];
70 int sz[] = {
static_cast<int>(batchSize), numChannels, height, width};
71 out.create(4, sz, CV_32F);
72 for (
int i = 0; i < batchSize; i++) {
73 for (
int j = 0; j < numChannels; j++) {
74 cv::Mat plane(inp->size[2], inp->size[3], CV_32F, inp->ptr<
float>(i, j));
75 cv::Mat crop = plane(cv::Range(ystart, yend), cv::Range(xstart, xend));
76 cv::Mat targ(height, width, CV_32F, out.ptr<
float>(i, j));
82void MyCropLayer::forward(cv::InputArrayOfArrays inputs_arr,
83 cv::OutputArrayOfArrays outputs_arr,
84 cv::OutputArrayOfArrays internals_arr)
86 std::cerr <<
"MyCropLayer:forward ENTERNING" << std::endl << std::flush;
88 std::vector<cv::Mat> inputs, outputs;
89 inputs_arr.getMatVector(inputs);
90 outputs_arr.getMatVector(outputs);
92 cv::Mat &inp = inputs[0];
93 cv::Mat &out = outputs[0];
95 int ystart = (inp.size[2] - out.size[2]) / 2;
96 int xstart = (inp.size[3] - out.size[3]) / 2;
97 int yend = ystart + out.size[2];
98 int xend = xstart + out.size[3];
100 const int batchSize = inp.size[0];
101 const int numChannels = inp.size[1];
102 const int height = out.size[2];
103 const int width = out.size[3];
105 int sz[] = {
static_cast<int>(batchSize), numChannels, height, width};
106 out.create(4, sz, CV_32F);
107 for (
int i = 0; i < batchSize; i++) {
108 for (
int j = 0; j < numChannels; j++) {
109 cv::Mat plane(inp.size[2], inp.size[3], CV_32F, inp.ptr<
float>(i, j));
110 cv::Mat crop = plane(cv::Range(ystart, yend), cv::Range(xstart, xend));
111 cv::Mat targ(height, width, CV_32F, out.ptr<
float>(i, j));