locked
Find the average color/brightness of an image RRS feed

  • Question

  • User587478106 posted

    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?

    Thanks in advance!

    Sunday, October 5, 2008 7:40 AM

Answers

  • User187056398 posted

    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, 
                                                   ImageLockMode.ReadOnly,
                                                   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;
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 5, 2008 11:09 AM

All replies

  • User187056398 posted

    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, 
                                                   ImageLockMode.ReadOnly,
                                                   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;
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 5, 2008 11:09 AM
  • User587478106 posted

    Thanks a lot!

    It's just what I was looking for.

    Monday, October 6, 2008 5:39 PM