0

I have a C# desktop application.

I am using emgu framework for image processing.

What I am trying to do is improve 'washed' out images and make them more vibrant.

Using myImage._EqualHist() works OK for when the picture has a rich array of colours (like during the day) but at night-time the white-balancing will distort after applying that emgu filter and I get a very bright/busy night-time image. If I could just adjust the contrast that would be good - I think.

I have found some code that will adjust the contrast for me. I am now trying to find what value should I use to adjust the contrast by. Obviously, this value will vary during a 24hr cycle.

I have made a 1st stab at what relationship/formula I can use to auto-contrast an image. Obviously, I am guessing and trying things out. Not very scientific//mathematical and I am searching the net for some logic I can apply.

Should I also consider the gamma of an image?

This is my code so far:

           Image<Bgr, Byte> imgColor = new Image<Bgr, byte>(@"d:\20140320022038047.jpg");
            Image<Hsv,Byte> hsv = new Image<Hsv,byte>(imgColor.ToBitmap());
            double averageHue = hsv[0].GetAverage().Intensity;
            double averageSat = hsv[1].GetAverage().Intensity;
            double averageLum = hsv[2].GetAverage().Intensity;

            //I am guessing here and playing around with the constants
            float adjustContrastBy =(float)( averageLum / averageHue);

            byte[, ,] data = imgColor.Data;
            //this part of the code enumertaes through the Image byte array
            for (int y = 0; y < imgColor.Height ; y++)
            {
                for (int x = 0; x < imgColor.Width; x++)
                {
                    byte B = data[y, x, 0];
                    byte G = data[y, x, 1];
                    byte R = data[y, x, 2];

                    float Red = R / 255.0f;
                    float Green = G / 255.0f;
                    float Blue = B / 255.0f;
                    Red = (((Red - 0.5f) * adjustContrastBy) + 0.5f) * 255.0f;
                    Green = (((Green - 0.5f) * adjustContrastBy) + 0.5f) * 255.0f;
                    Blue = (((Blue - 0.5f) * adjustContrastBy) + 0.5f) * 255.0f;

                    int iR = (int)Red;
                    iR = iR > 255 ? 255 : iR;
                    iR = iR < 0 ? 0 : iR;
                    int iG = (int)Green;
                    iG = iG > 255 ? 255 : iG;
                    iG = iG < 0 ? 0 : iG;
                    int iB = (int)Blue;
                    iB = iB > 255 ? 255 : iB;
                    iB = iB < 0 ? 0 : iB;

                    data[y, x, 0] = (byte)iB;
                    data[y, x, 1] = (byte)iG;
                    data[y, x, 2] = (byte)iR;
                }
            }
            pictureBox1.Image = imgColor.ToBitmap();

I also adjust the gamma using this method:

double intensity = grayCurrent.GetAverage().Intensity;
double g = Math.Log(Shared.Optimum / 255) / Math.Log(intensity / 255);
grayCurrent._GammaCorrect(g);

This certainly helps me with the motion detection but want I am now focusing on is improving what the User 'sees' as opposed to what computer detects..

Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179
  • [This question](http://stackoverflow.com/questions/3115076/adjust-the-contrast-of-an-image-in-c-sharp-efficiently) while not necessarily an exact duplicate may have some useful information for you. – 500 - Internal Server Error Mar 20 '14 at 14:07
  • Hi, thanks for your comment and time but that is the page I got my code to ADJUST the contrast of the image from. I am trying to take it 1 step further and use an auto-varying factor to apply as the contrast ratio per image :) – Andrew Simpson Mar 20 '14 at 14:16
  • _Should I also consider the gamma of an image?_ By all means!! Gamma, not Contrast is always the 1st thing to try, when _improving_ an image.. – TaW Mar 20 '14 at 14:46
  • @TaW Hi, thanks for your interest I edited my question to show how I am using that currently. I just wanted to know should I use my derived figure for Gamma somewhere? – Andrew Simpson Mar 20 '14 at 14:53
  • You mention _auto-varying_. So, do you offer the user variations to pick from so he can drill down towards the best correction? – TaW Mar 20 '14 at 14:58
  • Hi, no not at all. That was my point. I need it to auto-correct itself to an optimum I decide. But the images will vary in saturation, hue and luminance during every 24 hours which will make some images look way too bright/contrasted(if that made sense?) – Andrew Simpson Mar 20 '14 at 15:00

0 Answers0