persistable sclass G22PixelSplitMasks is IG22OptimizedMasksHolder {
replace Mask with IG22Mask.
settable Pt splitPixel;
settable IG22MasksHolder darkHolder;
settable IG22MasksHolder brightHolder;
// for search - how much the "correct" branch is preferred
settable double bias = 0.9;
public L masks() {
ret new ConcatOnDemandList(
darkHolder.masks(),
brightHolder.masks());
}
public L> subHolders() {
ret ll(darkHolder, brightHolder);
}
public void transformSubHolders(IF1> f) {
darkHolder = f.get(darkHolder);
brightHolder = f.get(brightHolder);
}
@Override
public PStackComputable findSimilarMasks(G22FindSimilarMasksTask task) {
ret new FindSimilarMasks(task);
}
record noeq FindSimilarMasks(G22FindSimilarMasksTask task) extends PStackComputableWithStep is IMakeEmptyClone {
bool brightBranch;
public selfType makeEmptyClone() { ret new selfType; }
void step(IPStack stack) {
if (task.ended()) ret with stack.ret();
if (step == 0) {
++step;
bool bright = isMaskBright(task.queryImage);
if (task.verbose()) printVars("step 0", +splitPixel, +bright);
stack.probabilisticOptions(this, ll(
withProbability(1.0, self -> self.brightBranch = bright),
withProbability(1-bias, self -> self.brightBranch = !bright)));
} else {
if (task.verbose()) printVars("step 1", +splitPixel, +brightBranch);
IG22MasksHolder branch = brightBranch ? brightHolder : darkHolder;
stack.tailCall(branch.findSimilarMasks(task));
}
}
}
// Does the mask belong in darkHolder or brightHolder?
bool isMaskBright(Image2B maskImage) {
ret maskImage.getBoolPixel(splitPixel);
}
public IG22MasksHolder cloneTreeWithLabelTransform(IF1 f) {
ret new G22PixelSplitMasks().splitPixel(splitPixel)
.darkHolder(darkHolder.cloneTreeWithLabelTransform(f))
.brightHolder(brightHolder.cloneTreeWithLabelTransform(f));
}
!include early #1035854 // Masks holder Include
}