where : ibrtses delphi
Delphi - floodfill without canvas
disclaimer
the source code of this page may not appear correctly in certain browsers
due to special characters. Have a look at the source of this HTML page
with notepad instead
Sometimes a region with a certain property has to be
filled. Eg the brightness is above a threshold. The image
is assumed a field where the pixels are :
MyField=array[0..width-1,0..height-1]of byte, integer;
The result is assumed also a field as above, the result
pixel is set to 1 when it belongs to the area.
The property test is external and returns true when the
property meets the criteria :
function test(x,y):boolean;
eg : function test(x,y):boolean;
begin
result:=(Image.canvas.pixels[x,y] > threshold);
end;
an additional function inside(x,y) has to be provided,
which checks a new pixel being inside the image.
procedure FloodFill(var image,FResult:MyField; startx,starty:integer);
procedure ff(a,b:integer);
begin
if (FResult[a,b]=0)and(test(image[a,b])) then begin // valid pixel
FResult[a,b]:=1; // set the pixel as used
if inside(a-1,b-1) then ff(a-1,b-1);
if inside(a-1,b) then ff(a-1,b);
if inside(a-1,b+1) then ff(a-1,b+1);
if inside(a,b-1) then ff(a,b-1);
if inside(a,b+1) then ff(a,b+1);
if inside(a+1,b-1) then ff(a+1,b-1);
if inside(a+1,b) then ff(a+1,b);
if inside(a+1,b+1) then ff(a+1,b+1);
end
else FResult[a,b]:=-1; // invalidate pixel
end; // local proc ff
begin
Fresult.clear; // set the result field to zero
ff(startx,starty);
end;
findings
The algorithm runs into a corner and fills from there. Should the
boundary be just one pixel wide, the algorithm may jump across it
diagonally.
This is a core structure and may be extended to :
- find regions which are greater/smaller than a certain number of pixels
- integrate over a region
Feedback is welcome
sponsored links
Delphi
home
last updated: 23.june.99
Copyright (99,2000) Ing.Büro R.Tschaggelar