AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Scanline Schleife sehr langsam!?

Ein Thema von chrizl08 · begonnen am 15. Sep 2008 · letzter Beitrag vom 16. Sep 2008
Antwort Antwort
Seite 1 von 2  1 2      
chrizl08

Registriert seit: 14. Sep 2008
14 Beiträge
 
#1

Scanline Schleife sehr langsam!?

  Alt 15. Sep 2008, 14:07
Hi,
ich habe folgende Funktion um einen bestimmten Farbwert in einem Bitmap zu lokalisieren:

Delphi-Quellcode:
Function FindColor(bmp:TBitmap):TPoint;
type
  PixArray = array[1..3] of Byte;
var
  i, iMax, x, y, w, h: Integer;
  p : ^PixArray;
begin

  Result:=Point(-1,-1);

  Form1.ProgressBar1.Max := bmp.Height-1;

  for y := 0 to bmp.Height-1 do begin
    p := bmp.ScanLine[y];
    Form1.ProgressBar1.Position := y;
    for x := 0 to bmp.Width-1 do begin
      if( (p^[3]=255) AND (p^[2]=0) AND (p^[1]=0) ) then begin
        Showmessage('Gefunden');
        Result := Point(x, y);
        exit;
      end;
    end;
  end;
end;
Das Bitmap wird mit folgendem Code erzeugt:
Delphi-Quellcode:
var DC: HDC;
begin
DC := GetDC(GetDesktopWindow);
bmp:=TBitmap.Create;
try
bmp.Width := GetDeviceCaps(DC, HORZRES);
bmp.Height := GetDeviceCaps(DC, VERTRES);
BitBlt(bmp.Canvas.Handle,0,0,bmp.Width, bmp.Height, DC,0,0,SRCCOPY );
Finally
ReleaseDC(GetDesktopWindow, DC);
end;
end;
Meine Frage nun: Ist es möglich diesen Code zu optimieren (und ich hoffe es ist möglich =) ) und schneller zu machen???

Vielen Dank für Eure Tipps
  Mit Zitat antworten Zitat
Benutzerbild von Neutral General
Neutral General

Registriert seit: 16. Jan 2004
Ort: Bendorf
5.219 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#2

Re: Scanline Schleife sehr langsam!?

  Alt 15. Sep 2008, 14:11
Ja, es ist möglich.

Entferne das

Form1.ProgressBar1.Position := y; aus der Schleife.
Michael
"Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."
  Mit Zitat antworten Zitat
chrizl08

Registriert seit: 14. Sep 2008
14 Beiträge
 
#3

Re: Scanline Schleife sehr langsam!?

  Alt 15. Sep 2008, 14:14
ok Danke aber gibt es noch Möglichkeiten? Ich hatte das ganze davor mit Canvas.Pixels gelöst und es war zwar auch langsam aber deutlich schneller als Scanline. Hatte gelesen das Scanline aber schneller als Pixels ist und dachte ich hätte nen Fehler?!?
  Mit Zitat antworten Zitat
Benutzerbild von smallsmoker
smallsmoker

Registriert seit: 12. Nov 2007
Ort: Duisburg
283 Beiträge
 
#4

Re: Scanline Schleife sehr langsam!?

  Alt 15. Sep 2008, 14:15
[ot]
habe ne andere idee zu deinem problem
habe ich hier gepostet
[/ot]
  Mit Zitat antworten Zitat
Benutzerbild von Neutral General
Neutral General

Registriert seit: 16. Jan 2004
Ort: Bendorf
5.219 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#5

Re: Scanline Schleife sehr langsam!?

  Alt 15. Sep 2008, 14:26
Hi,

Folgendes geht bei mir ziemlich schnell:

Delphi-Quellcode:
procedure TForm1.Image1Click(Sender: TObject);
var i,j: Integer;
    p: PRGBQuad;
    bmp: TBitmap;
