![]() |
Re: Region Growing und Rekursive Aufrufe
@Uwe Raabe
Dein Code hat leider immer noch einen Stack Overflow ausgelöst. Ich habe meinen Code noch ausgebessert. Er funktioniert jetzt ohne Fehler (zumindest habe ich noch keinen gefunden). Hier noch einmal der gesamte Code, für alle, die mal schnell einen Algorithmus für Region Growing brauchen:
Delphi-Quellcode:
i ist am Ende die Anzahl der Regionen.
//...
type TXYValues = class Values: array of array of byte; LeftX, LeftY: Integer; private procedure RegionGrowing(x,y,i: integer; var image: TImage); public constructor Create(LengthX, LengthY: Integer); function NoRegionLeft(i: integer): boolean; function IsAenlich(maxunterschied:Integer; var c1,c2:tcolor):boolean; procedure StartRegionGrowing(image: TImage; i: integer); end; //... procedure TXYValues.RegionGrowing(x,y,i: integer; var image: TImage); var c, c1, c2, c3, c4: TColor; begin c := image.Canvas.Pixels[x,y]; c1 := image.canvas.pixels[x+1,y]; c2 := image.Canvas.Pixels[x-1,y]; c3 := image.canvas.pixels[x,y-1]; c4 := image.canvas.pixels[x,y+1]; if form1.progressbar1.position = form1.progressbar1.max then form1.progressbar1.position := 0; form1.progressbar1.Position := form1.progressbar1.Position+1; if (High(values) >= x) and (x >= 0) then if (high(values[x]) >= y) and (y >= 0) then begin if high(values) > x then if (values[x+1][y]=0) then if (IsAenlich(50, c, c1)) then begin values[x+1][y] := i; RegionGrowing(x+1,y,i,image); end; if x > 0 then begin if (values[x-1][y]=0) then if (IsAenlich(50, c, c2)) then begin values[x-1][y] := i; RegionGrowing(x-1,y,i,image); end; end; if y > 0 then begin if (values[x][y-1]=0) then if (IsAenlich(50, c, c3)) then begin values[x][y-1] := i; RegionGrowing(x,y-1,i,image); end; end; if high(values[x]) > y then if (values[x][y+1]=0) then if (IsAenlich(50, c, c4)) then begin values[x][y+1] := i; RegionGrowing(x,y+1,i,image); end; end; end; procedure TXYValues.StartRegionGrowing(image: TImage; i: integer); begin form1.progressbar1.min := 0; form1.progressbar1.position := 0; form1.progressbar1.max := 100; values[leftx][lefty] := i; RegionGrowing(leftx,lefty,i,image); end; function TXYValues.IsAenlich(maxunterschied:Integer; var c1,c2:tcolor):boolean; var r1,g1,b1, r2,g2,b2: byte; crgb1,crgb2: Integer; begin c1 := colortorgb(c1); c2 := colortorgb(c2); r1 := getrvalue(c1); // ROT g1 := getgvalue(c1); // GRÜN b1 := getbvalue(c1); // BLAU r2 := getrvalue(c2); // ROT g2 := getgvalue(c2); // GRÜN b2 := getbvalue(c2); // BLAU if r1+g1+b1 < r2+g2+b2 then begin crgb1 := r1+g1+b1; crgb2 := r2+g2+b2; end else begin crgb2 := r1+g1+b1; crgb1 := r2+g2+b2; end; result := crgb1 >= crgb2 - maxunterschied; end; constructor TXYValues.Create(LengthX, LengthY: Integer); var x, y: integer; begin inherited create; SetLength(Values,lengthx,lengthy); for x := 0 to High(values) do begin for y := 0 to High(values[x]) do begin Values[x][y] := 0; end; end; end; function TXYValues.NoRegionLeft(i: integer): boolean; var x,y: Integer; begin result := true; for x := 1 to High(Values) do begin if result then for y := 1 to High(Values[x]) do begin if result then if Values[x][y] < 1 then begin result := false; Leftx := x; lefty := y; end; end; end; end; //... procedure TForm1.RegionGrowingButtonClick(Sender: TObject); var x, i: Integer; Regions: TXYValues; begin i := 0; Regions := TXYValues.Create(image1.Width, image1.Height); while not Regions.NoRegionLeft(i) do begin inc(i); Regions.StartRegionGrowing(image1,i); end; showmessage(inttostr(i)); Regions.Free; end; Mit Regions[x][y] kann man danach feststellen, welcher Region der Pixel x y angehört. Letztendlich noch einmal vielen Dank an alle, die mir geholfen haben. Falls noch jemand weiß, wie man die Methoden sonst noch verbessern oder schneller machen kann, bitte posten. |
Re: Region Growing und Rekursive Aufrufe
Zitat:
So muss es heißen:
Delphi-Quellcode:
Abde das hier solltest du noch mal überprüfen:procedure Check(c: TColor; x, y, i: integer; image: TImage); var c1: TColor; begin if (x >= 0) and (x < image.Width) and (y >= 0) and (y < image.Height) then begin // Rekursion endet an den Bildgrenzen! c1 := image.canvas.pixels[x, y]; if (values[x, y] = 0) and IsAenlich(50, c, c1) then begin values[x, y] := i; RegionGrowing(x, y, i, image); end; end; end; Zitat:
|
Re: Region Growing und Rekursive Aufrufe
"Solltest du hier nicht bei 0 anstatt 1 anfangen zu iterieren?"
Stimmt. Ich weiß auch nicht warum ich da mit 1 angefangen habe. Danke. Ich habe nochmal den Stack des Programms verzehnfacht, weil ein Stack-Error bei größeren Bildern immer noch aufgetreten ist. Was meint ihr, sollte man für die 50 bei isAenlich einsetzen? Ich habe es mal mit 200 versucht. Das ging ganz gut. Jetzt versuche ich das ganze mit echten Bildern. Schaut bitte noch mal in ![]() |
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:32 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz