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

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Average Color
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

AndyColmes

USA
351 Posts

Posted - Jun 06 2013 :  09:38:04  Show Profile  Reply
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.

w2m

USA
1990 Posts

Posted - Jun 06 2013 :  11:05:33  Show Profile  Reply
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
Go to Top of Page

AndyColmes

USA
351 Posts

Posted - Jun 07 2013 :  01:57:48  Show Profile  Reply
Thanks William. Will give it a try.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: