예제 #1
0
        //===================method=========================
        public void Get_Plate()
        {
            image_input     = IMAGE;
            backgroundFrame = new Bitmap(Application.StartupPath + "\\anh\\nen.jpg");
            hor_coe         = 0.6; //0.5
            ver_coe         = 0.4; //0.4
            number_coe      = 13;  //13
            min_freq        = 100;
            plate_ratio     = 14;  //14;

            IFilter filt = new GrayscaleY();

            backgroundFrame = filt.Apply(backgroundFrame);
            image_input     = filt.Apply(image_input);

            //IFilter f = new Threshold(180);
            //image_input = f.Apply(image_input);
            p = image_input;
            Subtract sub_img = new Subtract();

            sub_img.OverlayImage = backgroundFrame;
            Bitmap temp_img = sub_img.Apply(image_input);

            image_input = get_object(image_input, temp_img);
            image_input = fft(image_input);
            PLATE       = image_input;
        }
예제 #2
0
        int MotionDetection(Bitmap CurrentFrame)
        {
            if (PreFrame == null)
            {
                PreFrame = GrayFilter.Apply(CurrentFrame);
                PixFilter.ApplyInPlace(PreFrame);
                return(0);
            }

            Bitmap ProcFrame = GrayFilter.Apply(CurrentFrame);

            PixFilter.ApplyInPlace(ProcFrame);

            MoveTowardsFilter.StepSize     = 1;
            MoveTowardsFilter.OverlayImage = ProcFrame;
            MoveTowardsFilter.ApplyInPlace(PreFrame);

            DiffFilter.OverlayImage = PreFrame;
            Bitmap DiffImage = DiffFilter.Apply(ProcFrame);

            ThreFilter.ApplyInPlace(DiffImage);

            BlobsFilter.ProcessImage(DiffImage);
            Blob[] Blobs = BlobsFilter.GetObjectsInformation();

            int ret = 0;

            if (Blobs.Length != 0)
            {
                Graphics graph = Graphics.FromImage(CurrentFrame);
                Pen      pen   = new Pen(Color.Yellow, 2);
                graph.DrawRectangle(pen, new Rectangle(3, 3, 10, 10));
                foreach (Blob item in Blobs)
                {
                    if (item.Area > blobArea)
                    {
                        if (ShowMotionCheckBox.Checked)
                        {
                            graph.DrawRectangle(pen, item.Rectangle);
                        }
                        ++ret;
                    }
                }
            }

            videoShowPicBox.Image = CurrentFrame;

            PreFrame = ProcFrame;

            return(ret);
        }
예제 #3
0
//--------------------------------------------------------------------
        private void process_motion()
        {
            IFilter filt = new GrayscaleY();
            currentFrame = filt.Apply(currentFrame);
            backgroundFrame = filt.Apply(backgroundFrame);
            FiltersSequence filters = new FiltersSequence();
            Morph filt_morph = new Morph();
            filt_morph.OverlayImage = currentFrame;
            Bitmap tmp = filt_morph.Apply(backgroundFrame);
            filters.Add(new Difference(tmp));
            filters.Add(new Threshold(15));
            Bitmap tmp1 = filters.Apply(currentFrame);
            alarm = CalculateWhitePixels(tmp1);
        }
예제 #4
0
        protected virtual void FilterApplied(IplImage image)
        {
            try
            {
                if (previewImage.ptr == IntPtr.Zero)
                {
                    previewImage = cvlib.CvCreateImage(cvlib.CvGetSize(ref image), (int)cvlib.IPL_DEPTH_8U, 3);
                }

                cvlib.CvCvtColor(ref image, ref previewImage, cvlib.CV_GRAY2BGR);

                Bitmap bitmap    = cvlib.ToBitmap(previewImage, false);
                Bitmap grayscale = grayscaleFilter.Apply(bitmap);
                bitmap.Dispose();


                ImageStatistics imageStatistics = new ImageStatistics(grayscale);
                Histogram       grayHistogram   = imageStatistics.GrayWithoutBlack;

                synchronizationContext.Send(state =>
                {
                    UpdatePictureBox(grayscale);
                    histogram.Values             = imageStatistics.Gray.Values;
                    propertyGrid1.SelectedObject = grayHistogram;
                }, null);
            }
            catch (Exception e)
            {
                Trace.TraceInformation(e.Message);
            }
        }
예제 #5
0
        public Bitmap EscalaGrises()
        {
            GrayscaleY filter = new GrayscaleY();

            imagen = filter.Apply(imagen);
            return(imagen);
        }
예제 #6
0
        /// <summary>
        /// Creates a comic rendered copy of the input image.
        /// </summary>
        public override Bitmap Render(Bitmap sourceImage)
        {
            // Converters
            GrayscaleY     convertGray  = new GrayscaleY();
            GrayscaleToRGB convertColor = new GrayscaleToRGB();

            // Convert grayscal images
            if (sourceImage.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                sourceImage = convertColor.Apply(sourceImage);
            }

            Bitmap comicImage = AForge.Imaging.Image.Clone(sourceImage);
            Bitmap edgeLayer  = null;
            Bitmap glowLayer  = null;

            // Glow for smooth colors
            GaussianBlur filterBlur = new GaussianBlur();

            filterBlur.Sigma = 2.0;
            filterBlur.Size  = 4;
            glowLayer        = filterBlur.Apply(comicImage);

            //SmartBlur filterBlur = new SmartBlur(10, 0.2);
            //glowLayer = filterBlur.Apply(comicImage);

            ContrastCorrection filterContrast = new ContrastCorrection(1 - (-this.Coloring * 0.1));

            filterContrast.ApplyInPlace(glowLayer);

            BrightnessCorrection filterBrightness = new BrightnessCorrection((-this.Coloring * 0.1) + 0.1);

            filterBrightness.ApplyInPlace(glowLayer);

            Screen blendScreen = new Screen(glowLayer);

            blendScreen.ApplyInPlace(comicImage);


            // Create a layer for edges
            Convolution filterConvolution = new Convolution(ConvolutionKernel);

            edgeLayer = filterConvolution.Apply(comicImage);

            // Convert to grayscale
            edgeLayer = convertGray.Apply(edgeLayer);

            // Threshold (edge thickness)
            Threshold filterThreshold = new Threshold((byte)(this.Edging * 255 / 100));

            filterThreshold.ApplyInPlace(edgeLayer);
            edgeLayer = convertColor.Apply(edgeLayer);

            // intersect comic with top layer (Darken blend)
            Intersect blendIntersect = new Intersect(edgeLayer);

            blendIntersect.ApplyInPlace(comicImage);

            return(comicImage);
        }
예제 #7
0
        void ShowPreview(IplImage image)
        {
            if (previewImage.ptr == IntPtr.Zero)
            {
                previewImage = cvlib.CvCreateImage(cvlib.CvGetSize(ref image), (int)cvlib.IPL_DEPTH_8U, 3);
            }

            cvlib.CvCvtColor(ref image, ref previewImage, cvlib.CV_GRAY2BGR);

            Bitmap bitmap = cvlib.ToBitmap(previewImage, false);

            Bitmap          grayscale       = grayscaleFilter.Apply(bitmap);
            ImageStatistics imageStatistics = new ImageStatistics(grayscale);
            Histogram       grayHistogram   = imageStatistics.GrayWithoutBlack;


            PaintBlobs(bitmap);

            synchronizationContext.Send(state =>
            {
                UpdatePictureBox(bitmap);
                histogram.Values             = imageStatistics.Gray.Values;
                propertyGrid1.SelectedObject = grayHistogram;
            }, null);
            bitmap.Dispose();
        }
예제 #8
0
        public Bitmap ToGrayScale(Bitmap Im)
        {
            AForge.Imaging.Filters.GrayscaleY Img = new GrayscaleY();
            Bitmap bmImage = AForge.Imaging.Image.Clone(new Bitmap(Im), PixelFormat.Format24bppRgb);

            return(Img.Apply(bmImage));
        }
예제 #9
0
        /// <summary>
        /// Creates a comic rendered copy of the input image.
        /// </summary>
        public override Bitmap Render(Bitmap sourceImage)
        {
            Bitmap sketchImage = AForge.Imaging.Image.Clone(sourceImage);

            GrayscaleY convertGray = new GrayscaleY();

            // Blur
            GaussianBlur filterBlur = new GaussianBlur();

            filterBlur.Sigma = this.PencilTipSize;
            filterBlur.Size  = this.PencilTipSize;
            Bitmap overLayer = filterBlur.Apply(sketchImage);

            // Invert over layer
            Invert sketchInvert = new Invert();

            sketchInvert.ApplyInPlace(overLayer);

            BrightnessCorrection filterBrightness = new BrightnessCorrection(-this.Range * 0.01);

            filterBrightness.ApplyInPlace(overLayer);

            ContrastCorrection filterContrast = new ContrastCorrection(1 - (-this.Range * 0.01));

            filterContrast.ApplyInPlace(overLayer);

            // Convert to grayscale
            sketchImage = convertGray.Apply(sketchImage);
            overLayer   = convertGray.Apply(overLayer);

            // Dodge blending for the win!
            ColorDodge dodgeBlend = new ColorDodge(overLayer);

            dodgeBlend.ApplyInPlace(sketchImage);

            return(sketchImage);
        }
예제 #10
0
        /// <summary>
        ///     Performs the analysis on the image
        /// </summary>
        public void Analyze(bool thorough = true)
        {
            if (m_disposed)
            {
                throw new ObjectDisposedException("ImageProcessor");
            }

            if (IsScannable)
            {
                return;
            }

            LuminanceSource source    = new BitmapLuminanceSource(m_bitmap);
            var             binarizer = new HybridBinarizer(source);
            var             binBitmap = new BinaryBitmap(binarizer);

            // Try to extract the form data
            var barReader = new BarcodeReader();

            barReader.AutoRotate              = true;
            barReader.Options.TryHarder       = thorough;
            barReader.Options.PossibleFormats = new List <BarcodeFormat> {
                BarcodeFormat.CODE_128
            };

            m_barcodeResults = barReader.DecodeMultiple(source);

            // Look for barcode markers if possible
            if (m_barcodeResults != null)
            {
                MarkerCodes = m_barcodeResults.Where(o => o.Text.StartsWith("OMR:")).ToArray();
            }
            IsScannable = true;
            // Get the template data
            var markerCode = MarkerCodes == null
                ? null
                : MarkerCodes.FirstOrDefault(o => o.Text.StartsWith("OMR:TL") || o.Text.StartsWith("OMR:ID"));

            // Get the guiding points by circles
            var grayFilter      = new GrayscaleY();
            var thresholdFilter = new Threshold(127);
            var invertFilter    = new Invert();

            using (var searchImage = invertFilter.Apply(thresholdFilter.Apply(grayFilter.Apply(m_bitmap))))
            {
                // Blobs
                var blobCounter = new BlobCounter();
                blobCounter.FilterBlobs = true;
                blobCounter.MinHeight   = 30;
                blobCounter.MinWidth    = 30;

                // Check for circles
                blobCounter.ProcessImage(searchImage);
                var blobs         = blobCounter.GetObjectsInformation();
                var shapeChecker  = new SimpleShapeChecker();
                var controlPoints = new List <Point>();
                var currentCheck  = 45;
                while ((currentCheck-- > 20) && (controlPoints.Count != 4))
                {
                    controlPoints.Clear();
                    // Get the positions
                    foreach (var blob in blobs)
                    {
                        var   center = new Point();
                        float radius = 0;

                        if (shapeChecker.IsCircle(blobCounter.GetBlobsEdgePoints(blob), out center, out radius) &&
                            (radius > currentCheck))
                        {
                            controlPoints.Add(center);
                        }
                    }
                }

                // Control points
                IsScannable &= controlPoints.Count == 4;
                if (!IsScannable)
                {
                    return;
                }

                // Now set markers
                m_topLeft     = controlPoints[0]; //new AForge.Point(this.m_bitmap.Width + 10, this.m_bitmap.Height + 10);
                m_topRight    = controlPoints[1];
                m_bottomLeft  = controlPoints[2];
                m_bottomRight = controlPoints[3];

                // Find the right most bubble
                float rightMost = controlPoints.Select(o => o.X).Max(),
                      leftMost  = controlPoints.Select(o => o.X).Min();
                // Organize those that are left/right
                Point[] lefties = controlPoints.Where(o => o.X < leftMost + (rightMost - leftMost) / 2).ToArray(),
                righties = controlPoints.Where(o => o.X > leftMost + (rightMost - leftMost) / 2).ToArray();

                // HACK:
                if (lefties[0].Y < lefties[1].Y)
                {
                    m_topLeft    = lefties[0];
                    m_bottomLeft = lefties[1];
                }
                else
                {
                    m_topLeft    = lefties[1];
                    m_bottomLeft = lefties[0];
                }

                // HACK:
                if (righties[0].Y < righties[1].Y)
                {
                    m_topRight    = righties[0];
                    m_bottomRight = righties[1];
                }
                else
                {
                    m_topRight    = righties[1];
                    m_bottomRight = righties[0];
                }
            }

            if (!IsScannable)
            {
                return;
            }

            // Get the template data
            if ((MarkerCodes != null) && (markerCode != null))
            {
                var templateData = markerCode.Text.Split(':');
                if (templateData.Length > 2)
                {
                    TemplateName = templateData[2];
                    if (templateData.Length > 3)
                    {
                        Parameters = templateData.Skip(3).ToArray();
                    }
                }
            }
        }
