Пример #1
0
        private void button3_Click(object sender, EventArgs e)
        {
            transferedimage = new Bitmap(width, Height);
            my_color newpixel = new my_color();

            for (int i = 0; i < Height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    Color clr1;
                    Color clr2;
                    clr1           = localimage.GetPixel(j, i);
                    clr2           = b1.GetPixel(j, i);
                    newpixel.Red   = clr1.R + clr2.R;
                    newpixel.Blue  = clr1.B + clr2.B;
                    newpixel.Green = clr1.G + clr2.G;
                    if (newpixel.Red > 255)
                    {
                        newpixel.Red = 255;
                    }
                    if (newpixel.Blue > 255)
                    {
                        newpixel.Blue = 255;
                    }
                    if (newpixel.Green > 255)
                    {
                        newpixel.Green = 255;
                    }
                    Color clr3;
                    clr3 = Color.FromArgb(newpixel.Red, newpixel.Green, newpixel.Blue);
                    transferedimage.SetPixel(j, i, clr3);
                }
            }
            pictureBox3.Image = transferedimage;
        }
Пример #2
0
        private void button4_Click(object sender, EventArgs e)
        {
            //apply the past two detection ways , and sum their results , then post processing : cut off
            float[,] operation_mask1         = { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } };
            float[,] operation_mask2         = { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
            my_color[,] the_magnitude_image1 = new my_color[fst_image.image_buffer.GetLength(0), fst_image.image_buffer.GetLength(1)];
            my_color[,] the_magnitude_image2 = new my_color[fst_image.image_buffer.GetLength(0), fst_image.image_buffer.GetLength(1)];

            //1
            the_magnitude_image1 = Linear_filter(ref fst_image.image_buffer, ref operation_mask1, "abs");
            the_magnitude_image2 = Linear_filter(ref fst_image.image_buffer, ref operation_mask2, "abs");

            //2
            for (int i = 0; i < fst_image.image_buffer.GetLength(0); i++)
            {
                for (int j = 0; j < fst_image.image_buffer.GetLength(1); j++)
                {
                    the_magnitude_image2[i, j].Red   = (the_magnitude_image1[i, j].Red + the_magnitude_image2[i, j].Red);
                    the_magnitude_image2[i, j].Green = (the_magnitude_image1[i, j].Green + the_magnitude_image2[i, j].Green);
                    the_magnitude_image2[i, j].Blue  = (the_magnitude_image1[i, j].Blue + the_magnitude_image2[i, j].Blue);
                }
            }

            //3
            pictureBox2.Image = display_image(Post_processing(the_magnitude_image2, "cutoff"));
            MessageBox.Show("Congratulations , Edges detected succsefully , Using : Apply Edge Magnitude sobel detection");
        }
Пример #3
0
        public Bitmap openp3(string path)
        {
            Bitmap       b1;
            FileStream   FS    = new FileStream(path, FileMode.Open);
            StreamReader SR    = new StreamReader(FS);
            string       line1 = SR.ReadLine();

            if (line1.Equals("P3"))
            {
                string   line2 = SR.ReadLine();
                string[] words = line2.Split(' ');
                if (words[0].Equals("#"))
                {
                    line2 = SR.ReadLine();
                }
                string[] widthheight = line2.Split(' ');
                int      width       = int.Parse(widthheight[0]);
                int      height      = int.Parse(widthheight[1]);
                //textBox1.Text = widthheight[0];
                string   num_of_bits       = SR.ReadLine();
                string   Remaining_of_file = SR.ReadToEnd();
                char[]   delimiters        = new char[] { ' ', '\n' };
                string[] Data_of_Image     = Remaining_of_file.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                my_color[,] Buffer2D = new my_color[height, width];
                int current = 0;
                b1 = new Bitmap(width, height);
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        Buffer2D[i, j]       = new my_color();
                        Buffer2D[i, j].Red   = int.Parse(Data_of_Image[current]);
                        Buffer2D[i, j].Green = int.Parse(Data_of_Image[current + 1]);
                        Buffer2D[i, j].Blue  = int.Parse(Data_of_Image[current + 2]);
                        current += 3;
                        Color clr;
                        clr = Color.FromArgb(Buffer2D[i, j].Red, Buffer2D[i, j].Green, Buffer2D[i, j].Blue);
                        b1.SetPixel(j, i, clr);
                    }
                }
            }
            else
            {
                b1 = new Bitmap(100, 100);
                return(b1);
            }
            SR.Close();
            return(b1);
        }
