MotionExtrapolatorOp.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 #ifndef MOTION_OP_H_
32 #define MOTION_OP_H_
33 
34 
35 #include <math.h>
36 
37 #include "ImageOp.h"
38 
39 namespace drain
40 {
41 
42 namespace image
43 {
44 
45 
53 //template <class T=unsigned char,class T2=unsigned char>
55 {
56 public:
57 
58 
59  MotionExtrapolatorOp(const std::string &p="1,0") : ImageOp(__FUNCTION__,
60  "Extrapolates an image using a motion field in injection.",
61  "scale,offset",p){};
62  //Applies recursive corrections as post-interpolator. ,rw,rh,rl,rd
63 
67  void process(const ImageT<T> &motion,ImageT<T2> &dst) const {
68  process(motion,dst,dst);
69  };
70 
74  void process(const ImageT<T> &motion,ImageT<T2> &src,ImageT<T2> &dst) const {
75 
76  const int width = src.getWidth();
77  const int height = src.getHeight();
78 
79  AccumulationArray<T> cumulator(width, height);
80 
81  const float scale = this->getParameter("scale",1.0f);
82  const float offset = this->getParameter("offset",0.0f);
83 
84  const ImageT<T> &u = motion.getChannel(0);
85  const ImageT<T> &v = motion.getChannel(1);
86 
87  CoordinateHandler c(width,height);
88  Point2D<int> p;
89  unsigned int a;
90 
91  dst.setGeometry(width,height,1,1);
92 
93  // WEIGHTED
94  //if (motion.getChannelCount()>2){
95  if ((motion.getChannelCount()>2) && (src.hasAlphaChannel())){
96  cumulator.setMethod(AccumulationArray<T>::WAVG,1.0,1.0);
97  std::cerr << "Motion Extrap: weighted\n";
98  const ImageT<T> &wMotion = motion.getChannel(motion.getChannelCount()-1);
99  const ImageT<T2> &wData = src.getAlphaChannel();
100 
101  if (drain::Debug > 3){
102  ImageT<T2> tmp;
103  ScaleOp<T,T2>(1.0,0).process(wMotion,tmp); File::write(tmp,"motion-w0.png");
104  }
105  //const Image<T> &w = motion.getChannel(2); // TODO KORJAA MYƖHEMMIN!
106  for (int j = 0; j < height; ++j) {
107  for (int i = 0; i < width; ++i) {
108  a = src.address(i,j);
109  p.setLocation(i+scale*u.at(a)+offset,j+scale*v.at(a)+offset);
110  // Still inside image?
111  if (c.handle(p) == CoordinateHandler::UNCHANGED){
112  cumulator.add(p.x,p.y,src.at(a),wData.at(a));
113  //cumulator.add(p.x,p.y,src.at(a),0.15*wData.at(a)+0.85*wMotion.at(a)); // TODO 10 pois
114  // if (w.at(a)>1.0) std::cerr << p << ':' << w.at(a) << '\n';
115  //dst.at(a) = src.at(p.x,p.y);
116  //dst.at(i,j,1) = w.at(a);
117  }
118  }
119  }
120  }
121  // UNWEIGHTED
122  else {
123  cumulator.setMethod(AccumulationArray<T>::AVG);
124  for (int j = 0; j < height; ++j) {
125  for (int i = 0; i < width; ++i) {
126  a = src.address(i,j);
127  p.setLocation(i+scale*u.at(a)+offset,j+scale*v.at(a)+offset);
128  // Still inside image?
129  if (c.handle(p) == CoordinateHandler::UNCHANGED){ // TODO ::REVERSIBLE
130  cumulator.add(p.x,p.y,src.at(a),10.0);
131  }
132  //cumulator.add(p.x,p.y,src.at(a),1.0);
133  }
134  }
135  }
136 
137  cumulator.extractTo(dst,"dw");
138  //dst.setChannelCount(1,1);
139 
140 
141  if (drain::Debug > 0){
142  std::cerr << this->name;
143  std::cerr << " offset=" << offset;
144  std::cerr << " scale=" << scale;
145  dst.debug();
146  std::cerr << '\n';
147  }
148  };
149 
150 };
151 
152 }
153 }
154 
155 
156 #endif /*MotionExtrapolatorOP_H_*/
157 
158 // Drain
F getParameter(const std::string &p) const
Gets a single parameter.
Definition: BeanLike.h:86
General-purpose image compositing.
Definition: AccumulationArray.h:112
size_t address(size_t i) const
Computes the index location from image coordinates. Does not involve bit resolution.
Definition: ImageFrame.h:148
Base class for image processing functions.
Definition: ImageOp.h:49
ImageOp(const std::string &name=__FUNCTION__, const std::string &description="")
Definition: ImageOp.h:156
A template class for images with static storage type.
Definition: ImageT.h:67
virtual void setGeometry(size_t width, size_t height, size_t imageChannels=1, size_t alphaChannels=0)
Resizes the image, keeps the current type.
Definition: Image.h:95
Definition: MotionExtrapolatorOp.h:55
Definition: DataSelector.cpp:1277