hypothesis.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * hypothesis.h
  3. *
  4. * Created on: Jan 18, 2016
  5. * Author: Janai
  6. */
  7. #ifndef HYPOTHESIS_H_
  8. #define HYPOTHESIS_H_
  9. #include <stdio.h>
  10. #include <iostream>
  11. #include <gsl/gsl_fit.h>
  12. #include <gsl/gsl_multifit.h>
  13. #include <opencv2/core.hpp>
  14. #include <opencv2/highgui.hpp>
  15. #include "../penalty_functions/penalty_function.h"
  16. // value to use to represent unknown flow
  17. #define UNKNOWN_FLOW 1e10
  18. #define UNKNOWN_FLOW_THRESH 1e9
  19. using namespace std;
  20. using namespace cv;
  21. enum EXTRA_TYPE { LINEAR_EXTRAPOLATION = 0, QUADRATIC_EXTRAPOLATION = 1 };
  22. enum COMP_METHOD {ADJ = 0, ACC = 1, FINAL = 2};
  23. class hypothesis {
  24. public:
  25. hypothesis():
  26. jet_est(0), energy(DBL_MAX), p(Point2d(0,0)), flow_x(NULL), flow_y(NULL), occlusions(NULL), F(0), startF(0), endF(0), not_extrapolated_length(0), extrapolation_err(0) {
  27. }
  28. hypothesis(int F_):
  29. jet_est(0), energy(DBL_MAX), p(Point2d(0,0)), flow_x(new double[F_]), flow_y(new double[F_]), occlusions(NULL), F(F_), startF(0), endF(F_), not_extrapolated_length(F_), extrapolation_err(0) {
  30. }
  31. hypothesis(int F_, int startF_, int endF_) :
  32. jet_est(0), energy(DBL_MAX), p(Point2d(0,0)), flow_x(new double[F_]), flow_y(new double[F_]), occlusions(NULL), F(F_), startF(startF_), endF(endF_), not_extrapolated_length(endF_ - startF_), extrapolation_err(0) {
  33. }
  34. hypothesis(int F_, double *f_x, double *f_y, double x, double y) :
  35. jet_est(0), energy(DBL_MAX), p(Point2d(x,y)), flow_x(f_x), flow_y(f_y), occlusions(NULL), F(F_), startF(0), endF(F_), not_extrapolated_length(F_), extrapolation_err(0) {
  36. }
  37. hypothesis(int F_, Vec2d f_, Vec2d p_) :
  38. jet_est(0), energy(DBL_MAX), p(Point2d(p_[1],p_[0])), flow_x(new double[F_]), flow_y(new double[F_]), occlusions(NULL), F(F_), startF(0), endF(F_), not_extrapolated_length(F_), extrapolation_err(0) {
  39. for(int t = 0; t < F; t++) {
  40. flow_y[t] = f_[0] * (t + 1);
  41. flow_x[t] = f_[1] * (t + 1);
  42. }
  43. }
  44. hypothesis(int F_, int startF_, int endF_, double *f_x, double *f_y, double x, double y) :
  45. jet_est(0), energy(DBL_MAX), p(Point2d(x,y)), flow_x(f_x), flow_y(f_y), occlusions(NULL), F(F_), startF(startF_), endF(endF_), not_extrapolated_length(endF_ - startF_), extrapolation_err(0) {
  46. }
  47. hypothesis(const hypothesis& h) :
  48. jet_est(h.jet_est), energy(h.energy), p(h.p), flow_x(NULL), flow_y(NULL), occlusions(NULL), F(h.F), startF(h.startF), endF(h.endF), not_extrapolated_length(h.not_extrapolated_length), extrapolation_err(h.extrapolation_err) {
  49. if(h.flow_x != NULL) {
  50. flow_x = new double[F];
  51. memcpy(flow_x, h.flow_x, sizeof(double) * F);
  52. }
  53. if(h.flow_y != NULL) {
  54. flow_y = new double[F];
  55. memcpy(flow_y, h.flow_y, sizeof(double) * F);
  56. }
  57. if(h.occlusions != NULL) {
  58. occlusions = new int[F + 1];
  59. memcpy(occlusions, h.occlusions, sizeof(int) * (F + 1));
  60. }
  61. }
  62. hypothesis(const hypothesis* h) :
  63. jet_est(h->jet_est), energy(h->energy), p(h->p), flow_x(NULL), flow_y(NULL), occlusions(NULL), F(h->F), startF(h->startF), endF(h->endF), not_extrapolated_length(h->not_extrapolated_length), extrapolation_err(h->extrapolation_err) {
  64. if(h->flow_x != NULL) {
  65. flow_x = new double[F];
  66. memcpy(flow_x, h->flow_x, sizeof(double) * F);
  67. }
  68. if(h->flow_y != NULL) {
  69. flow_y = new double[F];
  70. memcpy(flow_y, h->flow_y, sizeof(double) * F);
  71. }
  72. if(h->occlusions != NULL) {
  73. occlusions = new int[(F + 1)];
  74. memcpy(occlusions, h->occlusions, sizeof(int) * (F + 1));
  75. }
  76. }
  77. ~hypothesis() {
  78. if(flow_x != NULL) delete[] flow_x;
  79. if(flow_y != NULL) delete[] flow_y;
  80. if(occlusions != NULL) delete[] occlusions;
  81. }
  82. // extrapolate hypothesis to finalize
  83. hypothesis* new_complete(int approach);
  84. hypothesis* new_perturbed(double u_p, double v_p);
  85. void setLocation(double x_, double y_) {
  86. p.x = x_;
  87. p.y = y_;
  88. }
  89. void setOcclusions(const Mat* forward_flow, const Mat* backward_flow, float occlusion_threshold = 5.0, float occlusion_fb_threshold = 10.0);
  90. static hypothesis* outlier(int F, PenaltyFunction* phi, float e = 1.0f, int cmp_F_ = 0, int cmp_fps_ = 1, int jet_jps_ = 1) {
  91. if(cmp_F_ == 0) cmp_F_ = F;
  92. hypothesis* o = new hypothesis(F, 0, F);
  93. o->jet_est = 0;
  94. o->occlusions = new int[F+1];
  95. o->occlusions[0] = 0;
  96. for(int t = 0; t < F; t++) {
  97. o->flow_y[t] = UNKNOWN_FLOW;
  98. o->flow_x[t] = UNKNOWN_FLOW;
  99. o->occlusions[t+1] = 0;
  100. }
  101. o->energy = phi->apply(e * e);
  102. return o;
  103. }
  104. void replaceFlow(double *f_x, double *f_y) {
  105. if(flow_x != NULL) delete[] flow_x;
  106. if(flow_y != NULL) delete[] flow_y;
  107. flow_x = f_x;
  108. flow_y = f_y;
  109. }
  110. void adaptFPS(int nF) {
  111. if(F > nF && F % nF != 0) cerr << "WARNING: " << F << " is not a multiple of " << nF << "!" << endl;
  112. if(nF > F && nF % F != 0) cerr << "WARNING: " << nF << " is not a multiple of " << F << "!" << endl;
  113. float skip = (1.0f * F) / nF;
  114. F = nF;
  115. startF = 0;
  116. endF = F;
  117. double* n_flow_x = new double[F];
  118. double* n_flow_y = new double[F];
  119. if(skip >= 1) {
  120. for(int i = 0; i < F; i++) {
  121. int off = i * skip + (skip - 1);
  122. n_flow_x[i] = flow_x[off];
  123. n_flow_y[i] = flow_y[off];
  124. }
  125. } else {
  126. for(int i = 0; i < F; i++) {
  127. float last_x = 0;
  128. float last_y = 0;
  129. int off = floor(i * skip);
  130. int offm1 = floor((i - 1) * skip);
  131. if(i > 0) {
  132. last_x = flow_x[offm1];
  133. last_y = flow_y[offm1];
  134. }
  135. n_flow_x[i] = last_x + skip * (flow_x[off] - last_x); // scale last flow
  136. n_flow_y[i] = last_y + skip * (flow_y[off] - last_y); // scale last flow
  137. }
  138. }
  139. delete[] flow_x;
  140. delete[] flow_y;
  141. delete[] occlusions;
  142. flow_x = n_flow_x;
  143. flow_y = n_flow_y;
  144. }
  145. hypothesis& operator =(const hypothesis& h);
  146. double getEnergy() {
  147. return energy;
  148. }
  149. // compare and check status
  150. double distance(hypothesis& h, int method = ACC);
  151. /*
  152. * output:
  153. * -2: distance > thres
  154. * -1: this hypothesis shorter
  155. * 0: both similar
  156. * 1: this hypothesis longer
  157. */
  158. int compare(const hypothesis& h, double thres, int method);
  159. int compare(int x, int y, const Mat *acc_cons_flow, int start, int end, double thres=0.1, int method = ACC);
  160. bool completed() const {
  161. return (startF == 0 && endF == F);
  162. }
  163. // get pixels and flow
  164. double x(int t = 0) {
  165. if(t <= 0) return p.x;
  166. return p.x + flow_x[t - 1];
  167. }
  168. double y(int t = 0) {
  169. if(t <= 0) return p.y;
  170. return p.y + flow_y[t - 1];
  171. }
  172. double u(int t = 0) const {
  173. return flow_x[t];
  174. }
  175. double v(int t = 0) const {
  176. return flow_y[t];
  177. }
  178. int occluded(int t = 0) {
  179. if(occlusions == NULL)
  180. return 0;
  181. else
  182. return occlusions[t];
  183. }
  184. int visible (int t = 0) {
  185. return 1 - occluded(t);
  186. }
  187. // get first frame
  188. int start() {
  189. return startF;
  190. }
  191. int extrapolation_length() {
  192. return F - not_extrapolated_length;
  193. }
  194. int extrapolation_error() {
  195. return extrapolation_err;
  196. }
  197. float score() {
  198. return energy;
  199. }
  200. int jet_est;
  201. double energy;
  202. Point2d p;
  203. double* flow_x,* flow_y;
  204. int* occlusions;
  205. private:
  206. int F;
  207. int startF, endF;
  208. int not_extrapolated_length;
  209. double extrapolation_err;
  210. };
  211. #endif /* HYPOTHESIS_H_ */