Esempio n. 1
0
        private void button2_Click(object sender, EventArgs e)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();

            System.Drawing.Imaging.BitmapData objectsData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
                                                                       System.Drawing.Imaging.ImageLockMode.ReadOnly, b.PixelFormat);

            AForge.Imaging.UnmanagedImage    grayImage = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(new AForge.Imaging.UnmanagedImage(objectsData));
            AForge.Imaging.Filters.Threshold th        = new AForge.Imaging.Filters.Threshold(128);
            th.ApplyInPlace(grayImage);

            // unlock image
            b.UnlockBits(objectsData);
            pictureBox1.Image = grayImage.ToManagedImage();;
            b = grayImage.ToManagedImage();
            int count = 0;

            b2 = ResizeImage(b, new Size(size, size));
            String s = "";

            pictureBox2.Image = b2;
            for (int i = 0; i < Bsize; i++)
            {
                int k = 0;
                int t = 0;
                for (int j = 0; j < Bsize; j++)
                {
                    if (b.GetPixel(i, j).R > 100 && b.GetPixel(i, j).G > 100 &&
                        b.GetPixel(i, j).B > 100)
                    {
                        s = s + 0;
                    }
                    else
                    {
                        s = s + 1;
                        count++;
                        k++;
                    }
                    if (b.GetPixel(j, i).R > 100 && b.GetPixel(j, i).G > 100 &&
                        b.GetPixel(j, i).B > 100)
                    {
                    }
                    else
                    {
                        t++;
                    }
                }
                richTextBox2.Text = richTextBox2.Text + k + "";
                richTextBox1.Text = richTextBox1.Text + t + "\n";
                h      = h + "," + k;
                w      = w + "," + t;
                s      = s + "\n";
                result = string.Join("", h);
                result = count + "\n" + h + "\n" + w;
            }
            label1.Text = "Total 1 is " + count;
            //   System.IO.File.WriteAllText( "test.txt", s);
        }
Esempio n. 2
0
        private void Threshold()
        {
            if (!CheckIfGrayscale())
            {
                Greyscale();
            }

            AForge.Imaging.Filters.Threshold filter = new AForge.Imaging.Filters.Threshold(230);
            // apply the filter
            filter.ApplyInPlace(image);
        }
