Blender  V2.93
tracking_detect.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2011 Blender Foundation.
17  * All rights reserved.
18  */
19 
26 #include "DNA_gpencil_types.h"
27 #include "DNA_movieclip_types.h"
28 #include "DNA_object_types.h" /* SELECT */
29 
30 #include "BLI_utildefines.h"
31 
32 #include "BKE_tracking.h"
33 
34 #include "IMB_imbuf_types.h"
35 
36 #include "libmv-capi.h"
37 
38 /* Check whether point is inside grease pencil stroke. */
39 static bool check_point_in_stroke(bGPDstroke *stroke, float x, float y)
40 {
41  int count = 0;
42  bGPDspoint *points = stroke->points;
43 
44  /* Count intersections of horizontal ray coming from the point.
45  * Point will be inside layer if and only if number of intersection
46  * is uneven.
47  *
48  * Well, if layer has got self-intersections, this logic wouldn't
49  * work, but such situation is crappy anyway.
50  */
51 
52  for (int i = 0, prev = stroke->totpoints - 1; i < stroke->totpoints; prev = i, i++) {
53  if ((points[i].y < y && points[prev].y >= y) || (points[prev].y < y && points[i].y >= y)) {
54  float fac = (y - points[i].y) / (points[prev].y - points[i].y);
55 
56  if (points[i].x + fac * (points[prev].x - points[i].x) < x) {
57  count++;
58  }
59  }
60  }
61 
62  return (count % 2) ? true : false;
63 }
64 
65 /* Check whether point is inside any stroke of grease pencil layer. */
66 static bool check_point_in_layer(bGPDlayer *layer, float x, float y)
67 {
68  bGPDframe *frame = layer->frames.first;
69 
70  while (frame) {
71  bGPDstroke *stroke = frame->strokes.first;
72 
73  while (stroke) {
74  if (check_point_in_stroke(stroke, x, y)) {
75  return true;
76  }
77 
78  stroke = stroke->next;
79  }
80  frame = frame->next;
81  }
82 
83  return false;
84 }
85 
86 /* Get features detected by libmv and create tracks on the clip for them. */
88  ListBase *tracksbase,
89  struct libmv_Features *features,
90  int framenr,
91  int width,
92  int height,
93  bGPDlayer *layer,
94  bool place_outside_layer)
95 {
96  int a;
97 
98  a = libmv_countFeatures(features);
99  while (a--) {
100  MovieTrackingTrack *track;
101  double x, y, size, score;
102  bool ok = true;
103  float xu, yu;
104 
105  libmv_getFeature(features, a, &x, &y, &score, &size);
106 
107  /* In Libmv integer coordinate points to pixel center, in blender
108  * it's not. Need to add 0.5px offset to center.
109  */
110  xu = (x + 0.5) / width;
111  yu = (y + 0.5) / height;
112 
113  if (layer) {
114  ok = check_point_in_layer(layer, xu, yu) != place_outside_layer;
115  }
116 
117  if (ok) {
118  track = BKE_tracking_track_add(tracking, tracksbase, xu, yu, framenr, width, height);
119  track->flag |= SELECT;
120  track->pat_flag |= SELECT;
121  track->search_flag |= SELECT;
122  }
123  }
124 }
125 
127  ListBase *tracksbase,
128  ImBuf *ibuf,
129  int framenr,
130  bGPDlayer *layer,
131  bool place_outside_layer,
133 {
134  struct libmv_Features *features = NULL;
135 
136  if (ibuf->rect_float) {
137  features = libmv_detectFeaturesFloat(ibuf->rect_float, ibuf->x, ibuf->y, 4, options);
138  }
139  else if (ibuf->rect) {
140  features = libmv_detectFeaturesByte((unsigned char *)ibuf->rect, ibuf->x, ibuf->y, 4, options);
141  }
142 
143  if (features != NULL) {
145  tracking, tracksbase, features, framenr, ibuf->x, ibuf->y, layer, place_outside_layer);
146 
148  }
149 }
150 
151 /* Detect features using FAST detector */
153  ListBase *tracksbase,
154  ImBuf *ibuf,
155  int framenr,
156  int margin,
157  int min_trackness,
158  int min_distance,
159  bGPDlayer *layer,
160  bool place_outside_layer)
161 {
163 
164  options.detector = LIBMV_DETECTOR_FAST;
165  options.margin = margin;
166  options.min_distance = min_distance;
167  options.fast_min_trackness = min_trackness;
168 
170  tracking, tracksbase, ibuf, framenr, layer, place_outside_layer, &options);
171 }
172 
173 /* Detect features using Harris detector */
175  ListBase *tracksbase,
176  ImBuf *ibuf,
177  int framenr,
178  int margin,
179  float threshold,
180  int min_distance,
181  bGPDlayer *layer,
182  bool place_outside_layer)
183 {
185 
186  options.detector = LIBMV_DETECTOR_HARRIS;
187  options.margin = margin;
188  options.min_distance = min_distance;
189  options.harris_threshold = threshold;
190 
192  tracking, tracksbase, ibuf, framenr, layer, place_outside_layer, &options);
193 }
struct MovieTrackingTrack * BKE_tracking_track_add(struct MovieTracking *tracking, struct ListBase *tracksbase, float x, float y, int framenr, int width, int height)
Definition: tracking.c:580
Object is a sort of wrapper for general info.
_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 width
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
_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 y
Contains defines and structs used throughout the imbuf module.
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
#define SELECT
CCL_NAMESPACE_BEGIN struct Options options
libmv_Features * libmv_detectFeaturesByte(const unsigned char *image_buffer, int width, int height, int channels, libmv_DetectOptions *options)
Definition: detector.cc:75
void libmv_getFeature(const libmv_Features *libmv_features, int number, double *x, double *y, double *score, double *size)
Definition: detector.cc:130
libmv_Features * libmv_detectFeaturesFloat(const float *image_buffer, int width, int height, int channels, libmv_DetectOptions *options)
Definition: detector.cc:97
void libmv_featuresDestroy(libmv_Features *libmv_features)
Definition: detector.cc:119
int libmv_countFeatures(const libmv_Features *libmv_features)
Definition: detector.cc:126
@ LIBMV_DETECTOR_FAST
Definition: detector.h:30
@ LIBMV_DETECTOR_HARRIS
Definition: detector.h:32
int count
static unsigned a[3]
Definition: RandGen.cpp:92
unsigned int * rect
float * rect_float
void * first
Definition: DNA_listBase.h:47
struct bGPDframe * next
ListBase strokes
ListBase frames
bGPDspoint * points
struct bGPDstroke * next
Feature * features
Definition: detector.cc:32
static bool check_point_in_stroke(bGPDstroke *stroke, float x, float y)
static void run_configured_detector(MovieTracking *tracking, ListBase *tracksbase, ImBuf *ibuf, int framenr, bGPDlayer *layer, bool place_outside_layer, libmv_DetectOptions *options)
void BKE_tracking_detect_fast(MovieTracking *tracking, ListBase *tracksbase, ImBuf *ibuf, int framenr, int margin, int min_trackness, int min_distance, bGPDlayer *layer, bool place_outside_layer)
static void detect_retrieve_libmv_features(MovieTracking *tracking, ListBase *tracksbase, struct libmv_Features *features, int framenr, int width, int height, bGPDlayer *layer, bool place_outside_layer)
static bool check_point_in_layer(bGPDlayer *layer, float x, float y)
void BKE_tracking_detect_harris(MovieTracking *tracking, ListBase *tracksbase, ImBuf *ibuf, int framenr, int margin, float threshold, int min_distance, bGPDlayer *layer, bool place_outside_layer)