begin
  bmp := TBitmap.Create;
  try
    bmp.Assign(Image1.Picture.Bitmap);
    bmp.PixelFormat := pf32Bit;
    for i:= 0 to bmp.Height-1 do
    begin
      p := bmp.ScanLine[i];
      for j:= 0 to bmp.Width-1 do
      begin
        if PCardinal(p)^ = $00FF0000 then // farbe = clred, wobei clred <> $00FF0000. :stupid:
          ShowMessage('Rot gefunden');
        inc(p);
      end;
    end;
  finally
    bmp.Free;
  end;
end;
Michael
"Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."
  Mit Zitat antworten Zitat
Flips

Registriert seit: 17. Feb 2005
Ort: Sankt Wendel
491 Beiträge
 
Delphi 7 Professional
 
#6

Re: Scanline Schleife sehr langsam!?

  Alt 15. Sep 2008, 14:47
Zitat von chrizl08:
Delphi-Quellcode:
Function FindColor(bmp:TBitmap):TPoint;
type
  PixArray = array[1..3] of Byte;
var
  i, iMax, x, y, w, h: Integer;
  p : ^PixArray;
begin

  Result:=Point(-1,-1);

  Form1.ProgressBar1.Max := bmp.Height-1;

  for y := 0 to bmp.Height-1 do begin
    p := bmp.ScanLine[y];
    Form1.ProgressBar1.Position := y;
    for x := 0 to bmp.Width-1 do begin
      if( (p^[3]=255) AND (p^[2]=0) AND (p^[1]=0) ) then begin
        Showmessage('Gefunden');
        Result := Point(x, y);
        exit;
      end;
    end;
  end;
end;
Der Code kann auch gar nicht ordentlich funktionieren,
in die innere Schleife muss noch ein inc(p)!


[Edit]
Mit einer Schleife gehts auch, was folglich auch schneller sein sollte (nur als Beispiel):
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var i : integer;
p:^TRGBTriple;
begin
image1.picture.bitmap.PixelFormat := pf24Bit;
with image1.picture.bitmap do
  begin
    p := scanline[height-1];
    for i := 0 to (height*width)-1 do
      begin
        if p^.rgbtRed = $FF then
          ShowMessage('ja');
        inc(p);
      end;
  end;
end;
[/Edit]
Philipp F.
  Mit Zitat antworten Zitat
chrizl08

Registriert seit: 14. Sep 2008
14 Beiträge
 
#7

Re: Scanline Schleife sehr langsam!?

  Alt 15. Sep 2008, 15:49
Super, vielen Dank!!! Geht echt sehr fix!
Aber eine Frage hab ich noch bzw. ein Problem:
Die Funktion übergibt anscheinend falsche Koordinaten. Ich liege ca. immer 300 Pixel rechts von dem Farbpunkt. Woher kommt das, irgendeine Idee?
  Mit Zitat antworten Zitat
Benutzerbild von Neutral General
Neutral General

Registriert seit: 16. Jan 2004
Ort: Bendorf
5.219 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#8

Re: Scanline Schleife sehr langsam!?

  Alt 15. Sep 2008, 15:58
@Flips: Du weißt schon, dass

if p^.rgbtRed = $FF then nicht zwangsweise "Rot" (clred) sein muss?
Michael
"Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."
  Mit Zitat antworten Zitat
chrizl08

Registriert seit: 14. Sep 2008
14 Beiträge
 
#9

Re: Scanline Schleife sehr langsam!?

  Alt 15. Sep 2008, 16:00
hehe ja

hab aber das if aber auch so gemacht:
( (p^[3]=200) AND (p^[2]=140) AND (p^[1]=60) )
  Mit Zitat antworten Zitat
Flips

Registriert seit: 17. Feb 2005
Ort: Sankt Wendel
491 Beiträge
 
Delphi 7 Professional
 
#10

Re: Scanline Schleife sehr langsam!?

  Alt 15. Sep 2008, 16:37
Zitat von Neutral General:
@Flips: Du weißt schon, dass

if p^.rgbtRed = $FF then nicht zwangsweise "Rot" (clred) sein muss?
Jaja schon klar, war ja auch nur ein schnell hingeschludertes Beispiel
Natürlich müsste man auch noch den G und B Wert prüfen!
Philipp F.
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 20:30 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz