예제 #1
0
        public void ProcessImage(Bitmap input_image)
        {
            lock (balanceLock)
            {
                int       side     = Math.Min(input_image.Height, input_image.Width);
                Rectangle cropRect = new Rectangle(0, 0, side, side);                                                               // this is square that represents feed from camera
                g.DrawImage(input_image, new Rectangle(0, 0, input_image.Width, input_image.Height), cropRect, GraphicsUnit.Pixel); // place it on original bitmap

                // set new processed
                if (processed != null)
                {
                    processed.Dispose();
                }

                //  Конвертируем изображение в градации серого
                processed = grayFilter.Apply(AForge.Imaging.UnmanagedImage.FromManagedImage(original));
                //  Пороговый фильтр применяем. Величина порога берётся из настроек, и меняется на форме
                threshldFilter.PixelBrightnessDifferenceLimit = ThresholdValue;
                threshldFilter.ApplyInPlace(processed);
                InvertFilter.ApplyInPlace(processed);
                Blober.ProcessImage(processed);
                AForge.Imaging.Blob[] blobs = Blober.GetObjectsInformation();
                BlobCount = blobs.Length;

                if (blobs.Length > 0)
                {
                    var BiggestBlob = blobs[0];
                    Recongnised = true;
                    Blober.ExtractBlobsImage(processed, BiggestBlob, false);
                    processed = BiggestBlob.Image;
                    AForge.Point mc = BiggestBlob.CenterOfGravity;
                    AForge.Point ic = new AForge.Point((float)BiggestBlob.Image.Width / 2, (float)BiggestBlob.Image.Height / 2);
                    AngleRad = (ic.Y - mc.Y) / (ic.X - mc.X);
                    Angle    = (float)(Math.Atan(AngleRad) * 180 / Math.PI);
                }
                else
                {
                    // TODO make arrengaments for No blobs case
                    Recongnised = false;
                    Angle       = 0;
                    AngleRad    = -1;
                }

                if (number != null)
                {
                    number.Dispose();
                }
                number = processed.ToManagedImage();
            }
        }