Esempio n. 3
0
        private void Go_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Windows.Media.Imaging.BitmapImage imagesource = imageBox.Source as System.Windows.Media.Imaging.BitmapImage;
                if (imageBox.Source == null)
                {
                    System.Windows.MessageBox.Show("Add your image to the Image 1 box!");
                    return;
                }


                System.Drawing.Bitmap image = BmpImage2Bmp(imagesource);
                System.Windows.Media.Imaging.BitmapImage final = new System.Windows.Media.Imaging.BitmapImage();

                if (!AForge.Imaging.Image.IsGrayscale(image))
                {
                    AForge.Imaging.Filters.ExtractChannel Grayer = new AForge.Imaging.Filters.ExtractChannel(0);
                    image = Grayer.Apply(image);
                }

                if (Threshold.IsChecked == true)
                {
                    imageBox.Source = null;
                    AForge.Imaging.Filters.Threshold threshold = new AForge.Imaging.Filters.Threshold((int)slider.Value);
                    threshold.ApplyInPlace(image);
                    final = Bmp2BmpImage(image);
                }
                else if (GaussianFilter.IsChecked == true)
                {
                    AForge.Imaging.Filters.GaussianBlur Gauss = new AForge.Imaging.Filters.GaussianBlur();
                    Gauss.Sigma = GaussSigma_Slide.Value;
                    Gauss.Size  = (int)GaussSize_Slide.Value;
                    AForge.Imaging.UnmanagedImage unmanagedImage = AForge.Imaging.UnmanagedImage.FromManagedImage(image);
                    AForge.Imaging.UnmanagedImage Dst            = unmanagedImage.Clone();
                    Gauss.Apply(unmanagedImage, Dst);
                    final = Bmp2BmpImage(Dst.ToManagedImage());
                }

                else if (HiPass.IsChecked == true)
                {
                    AForge.Imaging.Filters.Sharpen filter = new AForge.Imaging.Filters.Sharpen();
                    filter.ApplyInPlace(image);
                    final = Bmp2BmpImage(image);
                }

                else if (Erode.IsChecked == true)
                {
                    AForge.Imaging.Filters.Erosion filter = new AForge.Imaging.Filters.Erosion();
                    filter.ApplyInPlace(image);
                    final = Bmp2BmpImage(image);
                }

                else if (Invert.IsChecked == true)
                {
                    AForge.Imaging.Filters.Invert filter = new AForge.Imaging.Filters.Invert();
                    filter.ApplyInPlace(image);
                    final = Bmp2BmpImage(image);
                }
                else if (EdgeDetector.IsChecked == true)
                {
                    AForge.Imaging.Filters.CannyEdgeDetector filter = new AForge.Imaging.Filters.CannyEdgeDetector();
                    filter.ApplyInPlace(image);
                    final = Bmp2BmpImage(image);
                }

                else if (Median.IsChecked == true)
                {
                    AForge.Imaging.Filters.Median filter = new AForge.Imaging.Filters.Median();
                    filter.Size = (int)GaussSize_Slide.Value;
                    filter.ApplyInPlace(image);
                    final = Bmp2BmpImage(image);
                }

                else if (More.IsChecked == true)
                {
                    if (Dilate.IsSelected)
                    {
                        AForge.Imaging.Filters.Dilatation filter = new AForge.Imaging.Filters.Dilatation();
                        filter.ApplyInPlace(image);
                        final = Bmp2BmpImage(image);
                    }
                }

                imageBox.Source = final;
                TransformImage  = image;

                boxWidth  = imageBox.RenderSize.Width;
                boxHeight = imageBox.RenderSize.Height;
            }
            catch (Exception exc)
            {
                System.Windows.MessageBox.Show(exc.ToString());
            }
        }
Esempio n. 4
0
 public Bitmap Threshold(Bitmap image, int value)
 {
     AForge.Imaging.Filters.Threshold filter = new AForge.Imaging.Filters.Threshold(value);
     return filter.Apply(AForge.Imaging.Filters.Grayscale.CommonAlgorithms.Y.Apply(image));
 }
Esempio n. 5
0
        public Bitmap Process(Bitmap image)
        {
            Bitmap filteredImage = convertFormatTo32(image);

            if (filterLevel >= 0)
            {
                filteredImage = resize(filteredImage, ImageSize.Width, ImageSize.Height);
            }
            if (filterLevel >= 1)
            {

                if (ContrastStretch)
                {
                    AForge.Imaging.Filters.ContrastStretch stretcher = new AForge.Imaging.Filters.ContrastStretch();
                    filteredImage = stretcher.Apply(filteredImage);

                }

                if (Histogram)
                {
                    AForge.Imaging.Filters.HistogramEqualization histogrammer = new AForge.Imaging.Filters.HistogramEqualization();
                    filteredImage = histogrammer.Apply(filteredImage);

                }

                if (Gaussian)
                {
                    AForge.Imaging.Filters.GaussianBlur blurrer = new AForge.Imaging.Filters.GaussianBlur();
                    blurrer.Size = (int)GaussianStrength;
                    filteredImage = blurrer.Apply(filteredImage);
                }

                if (ContrastAdjustment)
                {
                    AForge.Imaging.Filters.ContrastCorrection contraster = new AForge.Imaging.Filters.ContrastCorrection();
                    contraster.Factor = (float)ContrastStrength;
                    filteredImage = contraster.Apply(filteredImage);
                }

                if (Greyscale)
                {
                    filteredImage = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(filteredImage);
                    //Greyscale downgrades format
                    // filteredImage  = convertFormatTo32(filteredImage.InternalBitmap);
                }
                if (Threshold)
                {
                    //filteredImage.InternalBitmap = convertFormatToGS(filteredImage.InternalBitmap);
                    AForge.Imaging.Filters.Threshold thresholder = new AForge.Imaging.Filters.Threshold();
                    thresholder.ThresholdValue = (int)(((double)ThresholdStrength / 10.0) * 255.0);
                    filteredImage = thresholder.Apply(filteredImage);

                }
                if (Bradley)
                {
                    AForge.Imaging.Filters.BradleyLocalThresholding bradlifier = new AForge.Imaging.Filters.BradleyLocalThresholding();

                    filteredImage = bradlifier.Apply(filteredImage);

                }
            }

            return filteredImage;
        }