예제 #11
0
        /// <summary>
        /// Apply template
        /// </summary>
        /// <param name="template"></param>
        /// <param name="image"></param>
        public OmrPageOutput ApplyTemplate(OmrTemplate template, ScannedImage image)
        {
            // Image ready for scan
            if (!image.IsReadyForScan)
            {
                if (!image.IsScannable)
                {
                    image.Analyze();
                }
                image.PrepareProcessing();
            }

            // Page output
            OmrPageOutput retVal = new OmrPageOutput()
            {
                Id         = image.TemplateName + DateTime.Now.ToString("yyyyMMddHHmmss"),
                TemplateId = image.TemplateName,
                Parameters = image.Parameters,
                StartTime  = DateTime.Now,
                Template   = template
            };

            // Save directory for output images
            string saveDirectory = String.Empty;
            var    parmStr       = new StringBuilder();

            if (this.SaveIntermediaryImages)
            {
                if (image.Parameters != null)
                {
                    foreach (var pv in image.Parameters)
                    {
                        parmStr.AppendFormat("{0}.", pv);
                    }
                }
                retVal.RefImages = new List <string>()
                {
                    String.Format("{0}-{1}-init.bmp", retVal.Id, parmStr),
                    String.Format("{0}-{1}-tx.bmp", retVal.Id, parmStr),
                    String.Format("{0}-{1}-fields.bmp", retVal.Id, parmStr),
                    String.Format("{0}-{1}-gs.bmp", retVal.Id, parmStr),
                    String.Format("{0}-{1}-bw.bmp", retVal.Id, parmStr),
                    String.Format("{0}-{1}-inv.bmp", retVal.Id, parmStr)
                };

                saveDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "imgproc");

                if (!Directory.Exists(saveDirectory))
                {
                    Directory.CreateDirectory(saveDirectory);
                }
                image.Image.Save(Path.Combine(saveDirectory, string.Format("{0}-{1}-init.bmp", DateTime.Now.ToString("yyyyMMddHHmmss"), parmStr)));
            }

            // First, we want to get the image from the scanned image and translate it to the original
            // position in the template
            Bitmap bmp = null;

            try
            {
                bmp = new Bitmap((int)template.BottomRight.X, (int)template.BottomRight.Y, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

                // Scale
                float width  = template.TopRight.X - template.TopLeft.X,
                      height = template.BottomLeft.Y - template.TopLeft.Y;


                // Translate to original
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    ResizeBicubic bc = new ResizeBicubic((int)width, (int)height);
                    g.DrawImage(bc.Apply((Bitmap)image.Image), template.TopLeft.X, template.TopLeft.Y);
                }

                if (this.SaveIntermediaryImages)
                {
                    bmp.Save(Path.Combine(saveDirectory, string.Format("{0}-{1}-tx.bmp", DateTime.Now.ToString("yyyyMMddHHmmss"), parmStr)));
                }


                // Now try to do hit from the template
                if (this.SaveIntermediaryImages)
                {
                    using (var tbmp = bmp.Clone() as Bitmap)
                    {
                        using (Graphics g = Graphics.FromImage(tbmp))
                        {
                            foreach (var field in template.Fields)
                            {
                                g.DrawRectangle(Pens.Black, field.TopLeft.X, field.TopLeft.Y, field.TopRight.X - field.TopLeft.X, field.BottomLeft.Y - field.TopLeft.Y);
                                g.DrawString(field.Id, SystemFonts.CaptionFont, Brushes.Black, field.TopLeft);
                            }
                        }

                        tbmp.Save(Path.Combine(saveDirectory, string.Format("{0}-{1}-fields.bmp", DateTime.Now.ToString("yyyyMMddHHmmss"), parmStr)));
                    }
                }


                // Now convert to Grayscale
                GrayscaleY grayFilter = new GrayscaleY();
                var        gray       = grayFilter.Apply(bmp);
                bmp.Dispose();
                bmp = gray;

                if (this.SaveIntermediaryImages)
                {
                    bmp.Save(Path.Combine(saveDirectory, string.Format("{0}-{1}-gs.bmp", DateTime.Now.ToString("yyyyMMddHHmmss"), parmStr)));
                }

                // Prepare answers
                Dictionary <OmrQuestionField, OmrOutputData> hitFields = new Dictionary <OmrQuestionField, OmrOutputData>();
                BarcodeReader barScan = new BarcodeReader();
                barScan.Options.UseCode39ExtendedMode        = true;
                barScan.Options.UseCode39RelaxedExtendedMode = true;
                barScan.Options.TryHarder   = true;
                barScan.TryInverted         = true;
                barScan.Options.PureBarcode = false;
                barScan.AutoRotate          = true;

                foreach (var itm in template.Fields.Where(o => o is OmrBarcodeField))
                {
                    PointF position = itm.TopLeft;
                    SizeF  size     = new SizeF(itm.TopRight.X - itm.TopLeft.X, itm.BottomLeft.Y - itm.TopLeft.Y);
                    using (var areaOfInterest = new Crop(new Rectangle((int)position.X, (int)position.Y, (int)size.Width, (int)size.Height)).Apply(bmp))
                    {
                        // Scan the barcode
                        var result = barScan.Decode(areaOfInterest);


                        if (result != null)
                        {
                            hitFields.Add(itm, new OmrBarcodeData()
                            {
                                BarcodeData = result.Text,
                                Format      = result.BarcodeFormat,
                                Id          = itm.Id,
                                TopLeft     = new PointF(result.ResultPoints[0].X + position.X, result.ResultPoints[0].Y + position.Y),
                                BottomRight = new PointF(result.ResultPoints[1].X + position.X, result.ResultPoints[0].Y + position.Y + 10)
                            });
                        }
                    }
                }

                // Now binarize
                Threshold binaryThreshold = new Threshold(template.ScanThreshold);
                binaryThreshold.ApplyInPlace(bmp);

                if (this.SaveIntermediaryImages)
                {
                    bmp.Save(Path.Combine(saveDirectory, string.Format("{0}-{1}-bw.bmp", DateTime.Now.ToString("yyyyMMddHHmmss"), parmStr)));
                }

                // Set return parameters
                String tAnalyzeFile = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
                bmp.Save(tAnalyzeFile, System.Drawing.Imaging.ImageFormat.Jpeg);
                retVal.AnalyzedImage = tAnalyzeFile;
                retVal.BottomRight   = new PointF(bmp.Width, bmp.Height);

                // Now Invert
                Invert invertFiter = new Invert();
                invertFiter.ApplyInPlace(bmp);

                if (this.SaveIntermediaryImages)
                {
                    bmp.Save(Path.Combine(saveDirectory, string.Format("{0}-{1}-inv.bmp", DateTime.Now.ToString("yyyyMMddHHmmss"), parmStr)));
                }


                // Crop out areas of interest
                List <KeyValuePair <OmrQuestionField, Bitmap> > areasOfInterest = new List <KeyValuePair <OmrQuestionField, Bitmap> >();
                foreach (var itm in template.Fields.Where(o => o is OmrBubbleField))
                {
                    PointF position = itm.TopLeft;
                    SizeF  size     = new SizeF(itm.TopRight.X - itm.TopLeft.X, itm.BottomLeft.Y - itm.TopLeft.Y);
                    areasOfInterest.Add(new KeyValuePair <OmrQuestionField, Bitmap>(
                                            itm,
                                            new Crop(new Rectangle((int)position.X, (int)position.Y, (int)size.Width, (int)size.Height)).Apply(bmp))
                                        );
                }

                // Queue analysis
                WaitThreadPool wtp      = new WaitThreadPool();
                Object         syncLock = new object();

                foreach (var itm in areasOfInterest)
                {
                    wtp.QueueUserWorkItem(img =>
                    {
                        var parm = (KeyValuePair <OmrQuestionField, Bitmap>)itm;

                        try
                        {
                            var areaOfInterest = parm.Value;
                            var field          = parm.Key;

                            BlobCounter blobCounter = new BlobCounter();
                            blobCounter.FilterBlobs = true;

                            // Check for circles
                            blobCounter.ProcessImage(areaOfInterest);
                            Blob[] blobs = blobCounter.GetObjectsInformation();
                            var blob     = blobs.FirstOrDefault(o => o.Area == blobs.Max(b => b.Area));
                            if (blob != null)
                            {
                                //var area = new AForge.Imaging.ImageStatistics(blob).PixelsCountWithoutBlack;
                                if (blob.Area < 30)
                                {
                                    return;
                                }
                                var bubbleField = field as OmrBubbleField;
                                lock (syncLock)
                                    hitFields.Add(field, new OmrBubbleData()
                                    {
                                        Id          = field.Id,
                                        Key         = bubbleField.Question,
                                        Value       = bubbleField.Value,
                                        TopLeft     = new PointF(blob.Rectangle.X + field.TopLeft.X, blob.Rectangle.Y + field.TopLeft.Y),
                                        BottomRight = new PointF(blob.Rectangle.X + blob.Rectangle.Width + field.TopLeft.X, blob.Rectangle.Y + blob.Rectangle.Height + field.TopLeft.Y),
                                        BlobArea    = blob.Area
                                    });
                            }
                        }
                        catch (Exception e) {
                            Trace.TraceError(e.ToString());
                        }
                        finally
                        {
                            parm.Value.Dispose();
                        }
                    }, itm);
                }

                wtp.WaitOne();

                // Organize the response
                foreach (var res in hitFields)
                {
                    if (String.IsNullOrEmpty(res.Key.AnswerRowGroup))
                    {
                        this.AddAnswerToOutputCollection(retVal, res);
                    }
                    else
                    {
                        // Rows of data
                        OmrRowData rowGroup = retVal.Details.Find(o => o.Id == res.Key.AnswerRowGroup) as OmrRowData;
                        if (rowGroup == null)
                        {
                            rowGroup = new OmrRowData()
                            {
                                Id = res.Key.AnswerRowGroup
                            };
                            retVal.Details.Add(rowGroup);
                        }

                        this.AddAnswerToOutputCollection(rowGroup, res);
                    }
                }

                // Remove temporary images
                //foreach (var f in retVal.RefImages)
                //    File.Delete(Path.Combine(saveDirectory, f));

                // Outcome is success
                retVal.Outcome = OmrScanOutcome.Success;
            }
            catch (Exception e)
            {
                retVal.Outcome      = OmrScanOutcome.Failure;
                retVal.ErrorMessage = e.Message;
                Trace.TraceError(e.ToString());
            }
            finally
            {
                retVal.StopTime = DateTime.Now;
                bmp.Dispose();
            }

            return(retVal);
        }