[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [47757] branches/soc-2011-tomato/source/ blender: Couple of changes to keying node:

Sergey Sharybin sergey.vfx at gmail.com
Mon Jun 11 21:31:51 CEST 2012


Revision: 47757
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=47757
Author:   nazgul
Date:     2012-06-11 19:31:51 +0000 (Mon, 11 Jun 2012)
Log Message:
-----------
Couple of changes to keying node:

- Switch from gaussian-bokeh blur to mean-value which is much faster
  and from matte's quality behaves not worse than gaussian-bokeh.
- Added Edge matte output so areas where black/white clipping happens
  could be easily controlled.

  Currently not sure if it's possible than operation returns two
  buffers, so now implemented as separate clipping operation call
  with specific options.

- Changed logic of detecting edges - check if most pixels within
  kernel are different fits some tolerance interval.

- Added two extra options which controls how edges are detecting:

  * Edge Kernel Radius defines radius of kernel within pixel's value
    is checking.
  * Edge Kernel Tolerance defines maximal difference between value
    of current pixel and it's neigbours which are trated as belng to
    the same plane )background of foreground)

Unfortunately, value for this two sliders for existing files shall
be tweaked manually. Default values are 3 for radius and 0.1 for
tolerance.

Modified Paths:
--------------
    branches/soc-2011-tomato/source/blender/compositor/nodes/COM_KeyingNode.cpp
    branches/soc-2011-tomato/source/blender/compositor/nodes/COM_KeyingNode.h
    branches/soc-2011-tomato/source/blender/compositor/operations/COM_KeyingClipOperation.cpp
    branches/soc-2011-tomato/source/blender/compositor/operations/COM_KeyingClipOperation.h
    branches/soc-2011-tomato/source/blender/editors/space_node/drawnode.c
    branches/soc-2011-tomato/source/blender/makesdna/DNA_node_types.h
    branches/soc-2011-tomato/source/blender/makesrna/intern/rna_nodetree.c
    branches/soc-2011-tomato/source/blender/nodes/composite/nodes/node_composite_keying.c

Modified: branches/soc-2011-tomato/source/blender/compositor/nodes/COM_KeyingNode.cpp
===================================================================
--- branches/soc-2011-tomato/source/blender/compositor/nodes/COM_KeyingNode.cpp	2012-06-11 19:31:22 UTC (rev 47756)
+++ branches/soc-2011-tomato/source/blender/compositor/nodes/COM_KeyingNode.cpp	2012-06-11 19:31:51 UTC (rev 47757)
@@ -41,18 +41,12 @@
 
 #include "COM_SetAlphaOperation.h"
 
-#define USE_GAUSSIAN_BLUR
-
 KeyingNode::KeyingNode(bNode *editorNode): Node(editorNode)
 {
 }
 
 OutputSocket *KeyingNode::setupPreBlur(ExecutionSystem *graph, InputSocket *inputImage, int size, OutputSocket **originalImage)
 {
-	memset(&preBlurData, 0, sizeof(preBlurData));
-	preBlurData.sizex = size;
-	preBlurData.sizey = size;
-
 	ConvertRGBToYCCOperation *convertRGBToYCCOperation = new ConvertRGBToYCCOperation();
 	convertRGBToYCCOperation->setMode(0);  /* ITU 601 */
 
@@ -72,20 +66,6 @@
 			addLink(graph, separateOperation->getOutputSocket(0), combineOperation->getInputSocket(channel));
 		}
 		else {
-#ifdef USE_GAUSSIAN_BLUR
-			SetValueOperation *setValueOperation = new SetValueOperation();
-			setValueOperation->setValue(1.0f);
-			graph->addOperation(setValueOperation);
-
-			GaussianBokehBlurOperation *blurOperation = new GaussianBokehBlurOperation();
-			blurOperation->setData(&preBlurData);
-			blurOperation->setQuality(COM_QUALITY_HIGH);
-
-			addLink(graph, separateOperation->getOutputSocket(0), blurOperation->getInputSocket(0));
-			addLink(graph, setValueOperation->getOutputSocket(0), blurOperation->getInputSocket(1));
-			addLink(graph, blurOperation->getOutputSocket(0), combineOperation->getInputSocket(channel));
-			graph->addOperation(blurOperation);
-#else
 			KeyingBlurOperation *blurOperation = new KeyingBlurOperation();
 
 			blurOperation->setSize(size);
@@ -93,7 +73,6 @@
 			addLink(graph, separateOperation->getOutputSocket(0), blurOperation->getInputSocket(0));
 			addLink(graph, blurOperation->getOutputSocket(0), combineOperation->getInputSocket(channel));
 			graph->addOperation(blurOperation);
-#endif
 		}
 	}
 
