ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Average Color

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

View 
UserName:
Password:
Format  Bold Italicized Underline  Align Left Centered Align Right  Horizontal Rule  Insert Hyperlink   Browse for an image to attach to your post Browse for a zip to attach to your post Insert Code  Insert Quote Insert List
   
Message 

 

Emoji
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Black Eye [B)]
Frown [:(] Shocked [:0] Angry [:(!] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
AndyColmes Posted - Jun 06 2013 : 09:38:04
How do I get an average color of pixels surrounding a pixel from a point? I would like to get the average color, for example in a 3x3 pixel square and display that average color in a TShape.

Thanks.
2   L A T E S T    R E P L I E S    (Newest First)
AndyColmes Posted - Jun 07 2013 : 01:57:48
Thanks William. Will give it a try.
w2m Posted - Jun 06 2013 : 11:05:33
I just made this and tested it a little bit... It seems to work but I can not guarantee it is perfect:
function GetAverage3X3Color(APoint: TPoint; AIEBitmap: TIEBitmap): TColor;
{ Return the average color around a 3x3 sample from a APoint. }
var
  iPixelColor: TColor;
  iAverageR, iAverageG, iAverageB: Cardinal;
  x, y: shortint;
begin
  iAverageR := 0;
  iAverageG := 0;
  iAverageb := 0;
  for y := -1 to 1 do
    for x := -1 to 1 do
    begin
      iPixelColor := AIEBitmap.Canvas.Pixels[APoint.x + x, APoint.y + y];
      iAverageR := iAverageR + GetRValue(iPixelColor);
      iAverageG := iAverageG + GetGValue(iPixelColor);
      iAverageB := iAverageB + GetBValue(iPixelColor);
    end;
  iAverageR := iAverageR div 9;
  iAverageG := iAverageG div 9;
  iAverageB := iAverageB div 9;
  iPixelColor := RGB(Lo(iAverageR), Lo(iAverageG), Lo(iAverageB));
  result := iPixelColor;
end;

function GetAverage5X5Color(APoint: TPoint; AIEBitmap: TIEBitmap): TColor;
{ Return the average color around a 5x5 sample from a APoint. }
var
  iPixelColor: TColor;
  iAverageR, iAverageG, iAverageB: Cardinal;
  x, y: shortint;
begin
  iAverageR := 0;
  iAverageG := 0;
  iAverageB := 0;
  for y := -2 to 2 do
    for x := -2 to 2 do
    begin
      iPixelColor := AIEBitmap.Canvas.Pixels[APoint.x + x, APoint.y + y];
      iAverageR := iAverageR + GetRValue(iPixelColor);
      iAverageG := iAverageG + GetGValue(iPixelColor);
      iAverageB := iAverageB + GetBValue(iPixelColor);
    end;
  iAverageR := iAverageR div 25;
  iAverageG := iAverageG div 25;
  iAverageB := iAverageB div 25;
  iPixelColor := RGB(Lo(iAverageR), Lo(iAverageG), Lo(iAverageB));
  result := iPixelColor;
end;

function GetAverage9X9Color(APoint: TPoint; AIEBitmap: TIEBitmap): TColor;
{ Return the average color around a 9x9 sample from a APoint. }
var
  iPixelColor: TColor;
  iAverageR, iAverageG, iAverageB: Cardinal;
  x, y: shortint;
begin
  iAverageR := 0;
  iAverageG := 0;
  iAverageB := 0;
  for y := -4 to 4 do
    for x := -4 to 4 do
    begin
      iPixelColor := AIEBitmap.Canvas.Pixels[APoint.x + x, APoint.y + y];
      iAverageR := iAverageR + GetRValue(iPixelColor);
      iAverageG := iAverageG + GetGValue(iPixelColor);
      iAverageB := iAverageB + GetBValue(iPixelColor);
    end;
  iAverageR := iAverageR div 81;
  iAverageG := iAverageG div 81;
  iAverageB := iAverageB div 81;
  iPixelColor := RGB(Lo(iAverageR), Lo(iAverageG), Lo(iAverageB));
  result := iPixelColor;
end;

Usage:
iAverageColor := GetAverage3x3Color(Point(Layers[LayersCurrent].ConvXScr2Bmp(X),
    Layers[LayersCurrent].ConvYScr2Bmp(Y)), ImageEnView1.IEBitmap);
Shape1.BrushColor := iAverageColor;

Different distinct color values are returned by the methods, but visually I do not see much color difference between them, so I am not sure if my math is correct or if it just difficult to perceive the color differences.

William Miller
Adirondack Software & Graphics
Email: w2m@frontiernet.net
EBook: http://www.imageen.com/ebook/
Apprehend: http://www.frontiernet.net/~w2m/index.html