Wednesday, February 5, 2014

Normalizing Image in C # ( Normalization Image )

This is a post I am related to C # (C sharp). In this post, I will explain the steps of making the application of C #. Tutorial How to normalize application creation of an image is the basis of the use of the C # application-related image. The image to be processed can be normalized for further processing. Now it's time to direct the implementation of the program.
Starting from the simplest, though probably many of you already know very well about the programming language one, namely how to normalize a picture of any type using C #. In each implementation I use Microsoft Visual Studio 2012.
Please read:
1. the first step, open the C # program and create a new file by choosing File > > New Project (Ctrl + Shift + N) > > select Windows Application, and name the file according to your wishes (NormalisasiImage) and press OK. Next would come an empty form.


2. a Component used for this simple program is:
• OpenFileDialog
• PictureBox
• Button
Click and put option component of the program which will be used as the image below:


3. Then double click on the button "Load Image" and add the following code:


OpenFileDialog file = new OpenFileDialog();
string lokasi = file.InitialDirectory;
lokasi = @"C:\Users\ardha\Desktop";
file.Title = "Open Image File";
file.Filter = "Image Files(*.jpg,*.png,*.tiff,*.bmp,*.gif)|*.jpg;*.png;*.tiff;*.bmp;*.gif";
file.FilterIndex = 2;
file.RestoreDirectory = true;
if (file.ShowDialog() == DialogResult.OK)
                     {
                     pictureBox1.Image = new Bitmap(file.FileName);
                                     }

The purpose of the Load Image button to select an image file is what you want to display in the picture box. The desired file types can be specified via the syntax Filter (in the above example file.Filter), whereas if you want to directly refer to a particular directory can be used syntax InitialDirectory (in the above example file.InitialDirectory)

4. as for the save image file, double click on the button "Image Normalization" and add the following code:


              pictureBox1.Image = ImageBrightnessNormalizer.NormalizeImageBrightness(new
              Bitmap(pictureBox1.Image));

Here we need the classs to "ImageBrigtnessNormalizer", the code ImageBrigtnessNormalizer as follows:


class ImageBrightnessNormalizer
    {
        private const float MIN_BRIGHTNESS = 0;
        private const float MAX_BRIGHTNESS = 1;

        public static Bitmap NormalizeImageBrightness(Bitmap image)
        {
            float minBrightness = MAX_BRIGHTNESS;
            float maxBrightness = MIN_BRIGHTNESS;

            /* Get the brightness range of the image. */
            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    float pixelBrightness = image.GetPixel(x, y).GetBrightness();
                    minBrightness = Math.Min(minBrightness, pixelBrightness);
                    maxBrightness = Math.Max(maxBrightness, pixelBrightness);
                }
            }

            /* Normalize the image brightness. */
            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    Color pixelColor = image.GetPixel(x, y);
                    float normalizedPixelBrightness = (pixelColor.GetBrightness() –
                    minBrightness) / (maxBrightness - minBrightness);
                    Color normalizedPixelColor =
                    ColorConverter.ColorFromAhsb(pixelColor.A, pixelColor.GetHue(),
                    pixelColor.GetSaturation(), normalizedPixelBrightness);
                    image.SetPixel(x, y, normalizedPixelColor);
                }
            }
            return image;
        }
    }

Then we also need a class for "ColorConverter" ColorConverter and source code are as follows:


class ColorConverter
    {
        public static Color ColorFromAhsb(int a, float h, float s, float b)
        {
            if (0 > a || 255 < a)
            {
                throw new Exception("a");
            }
            if (0f > h || 360f < h)
            {
                throw new Exception("h");
            }
            if (0f > s || 1f < s)
            {
                throw new Exception("s");
            }
            if (0f > b || 1f < b)
            {
                throw new Exception("b");
            }

            if (0 == s)
            {
                return Color.FromArgb(a, Convert.ToInt32(b * 255),
                  Convert.ToInt32(b * 255), Convert.ToInt32(b * 255));
            }

            float fMax, fMid, fMin;
            int iSextant, iMax, iMid, iMin;

            if (0.5 < b)
            {
                fMax = b - (b * s) + s;
                fMin = b + (b * s) - s;
            }
            else
            {
                fMax = b + (b * s);
                fMin = b - (b * s);
            }

            iSextant = (int)Math.Floor(h / 60f);
            if (300f <= h)
            {
                h -= 360f;
            }
            h /= 60f;
            h -= 2f * (float)Math.Floor(((iSextant + 1f) % 6f) / 2f);
            if (0 == iSextant % 2)
            {
                fMid = h * (fMax - fMin) + fMin;
            }
            else
            {
                fMid = fMin - h * (fMax - fMin);
            }

            iMax = Convert.ToInt32(fMax * 255);
            iMid = Convert.ToInt32(fMid * 255);
            iMin = Convert.ToInt32(fMin * 255);

            switch (iSextant)
            {
                case 1:
                    return Color.FromArgb(a, iMid, iMax, iMin);
                case 2:
                    return Color.FromArgb(a, iMin, iMax, iMid);
                case 3:
                    return Color.FromArgb(a, iMin, iMid, iMax);
                case 4:
                    return Color.FromArgb(a, iMid, iMin, iMax);
                case 5:
                    return Color.FromArgb(a, iMax, iMin, iMid);
                default:
                    return Color.FromArgb(a, iMax, iMid, iMin);
            }
        }
    }

5. the application was complete, after adding the source code above, lives in the run course. And the results were as follows:



For more details on this please download program understanding program files and full-length project
download program files binary here
download full project here
download the exe only here
download the document here
download picture here
for more information contact on ardhamail@gmail.com

Guide to download: click on the download link above, after it would appear adf.ly page, wait a few seconds until appears on the top right corner of the ad skip writing. Click the button, the page will appear after that ziddu, please click on the download button that appears on the page ziddu
NB: in this tutorial program using the Visual studio IDE. with the target framework > 4.0. So at a minimum your computer must be installed or windows vista or windows xp with the .NET framework 4.0 to be able to run programs *. exe, while for open this project inevitably must install microsoft visual studio 2012

0 comments:

Post a Comment