Пример #4
0
        private void button2_Click(object sender, EventArgs e)
        {
            transferedimage = new Bitmap(localimage.Width, localimage.Height);
            my_color newpixel = new my_color();

            for (int i = 0; i < Height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    Color clr = new Color();
                    clr            = localimage.GetPixel(j, i);
                    newpixel.Red   = 255 - clr.R;
                    newpixel.Green = 255 - clr.G;
                    newpixel.Blue  = 255 - clr.B;
                    Color clr2;
                    clr2 = Color.FromArgb(newpixel.Red, newpixel.Green, newpixel.Blue);
                    transferedimage.SetPixel(j, i, clr2);
                }
            }
            pictureBox2.Image = transferedimage;
        }
Пример #5
0
 public void setdata(Bitmap imagetransfer)
 {
     original_bm      = imagetransfer;
     inputimage.Image = original_bm;
     #region chart Draw
     //convert image to buffer
     Bitmap im = new Bitmap(original_bm);
     my_color[,] image_buffer = new my_color[original_bm.Height, original_bm.Width];
     for (int i = 0; i < original_bm.Height; i++)
     {
         for (int j = 0; j < original_bm.Width; j++)
         {
             Color _clr;
             _clr = im.GetPixel(j, i);
             image_buffer[i, j].Red   = _clr.R;
             image_buffer[i, j].Green = _clr.G;
             image_buffer[i, j].Blue  = _clr.B;
         }
     }
     _fill_charts(image_buffer);
     #endregion
 }
Пример #6
0
        public my_color[,] histo_equalization(my_color[,] given)
        {
            my_color[] given_img_histogram  = new my_color[256];   //1D arrays to calculate histogram
            my_color[] given_img_cumulative = new my_color[256];   //1D arrays to calculate cumulative
            my_color[] change_with          = new my_color[256];   //1D arrays to displacment values
            my_color[,] resulted_img = new my_color[given.GetLength(0), given.GetLength(1)];

            //1- calculate image histogram
            //intiallize the histogram magnitude with 0
            for (int g = 0; g < 256; g++)
            {
                given_img_histogram[g].Red   = 0;
                given_img_histogram[g].Green = 0;
                given_img_histogram[g].Blue  = 0;
            }
            //calculate the magnitudes
            for (int j = 0; j < given.GetLength(0); j++)
            {
                for (int l = 0; l < given.GetLength(1); l++)
                {
                    given_img_histogram[given[j, l].Red].Red++;
                    given_img_histogram[given[j, l].Green].Green++;
                    given_img_histogram[given[j, l].Blue].Blue++;
                }
            }
            //2- calculate cumulative for it
            for (int i = 0; i < 256; i++)
            {
                if (i == 0)
                {
                    given_img_cumulative[i].Red   = given_img_histogram[i].Red;
                    given_img_cumulative[i].Green = given_img_histogram[i].Green;
                    given_img_cumulative[i].Blue  = given_img_histogram[i].Blue;
                }
                else
                {
                    given_img_cumulative[i].Red   = given_img_histogram[i].Red + given_img_cumulative[i - 1].Red;
                    given_img_cumulative[i].Green = given_img_histogram[i].Green + given_img_cumulative[i - 1].Green;
                    given_img_cumulative[i].Blue  = given_img_histogram[i].Blue + given_img_cumulative[i - 1].Blue;
                }
            }
            //**************************************************************************
            //3- divide each value in it by the maximum cumulative
            //4- multibly by the new range : const 255
            for (int j = 0; j < 256; j++)
            {
                change_with[j].Red   = (int)Math.Round((decimal)(((double)given_img_cumulative[j].Red / given_img_cumulative[255].Red) * 255));
                change_with[j].Green = (int)Math.Round((decimal)(((double)given_img_cumulative[j].Green / given_img_cumulative[255].Green) * 255));
                change_with[j].Blue  = (int)Math.Round((decimal)(((double)given_img_cumulative[j].Blue / given_img_cumulative[255].Blue) * 255));
            }
            //5- from given image and change_with list >> produce the result image
            for (int j = 0; j < given.GetLength(0); j++)
            {
                for (int l = 0; l < given.GetLength(1); l++)
                {
                    resulted_img[j, l].Red   = change_with[given[j, l].Red].Red;
                    resulted_img[j, l].Green = change_with[given[j, l].Green].Green;
                    resulted_img[j, l].Blue  = change_with[given[j, l].Blue].Blue;
                }
            }
            return(resulted_img);
        }
