![]() |
Wie hell ist ein Bild?
Hallo,
kann mir jemand auf die Sprünge helfen die Helligkeit eines Bildes (oder ein Teil davon) zu bestimmen? Habe da überhaupt keine Idee wo man anfangen kann. Danke! |
AW: Wie hell ist ein Bild?
Dafür kannst du die Werte der einzelnen Pixel in den HLS Farbraum umwandeln. Dort hast du dann direkt die Helligkeit.
Dafür gibt es in der Windows-API wie auch in Delphi und Lazarus die Funktion ColorRGBToHLS, bei Delphi in der Unit Vcl.GraphUtil und bei Lazarus in GraphUtil. Dann kannst du die durchschnittliche Helligkeit verschiedener Pixel ausrechnen. |
AW: Wie hell ist ein Bild?
Ok, danke Dir!
|
AW: Wie hell ist ein Bild?
Hier was ich immer genommen habe:
Delphi-Quellcode:
Wenn es auf Geschwindigkeit ankommt kann man das sogar mit extrem wenigen Integer-Operationen abbilden:
uses WinApi.Windows;
type TColorHelper = record helper for Vcl.Graphics.TColor public function getPerceptiveLuminance(): Single; end; function TColorHelper.getPerceptiveLuminance(): Single; begin Result := // Nach https://en.wikipedia.org/w/index.php?title=Relative_luminance&oldid=634803879 GetRValue(self) * 0.2126 + GetGValue(self) * 0.7152 + GetBValue(self) * 0.0722; end; ![]() |
AW: Wie hell ist ein Bild?
Danke Dir! Aber eine Frage, wo ist das Bild?
|
AW: Wie hell ist ein Bild?
Delphi-Record-Helper und Lazarus?
Und dann der Mittelwert aller
Delphi-Quellcode:
:zwinker:
DeinBitmap.Canvas.Pixels[X, Y].getPerceptiveLuminance
|
AW: Wie hell ist ein Bild?
Zitat:
![]() |
AW: Wie hell ist ein Bild?
Angenommen schwarze Pixel zählen als 0 und weiße als 1. Weiterhin angenommen dein Bild bestünde aus 499 weißen und 501 schwarzen Pixeln. Dann wäre der Helligkeitsmedian 0. Ist das sinnvoll für deinen Anwendungszweck? Bei einem Durchschnitt hättest du stattdessen etwa 0,5. In jedem Fall wäre die Angabe der Standardabweichung wohl sinnvoll.
|
AW: Wie hell ist ein Bild?
Zitat:
|
AW: Wie hell ist ein Bild?
Zitat:
Code:
{$IFDEF CPUX86}
FUNCTION BrightnessOfLine(P,W:NativeInt):NativeInt; asm // EAX=P, EDX=W push ebx push esi mov esi,eax // P xor eax,eax // Summe Brightness xor ecx,ecx @Loop: mov bx,[esi] // BL=Blau, BH=Grün mov cl,byte[esi+2] // ECX=Rot cmp cl,bl jae @1 mov cl,bl @1: cmp cl,bh jae @2 mov cl,bh @2: add eax,ecx add esi,3 sub edx,1 jne @Loop pop esi pop ebx end; {$ENDIF}
Code:
{$IFDEF CPUX64}
FUNCTION BrightnessOfLine(P,W:NativeInt):NativeInt; asm // RCX=P, RDX=W mov r8,rcx // P xor r9,r9 // Summe Brightness xor rcx,rcx @Loop: mov ax,[r8] // AL=Blau, AH=Grün mov cl,[r8+2] // CL=Rot cmp cl,al jae @1 mov cl,al @1: cmp cl,ah jae @2 mov cl,ah @2: add r9,rcx add r8,3 sub rdx,1 jne @Loop mov rax,r9 end; {$ENDIF}
Code:
{$IFDEF CPUX86} FUNCTION LuminanceOfLine(P,W:NativeInt):NativeInt; asm // EAX=P, EDX=W push ebx push edi push esi mov esi,eax // P mov edi,edx // W xor eax,eax // Summe Luminance xor ecx,ecx xor edx,edx @Loop: mov bx,[esi] // BL=Blau, BH=Grün mov cl,[esi+2] // ECX=Rot mov dl,cl // EDX=Rot cmp cl,bl je @2 // Rot=Blau ja @1 // Rot>Blau mov cl,bl // ECX=Max(Rot,Blau) jmp @2 @1: mov dl,bl // EDX=Min(Rot,Blau) @2: cmp cl,bh jae @3 mov cl,bh // ECX=Max(Grün,Max(Rot,Blau)) @3: cmp dl,bh jbe @4 mov dl,bh // EDX=Min(Grün,Min(Rot,Blau)) @4: add eax,ecx add eax,edx add esi,3 sub edi,1 jne @Loop pop esi pop edi pop ebx end; {$ENDIF}
Code:
{$IFDEF CPUX64}
FUNCTION LuminanceOfLine(P,W:NativeInt):NativeInt; asm // RCX=P, RDX=W mov r11,rcx // P mov r10,rdx // W xor r9,r9 // Summe Luminance xor rcx,rcx // Für MaxValue xor rdx,rdx // Für MinValue @Loop: mov ax,[r11] // AL=Blau, AH=Grün mov cl,[r11+2] // RCX=Rot mov dl,cl // RDX=Rot cmp cl,al je @2 // Rot=Blau ja @1 // Rot>Blau mov cl,al // ECX=Max(Rot,Blau) jmp @2 @1: mov dl,al // EDX=Min(Rot,Blau) @2: cmp cl,ah jae @3 mov cl,ah // ECX=Max(Grün,Max(Rot,Blau)) @3: cmp dl,ah jbe @4 mov dl,ah // EDX=Min(Grün,Min(Rot,Blau)) @4: add r9,rcx // Sum=Sum+MaxValue add r9,rdx // Sum=Sum+MinValue add r11,3 sub r10,1 jne @Loop mov rax,r9 end; {$ENDIF}
Delphi-Quellcode:
{------------------------------------------------------------------------------}
{ Brightness } { Gibt die Helligkeit eines Bildausschnittes in Promille zurück } { Wenn Luminance=True, dann wird die Luminanz (HSL-Modell) zurückgegeben, } { andernfalls die Brightness (HSB-Modell) } { In folgenden Fällen wird 0 zurückgegeben } { - Bmp nicht assigned } { - Bmp.Heigth=0 oder Bmp.Width=0 } { - Bmp.PixelFormat<>pf24bit } { - R ist nicht komplett innerhalb der Bitmap } {------------------------------------------------------------------------------} FUNCTION Brightness(Bmp:TBitmap; R:TRect; Luminance:Boolean):Integer; overload; type TCalcFunc=Function(P,W:NativeInt):NativeInt; const Factor:Array[Boolean] of Double=(1000/255,1000/510); CalcLine:Array[Boolean] of TCalcFunc=(BrightnessOfLine,LuminanceOfLine); var I,W,H,P,PP,O:NativeInt; Sum:Int64; RR:TRect; begin if not Assigned(Bmp) or (Bmp.PixelFormat<>pf24bit) then Exit(0); if (Bmp.Width=0) or (Bmp.Height=0) then Exit(0); IntersectRect(RR,R,Rect(0,0,Bmp.Width,Bmp.Height)); if not EqualRect(RR,R) then Exit(0); W:=R.Width; H:=R.Height; PP:=NativeInt(Bmp.ScanLine[R.Top]); P:=PP+R.Left*3; Sum:=CalcLine[Luminance](P,W); if H>1 then begin O:=NativeInt(Bmp.ScanLine[R.Top+1])-PP; for I:=2 to H do begin Inc(P,O); Inc(Sum,CalcLine[Luminance](P,W)); end; end; Result:=Round(Sum/(W*H)*Factor[Luminance]); end;
Delphi-Quellcode:
{------------------------------------------------------------------------------}
{ Brightness } { Gibt die Helligkeit eines Bildes in Promille zurück } { Wenn Luminance=True, dann wird die Luminanz (HSL-Modell) zurückgegeben, } { andernfalls die Brightness (HSB-Modell) } { In folgenden Fällen wird 0 zurückgegeben } { - Bmp nicht assigned } { - Bmp.Heigth=0 oder Bmp.Width=0 } { - Bmp.PixelFormat<>pf24bit } {------------------------------------------------------------------------------} FUNCTION Brightness(Bmp:TBitmap; Luminance:Boolean):Integer; overload; begin if not Assigned(Bmp) then Exit(0); Result:=Brightness(Bmp,Rect(0,0,Bmp.Width,Bmp.Height),Luminance); end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 13:31 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 by Thomas Breitkreuz