Tracking: Image from Plane Marker operators

There are two operators added, which are available via a special
content menu next to the plane track image selector:

- New Image from Plane Marker
- Update Image from Plane Marker

The former one creates an image from pixels which the active plane
track marker "sees" at the current frame and sets it as the plane
track's image.

The latter one instead of creating the new image data-block updates
the image in-place.

This allows to create unwarped texture from a billboard from footage.
The intent is to allow this image to be touched up and re-projected
back to the footage with an updated content.

Available from a plane track image context menu, as well as from the
Track menu.

{F13243219}

The demo of the feature from Sebastian Koenig: https://www.youtube.com/watch?v=PDphO-w2SsA

Differential Revision: https://developer.blender.org/D15312
This commit is contained in:
Sergey Sharybin 2022-06-28 16:03:35 +02:00
parent 72b9e07cf2
commit e4bf58e285
6 changed files with 264 additions and 1 deletions

View File

@ -771,8 +771,10 @@ class CLIP_PT_plane_track(CLIP_PT_tracking_panel, Panel):
layout.prop(active_track, "name")
layout.prop(active_track, "use_auto_keying")
layout.template_ID(
row = layout.row()
row.template_ID(
active_track, "image", new="image.new", open="image.open")
row.menu("CLIP_MT_plane_track_image_context_menu", icon='DOWNARROW_HLT', text="")
row = layout.row()
row.active = active_track.image is not None
@ -1482,6 +1484,10 @@ class CLIP_MT_track(Menu):
layout.operator("clip.detect_features")
layout.operator("clip.create_plane_track")
layout.separator()
layout.operator("clip.new_image_from_plane_marker")
layout.operator("clip.update_image_from_plane_marker")
layout.separator()
layout.operator(
@ -1634,6 +1640,16 @@ class CLIP_MT_tracking_context_menu(Menu):
draw_mask_context_menu(layout, context)
class CLIP_MT_plane_track_image_context_menu(Menu):
bl_label = "Plane Track Image Specials"
def draw(self, _context):
layout = self.layout
layout.operator("clip.new_image_from_plane_marker")
layout.operator("clip.update_image_from_plane_marker")
class CLIP_PT_camera_presets(PresetPanel, Panel):
"""Predefined tracking camera intrinsics"""
bl_label = "Camera Presets"
@ -1935,6 +1951,7 @@ classes = (
CLIP_MT_select,
CLIP_MT_select_grouped,
CLIP_MT_tracking_context_menu,
CLIP_MT_plane_track_image_context_menu,
CLIP_PT_camera_presets,
CLIP_PT_track_color_presets,
CLIP_PT_tracking_settings_presets,

View File

@ -560,6 +560,11 @@ struct ImBuf *BKE_tracking_get_search_imbuf(struct ImBuf *ibuf,
bool anchored,
bool disable_channels);
/* Create a new image buffer which consists of pixels which the plane marker "sees".
* The function will choose best image resolution based on the plane marker size. */
struct ImBuf *BKE_tracking_get_plane_imbuf(const struct ImBuf *frame_ibuf,
const struct MovieTrackingPlaneMarker *plane_marker);
/**
* Zap channels from the imbuf that are disabled by the user. this can lead to
* better tracks sometimes. however, instead of simply zeroing the channels

View File

@ -2798,6 +2798,96 @@ ImBuf *BKE_tracking_get_search_imbuf(ImBuf *ibuf,
return searchibuf;
}
BLI_INLINE int plane_marker_size_len_in_pixels(const float a[2],
const float b[2],
const int frame_width,
const int frame_height)
{
const float a_px[2] = {a[0] * frame_width, a[1] * frame_height};
const float b_px[2] = {b[0] * frame_width, b[1] * frame_height};
return ceilf(len_v2v2(a_px, b_px));
}
ImBuf *BKE_tracking_get_plane_imbuf(const ImBuf *frame_ibuf,
const MovieTrackingPlaneMarker *plane_marker)
{
/* Alias for corners, allowing shorter access to coordinates. */
const float(*corners)[2] = plane_marker->corners;
/* Dimensions of the frame image in pixels. */
const int frame_width = frame_ibuf->x;
const int frame_height = frame_ibuf->y;
/* Lengths of left and right edges of the plane marker, in pixels. */
const int left_side_len_px = plane_marker_size_len_in_pixels(
corners[0], corners[3], frame_width, frame_height);
const int right_side_len_px = plane_marker_size_len_in_pixels(
corners[1], corners[2], frame_width, frame_height);
/* Lengths of top and bottom edges of the plane marker, in pixels. */
const int top_side_len_px = plane_marker_size_len_in_pixels(
corners[3], corners[2], frame_width, frame_height);
const int bottom_side_len_px = plane_marker_size_len_in_pixels(
corners[0], corners[1], frame_width, frame_height);
/* Choose the number of samples as a maximum of the corresponding sides in pixels. */
const int num_samples_x = max_ii(top_side_len_px, bottom_side_len_px);
const int num_samples_y = max_ii(left_side_len_px, right_side_len_px);
/* Create new result image with the same type of content as the original. */
ImBuf *plane_ibuf = IMB_allocImBuf(
num_samples_x, num_samples_y, 32, frame_ibuf->rect_float ? IB_rectfloat : IB_rect);
/* Calculate corner coordinates in pixel space, as spearate X/Y arrays. */
const double src_pixel_x[4] = {corners[0][0] * frame_width,
corners[1][0] * frame_width,
corners[2][0] * frame_width,
corners[3][0] * frame_width};
const double src_pixel_y[4] = {corners[0][1] * frame_height,
corners[1][1] * frame_height,
corners[2][1] * frame_height,
corners[3][1] * frame_height};
/* Warped Position is unused but is expected to be provided by the API. */
double warped_position_x, warped_position_y;
/* Actual sampling. */
if (frame_ibuf->rect_float != NULL) {
libmv_samplePlanarPatchFloat(frame_ibuf->rect_float,
frame_ibuf->x,
frame_ibuf->y,
4,
src_pixel_x,
src_pixel_y,
num_samples_x,
num_samples_y,
NULL,
plane_ibuf->rect_float,
&warped_position_x,
&warped_position_y);
}
else {
libmv_samplePlanarPatchByte((unsigned char *)frame_ibuf->rect,
frame_ibuf->x,
frame_ibuf->y,
4,
src_pixel_x,
src_pixel_y,
num_samples_x,
num_samples_y,
NULL,
(unsigned char *)plane_ibuf->rect,
&warped_position_x,
&warped_position_y);
}
plane_ibuf->rect_colorspace = frame_ibuf->rect_colorspace;
plane_ibuf->float_colorspace = frame_ibuf->float_colorspace;
return plane_ibuf;
}
void BKE_tracking_disable_channels(
ImBuf *ibuf, bool disable_red, bool disable_green, bool disable_blue, bool grayscale)
{

View File

@ -251,6 +251,9 @@ void CLIP_OT_slide_plane_marker(struct wmOperatorType *ot);
void CLIP_OT_keyframe_insert(struct wmOperatorType *ot);
void CLIP_OT_keyframe_delete(struct wmOperatorType *ot);
void CLIP_OT_new_image_from_plane_marker(struct wmOperatorType *ot);
void CLIP_OT_update_image_from_plane_marker(struct wmOperatorType *ot);
/* tracking_select.c */
void CLIP_OT_select(struct wmOperatorType *ot);

View File

@ -515,6 +515,9 @@ static void clip_operatortypes(void)
WM_operatortype_append(CLIP_OT_keyframe_insert);
WM_operatortype_append(CLIP_OT_keyframe_delete);
WM_operatortype_append(CLIP_OT_new_image_from_plane_marker);
WM_operatortype_append(CLIP_OT_update_image_from_plane_marker);
/* ** clip_graph_ops.c ** */
/* graph editing */

View File

@ -16,6 +16,7 @@
#include "BLI_utildefines.h"
#include "BKE_context.h"
#include "BKE_image.h"
#include "BKE_movieclip.h"
#include "BKE_report.h"
#include "BKE_tracking.h"
@ -33,6 +34,9 @@
#include "BLT_translation.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
#include "clip_intern.h"
#include "tracking_ops_intern.h"
@ -2213,3 +2217,144 @@ void CLIP_OT_keyframe_delete(wmOperatorType *ot)
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Image from plane track marker
* \{ */
static ImBuf *sample_plane_marker_image_for_operator(bContext *C)
{
SpaceClip *space_clip = CTX_wm_space_clip(C);
const int clip_frame_number = ED_space_clip_get_clip_frame_number(space_clip);
MovieClip *clip = ED_space_clip_get_clip(space_clip);
MovieTracking *tracking = &clip->tracking;
MovieTrackingPlaneTrack *plane_track = tracking->act_plane_track;
const MovieTrackingPlaneMarker *plane_marker = BKE_tracking_plane_marker_get(plane_track,
clip_frame_number);
ImBuf *frame_ibuf = ED_space_clip_get_buffer(space_clip);
if (frame_ibuf == NULL) {
return NULL;
}
ImBuf *plane_ibuf = BKE_tracking_get_plane_imbuf(frame_ibuf, plane_marker);
IMB_freeImBuf(frame_ibuf);
return plane_ibuf;
}
static bool new_image_from_plane_marker_poll(bContext *C)
{
if (!ED_space_clip_tracking_poll(C)) {
return false;
}
SpaceClip *space_clip = CTX_wm_space_clip(C);
MovieClip *clip = ED_space_clip_get_clip(space_clip);
const MovieTracking *tracking = &clip->tracking;
if (tracking->act_plane_track == NULL) {
return false;
}
return true;
}
static int new_image_from_plane_marker_exec(bContext *C, wmOperator *UNUSED(op))
{
SpaceClip *space_clip = CTX_wm_space_clip(C);
MovieClip *clip = ED_space_clip_get_clip(space_clip);
MovieTracking *tracking = &clip->tracking;
MovieTrackingPlaneTrack *plane_track = tracking->act_plane_track;
ImBuf *plane_ibuf = sample_plane_marker_image_for_operator(C);
if (plane_ibuf == NULL) {
return OPERATOR_CANCELLED;
}
plane_track->image = BKE_image_add_from_imbuf(CTX_data_main(C), plane_ibuf, plane_track->name);
IMB_freeImBuf(plane_ibuf);
WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, clip);
return OPERATOR_FINISHED;
}
void CLIP_OT_new_image_from_plane_marker(wmOperatorType *ot)
{
/* identifiers */
ot->name = "New Image from Plane Marker";
ot->description = "Create new image from the content of the plane marker";
ot->idname = "CLIP_OT_new_image_from_plane_marker";
/* api callbacks */
ot->poll = new_image_from_plane_marker_poll;
ot->exec = new_image_from_plane_marker_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
static bool update_image_from_plane_marker_poll(bContext *C)
{
if (!ED_space_clip_tracking_poll(C)) {
return false;
}
SpaceClip *space_clip = CTX_wm_space_clip(C);
MovieClip *clip = ED_space_clip_get_clip(space_clip);
const MovieTracking *tracking = &clip->tracking;
if (tracking->act_plane_track == NULL || tracking->act_plane_track->image == NULL) {
return false;
}
const Image *image = tracking->act_plane_track->image;
return image->type == IMA_TYPE_IMAGE && ELEM(image->source, IMA_SRC_FILE, IMA_SRC_GENERATED);
}
static int update_image_from_plane_marker_exec(bContext *C, wmOperator *UNUSED(op))
{
SpaceClip *space_clip = CTX_wm_space_clip(C);
MovieClip *clip = ED_space_clip_get_clip(space_clip);
MovieTracking *tracking = &clip->tracking;
MovieTrackingPlaneTrack *plane_track = tracking->act_plane_track;
ImBuf *plane_ibuf = sample_plane_marker_image_for_operator(C);
if (plane_ibuf == NULL) {
return OPERATOR_CANCELLED;
}
BKE_image_replace_imbuf(plane_track->image, plane_ibuf);
IMB_freeImBuf(plane_ibuf);
WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, clip);
WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, plane_track->image);
BKE_image_partial_update_mark_full_update(plane_track->image);
return OPERATOR_FINISHED;
}
void CLIP_OT_update_image_from_plane_marker(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Update Image from Plane Marker";
ot->description =
"Update current image used by plane marker from the content of the plane marker";
ot->idname = "CLIP_OT_update_image_from_plane_marker";
/* api callbacks */
ot->poll = update_image_from_plane_marker_poll;
ot->exec = update_image_from_plane_marker_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/** \} */