Warning: session_start(): open(/var/lib/php/sessions/sess_q6nu1vlmo0gdh1mhk7i5nss1me, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
do not include class IntegralImage.
do not include class IIntegralImage.
srecord noeq MinimalRecognizer(BufferedImage inputImage) {
replace Channel with int.
IIntegralImage mainImage;
new Map clipCache;
static final int grayscale = 3; // channel number for grayscale
abstract class IIntegralImage {
// width and height of image
int w, h;
int liveliness_cachedChannel = -1;
double liveliness_cache;
abstract double integralValue(int x, int y, Channel channel);
BufferedImage render() {
ret imageFromFunction(w, h, (x, y) -> rgbPixel(x, y, x+1, y+1) | fullAlphaMask());
}
double getPixel(Rect r, int channel) {
ret getPixel(r.x, r.y, r.x2(), r.y2(), channel);
}
double getPixel(int channel) { ret getPixel(0, 0, w, h, channel); }
// return value ranges from 0 to 1 (usually)
double getPixel(int x1, int y1, int x2, int y2, int channel) {
ret doubleRatio(rectSum(x1, y1, x2, y2, channel), (x2-x1)*(y2-y1)*255.0);
}
double rectSum(Rect r, int channel) {
ret rectSum(r.x, r.y, r.x2(), r.y2(), channel);
}
double rectSum(int x1, int y1, int x2, int y2, int channel) {
double bottomLeft = integralValue(x1-1, y2-1, channel);
double bottomRight = integralValue(x2-1, y2-1, channel);
double topLeft = integralValue(x1-1, y1-1, channel);
double topRight = integralValue(x2-1, y1-1, channel);
ret bottomRight-topRight-bottomLeft+topLeft;
}
int rgbPixel(int x1, int y1, int x2, int y2) {
int r = iround(clampZeroToOne(getPixel(x1, y1, x2, y2, 0))*255);
int g = iround(clampZeroToOne(getPixel(x1, y1, x2, y2, 1))*255);
int b = iround(clampZeroToOne(getPixel(x1, y1, x2, y2, 2))*255);
ret rgbInt(r, g, b);
}
double liveliness(int channel) {
if (liveliness_cachedChannel != channel) {
// optimization (but no change in semantics):
// if (w <= 1 && h <= 1) ret 0; // liveliness of single pixel is 0
liveliness_cache = standardDeviation(map(q -> q.getPixel(channel), quadrants()));
liveliness_cachedChannel = channel;
}
ret liveliness_cache;
}
// no duplicates, without full image
L descentShapes_cleaned() {
ret uniquify(listMinus(descentShapes(), this));
}
L descentShapes() {
ret centerPlusQuadrants();
}
L centerPlusQuadrants() {
int midX = w/2, midY = h/2;
Rect r = rectAround(iround(midX), iround(midY), max(midX, 1), max(midY, 1));
ret itemPlusList(clip(r), quadrants());
}
L quadrants() {
if (w <= 1 && h <= 1) null; // let's really not have quadrants of a single pixel
int midX = w/2, midY = h/2;
ret mapLL clip(
rect(0, 0, max(midX, 1), max(midY, 1)),
rect(midX, 0, w-midX, max(midY, 1)),
rect(0, midY, max(midX, 1), h-midY),
rect(midX, midY, w-midX, h-midY)
);
}
IIntegralImage liveliestSubshape(int channel) {
ret highestBy(q -> q.liveliness(channel), quadrants());
}
ProbabilisticList liveliestSubshape_probabilistic(int channel) {
ret new ProbabilisticList(map(descentShapes(), shape ->
withProbability(shape.liveliness(channel), shape)));
}
IIntegralImage clip(Rect r) {
ret r.x == 0 && r.y == 0 && r.w == w && r.h == h
? this
: actuallyClip(r);
}
IIntegralImage actuallyClip(Rect r) {
ret newClip(this, r);
}
IIntegralImage clip(int x1, int y1, int w, int h) { ret clip(rect(x1, y1, w, h)); }
Rect positionInImage(IIntegralImage mainImage) {
ret this == mainImage ? positionInImage() : null;
}
Rect positionInImage() {
ret rect(0, 0, w, h);
}
double area() { ret w*h; }
double relativeArea() { ret area()/mainImage.area(); }
bool singlePixel() { ret w <= 1 && h <= 1; }
}
// virtual clip of an integral image
class Clip extends IIntegralImage {
IIntegralImage fullImage;
int x1, y1;
*(IIntegralImage *fullImage, Rect r) {
x1 = r.x; y1 = r.y; w = r.w; h = r.h;
}
*(IIntegralImage *fullImage, int *x1, int *y1, int *w, int *h) {}
public double integralValue(int x, int y, int channel) {
ret fullImage.integralValue(x+x1, y+y1, channel);
}
// don't clip a clip - be smarter than that!
IIntegralImage actuallyClip(Rect r) {
ret newClip(fullImage, translateRect(r, x1, y1));
}
Rect positionInImage() {
ret rect(x1, y1, w, h);
}
Rect positionInImage(IIntegralImage mainImage) {
try object Rect r = super.positionInImage(mainImage);
if (fullImage == mainImage) ret rect(x1, y1, w, h);
null;
}
toString { ret positionInImage() + " in " + fullImage; }
@Override public bool equals(O o) {
if (o == this) true;
if (o cast Clip)
ret eq(positionInImage(), o.positionInImage());
false;
}
@Override public int hashCode() {
ret positionInImage().hashCode();
}
}
class IntegralImage extends IIntegralImage {
int[] data;
*(BufferedImage img) {
w = img.getWidth(); h = img.getHeight();
if (longMul(w, h) > 8000000) fail("Image too big: " + w + "*" + h);
int[] pixels = pixelsOfBufferedImage(img);
data = new int[w*h*3];
int i = 0, j = 0;
int[] sum = new[3];
for y to h: {
for c to 3: sum[c] = 0;
for x to w: {
int rgb = pixels[j++] & 0xFFFFFF;
for c to 3: {
data[i] = (sum[c] += rgb >> 16);
if (y > 0)
data[i] += data[i-w*3];
i++;
rgb = (rgb << 8) & 0xFFFFFF;
}
}
}
}
public double integralValue(int x, int y, Channel channel) {
if (channel == grayscale)
ret doubleAvg(countIterator(3, c -> integralValue(x, y, c)));
ret x < 0 || y < 0 ? 0
: data[(min(y, h-1)*w+min(x, w-1))*3+channel];
}
}
IIntegralImage newClip(IIntegralImage fullImage, Rect r) {
assertSame(fullImage, mainImage);
ret getOrCreate(clipCache, r, () -> new Clip(fullImage, r));
}
IIntegralImage liveliestPointIn(IIntegralImage image) {
ret applyUntilEqual_goOneBackOnNull(c -> c.liveliestSubshape(grayscale), image);
}
// algorithm 1 (old)
int highLevel = 3; // determines how many points you get
// end algorithm 1
int maxPoints = 1000;
int maxSteps = 1000;
// probability of a completely un-lively block to be looked at
double minLiveliness = .1;
// How likely we are to drill down from an area we actually look at
double drillDownProbability = 1.0;
// child must improve liveliness by a factor of this
// in order to win against the parent in search order
// (child is assumed to have a quarter the size of the parent)
double childLivelinessFactor = 1.1;
double featureSize = 0.1;
// feature-level liveliness is scaled with this in the end
double finalLivelinessFactor = 2.0;
int steps;
double lastProbability;
//new BetterLinkedHashSet> liveliestPoints;
new ProbabilisticList liveliestPoints;
void algorithm1 {
new L> schedulers;
schedulers.add(mainImage.liveliestSubshape_probabilistic(grayscale));
while ping (nempty(schedulers)) {
// go to high level
truncateList(schedulers, highLevel);
// clean up used schedulers
while (nempty(schedulers) && empty(last(schedulers)))
removeLast(schedulers);
if (empty(schedulers)) break;
// quick descend
while (l(schedulers) < 20) {
var nextSeed = first(last(schedulers))!;
var newOptions = nextSeed.liveliestSubshape_probabilistic(grayscale);
print(+nextSeed);
pnl(newOptions);
if (empty(newOptions)) break;
removeFirst(last(schedulers));
schedulers.add(newOptions);
}
if (empty(schedulers)) break;
// return a result, take probability from next to last level
liveliestPoints.add(popFirst(last(schedulers)));
if (l(liveliestPoints) >= maxPoints) break;
}
}
// level++ <=> a fourth the area
double level(IIntegralImage image) {
ret -log(image.relativeArea(), 4);
}
double descentProbability(IIntegralImage image, int channel) {
// what depth we at
double level = level(image);
// liveliness of area
double liveliness = rebaseZeroTo(minLiveliness, image.liveliness(channel));
// correct liveliness for child-ness (distance from root)
double factor = pow(1.0/childLivelinessFactor, level);
double corrected = liveliness*factor;
printVars(+image, +level, +liveliness, +factor, +corrected);
ret scoreToProbability(corrected);
}
// featureSize = relative to smaller image dimension
double actualFeatureSize() {
ret featureSize*min(mainImage.w, mainImage.h);
}
double leafValue(IIntegralImage image, int channel) {
Rect r = rectAround(center(image.positionInImage()),
iround(max(actualFeatureSize()/2, 1)));
double value = image.clip(r).liveliness(channel);
double scaled = value*finalLivelinessFactor;
printVars(+scaled, +value, +r, +image);
ret value;
}
run {
mainImage = new IntegralImage(inputImage);
inputImage = null; // save space
//print(liveliness := mainImage.liveliness(grayscale));
print("Full image size: " + mainImage.w + "*" + mainImage.h);
new ProbabilisticList scheduler;
new Set lookedAt;
scheduler.add(WithProbability(mainImage));
int channel = grayscale;
while (nempty(scheduler) && steps++ < maxSteps) {
WithProbability clip = popFirst(scheduler);
lastProbability = clip.probability();
if (!lookedAt.add(clip!))
continue; // We were here before...
print("Looking at clip " + clip);
//var subs = clip->liveliestSubshape_probabilistic(grayscale);
ProbabilisticList subs
= mapToProbabilisticList2(clip->descentShapes_cleaned(),
shape -> descentProbability(shape, channel));
if (empty(subs)) {
print(" Is leaf");
// leaf (single point) - save with value based on
// liveliness of surroundings on a certain level (=scale)
if (!liveliestPoints.containsElement(clip!)) {
liveliestPoints.add(withProbability(leafValue(clip!, channel), clip!));
if (l(liveliestPoints) >= maxPoints) break;
}
} else {
print(" Has " + n2(subs, "sub") + ":");
pnlIndent(subs);
for (var sub : subs) {
// always force at least one descent of every area we actually looked at
var p = sub == first(subs)
? drillDownProbability : descentProbability(sub!, channel);
print(" Descending at " + p + " to " + sub!);
scheduler.at(p, sub!);
}
}
}
print("Have " + nPoints(liveliestPoints) + " after " + nSteps(steps) + " (areas looked at: " + n2(lookedAt) + ", cache size=" + n2(clipCache) + ")");
print("p=" + lastProbability);
pnl(takeFirst(10, liveliestPoints));
var markedImage = mainImage.render();
int markSize = max(3, iround(actualFeatureSize()));
forEach(liveliestPoints, p ->
markPointInImageWithAlpha(
markedImage,
center(p->positionInImage()),
Color.red,
p.probability(),
markSize));
showImage(markedImage);
/*showImageWithSelections(mainImage.render(),
map(liveliestPoints, p -> growRect(1.5, p->positionInImage(mainImage))));*/
//pnl(subshapes);
}
}