Fizzi
2007-08-01 19:58:54
First of all, I'd like to thank Smartzkid for helping me with arrays :). Secondly, I wrote this script on a piece of paper when I was bored at work. I'm not really sure how much use it's gonna be, but I thought it was pretty cool. Here's what it does (I suggest you look at the script first before reading this, it might be confusing, I'm not sure):
Step 1: Saves the coordinates of pixels inside the boundaries specified in a checkerboard pattern to Points[].x and Points[].y
Example:
[ ][ ][ ]
[ ][ ][ ]
[ ][ ][ ]
Let these boxes represent pixels inside the boundaries specified by (x1 = 0, y1 = 0, x2 = 2, y2 = 2).
The script would record these coordinates:
Points[0].x := 0, Points[0].y := 0
Points[1].x := 2, Points[1].y := 0
Points[2].x := 1, Points[2].y := 1
Points[3].x := 0, Points[3].y := 2
Points[4].x := 2, Points[4].y := 2
Step 2: Records the color of all the pixels in Points[].Color[0] at time = 0. Then will record all the colors of the same coordinates at time = frequency. Then at time = 2 * frequency, and so on.
Example
[R][Y][R]
[R][Y][R]
[Y][R][R]
Let the pixels with R's in them represent red, and the pixels with Y's in them represent yellow.
At time = 0 the program would record the values:
Points[0].Color[0] := Red
Points[1].Color[0] := Red
Points[2].Color[0] := Yellow
Points[3].Color[0] := Yellow
Points[4].Color[0] := Red
Then after time specified in the variable Frequency, it will do the same thing but record the colors to Points[].Color[1], and so on until time specified in the variable TStop has gone.
Step 3: Compares all the color values recorded in each point. If some colors recorded in a point don't match, then the script knows there was movement. FindMovement would result as true at this point, but FindMovementColorTol has another step.
Example:
Points[0].Colors[0] = Red
Points[0].Colors[1] = Yellow
Points[0].Colors[2] = Red
This would mean that movement happened because the color at that pixel changes.
Step 4 (FindMovementColorTol only): Checks the Points[].Color[] array that resulted in movement being true for any colors that are similar to the color specified in the variable ColorMoving, checks colors with tolerance. This function results as true if movement is found and if one or more of the colors in the pixel that had changing colors is similar to the specified color. This is useful for finding moving colors.
Uses: Really the only thing I'm going to use it for is to detect gas, because the SRL one doesn't work too well for me. If anyone else can think of any other uses, please tell me, i'd love to know :-P.
Here it is:
Step 1: Saves the coordinates of pixels inside the boundaries specified in a checkerboard pattern to Points[].x and Points[].y
Example:
[ ][ ][ ]
[ ][ ][ ]
[ ][ ][ ]
Let these boxes represent pixels inside the boundaries specified by (x1 = 0, y1 = 0, x2 = 2, y2 = 2).
The script would record these coordinates:
Points[0].x := 0, Points[0].y := 0
Points[1].x := 2, Points[1].y := 0
Points[2].x := 1, Points[2].y := 1
Points[3].x := 0, Points[3].y := 2
Points[4].x := 2, Points[4].y := 2
Step 2: Records the color of all the pixels in Points[].Color[0] at time = 0. Then will record all the colors of the same coordinates at time = frequency. Then at time = 2 * frequency, and so on.
Example
[R][Y][R]
[R][Y][R]
[Y][R][R]
Let the pixels with R's in them represent red, and the pixels with Y's in them represent yellow.
At time = 0 the program would record the values:
Points[0].Color[0] := Red
Points[1].Color[0] := Red
Points[2].Color[0] := Yellow
Points[3].Color[0] := Yellow
Points[4].Color[0] := Red
Then after time specified in the variable Frequency, it will do the same thing but record the colors to Points[].Color[1], and so on until time specified in the variable TStop has gone.
Step 3: Compares all the color values recorded in each point. If some colors recorded in a point don't match, then the script knows there was movement. FindMovement would result as true at this point, but FindMovementColorTol has another step.
Example:
Points[0].Colors[0] = Red
Points[0].Colors[1] = Yellow
Points[0].Colors[2] = Red
This would mean that movement happened because the color at that pixel changes.
Step 4 (FindMovementColorTol only): Checks the Points[].Color[] array that resulted in movement being true for any colors that are similar to the color specified in the variable ColorMoving, checks colors with tolerance. This function results as true if movement is found and if one or more of the colors in the pixel that had changing colors is similar to the specified color. This is useful for finding moving colors.
Uses: Really the only thing I'm going to use it for is to detect gas, because the SRL one doesn't work too well for me. If anyone else can think of any other uses, please tell me, i'd love to know :-P.
Here it is:
type ColorPoints = record
x, y : integer;
Color : array of integer;
end;
var
Points: array of ColorPoints;
//Detects movement inside boundaries x1, y1, x2, y2
function FindMovement(TStop, Frequency: Integer; x1, y1, x2, y2 : Integer) : boolean;
var
TSearch, PCount, CCount, slength, sheight, i, k : Integer;
begin
slength := (x2 - x1);
sheight := (y2 - y1);
PCount := 0; CCount := 0;
for i := 0 to sheight do
begin
if (i = 0) or ((i mod 2) = 0) then
begin
for k := 0 to (slength/2) do
begin
setlength(Points, length(Points)+1);
Points[PCount].x := (x1 + (2*k));
Points[PCount].y := (y1 + i);
PCount := (PCount + 1);
end;
end else
begin
if((slength mod 2) = 0) then
begin
for k := 0 to ((slength/2) - 1) do
begin
setlength(Points, length(Points)+1);
Points[PCount].x := (x1 + 1 + (2*k));
Points[PCount].y := (y1 + i);
PCount := PCount + 1;
end;
end else
begin
for k := 0 to (slength/2) do
begin
setlength(Points, length(Points)+1);
Points[PCount].x := (x1 + 1 + (2*k));
Points[PCount].y := (y1 + i);
PCount := (PCount + 1);
end;
end;
end;
end;
MarkTime(TSearch);
repeat
for i := 0 to (PCount - 1) do
begin
setlength(Points[i].Color, length(Points[i].Color)+1);
Points[i].Color[CCount] := GetColor(Points[i].x, Points[i].y);
end;
CCount := CCount + 1;
Wait(Frequency);
until(TimeFromMark(TSearch) > TStop);
for i := 0 to (PCount - 1) do
begin
for k := 0 to (CCount - 2) do
begin
if(Points[i].Color[k] <> Points[i].Color[k+1]) then
begin
Result := True;
Exit;
end;
end;
end;
end;
//Detects movement of a certain color with tolerance
function FindMovementColorTol(TStop, Frequency : Integer; ColorMoving : Integer;
x1, y1, x2, y2 : Integer; Tolerance : Integer) : boolean;
var
TSearch, PCount, CCount, slength, sheight, i, k : Integer;
Movement : Boolean;
begin
slength := (x2 - x1);
sheight := (y2 - y1);
PCount := 0; CCount := 0;
for i := 0 to sheight do
begin
if (i = 0) or ((i mod 2) = 0) then
begin
for k := 0 to (slength/2) do
begin
setlength(Points, length(Points)+1);
Points[PCount].x := (x1 + (2*k));
Points[PCount].y := (y1 + i);
PCount := (PCount + 1);
end;
end else
begin
if((slength mod 2) = 0) then
begin
for k := 0 to ((slength/2) - 1) do
begin
setlength(Points, length(Points)+1);
Points[PCount].x := (x1 + 1 + (2*k));
Points[PCount].y := (y1 + i);
PCount := PCount + 1;
end;
end else
begin
for k := 0 to (slength/2) do
begin
setlength(Points, length(Points)+1);
Points[PCount].x := (x1 + 1 + (2*k));
Points[PCount].y := (y1 + i);
PCount := (PCount + 1);
end;
end;
end;
end;
MarkTime(TSearch);
repeat
for i := 0 to (PCount - 1) do
begin
setlength(Points[i].Color, length(Points[i].Color)+1);
Points[i].Color[CCount] := GetColor(Points[i].x, Points[i].y);
end;
CCount := CCount + 1;
Wait(Frequency);
until(TimeFromMark(TSearch) > TStop);
for i := 0 to (PCount - 1) do
begin
Movement := False;
for k := 0 to (CCount - 2) do
begin
if(Points[i].Color[k] <> Points[i].Color[k+1]) then Movement := True;
end;
if Movement then
begin
for k := 0 to (CCount - 1) do
begin
if(SimilarColors(Points[i].Color[k], ColorMoving, Tolerance)) then
begin
Result := True;
Exit;
end;
end;
end;
end;
end;