Пример #7
0
        private void button3_Click(object sender, EventArgs e)
        {
            DateTime dt1 = new DateTime();
            DateTime dt2 = new DateTime();
            TimeSpan dt3 = new TimeSpan();

            dt1 = DateTime.Now;
            int n_width  = int.Parse(textBox2.Text);
            int n_hieght = int.Parse(textBox3.Text);

            my_color [,] resizeee = new my_color[n_hieght, n_width];
            float w_ratio = (float)width / n_width;
            float h_ratio = (float)Height / n_hieght;

            int      X1, X2, Y1, Y2;
            my_color P1, P2, P3, P4;
            float    OldX, OldY, NewX, NewY;

            float XFraction, YFraction;
            float Z1, Z2;

            Bitmap   b1       = new Bitmap(n_width, n_hieght);
            my_color newpixel = new my_color();

            //int i, j;


            Parallel.For(0, n_hieght, i =>
            {
                NewY = i;
                Parallel.For(0, n_width, j =>
                {
                    NewX = j;
                    OldX = NewX * w_ratio;
                    OldY = NewY * h_ratio;

                    X1 = (int)Math.Floor(OldX); X2 = X1 + 1;
                    Y1 = (int)Math.Floor(OldY); Y2 = Y1 + 1;

                    if (X2 == width)
                    {
                        X2 -= 1;
                    }
                    if (Y2 == Height)
                    {
                        Y2 -= 1;
                    }
                    if (X1 == width)
                    {
                        X1 -= 1;
                    }
                    if (Y1 == Height)
                    {
                        Y1 -= 1;
                    }

                    P1 = Buffer2D[Y1, X1]; P2 = Buffer2D[Y1, X2];
                    P3 = Buffer2D[Y2, X1]; P4 = Buffer2D[Y2, X2];

                    XFraction = OldX - X1;
                    YFraction = OldY - Y1;

                    Z1        = (float)(P1.Red * (1 - XFraction) + P2.Red * XFraction);
                    Z2        = (float)(P3.Red * (1 - XFraction) + P4.Red * XFraction);
                    float Z12 = (Z1 * (1 - YFraction) + Z2 * YFraction);
                    int temp1 = (int)(Z12 * 10) % 10;
                    if (temp1 > 5)
                    {
                        newpixel.Red = (int)Math.Ceiling(Z12);
                    }
                    else
                    {
                        newpixel.Red = (int)Math.Floor(Z12);
                    }

                    //newpixel.Red = (int)(Z1 * (1 - YFraction) + Z2 * YFraction);

                    Z1        = (float)(P1.Green * (1 - XFraction) + P2.Green * XFraction);
                    Z2        = (float)(P3.Green * (1 - XFraction) + P4.Green * XFraction);
                    float Z11 = (Z1 * (1 - YFraction) + Z2 * YFraction);
                    int temp2 = (int)(Z11 * 10) % 10;
                    if (temp2 > 5)
                    {
                        newpixel.Green = (int)Math.Ceiling(Z11);
                    }
                    else
                    {
                        newpixel.Green = (int)Math.Floor(Z11);
                    }


                    Z1        = (float)(P1.Blue * (1 - XFraction) + P2.Blue * XFraction);
                    Z2        = (float)(P3.Blue * (1 - XFraction) + P4.Blue * XFraction);
                    float Z13 = (Z1 * (1 - YFraction) + Z2 * YFraction);
                    int temp3 = (int)(Z13 * 10) % 10;
                    if (temp3 > 5)
                    {
                        newpixel.Blue = (int)Math.Ceiling(Z13);
                    }
                    else
                    {
                        newpixel.Blue = (int)Math.Floor(Z13);
                    }

                    //newpixel.Blue = (int)(Z1 * (1 - YFraction) + Z2 * YFraction);

                    //Thread.CurrentThread.ManagedThreadId);
                    //Thread.Sleep(1000);
                    resizeee[i, j].Red   = newpixel.Red;
                    resizeee[i, j].Blue  = newpixel.Blue;
                    resizeee[i, j].Green = newpixel.Green;
                });
                //Thread.CurrentThread.ManagedThreadId);
            });

            int        x, y;
            BitmapData bitmapData3 = b1.LockBits(new Rectangle(0, 0, b1.Width, b1.Height),
                                                 ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            unsafe
            {
                byte *imagePointer1 = (byte *)bitmapData3.Scan0;

                for (x = 0; x < bitmapData3.Height; x++)
                {
                    for (y = 0; y < bitmapData3.Width; y++)
                    {
                        imagePointer1[0] = (byte)resizeee[x, y].Blue;
                        imagePointer1[1] = (byte)resizeee[x, y].Green;
                        imagePointer1[2] = (byte)resizeee[x, y].Red;
                        imagePointer1[3] = (byte)255;
                        //4 bytes per pixel
                        imagePointer1 += 4;
                    }//end for j
                    //4 bytes per pixel
                    imagePointer1 += bitmapData3.Stride - (bitmapData3.Width * 4);
                } //end for i
            }     //end unsafe
            b1.UnlockBits(bitmapData3);
            transferedimage   = new Bitmap(b1);
            pictureBox2.Image = b1;
            dt2 = DateTime.Now;
            dt3 = dt2 - dt1;
            //image = image1;
            MessageBox.Show(dt3.ToString());
        }
Пример #8
0
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter           = "Bitmap files (*.bmp)|*.bmp|PNG files (*.png)|*.png|TIFF files (*.tif)|*tif|JPEG files (*.jpg)|*.jpg |All files (*.*)|*.*";
            ofd.FilterIndex      = 5;
            ofd.RestoreDirectory = true;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    pictureBox2.Image = Bitmap.FromFile(ofd.FileName);
                    choosedimage      = new Bitmap(ofd.FileName);
                    int hei = choosedimage.Height;
                    int wie = choosedimage.Width;
                    Buffer = new my_color[hei, wie];
                    mygray = new my_color[hei, wie];
                    int        x, y;
                    BitmapData bitmapData2 = choosedimage.LockBits(new Rectangle(0, 0, choosedimage.Width, choosedimage.Height),
                                                                   ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                    unsafe
                    {
                        byte *imagePointer1 = (byte *)bitmapData2.Scan0;

                        for (x = 0; x < bitmapData2.Height; x++)
                        {
                            for (y = 0; y < bitmapData2.Width; y++)
                            {
                                double b = (int)imagePointer1[0];
                                double g = (int)imagePointer1[1];
                                double r = (int)imagePointer1[2];
                                Buffer[x, y].Blue  = (int)b;
                                Buffer[x, y].Green = (int)g;
                                Buffer[x, y].Red   = (int)r;
                                choosed_his_red[(int)r]++;
                                choosed_his_green[(int)g]++;
                                choosed_his_blue[(int)b]++;
                                //4 bytes per pixel
                                imagePointer1 += 4;
                            }//end for j
                            //4 bytes per pixel
                            imagePointer1 += bitmapData2.Stride - (bitmapData2.Width * 4);
                        } //end for i
                    }     //end unsafe
                    choosedimage.UnlockBits(bitmapData2);
                }
                catch (Exception)
                {
                    MessageBox.Show("errooooooor");
                }
            }

            int n_width  = width;
            int n_hieght = Height;

            float w_ratio = (float)width / n_width;
            float h_ratio = (float)Height / n_hieght;

            int      X1, X2, Y1, Y2;
            my_color P1, P2, P3, P4;
            float    OldX, OldY, NewX, NewY;

            float XFraction, YFraction;
            float Z1, Z2;

            b1 = new Bitmap(n_width, n_hieght);
            my_color newpixel = new my_color();



            for (int i = 0; i < n_hieght; i++)
            {
                NewY = i;
                for (int j = 0; j < n_width; j++)
                {
                    NewX = j;
                    OldX = NewX * w_ratio;
                    OldY = NewY * h_ratio;

                    X1 = (int)Math.Floor(OldX); X2 = X1 + 1;
                    Y1 = (int)Math.Floor(OldY); Y2 = Y1 + 1;

                    if (X2 == width)
                    {
                        X2 -= 1;
                    }
                    if (Y2 == Height)
                    {
                        Y2 -= 1;
                    }
                    if (X1 == width)
                    {
                        X1 -= 1;
                    }
                    if (Y1 == Height)
                    {
                        Y1 -= 1;
                    }

                    P1 = Buffer[Y1, X1]; P2 = Buffer[Y1, X2];
                    P3 = Buffer[Y2, X1]; P4 = Buffer[Y2, X2];

                    XFraction = OldX - X1;
                    YFraction = OldY - Y1;

                    Z1 = (float)(P1.Red * (1 - XFraction) + P2.Red * XFraction);
                    Z2 = (float)(P3.Red * (1 - XFraction) + P4.Red * XFraction);
                    float Z12   = (Z1 * (1 - YFraction) + Z2 * YFraction);
                    int   temp1 = (int)(Z12 * 10) % 10;
                    if (temp1 > 5)
                    {
                        newpixel.Red = (int)Math.Ceiling(Z12);
                    }
                    else
                    {
                        newpixel.Red = (int)Math.Floor(Z12);
                    }

                    //newpixel.Red = (int)(Z1 * (1 - YFraction) + Z2 * YFraction);

                    Z1 = (float)(P1.Green * (1 - XFraction) + P2.Green * XFraction);
                    Z2 = (float)(P3.Green * (1 - XFraction) + P4.Green * XFraction);
                    float Z11   = (Z1 * (1 - YFraction) + Z2 * YFraction);
                    int   temp2 = (int)(Z11 * 10) % 10;
                    if (temp2 > 5)
                    {
                        newpixel.Green = (int)Math.Ceiling(Z11);
                    }
                    else
                    {
                        newpixel.Green = (int)Math.Floor(Z11);
                    }


                    Z1 = (float)(P1.Blue * (1 - XFraction) + P2.Blue * XFraction);
                    Z2 = (float)(P3.Blue * (1 - XFraction) + P4.Blue * XFraction);
                    float Z13   = (Z1 * (1 - YFraction) + Z2 * YFraction);
                    int   temp3 = (int)(Z13 * 10) % 10;
                    if (temp3 > 5)
                    {
                        newpixel.Blue = (int)Math.Ceiling(Z13);
                    }
                    else
                    {
                        newpixel.Blue = (int)Math.Floor(Z13);
                    }

                    //newpixel.Blue = (int)(Z1 * (1 - YFraction) + Z2 * YFraction);


                    Color clr;
                    clr = Color.FromArgb(255, newpixel.Red, newpixel.Green, newpixel.Blue);
                    b1.SetPixel(j, i, clr);
                }
            }
        }