Esempio n. 6
0
        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
            System.Drawing.Imaging.BitmapData objectsData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
                                                                       System.Drawing.Imaging.ImageLockMode.ReadOnly, b.PixelFormat);

            AForge.Imaging.UnmanagedImage    grayImage = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(new AForge.Imaging.UnmanagedImage(objectsData));
            AForge.Imaging.Filters.Threshold th        = new AForge.Imaging.Filters.Threshold(128);
            th.ApplyInPlace(grayImage);

            // unlock image
            b.UnlockBits(objectsData);
            pictureBox2.Image = grayImage.ToManagedImage();;
            Pen      pen  = new Pen(Color.Red, 2);
            Bitmap   img  = grayImage.ToManagedImage();
            Graphics g    = Graphics.FromImage(b);
            string   text = "0";
            int      z    = 0;


            {
                for (int j = 2; j < img.Height; j++)
                {
                    int pixel = img.GetPixel(1, j).R;
                    if (pixel == 255)
                    {
                        z = j;
                        break;
                    }
                    //  g.DrawString(j.ToString(), new System.Drawing.Font("Tahoma", 12, FontStyle.Bold), Brushes.Red, i, j);

                    //  g.DrawLine(pen, i, j, i+1, j+1 );
                }

                //  if (z!=0)
                //   break;
                //    text = text + "\n";
            }
            g.DrawRectangle(pen, 0, 0, z, z);
            bitsize = z;
            int l = bitsize % 3;

            int lastblack = 0;
            int lastwhite = 0;

            for (int i = bitsize + bitsize / 3; i < img.Width;)
            {
                int ptb          = 0;
                int ptw          = 0;
                int bitsizecount = 0;
                for (int j = bitsize; j < img.Height; j++)
                {
                    bitsizecount++;
                    g.DrawLine(pen, i, i, i + 1, j);
                    int pixel = img.GetPixel(i, j).R;
                    if (pixel == 255)
                    {
                        ptb++;
                        ptw = 0;
                    }
                    if (pixel == 0)
                    {
                        ptb = 0;
                        ptw++;
                    }
                    if (bitsize == bitsizecount)
                    {
                        if (ptb >= ptw)
                        {
                            horijontal = horijontal + 0;
                        }
                        else
                        {
                            horijontal = horijontal + 1;
                        }
                        bitsizecount = 0;
                        ptb          = 0;
                        ptw          = 0;
                    }
                }
                if (i % 3 == 0)
                {
                    i = i + bitsize + 1;
                }
                else if (i % 5 == 0)
                {
                    i = i + bitsize + 1;
                }
                else
                {
                    i = i + bitsize + bitsize % 3;
                }
            }

            for (int j = bitsize + bitsize / 3; j < img.Height;)
            {
                int ptb          = 0;
                int ptw          = 0;
                int bitsizecount = 0;
                for (int i = bitsize; i < img.Height; i++)
                {
                    bitsizecount++;
                    g.DrawLine(pen, i, j, i, j + 1);
                    int pixel = img.GetPixel(i, j).R;
                    if (pixel == 255)
                    {
                        ptb++;
                        ptw = 0;
                    }
                    if (pixel == 0)
                    {
                        ptb = 0;
                        ptw++;
                    }
                    if (bitsize == bitsizecount)
                    {
                        if (ptb > ptw)
                        {
                            vertical = vertical + 0;
                        }
                        else
                        {
                            vertical = vertical + 1;
                        }
                        bitsizecount = 0;
                        ptb          = 0;
                        ptw          = 0;
                    }
                }
                // j = j + bitsize+2 ;

                if (j % 3 == 0)
                {
                    j = j + bitsize + 1;
                }
                else if (j % 5 == 0)
                {
                    j = j + bitsize + 1;
                }
                else
                {
                    j = j + bitsize + bitsize % 3;
                }
            }


            pictureBox2.Image = b;


            int la = horijontal.LastIndexOf('1');

            horijontal = horijontal.Substring(0, la);
            int lb = vertical.LastIndexOf('1');

            vertical = vertical.Substring(0, lb);
            String s1 = horijontal + "" + vertical;
            String s2 = vertical + "" + horijontal;
            int    f  = 8 - s2.Length % 8;

            for (int k = 0; k < f; k++)
            {
                s2 = s2 + "0";
                s1 = s1 + "0";
            }
            richTextBox1.Text = "Result Binarycode: " + vertical + horijontal;
            label1.Text       = "Result TEXT: " + BinaryToString(vertical + horijontal);// +" - " + BinaryToString(horijontal);
            label2.Text       = "Bitsize: " + bitsize + "  Datalength " + s2.Length;
        }
