2 replies

Last post Oct 06, 2008 05:39 PM by iver56

• iver56

Member

6 Points

34 Posts

Find the average color/brightness of an image

Hi!

I've got a webcam which sends a picture (640x480, jpg) to the webserver once a minute. When somebody looks at the webcam page online, there should be a piece of code that finds the average color or brightness of the image (to find out if it is morning/day/evening or night). Based on that, some other code will archive the image or not.

My question is: How can I find the average color of the webcam image?

GDI+ average color webcam image average brightness

• SGWellens

All-Star

124328 Points

10142 Posts

Re: Find the average color/brightness of an image

Something like this?

```Bitmap MyBitmap = new Bitmap(MapPath(@"~\Images\April.jpg"));

Rectangle Rect = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);

BitmapData BmpData = MyBitmap.LockBits(Rect,
MyBitmap.PixelFormat);

int Size = BmpData.Stride * MyBitmap.Height;
byte[] RGBs = new byte[Size];

IntPtr Pointer = BmpData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(Pointer, RGBs, 0, Size);

MyBitmap.UnlockBits(BmpData);

// we've got all the RGB values in an array
int Sum = 0;
for (int Index = 0; Index < RGBs.Length; Index++)
Sum += RGBs[Index];

// higher Average means a lighter picture (255 = white, 0 = black)
int Average = Sum / RGBs.Length;```

Steve Wellens

My blog
• iver56

Member

6 Points

34 Posts