bonsai
2014-08-26 04:24:14
I've been reading about and messing with basic motion detection. It's pretty darn fun!
I figured I would share a little test harness so anyone else can play with it. It runs fun ghost videos of the motion detected in the sequence you gather.
I bet The Mayor can find a Gelatinous Abomination in this ehehe:

Run it through posterize 88 and the river goes away:

Make a test folder to hold screen snaps. Log into runescape and run the first script. It will grab a few hundred snaps.
Then you can quit runescape and use the second script to see what it looks like if you compare the differences between frame x and x+1. Messing with it to compare to frame x-5, x-10 is good too.
Data capture script:
Analysis script:
I figured I would share a little test harness so anyone else can play with it. It runs fun ghost videos of the motion detected in the sequence you gather.
I bet The Mayor can find a Gelatinous Abomination in this ehehe:

Run it through posterize 88 and the river goes away:

Make a test folder to hold screen snaps. Log into runescape and run the first script. It will grab a few hundred snaps.
Then you can quit runescape and use the second script to see what it looks like if you compare the differences between frame x and x+1. Messing with it to compare to frame x-5, x-10 is good too.
Data capture script:
program grabFrames;
{$DEFINE SMART}
{$include_once srl-6/srl.simba}
var
i: integer;
b1, b2: integer;
outpath, fname: string;
t: ttimemarker;
w, h: integer;
begin
inputquery('Original', 'Enter output folder:', outpath);
smartPlugins := ['d3d9.dll'];
SetupSRL();
writeln('Gathering in 5 seconds...');
sleep(5000);
writeln('Gathering');
t.start();
for i := 0 to 399 do
begin
b1 := bitmapfromclient(mainscreen.getbounds());
getbitmapsize(b1, w, h);
b2 := copybitmap(b1);
// xx := greyscalebitmap(b2); much faster with just grey
getbitmapsize(b2, w, h);
fname := outPath + '\snap.' + inttostr(i) + '.bmp';
savebitmap(b2, fname);
freebitmaps([b1, b2]);
end;
writeln('Done. Time: ', t.getTime(), ' fps=', 100/(t.getTime()/1000));
end.Analysis script:
program showMotion;
{$DEFINE SMART}
{$include_once srl-6/srl.simba}
procedure greyBmp(var bmp: integer);
var b, g: integer;
begin
b := copybitmap(bmp);
g := greyscalebitmap(b);
freebitmaps([bmp, b]);
bmp := g;
end;
procedure posterizeBmp(var bmp: integer; n: integer);
var b, g: integer;
begin
b := copybitmap(bmp);
g := posterizebitmap(b, n);
freebitmaps([bmp, b]);
bmp := g;
end;
procedure desatBmp(var bmp: integer);
var b, g: integer;
begin
b := copybitmap(bmp);
g := desaturate(b);
freebitmaps([bmp, b]);
bmp := g;
end;
function bmpDiff(image1, image2: integer; doGrey: boolean = false;
doDesat: boolean = false; doPost: boolean = false; pval: integer = 0 ): integer;
var
w, h, i, j: integer;
work1, work2, diffBmp, c: integer;
begin
getbitmapsize(image1, w, h);
diffBmp := createbitmap(w,h);
work1 := copybitmap(image1);
work2 := copybitmap(image2);
if (doGrey) then
begin
greyBmp(work1);
greyBmp(work2);
end;
if (doDesat) then
begin
desatBmp(work1);
desatBmp(work2);
end;
if (doPost) then
begin
posterizeBmp(work1, pval);
posterizeBmp(work2, pval);
end;
for i := 0 to w-1 do
for j := 0 to h-1 do
begin
c := fastgetpixel(work2, i, j);
if (fastgetpixel(work1, i, j) <> c) then
fastsetpixel(diffBmp, i, j, c);
end;
result := diffBmp;
freebitmaps([work1, work2]);
end;
{ --------------- main -------------------}
var
i, w, h, ans, pval: integer;
inpath, fname, pstr: string;
bmps: array[0..399] of integer;
outBmps: array[0..398] of integer;
t: TTimeMarker;
doGrey, doDesat, doPost: boolean;
begin
inputquery('Original', 'Enter input folder:', inpath);
ans := messagebox('Greyscale?', '', 4);
if (ans = 6) then doGrey := true;
ans := messagebox('Desaturate?', '', 4);
if (ans = 6) then doDesat := true;
ans := messagebox('Posterize?', '', 4);
if (ans = 6) then doPost := true;
if (doPost) then
begin
inputquery('Posterize', 'Enter number for posterize (0-255):', pstr);
pval := strtoint(pstr);
end;
t.start();
for i := 0 to 399 do
begin
writeln('. processing ', i);
fname := inpath + '\snap.' + inttostr(i) + '.bmp';
bmps[i] := loadbitmap(fname);
if (i > 0) then
outBmps[i-1] := bmpDiff(bmps[i-1], bmps[i], doGrey, doDesat, doPost, pval);
end;
freebitmaps(bmps);
getbitmapsize(outBmps[0], w, h);
writeln('Processing took ', t.getTime());
for i := 0 to 398 do
begin
DrawBitmapDebugImg(outBmps[i]);
displaydebugimgwindow(w,h);
freebitmap(outBmps[i]);
sleep(100);
end;
end.
