Blender  V3.3
intern/track_region.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2011 Blender Foundation. All rights reserved. */
3 
4 #include "intern/track_region.h"
5 #include "intern/image.h"
6 #include "intern/utildefines.h"
7 #include "libmv/image/image.h"
10 
11 /* define this to generate PNG images with content of search areas
12  tracking between which failed */
13 #undef DUMP_FAILURE
14 
15 /* define this to generate PNG images with content of search areas
16  on every iteration of tracking */
17 #undef DUMP_ALWAYS
18 
19 using libmv::FloatImage;
20 using libmv::TrackRegion;
23 
24 namespace {
25 
26 TrackRegionOptions::Direction convertDirection(
27  libmv_TrackRegionDirection direction) {
28  switch (direction) {
30  case LIBMV_TRACK_REGION_BACKWARD: return TrackRegionOptions::BACKWARD;
31  }
32 
33  LOG(FATAL) << "Unhandled tracking direction " << direction
34  << ", should never happen.";
35 
37 }
38 
39 TrackRegionOptions::Mode convertMotionModelToMode(int motion_model) {
40  switch (motion_model) {
41 #define LIBMV_CONVERT(the_model) \
42  case TrackRegionOptions::the_model: return TrackRegionOptions::the_model;
43 
44  LIBMV_CONVERT(TRANSLATION)
45  LIBMV_CONVERT(TRANSLATION_ROTATION)
46  LIBMV_CONVERT(TRANSLATION_SCALE)
47  LIBMV_CONVERT(TRANSLATION_ROTATION_SCALE)
48  LIBMV_CONVERT(AFFINE)
49  LIBMV_CONVERT(HOMOGRAPHY)
50 
51 #undef LIBMV_CONVERT
52  }
53 
54  LOG(FATAL) << "Unhandled motion model " << motion_model
55  << ", should never happen.";
56 
57  return TrackRegionOptions::TRANSLATION;
58 }
59 
60 } // namespace
61 
64  TrackRegionOptions* track_region_options) {
65  track_region_options->direction = convertDirection(options.direction);
66  track_region_options->mode = convertMotionModelToMode(options.motion_model);
67  track_region_options->minimum_correlation = options.minimum_correlation;
68  track_region_options->max_iterations = options.num_iterations;
69  track_region_options->sigma = options.sigma;
70  track_region_options->num_extra_points = 1;
71  track_region_options->image1_mask = NULL;
72  track_region_options->use_brute_initialization = options.use_brute;
73  /* TODO(keir): This will make some cases better, but may be a regression until
74  * the motion model is in. Since this is on trunk, enable it for now.
75  *
76  * TODO(sergey): This gives much worse results on mango footage (see 04_2e)
77  * so disabling for now for until proper prediction model is landed.
78  *
79  * The thing is, currently blender sends input coordinates as the guess to
80  * region tracker and in case of fast motion such an early out ruins the
81  * track.
82  */
83  track_region_options->attempt_refine_before_brute = false;
84  track_region_options->use_normalized_intensities = options.use_normalization;
85 }
86 
87 void libmv_regionTrackergetResult(const TrackRegionResult& track_region_result,
89  result->termination = (int)track_region_result.termination;
90  result->termination_reason = "";
91  result->correlation = track_region_result.correlation;
92 }
93 
95  const float* image1,
96  int image1_width,
97  int image1_height,
98  const float* image2,
99  int image2_width,
100  int image2_height,
101  const double* x1,
102  const double* y1,
103  libmv_TrackRegionResult* /*result*/,
104  double* x2,
105  double* y2) {
106  double xx1[5], yy1[5];
107  double xx2[5], yy2[5];
108  bool tracking_result = false;
109 
110  // Convert to doubles for the libmv api. The four corners and the center.
111  for (int i = 0; i < 5; ++i) {
112  xx1[i] = x1[i];
113  yy1[i] = y1[i];
114  xx2[i] = x2[i];
115  yy2[i] = y2[i];
116  }
117 
118  TrackRegionOptions track_region_options;
119  FloatImage image1_mask;
120 
121  libmv_configureTrackRegionOptions(*options, &track_region_options);
122  if (options->image1_mask) {
124  options->image1_mask, image1_width, image1_height, 1, &image1_mask);
125 
126  track_region_options.image1_mask = &image1_mask;
127  }
128 
129  // Convert from raw float buffers to libmv's FloatImage.
130  FloatImage old_patch, new_patch;
132  image1, image1_width, image1_height, 1, &old_patch);
134  image2, image2_width, image2_height, 1, &new_patch);
135 
136  TrackRegionResult track_region_result;
137  TrackRegion(old_patch,
138  new_patch,
139  xx1,
140  yy1,
141  track_region_options,
142  xx2,
143  yy2,
144  &track_region_result);
145 
146  // Convert to floats for the blender api.
147  for (int i = 0; i < 5; ++i) {
148  x2[i] = xx2[i];
149  y2[i] = yy2[i];
150  }
151 
152  // TODO(keir): Update the termination string with failure details.
153  if (track_region_result.termination == TrackRegionResult::CONVERGENCE ||
154  track_region_result.termination == TrackRegionResult::NO_CONVERGENCE) {
155  tracking_result = true;
156  }
157 
158  // Debug dump of patches.
159 #if defined(DUMP_FAILURE) || defined(DUMP_ALWAYS)
160  bool need_dump = !tracking_result;
161 
162 # ifdef DUMP_ALWAYS
163  need_dump = true;
164 # endif
165 
166  if (need_dump) {
167  libmv_saveImage(old_patch, "old_patch", x1[4], y1[4]);
168  libmv_saveImage(new_patch, "new_patch", x2[4], y2[4]);
169  if (options->image1_mask) {
170  libmv_saveImage(image1_mask, "mask", x2[4], y2[4]);
171  }
172  }
173 #endif
174 
175  return tracking_result;
176 }
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble y1
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble x2
CCL_NAMESPACE_BEGIN struct Options options
bool libmv_saveImage(const FloatImage &image, const char *prefix, int x0, int y0)
void libmv_floatBufferToFloatImage(const float *buffer, int width, int height, int channels, FloatImage *image)
#define LIBMV_CONVERT(the_model)
void libmv_configureTrackRegionOptions(const libmv_TrackRegionOptions &options, TrackRegionOptions *track_region_options)
void libmv_regionTrackergetResult(const TrackRegionResult &track_region_result, libmv_TrackRegionResult *result)
int libmv_trackRegion(const libmv_TrackRegionOptions *options, const float *image1, int image1_width, int image1_height, const float *image2, int image2_width, int image2_height, const double *x1, const double *y1, libmv_TrackRegionResult *, double *x2, double *y2)
libmv_TrackRegionDirection
@ LIBMV_TRACK_REGION_FORWARD
@ LIBMV_TRACK_REGION_BACKWARD
#define LOG(severity)
Definition: log.h:36
void TrackRegion(const FloatImage &image1, const FloatImage &image2, const double *x1, const double *y1, const TrackRegionOptions &options, double *x2, double *y2, TrackRegionResult *result)
Array3Df FloatImage