Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tracking_ops_detect.c « space_clip « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: be6eeffeba78446c8d2e40fca8bc7739744d1262 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2016 Blender Foundation. All rights reserved. */

/** \file
 * \ingroup spclip
 */

#include "DNA_gpencil_types.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"

#include "BKE_context.h"
#include "BKE_movieclip.h"
#include "BKE_report.h"
#include "BKE_tracking.h"

#include "WM_api.h"
#include "WM_types.h"

#include "ED_clip.h"

#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"

#include "RNA_access.h"
#include "RNA_define.h"

#include "clip_intern.h"
#include "tracking_ops_intern.h"

/********************** detect features operator *********************/

static bGPDlayer *detect_get_layer(MovieClip *clip)
{
  if (clip->gpd == NULL) {
    return NULL;
  }
  for (bGPDlayer *layer = clip->gpd->layers.first; layer != NULL; layer = layer->next) {
    if (layer->flag & GP_LAYER_ACTIVE) {
      return layer;
    }
  }
  return NULL;
}

static int detect_features_exec(bContext *C, wmOperator *op)
{
  SpaceClip *sc = CTX_wm_space_clip(C);
  MovieClip *clip = ED_space_clip_get_clip(sc);
  int clip_flag = clip->flag & MCLIP_TIMECODE_FLAGS;
  ImBuf *ibuf = BKE_movieclip_get_ibuf_flag(clip, &sc->user, clip_flag, MOVIECLIP_CACHE_SKIP);
  MovieTracking *tracking = &clip->tracking;
  ListBase *tracksbase = BKE_tracking_get_active_tracks(tracking);
  int placement = RNA_enum_get(op->ptr, "placement");
  int margin = RNA_int_get(op->ptr, "margin");
  int min_distance = RNA_int_get(op->ptr, "min_distance");
  float threshold = RNA_float_get(op->ptr, "threshold");
  int place_outside_layer = 0;
  int framenr = ED_space_clip_get_clip_frame_number(sc);
  bGPDlayer *layer = NULL;

  if (!ibuf) {
    BKE_report(op->reports, RPT_ERROR, "Feature detection requires valid clip frame");
    return OPERATOR_CANCELLED;
  }

  if (placement != 0) {
    layer = detect_get_layer(clip);
    place_outside_layer = placement == 2;
  }

  /* Deselect existing tracks. */
  ed_tracking_deselect_all_tracks(tracksbase);
  /* Run detector. */
  BKE_tracking_detect_harris(tracking,
                             tracksbase,
                             ibuf,
                             framenr,
                             margin,
                             threshold / 100000.0f,
                             min_distance,
                             layer,
                             place_outside_layer);

  IMB_freeImBuf(ibuf);

  BKE_tracking_dopesheet_tag_update(tracking);
  WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, NULL);

  return OPERATOR_FINISHED;
}

void CLIP_OT_detect_features(wmOperatorType *ot)
{
  static const EnumPropertyItem placement_items[] = {
      {0, "FRAME", 0, "Whole Frame", "Place markers across the whole frame"},
      {1,
       "INSIDE_GPENCIL",
       0,
       "Inside Annotated Area",
       "Place markers only inside areas outlined with the Annotation tool"},
      {2,
       "OUTSIDE_GPENCIL",
       0,
       "Outside Annotated Area",
       "Place markers only outside areas outlined with the Annotation tool"},
      {0, NULL, 0, NULL, NULL},
  };

  /* identifiers */
  ot->name = "Detect Features";
  ot->description = "Automatically detect features and place markers to track";
  ot->idname = "CLIP_OT_detect_features";

  /* api callbacks */
  ot->exec = detect_features_exec;
  ot->poll = ED_space_clip_tracking_poll;

  /* flags */
  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

  /* properties */
  RNA_def_enum(
      ot->srna, "placement", placement_items, 0, "Placement", "Placement for detected features");
  RNA_def_int(ot->srna,
              "margin",
              16,
              0,
              INT_MAX,
              "Margin",
              "Only features further than margin pixels from the image "
              "edges are considered",
              0,
              300);
  RNA_def_float(ot->srna,
                "threshold",
                0.5f,
                0.0001f,
                FLT_MAX,
                "Threshold",
                "Threshold level to consider feature good enough for tracking",
                0.0001f,
                FLT_MAX);
  RNA_def_int(ot->srna,
              "min_distance",
              120,
              0,
              INT_MAX,
              "Distance",
              "Minimal distance accepted between two features",
              0,
              300);
}