Пример #9
0
        private void button3_Click(object sender, EventArgs e)
        {
            DateTime dt1 = new DateTime();
            DateTime dt2 = new DateTime();
            TimeSpan dt3 = new TimeSpan();

            dt1 = DateTime.Now;
            int n_width  = int.Parse(textBox2.Text);
            int n_hieght = int.Parse(textBox3.Text);

            float w_ratio = (float)width / n_width;
            float h_ratio = (float)Height / n_hieght;

            int      X1, X2, Y1, Y2;
            my_color P1, P2, P3, P4;
            float    OldX, OldY, NewX, NewY;

            float XFraction, YFraction;
            float Z1, Z2;

            Bitmap   b1       = new Bitmap(n_width, n_hieght);
            my_color newpixel = new my_color();



            for (int i = 0; i < n_hieght; i++)
            {
                NewY = i;
                for (int j = 0; j < n_width; j++)
                {
                    NewX = j;
                    OldX = NewX * w_ratio;
                    OldY = NewY * h_ratio;

                    X1 = (int)Math.Floor(OldX); X2 = X1 + 1;
                    Y1 = (int)Math.Floor(OldY); Y2 = Y1 + 1;

                    if (X2 == width)
                    {
                        X2 -= 1;
                    }
                    if (Y2 == Height)
                    {
                        Y2 -= 1;
                    }
                    if (X1 == width)
                    {
                        X1 -= 1;
                    }
                    if (Y1 == Height)
                    {
                        Y1 -= 1;
                    }

                    P1 = Buffer2D[Y1, X1]; P2 = Buffer2D[Y1, X2];
                    P3 = Buffer2D[Y2, X1]; P4 = Buffer2D[Y2, X2];

                    XFraction = OldX - X1;
                    YFraction = OldY - Y1;

                    Z1 = (float)(P1.Red * (1 - XFraction) + P2.Red * XFraction);
                    Z2 = (float)(P3.Red * (1 - XFraction) + P4.Red * XFraction);
                    float Z12   = (Z1 * (1 - YFraction) + Z2 * YFraction);
                    int   temp1 = (int)(Z12 * 10) % 10;
                    if (temp1 > 5)
                    {
                        newpixel.Red = (int)Math.Ceiling(Z12);
                    }
                    else
                    {
                        newpixel.Red = (int)Math.Floor(Z12);
                    }

                    //newpixel.Red = (int)(Z1 * (1 - YFraction) + Z2 * YFraction);

                    Z1 = (float)(P1.Green * (1 - XFraction) + P2.Green * XFraction);
                    Z2 = (float)(P3.Green * (1 - XFraction) + P4.Green * XFraction);
                    float Z11   = (Z1 * (1 - YFraction) + Z2 * YFraction);
                    int   temp2 = (int)(Z11 * 10) % 10;
                    if (temp2 > 5)
                    {
                        newpixel.Green = (int)Math.Ceiling(Z11);
                    }
                    else
                    {
                        newpixel.Green = (int)Math.Floor(Z11);
                    }


                    Z1 = (float)(P1.Blue * (1 - XFraction) + P2.Blue * XFraction);
                    Z2 = (float)(P3.Blue * (1 - XFraction) + P4.Blue * XFraction);
                    float Z13   = (Z1 * (1 - YFraction) + Z2 * YFraction);
                    int   temp3 = (int)(Z13 * 10) % 10;
                    if (temp3 > 5)
                    {
                        newpixel.Blue = (int)Math.Ceiling(Z13);
                    }
                    else
                    {
                        newpixel.Blue = (int)Math.Floor(Z13);
                    }

                    //newpixel.Blue = (int)(Z1 * (1 - YFraction) + Z2 * YFraction);


                    Color clr;
                    clr = Color.FromArgb(255, newpixel.Red, newpixel.Green, newpixel.Blue);
                    b1.SetPixel(j, i, clr);
                }
            }
            transferedimage   = new Bitmap(b1);
            pictureBox2.Image = b1;
            dt2 = DateTime.Now;
            dt3 = dt2 - dt1;
            //image = image1;
            MessageBox.Show(dt3.ToString());
        }
