Wednesday, February 5, 2014

Range of Interest in C #

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-making an application range of interest from an image is the basis of the use of the C # application-related image. The image to be processed must be opened and saved. 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 make or find a range of interest from an image of any type using C #. In my implementation using 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 as per your liking (for example RangeOfInterest) 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 "Range of Interest" and add the following code:


             Bitmap copy = new Bitmap((Bitmap)this.pictureBox1.Image);
             Biner(copy, 4);
             Invert(copy);
             Erosi(copy);
             pictureBox1.Image = copy;

Here we need a declaration to "Binary" Binary code, as follows:


      public static bool Biner(Bitmap b, int signal)
              {
            BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
            ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int stride = bmData.Stride;
            System.IntPtr Scan0 = bmData.Scan0;

            unsafe
            {
                byte* p = (byte*)(void*)Scan0;
                int nOffset = stride - b.Width * 3;
                byte red, green, blue;

                for (int y = 0; y < b.Height; ++y)
                {
                    for (int z = 0; z < b.Width; ++z)
                    {
                        blue = p[0];
                        green = p[1];
                        red = p[2];

                        if (signal == 1)
                        {
                            if (red > 120)
                                p[0] = p[1] = p[2] = (byte)0;
                            else
                                p[0] = p[1] = p[2] = (byte)255;
                            p += 3;
                        }

                        if (signal == 2)
                        {
                            if (green > 120)
                                p[0] = p[1] = p[2] = (byte)0;
                            else
                                p[0] = p[1] = p[2] = (byte)255;
                            p += 3;
                        }

                        if (signal == 3)
                        {
                            if (blue > 120)
                                p[0] = p[1] = p[2] = (byte)0;
                            else
                                p[0] = p[1] = p[2] = (byte)255;
                            p += 3;
                        }

                        if (signal == 4)
                        {
                            if (((red + green + blue) / 3) > 120)
                                p[0] = p[1] = p[2] = (byte)0;
                            else
                                p[0] = p[1] = p[2] = (byte)255;
                            p += 3;
                        }
                    }
                    p += nOffset;
                }
            }
            b.UnlockBits(bmData);
            return true;
        }

Then we need a declaration to "Invert" Invert, code as follows:


public static bool Invert(Bitmap b)
        {
            BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
            ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int stride = bmData.Stride;
            System.IntPtr Scan0 = bmData.Scan0;

            unsafe
            {
                byte* p = (byte*)(void*)Scan0;
                int nOffset = stride - b.Width * 3;
                byte red, green, blue;

                for (int y = 0; y < b.Height; ++y)
                {
                    for (int z = 0; z < b.Width; ++z)
                    {
                        blue = p[0];
                        p[0] = (byte)(255 - blue);
                        green = p[1];
                        p[0] = (byte)(255 - green);
                        red = p[2];
                        p[0] = (byte)(255 - red);
                        p += 3;
                    }
                    p += nOffset;
                }
            }
            b.UnlockBits(bmData);
            return true;
        }

Then we need a declaration to "Erosion", Erosion code as follows:


public static bool Erosi(Bitmap b)
        {
            BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
            ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            byte[,] bindata = new byte[b.Width, b.Height];
            byte[,] tmpdata = new byte[b.Width, b.Height];

            int stride = bmData.Stride;
            System.IntPtr Scan0 = bmData.Scan0;

            unsafe
            {
                byte* p = (byte*)(void*)Scan0;
                int nOffset = stride - b.Width * 3;

                for (int y = 0; y < b.Height; ++y)
                {
                    for (int z = 0; z < b.Width; ++z)
                    {
                        tmpdata[z, y] = p[0];
                        bindata[z, y] = 0;
                        p += 3;
                    }
                    p += nOffset;
                }

                byte* p1 = (byte*)(void*)Scan0;

                for (int y = 1; y < b.Height - 1; ++y)
                {
                    for (int z = 1; z < b.Width - 1; ++z)
                    {
                        if (tmpdata[z, y] > 0)
                        {
                            if ((tmpdata[z - 1, y - 1]) == 0 || (tmpdata[z, y - 1]) == 0 || (tmpdata[z + 1, y - 1]) == 0 || (tmpdata[z + 1, y]) == 0 || (tmpdata[z + 1, y + 1]) == 0 || (tmpdata[z, y + 1]) == 0 || (tmpdata[z - 1, y + 1]) == 0 || (tmpdata[z - 1, y]) == 0)
                                bindata[z, y] = (byte)(0);
                            else
                                bindata[z, y] = (byte)(255);
                        }
                        else
                            bindata[z, y] = (byte)(0);
                        p1 += 3;
                    }
                    p1 += nOffset;
                }

                byte* p2 = (byte*)(void*)Scan0;

                for (int y = 0; y < b.Height; ++y)
                {
                    for (int z = 0; z < b.Width; ++z)
                    {
                        p2[0] = p2[1] = p2[2] = bindata[z, y];
                        p2 += 3;
                    }
                    p2 += nOffset;
                }
            }
            b.UnlockBits(bmData);
            return true;
        }

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 the 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 ditulisa 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 projectnya inevitably must install microsoft visual studio 2012

0 comments:

Post a Comment