Warning: session_start(): open(/var/lib/php/sessions/sess_s55ab625ifcul345dic4qbdcr5, 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
sclass AutoSegmenter {
int g = 3;
L clips;
bool[] grid;
int gw, gh;
bool diag; // merge diagonally too
BWImage img;
// contrastMethod2 = look at local contrast regardless of absolute brightness
bool contrastMethod2 = true, blackBG;
float contrastThreshold = 0.5f, brightnessThreshold = 0.5f;
int overlap = 1;
*() {}
*(int *g) {}
L go(RGBImage img) { ret go(new BWImage(img)); }
L go(BWImage img) {
this.img = img;
int w = img.getWidth(), h = img.getHeight();
gw = w/g;
gh = h/g; // width & height of grid
step1();
new L result;
clips = new L;
for (int y = 0; y < gh; y++)
for (int x = 0; x < gw; x++)
if (grid[y*gw+x]) {
Rect r = fill(x, y);
r = scaleRect(r, g);
r = blackBG
? autoCropOfBWImage_blackBG(img, r, brightnessThreshold)
: autoCropOfBWImage(img, r, brightnessThreshold);
clips.add(r);
}
ret clips;
}
void step1() {
int w = img.getWidth(), h = img.getHeight();
grid = new bool[gw*gh];
for (int gy = 0; gy <= h- g; gy += g)
next: for (int gx = 0; gx <= w- g; gx += g) {
float min = 1, max = 0;
int y2 = min(h, gy + g + overlap);
int x2 = min(w, gx + g + overlap);
for (int y = gy; y < y2; y++)
for (int x = gx; x < x2; x++) {
float b = img.getPixel(x, y);
min = Math.min(min, b);
max = Math.max(max, b);
if (contrastMethod2
? max-min >= contrastThreshold
: min < brightnessThreshold && max > brightnessThreshold) {
grid[(gy / g) * gw + (gx / g)] = true;
continue next;
}
}
}
}
// with virtual stack
Rect fill(int x, int y) {
Rect r = null;
new L stack;
stack.add(new Pt(x, y));
while (nempty(stack)) {
Pt p = popLast(stack);
x = p.x;
y = p.y;
if (!(x < 0 || y < 0 || x >= gw || y >= gh)) {
int idx = y*gw+x;
if (grid[idx]) {
grid[idx] = false;
Rect me = new Rect(x, y, 1, 1);
if (r == null)
r = me;
else
r = rectUnion(r, me);
stack.add(new Pt(x-1, y));
stack.add(new Pt(x+1, y));
stack.add(new Pt(x, y-1));
stack.add(new Pt(x, y+1));
}
}
}
ret r;
}
float visualizeGrid_darkening = 0.0f;
BWImage visualizeGrid() {
step1();
BWImage im = new BWImage(img);
for (int y = 0; y < gh; y++)
for (int x = 0; x < gw; x++) {
if (grid[y*gw+x]) continue;
darkenBWImagePart(im, new Rect(x*g, y*g, g, g), visualizeGrid_darkening);
}
ret im;
}
}