MauiKit Image Tools

mycroplayer.cpp
1/*****************************************************************************
2 * mycroplayer.cpp
3 *
4 * Created: 5/28/2020 2020 by mguludag
5 *
6 * Copyright 2020 mguludag. All rights reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *****************************************************************************/
27#include "mycroplayer.hpp"
28
29MyCropLayer::MyCropLayer(const cv::dnn::LayerParams &params)
30 : cv::dnn::Layer(params)
31{}
32
33cv::Ptr<cv::dnn::Layer> MyCropLayer::create(cv::dnn::LayerParams &params)
34{
35 return cv::Ptr<Layer>(new MyCropLayer(params));
36}
37
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
42{
43 CV_UNUSED(requiredOutputs);
44 CV_UNUSED(internals);
45 std::vector<int> outShape(4);
46 outShape[0] = inputs[0][0]; // batch size
47 outShape[1] = inputs[0][1]; // number of channels
48 outShape[2] = inputs[1][2];
49 outShape[3] = inputs[1][3];
50 outputs.assign(1, outShape);
51 return false;
52}
53
54void MyCropLayer::forward(std::vector<cv::Mat *> &input,
55 std::vector<cv::Mat> &output,
56 std::vector<cv::Mat> &internals)
57{
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];
64
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];
69
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));
77 crop.copyTo(targ);
78 }
79 }
80}
81
82void MyCropLayer::forward(cv::InputArrayOfArrays inputs_arr,
83 cv::OutputArrayOfArrays outputs_arr,
84 cv::OutputArrayOfArrays internals_arr)
85{
86 std::cerr << "MyCropLayer:forward ENTERNING" << std::endl << std::flush;
87
88 std::vector<cv::Mat> inputs, outputs;
89 inputs_arr.getMatVector(inputs);
90 outputs_arr.getMatVector(outputs);
91
92 cv::Mat &inp = inputs[0];
93 cv::Mat &out = outputs[0];
94
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];
99
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];
104
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));
112 crop.copyTo(targ);
113 }
114 }
115}
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 11 2025 11:57:09 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.