Esempio n. 7
0
        private void init()
        {
            Graphics g;

            System.Drawing.Image src;
            Pen p;

            AForge.Imaging.Filters.GaussianBlur         gb;
            AForge.Imaging.Filters.ContrastCorrection   cc;
            AForge.Imaging.Filters.BrightnessCorrection bc;
            AForge.Imaging.Filters.Threshold            th;

            gb = new AForge.Imaging.Filters.GaussianBlur();
            cc = new AForge.Imaging.Filters.ContrastCorrection();
            bc = new AForge.Imaging.Filters.BrightnessCorrection();
            th = new AForge.Imaging.Filters.Threshold();


            // load image source
            src = System.Drawing.Image.FromFile(@"d:\source.png");


            gb.Size        = 2;
            cc.Factor      = 40;
            bc.AdjustValue = -30;

            src = (System.Drawing.Image)(cc.Apply(bc.Apply(gb.Apply((Bitmap)src))));

            pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            g = Graphics.FromImage(pictureBox1.Image);

            img = (Bitmap)pictureBox1.Image;

            width  = img.Width;
            height = img.Height;
            blk_w  = width / 9;
            blk_h  = height / 9;

            g.FillRectangle(Brushes.White, pictureBox1.DisplayRectangle);
            g.DrawImage(src, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height), new Rectangle(0, 0, src.Width, src.Height), GraphicsUnit.Pixel);

            p = new Pen(Color.White, 20);


            for (int iy = 0; iy < 9; iy++)
            {
                g.DrawLine(p, 0, iy * blk_h, width, iy * blk_h);
            }

            g.DrawLine(p, 0, height - 1, width, height - 1);

            for (int ix = 0; ix < 9; ix++)
            {
                g.DrawLine(p, ix * blk_w, 0, ix * blk_w, height);
            }
            g.DrawLine(p, width - 1, 0, width - 1, height - 1);


            // init destination image
            // set destination width and height

            textBox1.Text = "290000";
        }
Esempio n. 8
0
 /// <summary>
 /// Change input to a binary image.
 /// </summary>
 /// <param name="image">Image to convert to binary.</param>
 /// <returns>Converted binary image (white and black).</returns>
 protected Bitmap BinaryImage(Bitmap image)
 {
     AForge.Imaging.Filters.Threshold filter = new AForge.Imaging.Filters.Threshold(1);
     return filter.Apply(AForge.Imaging.Filters.Grayscale.CommonAlgorithms.Y.Apply(image));
 }