@@ -109,28 +88,6 @@
 
 OutputSocket *KeyingNode::setupPostBlur(ExecutionSystem *graph, OutputSocket *postBLurInput, int size)
 {
-#ifdef USE_GAUSSIAN_BLUR
-	memset(&postBlurData, 0, sizeof(postBlurData));
-
-	postBlurData.sizex = size;
-	postBlurData.sizey = size;
-
-	SetValueOperation *setValueOperation = new SetValueOperation();
-
-	setValueOperation->setValue(1.0f);
-	graph->addOperation(setValueOperation);
-
-	GaussianBokehBlurOperation *blurOperation = new GaussianBokehBlurOperation();
-	blurOperation->setData(&postBlurData);
-	blurOperation->setQuality(COM_QUALITY_HIGH);
-
-	addLink(graph, postBLurInput, blurOperation->getInputSocket(0));
-	addLink(graph, setValueOperation->getOutputSocket(0), blurOperation->getInputSocket(1));
-
-	graph->addOperation(blurOperation);
-
-	return blurOperation->getOutputSocket();
-#else
 	KeyingBlurOperation *blurOperation = new KeyingBlurOperation();
 
 	blurOperation->setSize(size);
@@ -140,7 +97,6 @@
 	graph->addOperation(blurOperation);
 
 	return blurOperation->getOutputSocket();
-#endif
 }
 
 OutputSocket *KeyingNode::setupDilateErode(ExecutionSystem *graph, OutputSocket *dilateErodeInput, int distance)
@@ -177,12 +133,17 @@
 	return despillOperation->getOutputSocket(0);
 }
 
-OutputSocket *KeyingNode::setupClip(ExecutionSystem *graph, OutputSocket *clipInput, float clipBlack, float clipWhite)
+OutputSocket *KeyingNode::setupClip(ExecutionSystem *graph, OutputSocket *clipInput, int kernelRadius, float kernelTolerance,
+                                    float clipBlack, float clipWhite, bool edgeMatte)
 {
 	KeyingClipOperation *clipOperation = new KeyingClipOperation();
 
+	clipOperation->setKernelRadius(kernelRadius);
+	clipOperation->setKernelTolerance(kernelTolerance);
+
 	clipOperation->setClipBlack(clipBlack);
 	clipOperation->setClipWhite(clipWhite);
+	clipOperation->setIsEdgeMatte(edgeMatte);
 
 	addLink(graph, clipInput, clipOperation->getInputSocket(0));
 
@@ -197,7 +158,8 @@
 	InputSocket *inputScreen = this->getInputSocket(1);
 	OutputSocket *outputImage = this->getOutputSocket(0);
 	OutputSocket *outputMatte = this->getOutputSocket(1);
-	OutputSocket *postprocessedMatte, *postprocessedImage, *originalImage;
+	OutputSocket *outputEdges = this->getOutputSocket(2);
+	OutputSocket *postprocessedMatte, *postprocessedImage, *originalImage, *edgesMatte;
 
 	bNode *editorNode = this->getbNode();
 	NodeKeyingData *keying_data = (NodeKeyingData *) editorNode->storage;
@@ -223,9 +185,16 @@
 
 	postprocessedMatte = keyingOperation->getOutputSocket();
 
-	if (keying_data->clip_black > 0.0f || keying_data->clip_white< 1.0f)
-		postprocessedMatte = setupClip(graph, postprocessedMatte, keying_data->clip_black, keying_data->clip_white);
+	if (keying_data->clip_black > 0.0f || keying_data->clip_white< 1.0f) {
+		postprocessedMatte = setupClip(graph, postprocessedMatte,
+		                               keying_data->edge_kernel_radius, keying_data->edge_kernel_tolerance,
+		                               keying_data->clip_black, keying_data->clip_white, false);
+	}
 
+	edgesMatte = setupClip(graph, postprocessedMatte,
+	                       keying_data->edge_kernel_radius, keying_data->edge_kernel_tolerance,
+	                       keying_data->clip_black, keying_data->clip_white, true);
+
 	/* apply blur on matte if needed */
 	if (keying_data->blur_post)
 		postprocessedMatte = setupPostBlur(graph, postprocessedMatte, keying_data->blur_post);
@@ -250,6 +219,7 @@
 	/* connect result to output sockets */
 	outputImage->relinkConnections(postprocessedImage);
 	outputMatte->relinkConnections(postprocessedMatte);
+	outputEdges->relinkConnections(edgesMatte);
 
 	graph->addOperation(alphaOperation);
 }

Modified: branches/soc-2011-tomato/source/blender/compositor/nodes/COM_KeyingNode.h
===================================================================
--- branches/soc-2011-tomato/source/blender/compositor/nodes/COM_KeyingNode.h	2012-06-11 19:31:22 UTC (rev 47756)
+++ branches/soc-2011-tomato/source/blender/compositor/nodes/COM_KeyingNode.h	2012-06-11 19:31:51 UTC (rev 47757)
@@ -22,7 +22,6 @@
  */
 
 #include "COM_Node.h"
