PDA

View Full Version : Find them tree's accurately.



slacky
08-27-2017, 11:27 PM
Well, I thought I'd share a snippet I made many years ago - Adjusted it for this exact usage right now. Someone might find it useful for their scripts, at least if they are making a WC-script. At least the idea is kinda cool, and might be useful for other shit as well.

PS: Note that it's for OSRS, and will likely be useless for RS3.

The snippet looks like this:

function FindTrees(Area:TBox; EdgeDiff, Color, Tolerance:Int32): T2DPointArray;
var
i,c,wid,hei: Int32;
bmp: PtrUInt;
TPA,tmp: TPointArray;
pt: TPoint;
mat: T2DIntArray;
begin
bmp := BitmapFromClient(area.x1,area.y1,area.x2,area.y2);
mat := BitmapToMatrix(bmp);
FreeBitmap(bmp);

FindColorsTolerance(tmp, Color, area.x1+1,area.y1+1,area.x2-1,area.y2-1, Tolerance);
OffsetTPA(tmp, Point(-area.y1,-area.x1));

hei := High(mat);
wid := High(mat[0]);
SetLength(TPA, Length(TMP));
for i:=0 to High(tmp) do
begin
pt := tmp[i];
if (not SimilarColors(mat[pt.y][pt.x], mat[pt.y][pt.x+1], EdgeDiff)) and
(not SimilarColors(mat[pt.y][pt.x], mat[pt.y+1][pt.x], EdgeDiff)) and
(not SimilarColors(mat[pt.y][pt.x], mat[pt.y][pt.x-1], EdgeDiff)) and
(not SimilarColors(mat[pt.y][pt.x], mat[pt.y-1][pt.x], EdgeDiff)) then
TPA[Inc(c)-1] := pt;
end;
SetLength(TPA, c);
OffsetTPA(TPA, Point(area.y1,area.x1));
Result := ClusterTPA(TPA, 2);
end;


And as a result using these parameters:
> SetColorToleranceSpeed(2);
> ATPA := FindTrees([4,4,515,337], 2, 2586985, 90);
> FilterTPAsBetween(ATPA, 0, 350);
https://i.imgur.com/FePVLZR.png

Parameters:
- Area: where to search
- EdgeDiff: Minimum color-difference between adjacent pixels [a low number]
- Color: The color of the tree leafs
- Tolerance: Well, tolerance for searching for them leafs (can be quite high, and is probably preferred)

The method can be extended on as you see fit, it doesn't allow you to adjust everything. However, you might struggle to specify an exact type of tree, since tree leafs have very similar color, and you kinda wanna keep the "tolerance" parameter high.

Dan the man
08-28-2017, 02:33 AM
This is great. Just tested it and it worked perfectly. Used FilterTPAsBetween to filter the smaller trees from the larger trees. Seems to work well :D

slacky
08-28-2017, 02:49 AM
This is great. Just tested it and it worked perfectly. Used FilterTPAsBetween to filter the smaller trees from the larger trees. Seems to work well :D
Aha forgot about that function. Removed the noise parameter, as you can just use that function indeed, and it's more flexible.

Dan the man
08-28-2017, 03:00 AM
Aha forgot about that function. Removed the noise parameter, as you can just use that function indeed, and it's more flexible.

Edit: NVM you fixed it ^.^