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..