hpFloodFill plugin floods alpha channel of input image based on seed from mask and/or position input. It produces binary mask of flooded area, which can in turn be used for masking original alpha.
hpFloodFill
Updated: 15 October 2020
Author: hendrik
Compatible Nuke versions: 11.3 or later
Compatibility: Linux 64, Windows 64
Demo video
Installation
Copy the /hpTools folder to your Nuke plugin directory, for example ~/.nuke and add lines from init,py and menu.py files to your respective files. Loading code checks for Nuke version and points to correct plugin library (because compiled plugins are not binary compatible between Nuke dot versions).
How to use
NB! Plugin name in Nuke is hpFloodFill since version 1.1
Connect to image with alpha channel and either use position knob to set flooding center or attach mask input to specify seed area. Seed position input has additional radius parameter, which specifies radius around it that is considered to be seed area. If you want to use both mask and position input at the same time, check the 'Use seed position...' checkbox. If not checked, connecting mask disables position seed.
Floodable pixels are compared with seed pixels based on median pixel value under seed area. Use Max difference knob to control the maximum allowed difference. The bigger it is, the more can pixel value differ from seed median.
Debug panel allows viewing intermediate results like floodable area (final result is a subset of this), patch ID codes from first pass and final patch ID-s after they have been connected.
Algorithm
Source code of plugin can be found here: https://gitlab.com/hendrikproosa/floodfill/
Current version is single-threaded, so a bit slow. I will try to add multithreaded processing in next revision. General algorithm for filling goes like this:
- Find seed pixels from mask and/or position input and calculate median of image pixel values under these.
- Create a binary floodability map which shows generally floodable pixels. These are pixels which are within difference threshold from seed area median value.
- Create flood map by checking the floodability of each pixel against its neighbors. Floodability is stored as an 8-bit bit field with each bit representing one direction. Currently only left-right and up-down directions are actually used, diagonals are commented out.
- Create patch IDs by checking connections to left and down. This creates first set of ID-s where connected patches run up and right until breakage.
- Create patch connection data structure. This is a simple array where array index is the original patch ID from previous pass and value is new joined ID. For example if connection is found between patches 2 and 3, new ID is added to array (as current max id + 1) and positions 2 and 3 get the value of that new ID.
- Join data array is walked to find the root ID-s, which in this case are ID-s that don't point to any other ID but to themselves. Every ID checked during walk is then pointed to this root ID. After all values are walked, each member points to their final root, which is the ID of joined patches.
- Seed pixels are checked against final patch ID-s to create a list of ID-s that are floodable from seed. Every patch that is touched by seed pixels is floodable.
- Final render phase checks the ID of current pixel against floodable patch ID list, if it is in there, assing value 1, if not, 0. Result is a binary image of flooded / not flooded pixels.
Version history
v1.2 - fixed bug that occasionally caused Nuke freezing when opening scripts with hpFloodFill
v1.1 - updated plugin class name to hpFloodFill to avoid naming clash with internal hidden plugin.
v1.0 - first working version, floods alpha channel. Single-threaded and thus a bit slow.