![]() |
AW: Durchschnittsfarbe eines Bitmap "schnell" ermitteln
Hallo, ich will mal noch meine Version ins Spiel bringen.
Die Funktion GetAverageColor1 wäre die exakte Berechnung. Wenn das Ergebnis nicht 100% exakt sein muss, kann die Funktion GetAverageColor2 verwendet werden.
Delphi-Quellcode:
Ich habe absichtlich auf bereits vorhandene Strukturen (TRgbQuad) die bereits definiert sind verzichtet. Auch auf notwendige try .. finally .. end Strukturen habe ich zur Vereinfachung verzichtet. Der Code läßt sich natürlich noch optimieren, mir geht es hier in erster Linie um das Prinzip.
Type
TRgb = packed record b:Byte; g:Byte; r:Byte; d:Byte; end; TRgbLine = array[0..MaxInt div 4 - 1] of TRgb; PRgbLine = ^ TRgbLine; function GetAverageColor1(bmp:TBitmap):TRgb; var r,g,b:Integer; RgbLine: PRgbLine; p:Integer; PixelCount: Integer; begin PixelCount := bmp.Width * bmp.Height; r := 0; g := 0; b := 0; rgbline := bmp.ScanLine[bmp.Height - 1]; for p := 0 to PixelCount -1 do begin r := r + RgbLine[p].r; g := g + RgbLine[p].g; b := b + RgbLine[p].b; end; Result.r := r div PixelCount; Result.g := g div PixelCount; Result.b := b div PixelCount; end; function GetAverageColor2(bmp:TBitmap;DivFaktor:Integer):TRgb; var r,g,b:Integer; RgbLine: PRgbLine; p:Integer; PixelCount: Integer; i:Integer; begin PixelCount := bmp.Width * bmp.Height; r := 0; g := 0; b := 0; rgbline := bmp.ScanLine[bmp.Height - 1]; for i := 1 to PixelCount div DivFaktor do begin p := Random(PixelCount); r := r + RgbLine[p].r; g := g + RgbLine[p].g; b := b + RgbLine[p].b; end; Result.r := r div (PixelCount div DivFaktor); Result.g := g div (PixelCount div DivFaktor); Result.b := b div (PixelCount div DivFaktor); end; Die Funktion GetAverageColor2 ist bei einem DivFaktor = 1000 um den Faktor 60 schneller als die Funktion GetAverageColor1. Das Prinzip beruht grundsätzlich auf die Tatsache, dass man nicht jeden Pixel auswerten muss. Ich habe das Ganze mit 10 verschiedenen Bildern durchrechnen lassen. In der folgenden Tabelle ist die erste Spalte das exakte Ergebnis. Die 2. Spalte bei einen DivFaktor = 1000 und die 3. Spalte bei einem DivFaktor von 10000.
Code:
R G B R G B R G B
131 102 86 130 102 86 135 105 89 101 89 79 102 89 79 101 89 78 113 118 82 114 119 83 113 118 83 78 81 61 79 82 62 75 76 59 133 119 99 132 118 99 133 119 98 129 141 155 129 141 154 130 142 154 135 145 157 135 145 157 136 145 157 127 123 122 127 123 123 125 121 120 151 146 138 151 146 138 149 144 136 124 117 106 124 117 106 122 115 105 |
AW: Durchschnittsfarbe eines Bitmap "schnell" ermitteln
Zitat:
In einem anderen Projekt habe ich auch mal Durchschnittswerte gebildet, indem ich jedes Pixel in HSB umgerechnet, und die AvgWerte für H, S, B ermittelt und anschließend die Avg-HSB Werte wieder in RGB umgerechnet habe. Egal wie ich die Avg-Werte errechnet habe, irgendwie wirkte die Avg-Farbe immer, wie soll ich es ausdrücken, "schmutzig". Aber vielleicht ist das einfach so bei einer Durchschnittsfarbe. |
AW: Durchschnittsfarbe eines Bitmap "schnell" ermitteln
@Michael II: Ich habe eine mini Erklärung
![]() |
AW: Durchschnittsfarbe eines Bitmap "schnell" ermitteln
Liste der Anhänge anzeigen (Anzahl: 1)
Zitat:
Bei der 32Bit-Version werden für eine Zeile die R, G, B Werte in 32-Bit Registern summiert. Ein Überlauf kann also frühestens dann auftreten, wenn die Bitmap breiter ist als High(Cardinal)/255 = 16843009 Pixel. Die Summen je Zeile werden dann in UInt64-Variablen summiert. Bei der 64Bit-Version werden die Summen der R,G,B Werte in 64Bit-Registern gebildet Ein Überlauf kann somit erst auftreten, wenn die Bitmap mehr als High(UInt64)/255 = 72340172838076673 Pixel hat. Michael II hat das ja auch schon in #40 geschildert. Die von dir ins Spiel gebrachte Bitmap mit 100 Millionen Pixel Höhe und 1 Pixel Breite ist somit für beide Versionen unproblematisch. Wenn die Bitmap allerdings 100 Millionen Pixel breit und 1 Pixel hoch ist, könnte bei der 32Bit-Version ein Überlauf auftreten. Auch das kann man verhindern, aber das kostet Zeit. Ich habe das mal probiert mit einer Bitmap, 20 Mio breit, 1 hoch und alle Pixel = $FFFFFF. Das Testergebnis siehst du in anhängender .jpg Beachte die von der Funktion aus #3 gelieferte Durchschnittsfarbe. Achtung: Hierfür muss auch die
Delphi-Quellcode:
geringfügig geändert werden.
FUNCTION GetAvgColor(Bmp:TBitmap):TColor;
Delphi-Quellcode:
Und so sieht dann die Assembler-Routine aus.
FUNCTION GetAvgColor(Bmp:TBitmap):TColor; overload
var LO,P:NativeInt; begin Assert(Bmp.PixelFormat=pf24bit); Assert(Bmp.Width>0); Assert(Bmp.Height>0); P:=NativeInt(Bmp.ScanLine[0]); if Bmp.Height>1 then LO:=NativeInt(Bmp.ScanLine[1])-P else LO:=0; Result:=AvgColor(P,LO,Bmp.Width,Bmp.Height); end;
Delphi-Quellcode:
FUNCTION AvgColor(P,LO,W,H:NativeInt):TColor;
// P : Zeiger auf das erste Pixel der ersten Zeile einer Bitmap // LO : Offset (in Bytes) auf die jeweils nächste Zeit // W : Breite der Bitmap // H : Höhe der Bitmap {$IFDEF CPUX86} const OfsBlueLo=0; OfsBlueHi=OfsBlueLo+4; OfsGreenLo=OfsBlueHi+4; OfsGreenHi=OfsGreenLo+4; OfsRedLo=OfsGreenHi+4; OfsRedHi=OfsRedLo+4; OfsCount=OfsRedHi+4; OfsH=OfsCount+4; OfsLO=OfsH+4; OfsStack=OfsLO+4; {$ENDIF} asm {$IFDEF CPUX86}// EAX=P, EDX=LO, ECX=W, Stack=H // Register retten push ebx push edi push esi // LO, H und Anzahl Pixel auf Stack legen push edx // LO mov ebx,H push ebx // H imul ebx,ecx push ebx // Anzahl Pixel // Summen auf Stack push 0 push 0 push 0 push 0 push 0 push 0 // ESI hinter erste Zeile lea ebp,[ecx+ecx*2] lea esi,[eax+ebp] neg ebp // Summen ermitteln @Loop1: mov edi,ebp xor ebx,ebx xor ecx,ecx xor edx,edx @Loop2: movzx eax,byte[esi+edi] // Blue add ebx,eax jns @Green add [esp+OfsBlueLo],ebx // Summe Blue adc [esp+OfsBlueHi],0 xor ebx,ebx @Green: movzx eax,byte[esi+edi+1] // Green add ecx,eax jns @Red add [esp+OfsGreenLo],ecx // Summe Green adc [esp+OfsGreenHi],0 xor ecx,ecx @Red: movzx eax,byte[esi+edi+2] // Red add edx,eax jns @Next add [esp+OfsRedLo],edx // Summe Red adc [esp+OfsRedHi],0 xor edx,edx @Next: add edi,3 jl @Loop2 // Nächstes Pixel add [esp+OfsBlueLo],ebx // Summe Blue adc [esp+OfsBlueHi],0 add [esp+OfsGreenLo],ecx // Summe Green adc [esp+OfsGreenHi],0 add [esp+OfsRedLo],edx // Summe Red adc [esp+OfsRedHi],0 // Zeiger auf nächste Zeile add esi,[esp+OfsLO]; dec [esp+OfsH] jnz @Loop1 // AvgWerte erbitteln mov eax,[esp+OfsBlueLo] mov edx,[esp+OfsBlueHi] div [esp+OfsCount] movzx ecx,al shl ecx,16 mov eax,[esp+OfsGreenLo] mov edx,[esp+OfsGreenHi] div [esp+OfsCount] mov ch,al mov eax,[esp+OfsRedLo] mov edx,[esp+OfsRedHi] div [esp+OfsCount] mov cl,al mov eax,ecx // Result=AvgColor // Stack bereinigen add esp,OfsStack // Register wieder herstellen pop esi pop edi pop ebx {$ELSE} // RCX=P, RDX=LO, R8=W, R9=H push r12 push r13 push r14 // Anzahl Pixel in R13 mov r13,R8 imul R13,R9 // R11 hinter erste Zeile, R12=-W*3 lea r12,[r8+r8*2] lea r11,[rcx+r12] neg r12 // Summen ermitteln xor rcx,rcx // Summe Blue xor r8,r8 // Summe Green xor r10,r10 // Summe Red @Loop1: mov r14,r12 @Loop2: movzx rax,byte[r11+r14] // Blue add rcx,rax movzx rax,byte[r11+r14+1] // Green add r8,rax movzx rax,byte[r11+r14+2] // Red add r10,rax add r14,3 jl @Loop2 // Nächstes Pixel // Zeiger auf nächste Zeile add r11,rdx; dec r9 jnz @Loop1 // AvgWerte erbitteln mov rax,rcx // Blue xor rdx,rdx div r13 movzx rcx,al shl rcx,16 mov rax,r8 // Green xor rdx,rdx div r13 mov ch,al mov rax,r10 xor rdx,rdx div r13 mov cl,al mov rax,rcx // Result=AvgColor // Register wieder herstellen pop r14 pop r13 pop r12 {$ENDIF} end; |
AW: Durchschnittsfarbe eines Bitmap "schnell" ermitteln
Hier habe ich noch einen Code gefunden, aber wie es ausschaut machen die den gleichen Ansatz:
Delphi-Quellcode:
Imports System.Drawing.Imaging
Imports System.IO Imports System.Runtime.InteropServices Public Class Form1 Private WithEvents pb As New PictureBox Private WithEvents cb As New CheckBox Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. Me.Controls.Add(pb) Me.Controls.Add(cb) pb.Size = New Size(Me.ClientSize.Width - 50, Me.ClientSize.Height - 50) pb.Location = New Point(25, 25) pb.Anchor = AnchorStyles.Left Or AnchorStyles.Bottom Or AnchorStyles.Right Or AnchorStyles.Top pb.SizeMode = PictureBoxSizeMode.StretchImage pb.BackColor = Color.Gray cb.Location = New Point(2, 2) cb.Text = "Use Lockbits Technique" cb.Checked = True Me.Text = "click the picturebox" Me.Size = New Size(640, 480) Me.BackColor = SystemColors.Control End Sub Private Sub pb_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pb.Click Using ofd As New OpenFileDialog ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures ofd.Filter = "Image Files(*.Bmp;*.Jpg;*.Gif;*.Png)|*.Bmp;*.Jpg;*.Gif;*.Png|All files (*.*)|*.*" ofd.Title = "Select a picture" Dim result As DialogResult = ofd.ShowDialog If result = Windows.Forms.DialogResult.OK Then Dim bm As Bitmap Try bm = DirectCast(Bitmap.FromFile(ofd.FileName), Bitmap) Catch ex As OutOfMemoryException MessageBox.Show("Couldn't load that file") Exit Sub Catch ex As FileNotFoundException MessageBox.Show("Couldn't find that file") Exit Sub End Try pb.Image = bm If cb.Checked Then Me.BackColor = GetAverageColor1(bm) Else Me.BackColor = GetAverageColor2(bm) End If Me.Text = Me.BackColor.ToString End If End Using End Sub Private Function GetAverageColor1(ByVal bm As Bitmap) As Color If bm.PixelFormat <> PixelFormat.Format24bppRgb Then MessageBox.Show("Image was not 24bppRgb") Return Color.Black End If Dim bounds As New Rectangle(0, 0, bm.Width, bm.Height) Dim bmd As BitmapData = bm.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb) ' The stride is the width of 1 row of pixels in bytes. As 1 pixels requires 3 bytes of color ' information, you would think this would always be 3 * bm.Width - But it isn't. Each row of ' pixels is aligned so that it starts at a 4 byte boundary, this is done by padding rows with ' extra bytes if required. (might be 8 byte boundary on x64) Dim stride As Integer = bmd.Stride ' An array to store the color information: Dim pixels(bmd.Stride * bm.Height - 1) As Byte ' Copy it all out of the bitmap: Marshal.Copy(bmd.Scan0, pixels, 0, pixels.Length) bm.UnlockBits(bmd) Dim totalR As UInteger Dim totalG As UInteger Dim totalB As UInteger For y As Integer = 0 To bm.Height - 1 For x As Integer = 0 To bm.Width - 1 ' Get the index of a pixel in the array. ' The index will be the number of bytes in all the rows above the pixel, ' which is (y * stride) ' plus the number of bytes in all the pixels to the left of it ' so add x*3: Dim index As Integer = (y * stride) + (x * 3) totalB += pixels(index) totalG += pixels(index + 1) totalR += pixels(index + 2) Next Next ' Average the components Dim pixelCount As Integer = bm.Width * bm.Height Dim averageR As Integer = CType(totalR \ pixelCount, Integer) Dim averageG As Integer = CType(totalG \ pixelCount, Integer) Dim averageB As Integer = CType(totalB \ pixelCount, Integer) Return Color.FromArgb(averageR, averageG, averageB) End Function Private Function GetAverageColor2(ByVal bm As Bitmap) As Color ' Slower, but simpler, way. Dim totalR As UInteger Dim totalG As UInteger Dim totalB As UInteger For y As Integer = 0 To bm.Height - 1 For x As Integer = 0 To bm.Width - 1 totalR += bm.GetPixel(x, y).R totalG += bm.GetPixel(x, y).G totalB += bm.GetPixel(x, y).B Next Next Dim pixelCount As Integer = bm.Width * bm.Height Dim averageR As Integer = CType(totalR \ pixelCount, Integer) Dim averageG As Integer = CType(totalG \ pixelCount, Integer) Dim averageB As Integer = CType(totalB \ pixelCount, Integer) Return Color.FromArgb(averageR, averageG, averageB) End Function Private Sub cb_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cb.CheckedChanged pb.Image = Nothing Me.BackColor = SystemColors.Control End Sub End Class ![]() |
AW: Durchschnittsfarbe eines Bitmap "schnell" ermitteln
Zitat:
|
AW: Durchschnittsfarbe eines Bitmap "schnell" ermitteln
Danke skybibo und Amateurprofi ich werde es nach her mal durch-testen!
So werde ich vorgehen, hoffentlich ist das soweit okay, habe es selbst noch nicht getestet da ich gerade bemüht bin dutzende von methoden aus einer vcl-haupt-unit in eine seperate unit auszulagern. Hier mein Code der vor Eurem geschaltet wird:
Delphi-Quellcode:
function FilenameToBmp(const AFilename: string; out ABmp: TBitmap): Boolean;
var wic: TWICImage; begin if ((not FileExists(AFilename)) or (nil = ABmp)) then Exit(false); wic := TWICImage.Create; try ABmp.Dormant; ABmp.FreeImage; ABmp.ReleaseHandle; wic.LoadFromFile(AFilename); ABmp.Assign(wic); ABmp.PixelFormat := pf24bit; Result := Assigned(ABmp); finally wic.Free; end; end; |
AW: Durchschnittsfarbe eines Bitmap "schnell" ermitteln
OUT?
VAR Ohne (bzw. CONST) !!! Es kommt was rein, also ist mindestens es IN-OUT, aber eigentlich geht auch garnichts raus, denn die Instanz/Variable/Parameter ändert sich garnicht. Nur der Inhalt der Instanz ändert sich, aber das hat mit dem Objektzeiger nichts zu tun. |
AW: Durchschnittsfarbe eines Bitmap "schnell" ermitteln
Zitat:
Delphi-Quellcode:
so?
function FilenameToBmp(const AFilename: string; const ABmp: TBitmap): Boolean;
Danke fürs Lesen und Fehler erkennen! |
AW: Durchschnittsfarbe eines Bitmap "schnell" ermitteln
Jupp, wobei das CONST optional ist. (nach außen hin ist OHNE und CONST gleich)
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 06:39 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