Esempio n. 9
0
        public bool ProcessImage(Bitmap bitmap)
        {
            // На вход поступает необработанное изображение с веб-камеры

            //  Минимальная сторона изображения (обычно это высота)
            if (bitmap.Height > bitmap.Width)
            {
                throw new Exception("К такой забавной камере меня жизнь не готовила!");
            }
            //  Можно было, конечено, и не кидаться эксепшенами в истерике, но идите и купите себе нормальную камеру!
            int side = bitmap.Height;

            //  Отпиливаем границы, но не более половины изображения
            if (side < 4 * settings.border)
            {
                settings.border = side / 4;
            }
            side -= 2 * settings.border;

            //  Мы сейчас занимаемся тем, что красиво оформляем входной кадр, чтобы вывести его на форму
            Rectangle cropRect = new Rectangle((bitmap.Width - bitmap.Height) / 2 + settings.left + settings.border,
                                               settings.top + settings.border, side, side);

            //  Тут создаём новый битмапчик, который будет исходным изображением
            original = new Bitmap(cropRect.Width, cropRect.Height);

            //  Объект для рисования создаём
            Graphics g = Graphics.FromImage(original);

            g.DrawImage(bitmap, new Rectangle(0, 0, original.Width, original.Height), cropRect, GraphicsUnit.Pixel);
            Pen p = new Pen(Color.Red);

            p.Width = 1;

            //  Теперь всю эту муть пилим в обработанное изображение
            AForge.Imaging.Filters.Grayscale grayFilter = new AForge.Imaging.Filters.Grayscale(0.2125, 0.7154, 0.0721);
            var uProcessed = grayFilter.Apply(AForge.Imaging.UnmanagedImage.FromManagedImage(original));


            int blockWidth  = original.Width / settings.blocksCount;
            int blockHeight = original.Height / settings.blocksCount;

            for (int r = 0; r < settings.blocksCount; ++r)
            {
                for (int c = 0; c < settings.blocksCount; ++c)
                {
                    //  Тут ещё обработку сделать
                    g.DrawRectangle(p, new Rectangle(c * blockWidth, r * blockHeight, blockWidth, blockHeight));
                }
            }


            //  Масштабируем изображение до 500x500 - этого достаточно
            AForge.Imaging.Filters.ResizeBilinear scaleFilter =
                new AForge.Imaging.Filters.ResizeBilinear(settings.orignalDesiredSize.Width,
                                                          settings.orignalDesiredSize.Height);
            uProcessed = scaleFilter.Apply(uProcessed);
            original   = scaleFilter.Apply(original);
            g          = Graphics.FromImage(original);
            //  Пороговый фильтр применяем. Величина порога берётся из настроек, и меняется на форме
            AForge.Imaging.Filters.Threshold threshldFilter = new AForge.Imaging.Filters.Threshold();
            threshldFilter.ThresholdValue = settings.threshold;
            threshldFilter.ApplyInPlace(uProcessed);


            if (settings.processImg)
            {
                string info = processSample(ref uProcessed);
                Font   f    = new Font(FontFamily.GenericSansSerif, 20);
                g.DrawString(info, f, Brushes.Black, 30, 30);
            }

            processed = uProcessed.ToManagedImage();

            return(true);
        }
Esempio n. 10
0
 /// <summary>
 /// Change input to a binary image.
 /// </summary>
 /// <param name="image">Image to convert to binary.</param>
 /// <returns>Converted binary image (white and black).</returns>
 protected Bitmap BinaryImage(Bitmap image)
 {
     AForge.Imaging.Filters.Threshold filter = new AForge.Imaging.Filters.Threshold(1);
     return(filter.Apply(AForge.Imaging.Filters.Grayscale.CommonAlgorithms.Y.Apply(image)));
 }