SRL Archive - Senior Scripts and Snippets

Canny Edge Finder


Renax

2010-05-18 14:59:07

Awhile ago (07) benland was working on a "Fast" plugin (http://villavu.com/forum/showthread.php?t=17918&highlight=finding+edges)

what interests me is his work with the canny algorithm, If anyone has any knowledge regarding this, or other efficient methods of edge finding I'd love to talk to you :)

msn : [email]SeanLasher@gmail.com[/email]

mastaraymond

2010-05-18 15:14:36

You should talk to killerduo, the algorithm is actually not very hard to implement.. I need to make a canny filter for bitmaps in Simba some time (as I need it for mordaut solving).

Here is some sample code he send me a while ago

{.include SRL/SRL/MISC/SMART.scar}
{.include SRL/SRL.scar}

var
RunescapeBitmap: integer;
TempMat : Array of Array of Integer;

Procedure ColorFilter(var Mat: array of array of Integer; color: string);
var
x,y, red, green, blue: integer;
h,s,l: extended;
begin
Case lowercase(color) of
'grayscale' : begin
For x := 0 to Length(Mat) - 1 do
For y := 0 to Length(Mat[0]) - 1 do
begin
ColorToRGB(Mat[x][y], Red, Green, Blue);
Mat[x][y] := Round(red*0.30 + Green*0.59 + Blue*0.11);
end;
end;
'red' : begin
For x := 0 to Length(Mat) - 1 do
For y := 0 to Length(Mat[0]) - 1 do
begin
ColorToRGB(Mat[x][y], Red, Green, Blue);
Mat[x][y] := red;
end;
end;
'green' : begin
For x := 0 to Length(Mat) - 1 do
For y := 0 to Length(Mat[0]) - 1 do
begin
ColorToRGB(Mat[x][y], Red, Green, Blue);
Mat[x][y] := Green;
end;
end;
'blue' : begin
For x := 0 to Length(Mat) - 1 do
For y := 0 to Length(Mat[0]) - 1 do
begin
ColorToRGB(Mat[x][y], Red, Green, Blue);
Mat[x][y] := Blue;
end;
end;
'hue' : begin
For x := 0 to Length(Mat) - 1 do
For y := 0 to Length(Mat[0]) - 1 do
begin
ColorToHSL(Mat[x][y], h, s, l);
Mat[x][y] := round(h);
end;
end;
'sat' : begin
For x := 0 to Length(Mat) - 1 do
For y := 0 to Length(Mat[0]) - 1 do
begin
ColorToHSL(Mat[x][y], h, s, l);
Mat[x][y] := round(s);
end;
end;
'lum' : begin
For x := 0 to Length(Mat) - 1 do
For y := 0 to Length(Mat[0]) - 1 do
begin
ColorToHSL(Mat[x][y], h, s, l);
Mat[x][y] := round(l);
end;
end;
'x' : begin
For x := 0 to Length(Mat) - 1 do
For y := 0 to Length(Mat[0]) - 1 do
begin
ColorToXYZ(Mat[x][y], h, s, l);
Mat[x][y] := Round(h);
end;
end;
'y' : begin
For x := 0 to Length(TempMat) - 1 do
For y := 0 to Length(TempMat[0]) - 1 do
begin
ColorToXYZ(Mat[x][y], h, s, l);
Mat[x][y] := Round(s);
end;
end;
'z' : begin
For x := 0 to Length(TempMat) - 1 do
For y := 0 to Length(TempMat[0]) - 1 do
begin
ColorToXYZ(Mat[x][y], h, s, l);
Mat[x][y] := Round(l);
end;
end;
'test' : begin
For x := 0 to Length(TempMat) - 1 do
For y := 0 to Length(TempMat[0]) - 1 do
begin
ColorToRGB(Mat[x][y], red, green, blue);
ColorToHSL(Mat[x][y], h, s, l);
Mat[x][y] := integer(red + blue and green );
end;
end;
end;

end;

Function VEdgeDetection({Map : array of array of integer;}): boolean;
var
Gx, Gy : Array of Array of integer;
x, y, mx, my, c, New_x, New_y, g: integer;
begin
SetLength(Gx, 3);
SetLength(Gy, 3);
Gx[0] := [-1, 0, 1];
Gx[1] := [-2, 0, 2];
Gx[2] := [-1, 0, 1];
Gy[0] := [ 1, 2, 1];
Gy[1] := [ 0, 0, 0];
Gy[2] := [-1, -2, -1];
RunescapeBitmap := BitmapFromString(MsX2-MsX1,MsY2-MsY1,'');
CopyClientToBitmap(RunescapeBitmap,MsX1,MsY1,MsX2,MsY2);
TempMat := GetBitmapAreaColors(MsX1,MsY1,MsX2,MsY2);
ColorFilter(TempMat,'grayscale');
For x := 1 to MsX2-MsX1-1 do
For y := 1 to MsY2-MsY1-1 do
begin
New_x := 0;
New_y := 0;
For mx := -1 to 1 do
For my := -1 to 1 do
begin
C := TempMat[x + mx][y + my];
New_x := New_x + c * Gx[mx + 1][my + 1];
New_y := New_y + c * Gy[mx + 1][my + 1];
end;
G := IAbs(New_x) + IAbs(New_y);
C := G *255 / 1000;
FastSetPixel(RunescapeBitmap, x, y, RGBToColor(c,c,c))
end;

DisplayDebugImgWindow(MsX2-MsX1,MsY2-MsY1);
SafeDrawBitmap(RunescapeBitmap, GetDebugCanvas, 0, 0);
end;


begin
SetTargetDC(SmartGetDC);
VEdgeDetection();

end.

Note that would only compile/work in SCAR as he's lazy and won't code in Simba :p.

Renax

2010-05-18 20:55:38

much appreciated :)