Пример #10
0
        public Bitmap bil_resize(Bitmap image, int width, int height)
        {
            int n_width  = width;
            int n_hieght = height;

            float w_ratio = (float)(image.Width / n_width);
            float h_ratio = (float)(image.Height / n_hieght);

            Bitmap myimage = image;
            int    X1, X2, Y1, Y2;

            float OldX, OldY, NewX, NewY;

            float XFraction, YFraction;
            float Z1, Z2;

            Bitmap b1 = new Bitmap(n_width, n_hieght);
            int    x;
            int    y;

            my_color[,] Buffer2D      = new my_color[myimage.Height, myimage.Width];
            my_color[,] resized_image = new my_color[n_hieght, n_width];
            BitmapData bitmapData2 = myimage.LockBits(new Rectangle(0, 0, myimage.Width, myimage.Height),
                                                      ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            unsafe
            {
                byte *imagePointer1 = (byte *)bitmapData2.Scan0;

                for (x = 0; x < bitmapData2.Height; x++)
                {
                    for (y = 0; y < bitmapData2.Width; y++)
                    {
                        double b = (int)imagePointer1[0];
                        double g = (int)imagePointer1[1];
                        double r = (int)imagePointer1[2];
                        Buffer2D[x, y].Blue  = (int)b;
                        Buffer2D[x, y].Green = (int)g;
                        Buffer2D[x, y].Red   = (int)r;
                        //4 bytes per pixel
                        imagePointer1 += 4;
                    }//end for j
                    //4 bytes per pixel
                    imagePointer1 += bitmapData2.Stride - (bitmapData2.Width * 4);
                } //end for i
            }     //end unsafe
            myimage.UnlockBits(bitmapData2);
            my_color P1, P2, P3, P4;
            my_color newpixel = new my_color();

            for (int i = 0; i < n_hieght; i++)
            {
                NewY = i;
                OldY = NewY * h_ratio;
                for (int j = 0; j < n_width; j++)
                {
                    NewX = j;
                    OldX = NewX * w_ratio;


                    X1 = (int)Math.Floor(OldX); X2 = X1 + 1;
                    Y1 = (int)Math.Floor(OldY); Y2 = Y1 + 1;

                    if (X2 == image.Width)
                    {
                        X2 -= 1;
                    }
                    if (Y2 == image.Height)
                    {
                        Y2 -= 1;
                    }
                    if (X1 == image.Width)
                    {
                        X1 -= 1;
                    }
                    if (Y1 == image.Height)
                    {
                        Y1 -= 1;
                    }

                    P1 = Buffer2D[Y1, X1]; P2 = Buffer2D[Y1, X2];
                    P3 = Buffer2D[Y2, X1]; P4 = Buffer2D[Y2, X2];

                    XFraction = OldX - X1;
                    YFraction = OldY - Y1;

                    Z1 = (float)(P1.Red * (1 - XFraction) + P2.Red * XFraction);
                    Z2 = (float)(P3.Red * (1 - XFraction) + P4.Red * XFraction);
                    float Z12   = (Z1 * (1 - YFraction) + Z2 * YFraction);
                    int   temp1 = (int)(Z12 * 10) % 10;
                    if (temp1 > 5)
                    {
                        newpixel.Red = (int)Math.Ceiling(Z12);
                    }
                    else
                    {
                        newpixel.Red = (int)Math.Floor(Z12);
                    }

                    //newpixel.Red = (int)(Z1 * (1 - YFraction) + Z2 * YFraction);

                    Z1 = (float)(P1.Green * (1 - XFraction) + P2.Green * XFraction);
                    Z2 = (float)(P3.Green * (1 - XFraction) + P4.Green * XFraction);
                    float Z11   = (Z1 * (1 - YFraction) + Z2 * YFraction);
                    int   temp2 = (int)(Z11 * 10) % 10;
                    if (temp2 > 5)
                    {
                        newpixel.Green = (int)Math.Ceiling(Z11);
                    }
                    else
                    {
                        newpixel.Green = (int)Math.Floor(Z11);
                    }


                    Z1 = (float)(P1.Blue * (1 - XFraction) + P2.Blue * XFraction);
                    Z2 = (float)(P3.Blue * (1 - XFraction) + P4.Blue * XFraction);
                    float Z13   = (Z1 * (1 - YFraction) + Z2 * YFraction);
                    int   temp3 = (int)(Z13 * 10) % 10;
                    if (temp3 > 5)
                    {
                        newpixel.Blue = (int)Math.Ceiling(Z13);
                    }
                    else
                    {
                        newpixel.Blue = (int)Math.Floor(Z13);
                    }


                    resized_image[i, j].Blue  = newpixel.Blue;
                    resized_image[i, j].Red   = newpixel.Red;
                    resized_image[i, j].Green = newpixel.Green;
                    Color clr;
                    clr = Color.FromArgb(255, newpixel.Red, newpixel.Green, newpixel.Blue);
                    b1.SetPixel(j, i, clr);
                }
            }
            Bitmap     output      = new Bitmap(n_width, n_hieght);
            BitmapData bitmapData3 = output.LockBits(new Rectangle(0, 0, n_width, n_hieght),
                                                     ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            unsafe
            {
                byte *imagePointer1 = (byte *)bitmapData3.Scan0;
                for (int i = 0; i < bitmapData3.Height; i++)
                {
                    for (int j = 0; j < bitmapData3.Width; j++)
                    {
                        imagePointer1[0] = (byte)resized_image[i, j].Blue;
                        imagePointer1[1] = (byte)resized_image[i, j].Green;
                        imagePointer1[2] = (byte)resized_image[i, j].Red;
                        imagePointer1[3] = 255;
                        //4 bytes per pixel
                        imagePointer1 += 4;
                    }//end for j
                    //4 bytes per pixel
                    imagePointer1 += (bitmapData3.Stride - (bitmapData3.Width * 4));
                } //end for i
            }     //end unsafe
            output.UnlockBits(bitmapData3);
            return(b1);// col;
        }
Пример #11
0
        /*
         * public my_color[,] histo_equalization()
         * {
         *  // my temp 2D arrays to calculate cumulative
         *  int[] local_his_red_run_sum = new int[256];
         *  int[] local_his_green_run_sum = new int[256];
         *  int[] local_his_blue_run_sum = new int[256];
         *
         *  int[] choosed_his_red_run_sum = new int[256];
         *  int[] choosed_his_green_run_sum = new int[256];
         *  int[] choosed_his_blue_run_sum = new int[256];
         *  // my temp 2D arrays to the final values
         *  int[] local_his_red_final = new int[256];
         *  int[] local_his_green_final = new int[256];
         *  int[] local_his_blue_final = new int[256];
         *
         *  int[] choosed_his_red_final = new int[256];
         *  int[] choosed_his_green_final = new int[256];
         *  int[] choosed_his_blue_final = new int[256];
         *  //1- calculate image histogram // Done
         *  // 1D arrays
         *  //local_his_red -> first image histogram
         *  //local_his_green -> first image histogram
         *  //local_his_bkue -> first image histogram
         *  //-------------------------------------------------------------------------
         *  //choosed_his_red -> second image histogram
         *  //choosed_his_green -> second image histogram
         *  //choosed_his_blue -> second image histogram
         *  //**************************************************************************
         *  //**************************************************************************
         *  //2- calculate cumulative for it
         *  for (int i = 0; i < 256; i++)
         *  {
         *      if (i == 0)
         *      {
         *          local_his_red_run_sum[i] = local_his_red[i];
         *          local_his_green_run_sum[i] = local_his_green[i];
         *          local_his_blue_run_sum[i] = local_his_blue[i];
         *
         *          choosed_his_red_run_sum[i] = choosed_his_red[i];
         *          choosed_his_green_run_sum[i] = choosed_his_green[i];
         *          choosed_his_blue_run_sum[i] = choosed_his_blue[i];
         *      }
         *      else
         *      {
         *          local_his_red_run_sum[i] = local_his_red[i] + local_his_red_run_sum[i - 1];
         *          local_his_green_run_sum[i] = local_his_green[i] + local_his_green_run_sum[i - 1];
         *          local_his_blue_run_sum[i] = local_his_blue[i] + local_his_blue_run_sum[i - 1];
         *
         *          choosed_his_red_run_sum[i] = choosed_his_red[i] + choosed_his_red_run_sum[i - 1];
         *          choosed_his_green_run_sum[i] = choosed_his_green[i] + choosed_his_green_run_sum[i - 1];
         *          choosed_his_blue_run_sum[i] = choosed_his_blue[i] + choosed_his_blue_run_sum[i - 1];
         *      }
         *  }
         *  //**************************************************************************
         *  //3- divide each value in it by the maximum cumulative
         *  //4- multibly by the new range : const 255
         *  int load_maximum_cumulative_red = local_his_red_run_sum[255];
         *  int load_maximum_cumulative_green = local_his_green_run_sum[255];
         *  int load_maximum_cumulative_blue = local_his_blue_run_sum[255];
         *
         *  int choosed_maximum_cumulative_red = choosed_his_red_run_sum[255];
         *  int choosed_maximum_cumulative_green = choosed_his_green_run_sum[255];
         *  int choosed_maximum_cumulative_blue = choosed_his_blue_run_sum[255];
         *
         *  for (int j = 0; j < 256; j++)
         *  {
         *      local_his_red_final[j] = (int)Math.Round((decimal)((local_his_red_run_sum[j] / load_maximum_cumulative_red) * 255));
         *      local_his_green_final[j] = (int)Math.Round((decimal)((local_his_green_run_sum[j] / load_maximum_cumulative_green) * 255));
         *      local_his_blue_final[j] = (int)Math.Round((decimal)((local_his_blue_run_sum[j] / load_maximum_cumulative_blue) * 255));
         *
         *      choosed_his_red_final[j] = (int)Math.Round((decimal)((choosed_his_red_run_sum[j] / choosed_maximum_cumulative_red) * 255));
         *      choosed_his_green_final[j] = (int)Math.Round((decimal)((choosed_his_green_run_sum[j] / choosed_maximum_cumulative_green) * 255));
         *      choosed_his_blue_final[j] = (int)Math.Round((decimal)((choosed_his_blue_run_sum[j] / choosed_maximum_cumulative_blue) * 255));
         *  }
         *
         * }
         */
        private void button3_Click(object sender, EventArgs e)
        {
            DateTime dt1 = new DateTime();
            DateTime dt2 = new DateTime();
            TimeSpan dt3 = new TimeSpan();

            dt1 = DateTime.Now;
            int nearstR  = 1000;
            int nearstG  = 1000;
            int nearstB  = 1000;
            int in_red   = -1;
            int in_green = -1;
            int in_blue  = -1;

            for (int i = 0; i < 256; i++)
            {
                nearstR  = 1000;
                nearstG  = 1000;
                nearstB  = 1000;
                in_red   = -1;
                in_green = -1;
                in_blue  = -1;
                for (int j = 0; j < 256; j++)
                {
                    if (Math.Abs(local_his_red[i] - choosed_his_red[j]) <= nearstR)
                    {
                        nearstR = Math.Abs(local_his_red[i] - choosed_his_red[j]);
                        in_red  = j;
                    }
                    if (Math.Abs(local_his_blue[i] - choosed_his_blue[j]) <= nearstG)
                    {
                        nearstG  = Math.Abs(local_his_blue[i] - choosed_his_blue[j]);
                        in_green = j;
                    }
                    if (Math.Abs(local_his_green[i] - choosed_his_green[j]) <= nearstB)
                    {
                        nearstB = Math.Abs(local_his_green[i] - choosed_his_green[j]);
                        in_blue = j;
                    }
                }
                matched_his_red[i]   = in_red;
                matched_his_green[i] = in_green;
                matched_his_blue[i]  = in_blue;
            }

            Bitmap matchedimage = new Bitmap(localimage.Width, localimage.Height);
            Color  newpixel     = new Color();

            my_color[,] my_new_pixel = new my_color[matchedimage.Height, matchedimage.Width];
            int        x, y;
            BitmapData bitmapData3 = matchedimage.LockBits(new Rectangle(0, 0, matchedimage.Width, matchedimage.Height),
                                                           ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            unsafe
            {
                byte *imagePointer1 = (byte *)bitmapData3.Scan0;

                for (x = 0; x < bitmapData3.Height; x++)
                {
                    for (y = 0; y < bitmapData3.Width; y++)
                    {
                        my_new_pixel[x, y]       = Buffer2D[x, y];
                        my_new_pixel[x, y].Red   = matched_his_red[(int)my_new_pixel[x, y].Red];
                        my_new_pixel[x, y].Green = matched_his_green[(int)my_new_pixel[x, y].Green];
                        my_new_pixel[x, y].Blue  = matched_his_blue[(int)my_new_pixel[x, y].Blue];
                        imagePointer1[0]         = (byte)0;
                        imagePointer1[1]         = (byte)0;
                        imagePointer1[2]         = (byte)0;
                        imagePointer1[3]         = (byte)255;
                        //4 bytes per pixel
                        imagePointer1 += 4;
                    }//end for j
                    //4 bytes per pixel
                    imagePointer1 += bitmapData3.Stride - (bitmapData3.Width * 4);
                } //end for i
            }     //end unsafe
            matchedimage.UnlockBits(bitmapData3);
            BitmapData bitmapData4 = matchedimage.LockBits(new Rectangle(0, 0, matchedimage.Width, matchedimage.Height),
                                                           ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            unsafe
            {
                byte *imagePointer1 = (byte *)bitmapData4.Scan0;

                for (x = 0; x < bitmapData4.Height; x++)
                {
                    for (y = 0; y < bitmapData4.Width; y++)
                    {
                        //my_new_pixel[x, y] = Buffer2D[x, y];

                        imagePointer1[0] = (byte)my_new_pixel[x, y].Blue;
                        imagePointer1[1] = (byte)my_new_pixel[x, y].Green;
                        imagePointer1[2] = (byte)my_new_pixel[x, y].Red;
                        imagePointer1[3] = (byte)255;
                        //4 bytes per pixel
                        imagePointer1 += 4;
                    }//end for j
                    //4 bytes per pixel
                    imagePointer1 += bitmapData4.Stride - (bitmapData4.Width * 4);
                } //end for i
            }     //end unsafe
            matchedimage.UnlockBits(bitmapData4);
            pictureBox3.Image = matchedimage;
            transferedimage   = new Bitmap(matchedimage);
            dt2 = DateTime.Now;
            dt3 = dt2 - dt1;
            //image = image1;
            MessageBox.Show(dt3.ToString());
        }