PDA

View Full Version : getShiftedPixels();



Citrus
09-18-2015, 04:09 AM
AFAIK all of the pixelShift functions just give you an integer. I wanted the actual points (and bitmaps are weird), so I made this.
It's surprisingly good at finding NPCs when combined with MM2MS().

function getShiftedPixels(b: TBoxArray; time: integer): TPointArray;
var
i: integer;
cols1, cols2: TIntegerArray;
tpa: TPointArray;
begin
tpa := b.toTPA();
cols1 := getColors(tpa, false);
wait(time);
cols2 := getColors(tpa, false);
for i := 0 to high(tpa) do
if cols1[i] <> cols2[i] then result.append(tpa[i]);
end;

Daniel
09-19-2015, 12:13 PM
Nice. You could use inbuilt Simba functions (http://docs.villavu.com/simba/scriptref/tpa.html#cleartpafromtpa) to speed up the process. :)

Citrus
09-19-2015, 12:39 PM
Nice. You could use inbuilt Simba functions (http://docs.villavu.com/simba/scriptref/tpa.html#cleartpafromtpa) to speed up the process. :)

Wait how would that work? There's just one TPA.

Kyle
09-19-2015, 01:02 PM
I actually think this is a really clever idea, never thought of it before, but as with what Daniel said, is it slow? I could see speed being an issue..

Citrus
09-19-2015, 01:05 PM
I actually think this is a really clever idea, never thought of it before, but as with what Daniel said, is it slow? I could see speed being an issue..

It's plenty fast for me. I'm using 50ms and it feels instant.

edit: elfyyy Daniel
This is on my crappy laptop using getShiftedPixels([mainscreen.getBounds()], 100); Takes about 40ms to get the entire mainscreen, which is only 222k integers to compare in the WORST possible case. It's very easy to reduce that number.

https://www.youtube.com/watch?v=4RZ3ZdZmsC0&feature=youtu.be

The fact that it's so simple makes me feel both very smart and very stupid. bonsai This will make auto-coloring so easy.

bonsai
09-19-2015, 11:32 PM
You made a basic motion detector. We had a thread (https://villavu.com/forum/showthread.php?t=110012&highlight=) about it a while back.

That's how the motion detect in bonsaifighter works too but I used bitmaps. I also used the "motion colors" to pre-seed the color list.

Citrus
09-20-2015, 12:29 AM
You made a basic motion detector. We had a thread (https://villavu.com/forum/showthread.php?t=110012&highlight=) about it a while back.

That's how the motion detect in bonsaifighter works too but I used bitmaps. I also used the "motion colors" to pre-seed the color list.

I saw that you used pixelshift but I didn't look thoroughly enough to see exactly what it was doing. I figured you must have been doing something like this. What are the advantages of bitmaps? I was a little confused when I looked at some of the functions in Simba.
I've been running a 150-line combat script with this all day and it can compete with my 1k line auto-color script. It kinda feels wrong..
I'll probably start a new auto-color script using SRL-6 CTS2 functions once I mess with this a bit more.

bonsai
09-20-2015, 10:43 AM
I saw that you used pixelshift but I didn't look thoroughly enough to see exactly what it was doing. I figured you must have been doing something like this. What are the advantages of bitmaps? I was a little confused when I looked at some of the functions in Simba.
I've been running a 150-line combat script with this all day and it can compete with my 1k line auto-color script. It kinda feels wrong..
I'll probably start a new auto-color script using SRL-6 CTS2 functions once I mess with this a bit more.

It's two ways of getting at the same data. I guess it would come down to performance. getcolors is looking at the screen bitmap too. To get best performance, which may be needed, a plugin would be best. That way it can work outside the interpreter.

The problem with motion is it requires the npc to move! And you need to filter out the false results like streams or lights.

For best motion detection you need to look at multiple snaps and keep merging things into a background image. See how cool slacky's demo was in that other thread. That's what you get from background analysis. But of course the second you move it all goes out the window.

Let me know what problems you're having with bitmaps. Here's a bitmap version of your snip. Start it when already logged in:

program motion;
{$DEFINE SMART}
{$include_once srl-6/srl.simba}
var
orig, current, diff, w, h, x, y: integer;

begin
smartPlugins := ['d3d9.dll'];
smartEnableDrawing := true;
SetupSRL();
GetClientDimensions(w, h);
SmartSetEnabled(__smartCurrentTarget, false);
while (true) do
begin
orig := BitmapFromClient(0, 0, w-1, h-1);
wait(60);
current := BitmapFromClient(0, 0, w-1, h-1);
diff := CreateBitmap(w, h);
for x := 0 to w-1 do
for y := 0 to h-1 do
if (FastGetPixel(orig, x, y) <> FastGetPixel(current, x, y)) then
FastSetPixel(diff, x, y, 255);

smartImage.drawBitmap(diff, point(0, 0));
FreeBitmap(orig);
FreeBitmap(current);
FreeBitmap(diff);
end;
end.

Citrus
09-20-2015, 02:13 PM
It's two ways of getting at the same data. I guess it would come down to performance. getcolors is looking at the screen bitmap too. To get best performance, which may be needed, a plugin would be best. That way it can work outside the interpreter.

The problem with motion is it requires the npc to move! And you need to filter out the false results like streams or lights.

For best motion detection you need to look at multiple snaps and keep merging things into a background image. See how cool slacky's demo was in that other thread. That's what you get from background analysis. But of course the second you move it all goes out the window.

Let me know what problems you're having with bitmaps. Here's a bitmap version of your snip. Start it when already logged in:

Non-movings npcs don't seem to be a problem in RS3. They all move a little even when they're standing still.
Filtering out non-npcs is easy with MM2MS();

I was getting confused by calculatePixelShift(), but your code helped. I did a little demo using this:

procedure pixels3(b: TBoxArray; time: integer);
var
orig, curr, diff: TIntegerArray;
w, h, x, y, i: integer;
begin
setLength(orig, length(b));
setLength(curr, length(b));
setLength(diff, length(b));

for i := 0 to high(b) do
orig[i] := bitmapFromClient(b[i]);

wait(time);

for i := 0 to high(b) do
curr[i] := bitmapFromClient(b[i]);

for i := 0 to high(b) do
begin
w := b[i].getWidth();
h := b[i].getHeight();
diff[i] := createBitmap(w, h);
for x := 0 to w-1 do
for y := 0 to h-1 do
if (fastGetPixel(orig[i], x, y) <> fastGetPixel(curr[i], x, y)) then
fastSetPixel(diff[i], x, y, clLime);

smartImage.drawBitmap(diff[i], point(b[i].x1, b[i].y1));
smartImage.drawBox(b[i], false, clBlue);
end;

freeBitmaps(orig);
freeBitmaps(curr);
freeBitmaps(diff);
end;



https://www.youtube.com/watch?v=OYw2Ia_13gA

Daniel
09-21-2015, 10:34 AM
Wait how would that work? There's just one TPA.

Disregard what I said. For some reason, I thought GetColors returned a TPA and I didn't bother to check the data types. Geez, it's been a while.

bg5
09-22-2015, 10:08 AM
Citrus Floor66

I've made once aplugin for motion detection, doing exactly the same, and it seems I've never published it on SRL :P So here is it:

26721

procedure CompareBmp( BitmapAreaColors1, BitmapAreaColors2 : T2dIntegerArray; MaxSize : integer; var _Result : TPointArray );



function GetShiftedTPA ( Box : TBox; WaitTime : integer) : TPointArray;
var bmp1, bmp2 , w ,h : integer;
var BAC1,BAC2 :T2dIntArray;
begin

w := Box.x2 - Box.x1 - 1;
h := Box.y2 - Box.y1 - 1;
bmp1 := BitmapFromClient(Box.x1 , Box.y1 , Box.x2 , Box.y2);
BAC1 := GetBitmapAreaColors(bmp1,0,0,w,h);
wait (WaitTime);
bmp2 := BitmapFromClient(Box.x1 , Box.y1 , Box.x2 , Box.y2);
BAC2 := GetBitmapAreaColors(bmp1,0,0,w,h);

CompareBmp(BAC1,BAC2,(w*h) div 2,Result); // (w*h) div 2 - means MaxSize is a half of box

FreeBitmap(bmp1);
FreeBitmap(bmp2);
end;




* What is MaxSize parameter? Sometimes it happens that colors in the whole screen are shifted e.x. due to some random overall brightness change. In this erroneousness situation function will detect that number of shifted pixels is too big and it will return an empty array ( of the length of 1 ).s

Floor66
09-22-2015, 11:47 AM
Very interesting! I'm going to do some benchmarking :-)

bg5
09-22-2015, 04:20 PM
Floor66

Source, as requested.

26722

slacky
09-22-2015, 05:18 PM
Citrus Floor66

I've made once aplugin for motion detection, doing exactly the same, and it seems I've never published it on SRL :P So here is it:

26721

procedure CompareBmp( BitmapAreaColors1, BitmapAreaColors2 : T2dIntegerArray; MaxSize : integer; var _Result : TPointArray );
Simba has bultin function named `CalculatePixelShift` which pretty much just counts the diff pixels between 2 bitmaps (there are a few others methods for this purpose in simba swell). So I am not sure what good this will do.

bg5
09-22-2015, 05:39 PM
Simba has bultin function named `CalculatePixelShift` which pretty much just counts the diff pixels between 2 bitmaps (there are a few others methods for this purpose in simba swell). So I am not sure what good this will do.

It doesn't count pixels, it returns TPA. TPA can be used then for further processing, much better and more flexible option then simple number of pixels on output (as OP mentioned).

slacky
09-22-2015, 05:55 PM
It doesn't count pixels, it returns TPA. TPA can be used then for further processing, much better and more flexible option then simple number of pixels on output (as OP mentioned).
Aha, my bad. Didn't read over your post properly.