-#include "DNA_node_types.h"
 
 /**
   * @brief KeyingNode
@@ -30,14 +29,12 @@
   */
 class KeyingNode : public Node {
 protected:
-	NodeBlurData preBlurData;
-	NodeBlurData postBlurData;
-
 	OutputSocket *setupPreBlur(ExecutionSystem *graph, InputSocket *inputImage, int size, OutputSocket **originalImage);
 	OutputSocket *setupPostBlur(ExecutionSystem *graph, OutputSocket *postBLurInput, int size);
 	OutputSocket *setupDilateErode(ExecutionSystem *graph, OutputSocket *dilateErodeInput, int distance);
 	OutputSocket *setupDespill(ExecutionSystem *graph, OutputSocket *despillInput, InputSocket *inputSrceen, float factor);
-	OutputSocket *setupClip(ExecutionSystem *graph, OutputSocket *clipInput, float clipBlack, float clipWhite);
+	OutputSocket *setupClip(ExecutionSystem *graph, OutputSocket *clipInput, int kernelRadius, float kernelTolerance,
+	                        float clipBlack, float clipWhite, bool edgeMatte);
 public:
 	KeyingNode(bNode *editorNode);
 	void convertToOperations(ExecutionSystem *graph, CompositorContext *context);

Modified: branches/soc-2011-tomato/source/blender/compositor/operations/COM_KeyingClipOperation.cpp
===================================================================
--- branches/soc-2011-tomato/source/blender/compositor/operations/COM_KeyingClipOperation.cpp	2012-06-11 19:31:22 UTC (rev 47756)
+++ branches/soc-2011-tomato/source/blender/compositor/operations/COM_KeyingClipOperation.cpp	2012-06-11 19:31:51 UTC (rev 47757)
@@ -33,9 +33,14 @@
 	this->addInputSocket(COM_DT_VALUE);
 	this->addOutputSocket(COM_DT_VALUE);
 
+	this->kernelRadius = 3;
+	this->kernelTolerance = 0.1f;
+
 	this->clipBlack = 0.0f;
 	this->clipWhite = 1.0f;
 
+	this->isEdgeMatte = false;
+
 	this->setComplex(true);
 }
 
@@ -48,7 +53,8 @@
 
 void KeyingClipOperation::executePixel(float *color, int x, int y, MemoryBuffer *inputBuffers[], void *data)
 {
-	const int delta = 3;
+	const int delta = this->kernelRadius;
+	const float tolerance = this->kernelTolerance;
 
 	MemoryBuffer *inputBuffer = (MemoryBuffer*)data;
 	float *buffer = inputBuffer->getBuffer();
@@ -56,11 +62,12 @@
 	int bufferWidth = inputBuffer->getWidth();
 	int bufferHeight = inputBuffer->getHeight();
 
-	int count_black = 0, count_white = 0;
-	int i, j;
+	int i, j, count = 0, totalCount = 0;
 
-	int srcIndex = (y * bufferWidth + x) * 4;
+	float value = buffer[(y * bufferWidth + x) * 4];
 
+	bool ok = false;
+
 	for (i = -delta + 1; i < delta; i++) {
 		for (j = -delta + 1; j < delta; j++) {
 			int cx = x + j, cy = y + i;
@@ -70,25 +77,37 @@
 
 			if (cx >= 0 && cx < bufferWidth && cy >= 0 && cy < bufferHeight) {
 				int bufferIndex = (cy * bufferWidth + cx) * 4;
+				float currentValue = buffer[bufferIndex];
 
-				if (buffer[bufferIndex] < 0.4f)
-					count_black++;
-				else if (buffer[bufferIndex] > 0.6f)
-					count_white++;
+				if (fabsf(currentValue - value) < tolerance) {
+					count++;
+				}
+
+				totalCount++;
 			}
 		}
 	}
 
-	color[0] = buffer[srcIndex];
+	ok = count >= (float) totalCount * 0.9f;
 
-	if (count_black >= 22 || count_white >= 22) {
-		if (color[0] < this->clipBlack)
+	if (this->isEdgeMatte) {
+		if (ok)
 			color[0] = 0.0f;
-		else if (color[0] >= this->clipWhite)
+		else
 			color[0] = 1.0f;
-		else
-			color[0] = (color[0] - this->clipBlack) / (this->clipWhite - this->clipBlack);
 	}
+	else {
+		color[0] = value;
+
+		if (ok) {
+			if (color[0] < this->clipBlack)
+				color[0] = 0.0f;
+			else if (color[0] >= this->clipWhite)
+				color[0] = 1.0f;
+			else
+				color[0] = (color[0] - this->clipBlack) / (this->clipWhite - this->clipBlack);
+		}
+	}
 }
 
 bool KeyingClipOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)

Modified: branches/soc-2011-tomato/source/blender/compositor/operations/COM_KeyingClipOperation.h
===================================================================
--- branches/soc-2011-tomato/source/blender/compositor/operations/COM_KeyingClipOperation.h	2012-06-11 19:31:22 UTC (rev 47756)
+++ branches/soc-2011-tomato/source/blender/compositor/operations/COM_KeyingClipOperation.h	2012-06-11 19:31:51 UTC (rev 47757)
@@ -34,12 +34,21 @@
 	float clipBlack;
 	float clipWhite;
 
+	int kernelRadius;

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list